第九周上机练习
1、定义一个点类Point,包含2个成员变量x、y分
别表示x和y坐标,2个构造器Point()和Point(int
x0,y0),以及一个movePoint(intdx,intdy)方法实
现点的位置移动,创建两个Point对象p1、p2,分
别调用movePoint方法后,打印p1和p2的坐标。[
必作题]
2、定义一个矩形类Rectangle:(知识点:对象的
创建和使用)[必做题]
•2.1定义三个方法:getArea()求面积、getPer()求
周长,showAll()分别在控制台输出长、宽、面积
、周长。
•2.2有2个属性:长length、宽width
•2.3通过构造方法Rectangle(intwidth,intlength),
分别给两个属性赋值
•2.4创建一个Rectangle对象,并输出相关信息
3、定义一个笔记本类,该类有颜色(char)和cpu
型号(int)两个属性。[必做题]
•3.1无参和有参的两个构造方法;有参构造方法可
以在创建对象的同时为每个属性赋值;
•3.2输出笔记本信息的方法
•3.3然后编写一个测试类,测试笔记本类的各个
方法。
6、定义两个类,描述如下:[必做题]
•6.1定义一个人类Person:
•6.1.1定义一个方法sayHello(),可以向对方发出
问候语“hello,mynameisXXX”
•6.1.2有三个属性:名字、身高、体重
•6.1.3通过构造方法,分别给三个属性赋值
•6.2定义一个Constructor类:
•6.2.1创建两个对象,分别是zhangsan,33岁,
1.73;lishi,44,1.74
•6.2.2分别调用对象的sayHello()方法。
1.package itheima01.com;
public class Point {
int x;
int y;
Point() {
System.out.println(x);
System.out.println(y);
}
Point(int x,int y) {
x=x;
y=y;
System.out.println(x);
System.out.println(y);
}
void movePoint(int p1,int p2) {
p1=x+20;
p2=y+20;
System.out.println("p1的坐标为"+p1+"\n"+"p2的坐标为"+p2);
}
}
package itheima01.com;
public class HelloWorld {
public static void main(String[] args) {
Point p=new Point();
p.x=60;
p.y=70;
p.movePoint(p.x,p.y);
}
}
2.package itheima01.com;
public class Rectangle {
private int length;
private int width;
Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public void showAll(){
System.out.println("矩形长为:"+length+"\n"+"矩形的宽为:"+width+"\n"+"矩形的面积为:"+getArea()+"\n"+"矩形的周长为:"+getPer());
}
public int getPer(){
return 2*(length+width);
}
public int getArea(){
return length*width;
}
}
package itheima01.com;
public class HelloWorld {
public static void main(String[] args) {
Rectangle r=new Rectangle(20,25);
r.showAll();
}
}
3.package itheima01.com;
public class 笔记本3 {
char color;
int cpu;
笔记本3(char string,int i){
color=string;
cpu=i;
void show 笔记本3(string,i);
System.out.println("该笔记本的颜色是"+color+ "该笔记本的cpu名称是"+cpu);
}
笔记本3(){
}
void show笔记本3(char string,int i){
color=string;
cpu=i;
System.out.println("该笔记本的颜色是"+color+" 该笔记本的cpu名称是"+cpu);
}
package itheima01.com;
public class HelloWorld {
public static void main(String[] args) {
笔记本3 d=new 笔记本3();
d.show笔记本3('r',774);
}
}
4.package itheima01.com;
public class Person {
String name;
double height;
int age;
Person(String name,double height,int age){
this.name=name;
this.height=height;
this.age=age;
}
void sayHello(){
System.out.println("Hello,my name is "+name+"."+
" My weight "+height+"kg"+" and"+" I am "+age+" years old"+".");
}
}
package itheima01.com;
public class PersonCreate {
public static void main(String[]args){
Person i=new Person("zhangsan",1.73,33);
Person j=new Person("lishi",1.74,44);
i.sayHello();
j.sayHello();
}
}




浙公网安备 33010602011771号