第八周练习

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

public class book {
    char color;
    int cpu;
    public void show() {
        System.out.println("该笔记本的颜色是:" + color);
        System.out.println("该笔记本CPU的型号为:" + cpu);
    }
}
import java.awt.Color;
import java.util.Scanner;

public class practice_week8 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        book a = new book();
        a.cpu = 01;
        a.color = '黑';
        a.show();
    }

}

2.定义两个类,描述如下: [必做题]
• 2.1定义一个人类Person:
• 2.1.1定义一个方法sayHello(),可以向对方发出问候语“hello,my name is XXX”
• 2.1.2有三个属性:名字、身高、体重
• 2.2定义一个PersonCreate类:
• 2.2.1创建两个对象,分别是zhangsan,33岁,1.73;lishi,44,1.74
• 2.2.2分别调用对象的sayHello()方法。

public class Person {
    String name;
    double height;
    int weight;
    int age;
    public void sayhello() {
        System.out.println("hello,my name is " + name);
        System.out.println("my height is " + height + "米");
        System.out.println("my weight is " + weight + "斤");
        System.out.println("my age is " + age + "岁");
    }
}
public class PersonCreate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person a = new Person();
        Person b = new Person();
        a.name ="张三";
        a.age = 33;
        a.height = 1.73;
        a.weight = 135;
        
        b.name = "李十";
        b.age = 44;
        b.height = 1.74;
        b.weight = 140;
        
        a.sayhello();
        b.sayhello();
    }

}

原文地址:https://www.cnblogs.com/guoyongqi-blog/p/12760138.html