java类与对象基础题(末考)总结二

Student

要求:(1)按照题目所给变量名称、类型和方法名称进行编程,禁止修改;
(2)本题中所有内容写在源文件App1.java中。
(3)编程环境可选,Eclipse和JDK均可。
1、学生类(Student)
成员变量:
学生名称(StdName),每个学生有三分:test1,test2 和test3类型 int
学生id (StdId) 和学生年龄 (StdAge)类型 int
构造方法:
构造方法(1),通过形参初始化名称(StdName)
构造方法(2),除了StdId和StdAge通过形参初始化所有成员变量(调用构造方法(1))
普通方法:
设置getter和setter方法用于获取和设置类中StdId和StdAge变量的值, StdId和StdAge不能为负数;
设置getter方法用于获取和类中StdName变量的值;
setScore(),publicvoid 有两个参数的方法:(i是test变量指标,如果i等于1那么
score等于test1)
public void setScore(int i,int score){
if(i == 1){
test1 = score;
}
}
getAverage(), publicint无参数的方法:返回测试分数的平均值
Student compare(Student),用于比较两个对象的年龄。

2、通过main方法测试
(1)创建两个Student对象std1、std2,判断这两个对象那个年龄大。
(2)分别调用getStdName()和getAverage()输出两个对象。
第一个学生名是Bill,age= 20, id= 101,test1= 80,test2=90 和test3=100
第二个学生名是Mike,age= 22, id= 102, test1= 80,test2=80 和test3=90
输出应该是
在这里插入图片描述
ps:在机房写的忘记拷贝了,直接拿坤坤的cv了嗝

package tongxin3;

public class AppStudent1 {
		public static void main(String []args){
			Student std1=new Student("Bill",80,90,100);
			std1.setStdAge(20);
			std1.setStdid(101);
			Student std2=new Student("Mike",80,80,90);
			std2.setStdAge(22);
			std2.setStdid(102);
			std1.compare(std2);
			System.out.println(std1.toString());
			System.out.println(std2.toString());
		}

	}
	class Student{
		String StdName;
		int test1,test2,test3;
		int Stdid,StdAge;
		Student(String StdName){
			this.StdName=StdName;
		}
		Student(String StdName,int test1,int test2,int test3){
			this(StdName);
			this.test1=test1;
			this.test2=test2;
			this.test3=test3;
		}
		public int getStdid() {
			return Stdid;
		}
		public void setStdid(int stdid) {
			if(stdid>=0)
			Stdid = stdid;
		}
		public int getStdAge() {
			return StdAge;
		}
		public void setStdAge(int stdAge) {
			if(stdAge>=0)
			StdAge = stdAge;
		}
		public String getStdName() {
			return StdName;
		}
		public void setScore(int i,int score){
			if(i==1)test1=score;
			else if(i==2)test2=score;
			else if(i==3)test3=score;
		}
		public int getAverage(){
			return (test1+test2+test3)/3;
		}
		public void compare(Student s){
			if(s.StdAge>this.StdAge){
				System.out.println(s.StdName+"年龄大");
			}else if(s.StdAge<this.StdAge){
				System.out.println(this.StdName+"年龄大");
			}else{
				System.out.println(this.StdName+"与"+s.StdName+"一样大");
			}
		}
		public String toString(){
			return "姓名:"+StdName+",年龄:"+StdAge+",编号:"+Stdid+"平均分:"+getAverage();
		}
	
}

Plane

要求:(1)按照题目所给变量名称、类型和方法名称进行编程,禁止修改;
(2)本题中所有内容写在源文件App1.java中。
(3)编程环境可选,Eclipse和JDK均可。
1、飞机类(Plane)
成员变量:
名称(pName) String类型 如:无人机,直升机,客机,战机
型号(pModel) String类型 如:波音747,空客320,武直9
自重(pWeight) int类型
速度(speed) double类型,飞机的速度;
构造方法:
构造方法(1),通过形参初始化名称(pName)
构造方法(2),通过形参初始化所有成员变量(调用构造方法(1))
普通方法:
getPModel(),返回飞机型号
show(),用于输出所有成员变量的值。
speedUp(int s),加速
speedDown(int s),减速
void compare(Plane),用于比较两个对象是否相等。
2、通过main方法测试
(1)创建两个Plane对象plane1、plane2,判断这两个对象是否相等。
(2)分别调用show()输出两个对象。
(3)调用加速和减速方法。

package tongxin3;

public class AppPlane1 {
public static void main(String[] args) {
	Plane plane1=new Plane("无人机", "747", 32, 500);
	Plane plane2=new Plane("直升机","320",35,600);
	plane1.show(plane1);
	plane2.show(plane2);
	plane1.compare(plane2);

}
}
class Plane{
	String pName;
	String pModel;
	int pWeight;
	double speed;
	public Plane(String pName) {
		super();
		this.pName = pName;
	}
	public Plane(String pName, String pModel, int pWeight, double speed) {
		this(pName);//调用pName有参构造器
		this.pName = pName;
		this.pModel = pModel;
		this.pWeight = pWeight;
		this.speed = speed;
	}
	public String getpModel() {
		return pModel;
	}
	public void setpModel(String pModel) {
		this.pModel = pModel;
	}
	public void speedUp(double s) {
		System.out.println("该加速方法已经被调用");
	}
	public void speedDown(double s) {
		System.out.println("该减速方法已经被调用");
	}
	public void show(Plane p) {
		System.out.println("飞机名称:"+p.pName+"飞机型号:"+p.pModel+"飞机自重:"+p .pWeight+"飞机速度:"+p.speed);
	}
	public void compare(Plane p) {
		if (p.pName.equals(this.pName)) {
			System.out.println("这两个飞机是同一种");
		}else {
			System.out.println("这两个飞机简直不同");
		}
	}
	
}

Rectangle

要求:(1)按照题目所给变量名称、类型和方法名称进行编程,禁止修改;
	(2)本题中所有内容写在源文件App1.java中。
	(3)编程环境可选,Eclipse和JDK均可。

1、编写一个矩形类Rectangle,包含:
成员变量:两个属性:矩形的宽width;矩形的高length。
两个构造器方法:
(1)一个带有两个参数的构造器方法,用于将width和length属性初化;
(2)一个不带参数的构造器,调用构造方法(1)将矩形初始化为宽和高都为10。
普通方法:
(1)设置getter和setter方法用于获取和设置类中length和width变量的值, set方法应该验证length和width都是大于0.0小于20.0的浮点数, 构造方法(一)调用setWidth()和setLength();
(2)求矩形面积的方法area()任何矩形的面积是它的长度乘以它的宽度
(3)求矩形周长的方法perimeter() 矩形的周长是(宽+高)x2
(4)包含一个判断矩形是否为正方形的谓词方法isSquare()
(5)void compare(Rectangle),用于比较两个对象是否相等。

2、通过main方法测试
(1)创建两个Rectangle对象rect1和rect2判断这两个对象是否相等。
rect1的 length和width 都为10。
Rect2的 length等于5和width 等于10。
(2)分别调用area()和perimeter ()输出两个对象。
(3)调用 isSquare()方法该方法检查那个矩形是否为方形

package tongxin3;

public class AppRectangle1 {
	public static void main(String[] args) {
		Rectangle rect1=new Rectangle(10, 10);
		rect1.area();
		rect1.perimeter();
		rect1.isSquare(rect1);
		Rectangle rect2=new Rectangle(10, 5);
		rect2.area();
		rect2.perimeter();
		rect2.isSquare(rect2);
		rect1.compare(rect2);

	}
}
class Rectangle{
	float width,length;

	public Rectangle(float width, float length) {
		super();
		this.width = width;
		this.length = length;
		this.setLength(length);
		this.setWidth(width);
	}

	public Rectangle() {
		this(10, 10);//一个不带参数的构造器,调用构造方法(1)将矩形初始化为宽和高都为10。
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(float w) {
		this.width = w;
		if (w>0&&w<20) {
			System.out.println("width是大于0.0小于20.0的浮点数");
		}else {
			System.out.println("width不是大于0.0小于20.0的浮点数");
		}
	}

	public double getLength() {
		return length;
	}

	public void setLength(float l) {
		this.length = l;
		if (l>0&&l<20) {
			System.out.println("length是大于0.0小于20.0的浮点数");
		}else {
			System.out.println("length不是大于0.0小于20.0的浮点数");
		}
	}
	public float area() {
		float area;
		return area=width*length;
	}
	public float perimeter() {
		float perimeter;
		return perimeter=(width+length)*2;
		
	}
	public void isSquare(Object object) {
		if (width==length) {
			System.out.println("这是一个正方形");
		}else {
			System.out.println("这是一个长方形");
		}
	}
	public void compare(Rectangle r) {
		if (this.width==r.width&&this.length==r.length) {
			System.out.println("这两个对象相等");
		}else {
			System.out.println("这两个对象不等");
		}
	}
	
	
}

ps:部分代码有疑问,某些方法写的不好,需要再次纠正,这个仅供参考
  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值