第八周上机作业
/*3、定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。 [必做题]
• 3.1 无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
• 3.2 输出笔记本信息的方法
• 3.3 然后编写一个测试类,测试笔记本类的各个
方法。*/
package com.a03;
public class book {
char color;
int number;
public void a() {
System.out.println("该笔记本的型号是:" + color);
}
public void b() {
System.out.println("颜色为:" + number);
}
}
package com.a03;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
book n=new book();
n.color='黑';
n.number=45;
n.a();
n.b();
}
}
/*5、定义两个类,描述如下: [必做题]
• 5.1定义一个人类Person:
• 5.1.1定义一个方法sayHello(),可以向对方发出问
候语“hello,my name is XXX”
• 5.1.2有三个属性:名字、身高、体重
*/
package com.a03; public class Person { String name; int gao; int zong; public void sayHello() { System.out.println("hello,my name is" + name); System.out.println("身高" + gao+"cm"); System.out.println("体重" + zong+"kg"); } }
package com.a03; public class test2 { public static void main(String[] args) { // TODO Auto-generated method stub Person a1 = new Person(); a1.name = "王麻子"; a1.gao = 180; a1.zong = 65; a1.sayHello(); } }
/*• 5.2定义一个PersonCreate类:
• 5.2.1创建两个对象,分别是zhangsan,33岁,1.73
;lishi,44,1.74
• 5.2.2分别调用对象的sayHello()方法。
*/
package com.a03; public class PersonCrate { String name; int age; double shenggao; int tizhong; public void sayHello() { System.out.println("他的名字叫" + name); System.out.println("今年" + age + "岁"); System.out.println("身高" + shenggao + "米,体重" + tizhong + "kg"); } }
package com.a03; public class test3 { public static void main(String[] args) { // TODO Auto-generated method stub PersonCrate m1 = new PersonCrate(); m1.name = "zhangsan"; m1.age = 33; m1.shenggao = 1.73; m1.tizhong = 45; m1.sayHello(); System.out.println("========================"); PersonCrate m2 = new PersonCrate(); m2.name = "lishi"; m2.age = 44; m2.shenggao = 1.74; m2.tizhong = 44; m2.sayHello(); } }