第四周作业
来源:互联网 发布:最伟大的意大利人知乎 编辑:程序博客网 时间:2024/06/16 07:32
1.请定义一个交通工具(Vehicle)的类,其中有:
属性:速度(speed),体积(size)等
方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
package week2;public class Vehicle { private int speed,size; public static void main(String[] args) { // TODO Auto-generated method stub Vehicle vehicle = new Vehicle(); vehicle.move(); vehicle.setSize(10); vehicle.setSpeed(100); vehicle.speedUp(); vehicle.speedDown(); System.out.println(vehicle.toString()); } //移动方法 public void move(){ System.out.println("移动"); } //设置速度 public void setSpeed(int speed) { this.speed = speed; } //设置体积 public void setSize(int size) { this.size = size; } //加速 public void speedUp(){ System.out.println("加速"); } //减速 public void speedDown(){ System.out.println("减速"); } @Override public String toString() { return "Vehicle [speed=" + speed + ", size=" + size + "]"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
2.打印当前时间。学习使用Date类和Calendar类。(教材P194页)
package week2;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class TestDate { public static void main(String[] args) { // TODO Auto-generated method stub Date date = new Date(); System.out.println(date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(simpleDateFormat.format(date)); Calendar calendar = Calendar.getInstance(); String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(calendar.getTime()); System.out.println(str); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3.以Point类为基础,定义一个平面中的Circle类:
1、编写一个无参的构造函数;
2、编写一个有参的构造函数;
3、在主函数中调用无参的构造函数生成圆的实例c1,调用有参的构造函数生成圆的实例c2,调用实例方法判断c1和c2是否相重叠。
package week2;public class Circle { private double radius; public Circle(){ radius = 2.0; } public Circle(double radius){ this.radius = radius; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
package week2;public class TestTotal { public static void main(String[] args) { Circle circle1 = new Circle(); Circle circle2 = new Circle(3.0); if (circle1 == circle2) { System.out.println("是同一个实例"); }else{ System.out.println("不是同一个实例"); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
4.编写代码模拟手机与SIM卡的组合关系。
要求:
SIM卡类负责创建SIM卡;
Phone类负责创建手机;
手机可以组合一个SIM卡;
手机可以更换其中的SIM卡。
package week3;public class Phone { private SIM sim; public SIM getSim() { return sim; } public void setSim(SIM sim) { this.sim = sim; } Phone(){ System.out.println("我是Phone"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
package week3;public class SIM { public SIM() { // TODO Auto-generated constructor stub System.out.println("我是SIM"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
package week3;public class TestPhone { public static void main(String[] args) { // TODO Auto-generated method stub SIM sim = new SIM(); Phone phone = new Phone(); phone.setSim(sim); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
4.
package week3;public class CPU {public CPU() { // TODO Auto-generated constructor stub} private int speed; public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
package week3;public class HardDisk { private int amount; public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
package week3;public class PC { private CPU cpu; private HardDisk hardDisk; public HardDisk getHardDisk() { return hardDisk; } public void setHardDisk(HardDisk hardDisk) { this.hardDisk = hardDisk; } public CPU getCpu() { return cpu; } public void setCpu(CPU cpu) { this.cpu = cpu; } public void show(){ System.out.println("CPU速度为:"+getCpu().getSpeed()+" 硬盘容量为:"+getHardDisk().getAmount()); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
package week3;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub CPU cpu = new CPU(); cpu.setSpeed(2200); HardDisk hardDisk = new HardDisk(); hardDisk.setAmount(200); PC pc = new PC(); pc.setCpu(cpu); pc.setHardDisk(hardDisk); pc.show(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
5.–定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类。
–试着改变求体积方法的访问权限,查看并分析编译和运行结果
–把Cylinder类和A类置于不同的包中,通过对求体积方法设置不同的访问权限,查看并分析编译和运行结果
package bzu.info.com;/** * 圆柱类 * @author Lenovo_PC * */public class Cylinder { private int radius; private int hight; public Cylinder(int radius, int hight) { super(); this.radius = radius; this.hight = hight; } public double calu(){ return Math.PI*Math.pow(radius, 2)*hight; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
package bzu.info.com;public class A { public static void main(String[] args) { // TODO Auto-generated method stub Cylinder cylinder = new Cylinder(2,3); System.out.println(cylinder.calu()); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
阅读全文
0 0
- 第四周作业
- 第四周作业 1
- 第四周作业 4
- 第四周作业 2
- 第四周作业
- 第四周作业二
- 第四周作业三
- 第四周作业1
- 第四周作业2
- 第四周作业
- Java第四周作业
- 第四周作业:多线程
- 第四周作业
- 第四周作业
- 第四周C++作业
- 第四周作业
- 第四周作业
- 第四周作业
- 前端之API大全
- 转:玩转 Ceph 的正确姿势
- GN 编译配置
- 从零开始开发Java框架-flylib
- 【题解搬运】蓝桥杯2012年第4题题解
- 第四周作业
- libpcap
- 如何构建优秀的命令行用户界面的 Python 库
- 记录JDBC连接sqlserver(MSSQL) 数据库
- noip防低级错误手册
- OMA DRM介绍
- USB设备开发---- usb描述符概述
- 调整数组顺序使奇数位于偶数前面
- [LeetCode] 617. Merge Two Binary Trees