1. 程式人生 > >2018/12/10作業

2018/12/10作業

2018/12/10作業

1.定義一個點類Point,包含2個成員變數x、y分別表示x和y座標,2個構造器Point()和Point(int x0,y0),以及一個movePoint(int dx,int dy)方法實現點的位置移動,建立兩個Point物件p1、p2,分別呼叫movePoint方法後,列印p1和p2的座標。

	int x ;
	int y;
	Point(){
		
	}
	Point (int x0,int y0){
		x = x0;
		y = y0;
	}
	void movePoint(){
		x = x+10;
		y = y-10;
		System.out.println
("座標是"+x+","+y); }

主函式:

int x= 15;
		int y = 20;
		Point p1 = new Point();
		p1.x=x;
		p1.y=y;
		p1.movePoint();
		
		Point p2 = new Point(15,20);
		p2.movePoint();

2.定義一個矩形類Rectangle: [必做題]
2.1 定義三個方法:getArea()求面積、getPer()求周長,showAll()分別在控制檯輸出長、寬、面積、周長。
2.2 有2個屬性:長length、寬width
2.3 通過構造方法Rectangle(int width, int length),分別給兩個屬性賦值
2.4 建立一個Rectangle物件,並輸出相關資訊

	int length;
	int width;
	Rectangle(){
		
	}
	Rectangle (int length,int width){
		this.length = length;
		this.width = width;
	} 
	int getArea(){
		int area = length*width;
		return area;
	}
	int getPer(){
		int per = 2*(length+width);
		return per;
	
	}
	void showAll(){
		int area = this.getArea
(); int per = this.getPer(); System.out.println("長:"+length+"寬:"+width+"面積:"+area+"周長"+per); }

主函式

Rectangle r =  new Rectangle(15,20);
		r.showAll();

3.定義一個筆記本類,該類有顏色(char)和cpu型號(int)兩個屬性。 [必做題]
3.1 無參和有參的兩個構造方法;有參構造方法可以在建立物件的同時為每個屬性賦值;
3.2 輸出筆記本資訊的方法
3.3 然後編寫一個測試類,測試筆記本類的各個方法。

	char color;
	int cpu;
	
	Note(){
		
	}
	Note(char color,int cpu){
		this.color = color;
		this.cpu = cpu;
	}
	void Note1(){
		System.out.println("筆記本的顏色為: "+color);
		System.out.println("筆記本的CPU為: "+cpu);
	}

主函式

	Note note = new Note('黑',6);
		note.Note1();
	}

4.定義兩個類,描述如下: [必做題]
4.1定義一個人類Person:
4.1.1定義一個方法sayHello(),可以向對方發出問候語“hello,my name is XXX”
4.1.2有三個屬性:名字、身高、體重
4.2定義一個PersonCreate類:
4.2.1建立兩個物件,分別是zhangsan,33歲,1.73;lishi,44,1.74
4.2.2分別呼叫物件的sayHello()方法。

	String name;
	int age;
	int high;
	int weight;
	Person(){
		
	}
	Person (String name,int high,int weight){
		this.name = name;
		this.high = high;
		this.weight = weight;
	}
	void sayHello(){
		System.out.println("Hello My name is:"+name);
	}

主函式

		String name1 = "zhangsan";
		String name2 = "lishi";
		int age1 = 33;
		int age2 = 44;
		int high1 = 173;
		int high2 = 174;
		Person p1 = new Person();
		p1.name = name1;
		p1.age = age1;
		p1.high =high1;
		Person p2 = new Person();
		p2.name = name2;
		p2.age = age2;
		p2.high = high2;
		p1.sayHello();
		p2.sayHello();

5定義兩個類,描述如下: [必做題]
5.1定義一個人類Person:
5.1.1定義一個方法sayHello(),可以向對方發出問候語“hello,my name is XXX”
5.1.2有三個屬性:名字、身高、體重
5.1.3通過構造方法,分別給三個屬性賦值
5.2定義一個Constructor類:
5.2.1建立兩個物件,分別是zhangsan,33歲,1.73;lishi,44,1.74
5.2.2分別呼叫物件的sayHello()方法。

	String name;
	int age;
	int high;
	int weight;
	Person(){
		
	}
	Person (String name,int high,int weight){
		this.name = name;
		this.high = high;
		this.weight = weight;
	}
	void sayHello(){
		System.out.println("Hello My name is:"+name);
	}

主函式

	String name1 = "zhangsan";
		String name2 = "lishi";
		int age1 = 33;
		int age2 = 44;
		int high1 = 173;
		int high2 = 174;
		Person p1 = new Person(name1,age1,high1);
		p1.sayHello();
		Person p2 = new Person(name2,age2,high2);
		p2.sayHello();