Java第三周作业(2)

课堂练习4:

编写代码模拟手机与SIM卡的组合关系。
要求:
SIM卡类负责创建SIM卡;
Phone类负责创建手机;
手机可以组合一个SIM卡;
手机可以更换其中的SIM卡。


主要代码:

public class Card {
        String producer;
        String number;
        public String getProducer() {
            return producer;
        }
        public void setProducer(String producer) {
            this.producer = producer;
        }
        public String getNumber() {
            return number;
        }
        public void setNumber(String number) {
            this.number = number;
        }

}
public class Phone {
        Card card ;

        public Card getCard() {
            return card;
        }

        public void setCard(Card card) {
            this.card = card;
        }
        public void changecard(Phone phone,Card card){
            phone.setCard(card);
        }
}

public class PhoneAndCardTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Card card1=new Card();
        Card card2=new Card();
        Phone phone=new Phone();
        //卡一获取电话号码运行商
        card1.setNumber("10086");
        card1.setProducer("联通");
        //卡二获取电话号码运行商
        card2.setNumber("10010");
        card2.setProducer("移动");
        //手机卡一
        phone.setCard(card1);
        System.out.println("现在手机的卡:\n 卡号"+card1.getNumber()+"\t 运行商:"+card1.producer);
        //更换手机号
        phone.changecard(phone, card2);
        System.out.println("更换手机号后的手机号:\n 卡号"+card2.getNumber()+"\t 运行商:"+card2.producer);



    }

}

运行结果:

这里写图片描述


课堂练习5:

这里写图片描述


主要代码:


public class CPU {
        private int speed;

        public int getSpeed() {
            return speed;
        }

        public void setSpeed(int speed) {
            this.speed = speed;
        }

}

public class HardDisk {
        private int amount;//容量

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

}

public class PC {
        private CPU cpu;
        private HardDisk hardDisk;
        public CPU getCpu() {
            return cpu;
        }
        public void setCpu(CPU cpu) {
            this.cpu = cpu;
        }
        public HardDisk getHardDisk() {
            return hardDisk;
        }
        public void setHardDisk(HardDisk hardDisk) {
            this.hardDisk = hardDisk;
        }
        public void show(){
            System.out.println("计算机中CPU的速度:"+cpu.getSpeed()+"容量:"+hardDisk.getAmount());
        }

}

public class PCTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CPU cpu=new CPU();
        cpu.setSpeed(2200);
        HardDisk disk=new HardDisk();
        disk.setAmount(200);
        PC pc=new PC();
        pc.setCpu(cpu);
        pc.setHardDisk(disk);
        pc.show();

    }

}

运行结果:

这里写图片描述


课堂练习6:

–定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类。
–试着改变求体积方法的访问权限,查看并分析编译和运行结果
–把Cylinder类和A类置于不同的包中,通过对求体积方法设置不同的访问权限,查看并分析编译和运行结果


主要代码:

package bzu.info.software;

public class Circle {
        private int r;

        public int getR() {
            return r;
        }

        public void setR(int r) {
            this.r = r;
        }

}
package bzu.info.com;

import bzu.info.software.Circle;

public class Cylinder {
      Circle circle;
      int hight;
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
    public int getHight() {
        return hight;
    }
    public void setHight(int hight) {
        this.hight = hight;
    }

}
package bzu.info.com;

import bzu.info.software.Circle;

public class A {

    /**
     * @param args
     */
    static Circle circle;
    static Cylinder cylinder;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        circle=new Circle();
        circle.setR(5);
        cylinder=new Cylinder();
        cylinder.setCircle(circle);
        cylinder.setHight(12);
        PutVolume();
    }
    public static void PutVolume(){
        double Volume=3.14*(cylinder.getCircle().getR()*cylinder.getCircle().getR())*cylinder.getHight();
        System.out.println("圆柱半径"+circle.getR()+"高"+cylinder.getHight());
        System.out.println("圆柱的面积是:"+Volume);
    }
}

运行结果:

这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章