第五周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.变量的声明
- Java第五周作业
- 第五周java作业
- 9.27第五周java作业
- 第五次java作业
- Java第五次作业
- 第五次java作业
- 第五周作业
- 第五周作业
- 第五周作业 2
- 第五周作业 3
- 第五周作业 1
- 第五周作业1
- 第五周作业2
- 第五周作业3
- 第五周作业
- 第五周作业
- 第五周C++作业
- 第五周作业
- 搭建简单的struts2框架
- 现代计算机的存储体系 (转载)
- 关于大数相乘问题(Java实现)
- 找出数组中不同的两个数
- Spring+MyBatis多数据源配置实现
- 第五周java作业
- 考试总结2
- 01Redis学习
- lintcode Guess Number Game II
- 【福分系统】bootstrap表格控件
- jquery实现品牌列表的显示
- 自定义bs soup的抓取函数,查找不含某个class属性tag 以及一个小问题'NoneType' object has no attribute '__getitem__'
- URAL 1996 Cipher Message 3 FFT KMP
- leetcode200. Number of Islands