第六章课后习题

1.定义一个点类Point,包含2个成员变量x、y分别表示x和y坐标,2个构造器Point()和Point(int x0,y0),以及一个movePoint(int dx,int dy)方法实现点的位置移动。编写一个程序,创建两个Point对象p1、p2,分别调研movePoint方法后,打印p1和p2的坐标。

public class Point {
    private int x0;
    private int y0;
     public Point()
{
}
public Point(int x0,int y0)
{
    this.x0=x0;
this.y0=y0;
}
public void movePoint(int dx,int dy){
    this.x0 = this.x0 + dx;
    this.y0 = this.y0 + dy;     
}
}
public class Point {
    private int x0;
    private int y0;
     public Point()
{
}
public Point(int x0,int y0)
{
    this.x0=x0;
this.y0=y0;
}
public void movePoint(int dx,int dy){
    this.x0 = this.x0 + dx;
    this.y0 = this.y0 + dy;     
}
}

2.(1)定义一个矩形类Rectangle:
1)定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2)有2个属性:长length、宽width
3)通过构造方法Rectangle(int width, int length),分别给两个属性赋值

public class Rectangle
{
int width;
int height;

public Rectangle()
{
}
public Rectangle(int width,int height)
{
    this.width=width;
this.height=height;
}

public double getArea()
{
return this.width*this.height;
}
public double getPer()
{
return 2*(this.width+this.height);
}
public void showAll()
{
System.out.println("Width="+this.width);
System.out.println("Height="+this.height);
System.out.println("Area="+this.getArea());
System.out.println("Per="+this.getPer());
}
}

3、定义一个笔记本类,该类有颜色(char)和cpu型号(int)两个属性。
(1)无参和有参的两个构造方法;有参构造方法可以在创建对象的同时为每个属性赋值;
(2) 输出笔记本信息的方法
然后编写一个测试类,测试笔记本类的各个方法。

public class ComputeTest {

    public static void main(String[] args) {
        Computer c1 = new Computer();
        c1.showComputer();
        Computer c2 = new Computer('红', 32);
        c2.showComputer();
    }

}

class Computer {
    private char color;
    private int cpuNum;

    public Computer() {

    }

    public Computer(char color, int cpuNum) {
        this.color = color;
        this.cpuNum = cpuNum;
    }

    public char getColor() {
        return color;
    }

    public void setColor(char color) {
        this.color = color;
    }

    public int getCpuNum() {
        return cpuNum;
    }

    public void setCpuNum(int cpuNum) {
        this.cpuNum = cpuNum;
    }

    public void showComputer() {
        System.out.println("笔记本的颜色:" + getColor());
        System.out.println("笔记本的CPU型号:" + getCpuNum());

    }
}

4.(1)定义一个人类Person:
1)定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
2)有三个属性:名字、身高、体重

public class Person
{
String name; 
int age;
double height;

public void sayHello()
{
System.out.println("Hello");
}
}
  (2)定义一个PersonCreate类:
    1)创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
    2)分别调用对象的sayHello()方法。
public class PersonCreate
{
public static void main(String[] args)
{
Person p = new Person();
p.name="zhangsan";
p.age=33;
p.height=1.73;
p.sayHello();

Person q = new Person();
q.name="lisi";
q.age=44;
q.height=1.74;
q.sayHello();
}
}

5.(1)定义一个人类Person:
1)定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
2)有三个属性:名字、身高、体重
3)通过构造方法,分别给三个属性赋值

    class Person
{
String name;
int age;
double height;

public Person(){
    
}

public Person(String n,int a,double h)
{
name=n;
age=a;
height=h;
}

public void sayHello()
{
System.out.println("Hello, my name is "+name);
}
}



  (2)定义一个Constructor类:
    1)创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
    2)分别调用对象的sayHello()方法。
public class Constructor
{
public static void main(String[] args)
{
Person p=new Person("zhangsan",33,1.83);
Person q=new Person("lisi",44,1.74);
p.sayHello();
q.sayHello();   
}
}

1、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。

public class Student {
    private String name;
    private String number;
    private int score;
    
    public Student() {

    }

    public Student(String name, String number, int score) {
        this.name = name;
        this.number = number;
        this.score = score;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public void sortByScore(Student[] students){
        int max= students[0].score;
        for(int i =0 ;istudents[j].getScore()){
                    Student b = students[i];
                    students[i] = students[j];
                    students[j] = b;
                }
            }
        }
        System.out.println("按成绩排序后的结果如下:");
        for(int i = 0 ;i

2.定义一个汽车类Vehicle,要求如下:
(1)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。
(2)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
(3)为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
(4)定义一个一般方法run(),用打印语句描述汽车奔跑的功能

定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。

public class Vehicle {
    private String brand;
    private String color;
    private double speed;
    Vehicle(){
        
    }
    Vehicle(String brand,String color){
        this.brand = brand;
        this.color = color;
        speed = 0;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getSpeed() {
        return speed;
    }
    public void setSpeed(double speed) {
        this.speed = speed;
    }
    
    public void run(){
        System.out.println(getColor()+"的"+getBrand()+"的速度是"+getSpeed());
    }
    public String getBrand() {
        return brand;
    }

你可能感兴趣的:(第六章课后习题)