第五周java作业

来源:互联网 发布:nginx 点播服务器 编辑:程序博客网 时间:2024/06/05 20:18

题目1

编写代码模拟手机与SIM卡的组合关系。

要求:

           SIM卡类负责创建SIM卡;

           Phone类负责创建手机;

          手机可以组合一个SIM卡;

          手机可以更换其中的SIM卡。

 

代码:

package work4;

public class SIM {
 int sim;
 SIM(int n){
  sim=n;
 }
void  setCreate(){
 sim++;

}
int getCreate(){
 return sim;
}
void setChange(){
 this.sim =sim-2;
}
int getChange(){
 return sim;
}
void setCombintation(){
sim++;
}
int getCombintation(){
 return sim;
}
}

 

package work4;

public class Phone {
int phone;
SIM sim;
Phone(int m){
 phone=m;
}
void setcreate(){
 phone++;
 
}
int getcreate(){
 return phone;
}
int change(){
 return sim.getChange();
}
 int combintation(){
  return sim.getCombintation();
}
}

package work4;

public class TestPhone {

 public static void main(String[] args) {
 SIM si=new SIM(10);
 si.getCreate();
 System.out.println("SIM卡的数量为"+si.getCreate());
 Phone ph=new Phone(2);
 ph.getcreate();
 System.out.println("手机的数量为:"+ph.getcreate());
 ph.change();
 System.out.println("SIM卡改变之后的数量为:"+ph.change());
 ph.combintation();
 System.out.println("SIM卡组合之后的数量为:"+ph.combintation());
 
 }

}
截图:

题目2:

代码:

package work5;

public class CPU {
 int speed;
  CPU(int j){
  speed=j;
 }
 void setSpeed(int m){
  speed=m;
 }
int getSpeed(){
 return speed;
}
}

package work5;

public class HarkDisk {
int amount;
public HarkDisk(int i) {
 amount=i;
}
void setAmount(int m){
 amount=m;
}
int getAmount(){
 return amount;
}

}

package work5;

public class PC {
CPU cpu;
HarkDisk HD;
PC(){
 
}
void setCPU(CPU c){
 cpu=c;
 
 
}
void setHarkDisk(HarkDisk h){
 HD=h;
 
}
void show(){
 System.out.println("cpu的速度"+cpu.getSpeed());
 System.out.println("硬盘的容量"+HD.getAmount());
}
}

package work5;

public class Test {
 public static void main(String[] args){
CPU cpu=new CPU(2200);
PC pc=new PC();
HarkDisk disk=new HarkDisk(200);
pc.setCPU(cpu);
pc.setHarkDisk(disk);
pc.show();

 

 }
}



题目3

– 定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类。

– 试着改变求体积方法的访问权限,查看并分析编译和运行结果

– Cylinder类和A类置于不同的包中,通过对求体积方法设置不同的访问权限,查看并分析编译和运行结果

 

 

代码;

package bzu.info.software;

public class Circle {
double radius;
public Circle(double r){
 radius=r;
 
}
void setCircle(double area){
 area=radius*radius;
}

  public  double getCricle(){
  return radius*radius;
  }

}

package bzu.info.com;
public class Cylinder {
double bottom;
double height;
Cylinder(double bottom,double height){
 this.bottom = bottom;
 this.height = height;

}
 public double getCylinder(){
 return bottom*height*(1.0/3);

 
}
}

 

package bzu.info.com;
import bzu.info.software.Circle;
public class A {

 public static void main(String[] args) {
 
  Circle c = new Circle(6.00);
  Cylinder cylinder=new Cylinder(c.getCricle(),2.00);

 System.out.println("圆锥的体积"+cylinder.getCylinder());
 
 
 
 
 }

}

注意:1.方法的调用

2.变量的声明




 

 

 

 

原创粉丝点击