第四周作业

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值