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();

猜你喜欢

转载自blog.csdn.net/weixin_43986054/article/details/84944898
今日推荐