JAVA day07:代码作业(继承)

一、

建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求:  汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法

//Auto类
public class Auto {
	public int tire;//轮胎个数
	public double weight;//车身重量
	public String colour;//汽车颜色
	public double speed;//速度
	//加速,减速,停车
	public double speedUp(double up){
		speed+=up;
		return speed;
	}
	public double speedDown(double down){
		speed-=down;
		return speed;
	}
	public double stop(){
		speed=0;
		return speed;
	}
}

//Car类
public class Car extends Auto{
	//空调、CD
	public String kongTiao;
	public String CD;
	
	public Car(int tire,double weight,String colour,double speed,String kongTiao, String CD) {
		super();
		super.tire =tire;
		super.weight =weight;
		super.colour =colour;
		super.speed =speed;
		this.kongTiao = kongTiao;
		this.CD = CD;
	}

	public Car() {
		super();
	}
}

//测试类
public class Test {
	public static void main(String[] args) {
		Car car1=new Car(4,200,"红色",200,"开着","关着");
		Car car2=new Car();
		car2.tire=4;
		car2.weight=200;
		car2.colour="白色";
		car2.speed=100;
		car2.kongTiao="关着";
		car2.CD="关着";
		
		System.out.println("汽车1信息:");
		System.out.println("轮胎个数:"+car1.tire+"\t车身重量:"+car1.weight
				+"\t汽车颜色:"+car1.colour+"\t速度:"+car1.speed+"\t空调:"
				+car1.kongTiao+"\tCD:"+car1.CD);
		System.out.println("汽车2信息:");
		System.out.println("轮胎个数:"+car2.tire+"\t车身重量:"+car2.weight
				+"\t汽车颜色:"+car2.colour+"\t速度:"+car2.speed+"\t空调:"
				+car2.kongTiao+"\tCD:"+car2.CD);
		car1.speedUp(50);
		System.out.println("汽车1加速50后,速度:"+car1.speed);
		car2.speedDown(50);
		System.out.println("汽车2减速50后,速度:"+car2.speed);
		car1.stop();
		System.out.println("汽车1停下后,速度:"+car1.speed);
		car2.stop();
		System.out.println("汽车2停下后,速度:"+car1.speed);
	}
}

JAVA day07:代码作业(继承)_第1张图片

二、

写一个Person类,有编号id,姓名name,职位job。

构造方法带三个参数。

方法:

登陆login

注册register

自我介绍talk

写一个学生Student类继承Person类,方法有:考试test

属性有:学费money

写一个老师Teacher类继承Person类,

属性有 工资salary

方法有:工作work

写一个测试类TestPerson,测试学生和老师

学生:姓名-张三  职位-学生  学费-18000

老师:姓名-李四  职位-老师  工资-8000

//Person类
public class Person {
	//写一个Person类,有编号id,姓名name,职位job。
	public int id;
	public String name;
	public String job;
	//注册register
	public void register(Person p,int id,String name,String job){
		p.id = id;
		p.name = name;
		p.job = job;
		System.out.println("注册成功");
	}
	//登陆login
	public void login(Person[] pArr,int id,String name,String job){
		boolean dengLu=false;
		for (int i = 0; i < pArr.length; i++) {
			if(pArr[i].id==id&&pArr[i].name.equals(name)&&pArr[i].job.equals(job)){
				System.out.println("登陆成功");
				dengLu=true;
			}
		}
		if(dengLu==false){
			System.out.println("登录失败");
		}
	}
	//自我介绍talk
	public void talk(){
		System.out.println("id号:"+id+"\t姓名:"+name+"\t职位:"+job);
	}
	public Person(int id, String name, String job) {
		super();
		this.id = id;
		this.name = name;
		this.job = job;
	}
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
}

//Student类
public class Student extends Person{
//写一个学生Student类继承Person类,方法有:考试test
//属性有:学费money
	public double money;
	public void test(){
		System.out.println(job+"需要考试");
	}
}

//Teacher类
public class Teacher extends Person{
	//写一个老师Teacher类继承Person类,
	//属性有 工资salary 
	//方法有:工作work
	public double salary;
	public void work(){
		System.out.println(job+"需要工作");
	}
	@Override
	public void talk() {
		System.out.println("我是老师");
	}
	
}

//测试类
import java.util.Scanner;

public class TestPerson {
//写一个测试类TestPerson,测试学生和老师
	//学生:姓名-张三  职位-学生  学费-18000
	//老师:姓名-李四  职位-老师  工资-8000
	public static void main(String[] args) {
		Person[] pArr=new Person[2];
		Student stu=new Student();
		Teacher tec=new Teacher();
		stu.name="张三";
		stu.id=1;
		stu.job="学生";
		stu.money=18000;
		tec.name="李四";
		tec.id=2;
		tec.job="老师";
		tec.salary=8000;
		pArr[0]=stu;
		pArr[1]=tec;
		Scanner sc=new Scanner(System.in);
		boolean jiXu=true;
		while(jiXu){
			System.out.println("请选择功能:1登录\t2注册\t3退出");
			int xuan=sc.nextInt();
			switch (xuan) {
			case 1:
				System.out.println("开始登录");
				System.out.print("请输入序号:");
				int id=sc.nextInt();
				System.out.print("请输入姓名:");
				String name=sc.next();
				System.out.print("请输入职位:");
				String job=sc.next();
				pArr[0].login(pArr, id, name, job);
				break;
			case 2:
				Person[] p2=pArr;
				int num=p2.length;
				pArr=new Person[num+1];
				for (int i = 0; i < num; i++) {
					pArr[i]=p2[i];
				}
				pArr[num]=new Person(0,"","");
				System.out.println("开始注册");
				System.out.print("请输入序号:");
				int id2=sc.nextInt();
				System.out.print("请输入姓名:");
				String name2=sc.next();
				System.out.print("请输入职位:");
				String job2=sc.next();
				pArr[num].register(pArr[num],id2, name2, job2);
				break;
			case 3:
				jiXu=false;
				System.out.println("退出成功");
				break;
			default:
				System.out.println("输入错误,请重新输入");
			}
		}
	}
}

运行结果:

JAVA day07:代码作业(继承)_第2张图片

三、

定义一个父类:形状类Shapes,里面有两个方法,分别是求面积和周长。

定义一个子类:矩形Rectangle

定义一个子类:三角形 Triagle

定义一个子类:圆 Circle

定义一个测试类:传入圆的半径4 输出周长和面积

                传入矩形的长和宽4,5 输出周长和面积

                传入三角形三边:3,4,5 输出周长和面积

 

//父类
public class Shapes {
	public double area(){//面积
		double area=0;
		return area;
	}
	public double perimeter(){//周长
		double perimeter=0;
		return perimeter;
	}
}

//矩形类
public class Rectangle extends Shapes{
	public double chang;
	public double kuan;
	@Override
	public double area() {
		double area=chang*kuan;
		return area;
	}
	@Override
	public double perimeter() {
		double perimeter=(chang+kuan)*2;
		return perimeter;
	}
}

//三角形类
public class Triagle extends Shapes{
	public double a;
	public double b;
	public double c;
	@Override
	public double area(){
		double p=(a+b+c)/2;
		double area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
		return area;
	}
	@Override
	public double perimeter() {
		double perimeter = a+b+c;
		return perimeter;
	}
}

//圆类
public class Circle extends Shapes{
	public double r;
	@Override
	public double area() {
		double area=Math.PI*r*r;
		return area;
	}

	@Override
	public double perimeter() {
		double perimeter=2*Math.PI*r;
		return perimeter;
	}
}

//测试类
public class Test {
	public static void main(String[] args) {
		Rectangle cfx=new Rectangle();
		Triagle sjx=new Triagle();
		Circle yx=new Circle();
		cfx.chang=4;
		cfx.kuan=5;
		yx.r=4;
		sjx.a=3;
		sjx.b=4;
		sjx.c=5;
		System.out.println("圆形的周长为:"+yx.perimeter()+"\t面积为"+yx.area());
		System.out.println("矩形的周长为:"+cfx.perimeter()+"\t面积为"+cfx.area());
		System.out.println("三角形的周长为:"+sjx.perimeter()+"\t面积为"+sjx.area());
	}
}

 运行结果:

JAVA day07:代码作业(继承)_第3张图片

 

你可能感兴趣的:(JavaSE练习题,eclipse,java)