javaSE各阶段练习题--面向对象-多态-抽象类-接口

1 (多态)定义榨汁机JuiceMachine 有榨汁方法makeJuice,传入相应的水果。
   如果传入的是Apple 输出   "流出苹果汁"
       传入的是Orange 输出  "流出橙汁"
	   传入的是Banana 输出  "流出香蕉酱"
public class Test01 {
	
	public static void main(String[] args) {
		JuiceMachine jMachine = new JuiceMachine();
		Apple apple = new Apple();
		jMachine.makeJuice(apple);//流出苹果汁
		Orange orange = new Orange();
		jMachine.makeJuice(orange);
		Banana banana = new Banana();
		jMachine.makeJuice(banana);
	}

}


class JuiceMachine{
	public void makeJuice(Fruit f) {
		f.make();
	}
}

class Fruit{
	public void make() {
		System.out.println("制作水果汁");
	}
}

class Apple extends Fruit{
	public void make() {
		System.out.println("流出苹果汁");
	}
}

class Orange extends Fruit{
	public void make() {
		System.out.println("流出橙汁");
	}
}

class Banana extends Fruit{
	public void make() {
		System.out.println("流出香蕉酱");
	}
}
   
2 (多态)已知形状类  拥有一个计算面积的方法
           子类 矩形、圆形、三角形  
		   覆盖形状类的计算面积的方法 分别计算矩形、圆形、三角形的面积 
		   已知一个Object类型数组 装有 五个(各个图形的个数不定) 矩形、圆形、三角形的对象
           计算这五个对象的面积总和

设计形状类 和它的三个子类,完成上面的工作,  
提示: 三角形面积等于   半周长*(半周长-边1)*(半周长-边2)*(半周长-边2) 的积  开根号
  (海伦定理)
  
    如何开根号,请自行查阅Math类
 
public class Test02 {
	public static void main(String[] args) {
		
		Form rectangle = new Rectangle(5,4);
		Form rectangle2 = new Rectangle(8, 9);
		Form circle = new Circle(4);
		Form circle2 = new Circle(5);
		Form triangle = new Triangle(3, 4, 5);
		Object[] os = new Object[5];
		os[0] = rectangle;
		os[1] = rectangle2;
		os[2] = circle;
		os[3] = circle2;
		os[4] = triangle;
		double sum = 0;
		for (int i = 0; i < os.length; i++) {
			sum += ((Form) os[i]).cacluateArea();
		}
		System.out.println("面积总和为="+sum);
	}
}

//形状类
class Form{
	public double cacluateArea() {
		return 0;
	}
}

//矩形类
class Rectangle extends Form{
	
	public Rectangle(int length, int width) {
		super();
		this.length = length;
		this.width = width;
	}
	private int length;//长
	private int width;//宽
	public double cacluateArea() {
		return length*width;
	}
}
//圆
class Circle extends Form{
	
	public Circle(int r) {
		super();
		this.r = r;
	}
	private double r;//半径
	public double cacluateArea() {
		return (3.14*r*r);
	}
}
//三角
class Triangle extends Form{
	private int L1;//三边长度
	private int L2;
	private int L3;
	
	public Triangle(int l1, int l2, int l3) {
		super();
		L1 = l1;
		L2 = l2;
		L3 = l3;
	}

	//三角形面积等于   半周长*(半周长-边1)*(半周长-边2)*(半周长-边2) 的积  开根号
	public double cacluateArea() {
		int s = (L1+L2+L3)/2;
		int res = s*(s-L1)*(s-L2)*(s-L2);
		return ((int)(Math.sqrt(res)*100))/100.0;
	}
}
 

3 (抽象类)  雇员类(Employee-抽象类):包含抽象方法work()  和抽象方法 show()
	    work()方法表示 工作内容
		show()方法表示 员工属性的介绍
		
	程序员类:属性(姓名、工号、工资、奖金),行为(工作:软件开发)
    测试工程师:属性(姓名、工号、工资),行为(工作:软件测试)
    项目经理类:属性(姓名、工号、工资、奖金),行为(工作:控制进度)
	
	要求:子类在实现时,用System.out.println()在控制台输出
	例如: 程序员 work() 输出:"软件开发"
	              show() 输出:姓名为xxx 工号为xxx  ......
	
public class Test03 {
	public static void main(String[] args) {
		Employee e1 = new Programmer("技术总监", 30003, 16000, 4000);
		e1.work();
		e1.show();
		Employee e2 = new TestEngineer("首席测试师", 20001, 13000);
		e2.work();
		e2.show();
		Employee e3 = new Programmer("李孟冬", 10001, 20000, 8000);
		e3.work();
		e3.show();				
	}
}

abstract class Employee{//雇员类
	
	private String name;//姓名
	private int id;//工号
	private int salary;//工资
	
	public Employee(String name, int id, int salary) {
		super();
		this.name = name;
		this.id = id;
		this.salary = salary;
	}

	public String getName() {
		return name;
	}

	public int getId() {
		return id;
	}

	public int getSalary() {
		return salary;
	}

	public abstract void work();//工作内容
	public abstract void show();//员工属性的介绍
}
//程序猿类
class Programmer extends Employee{
	
	private int prize;//奖金
	
	public Programmer(String name, int id, int salary, int prize) {
		super(name, id, salary);
		this.prize = prize;
	}

	@Override
	public void work() {
		System.out.println("我的工作是软件开发。");
	}

	@Override
	public void show() {
		System.out.println("姓名:"+super.getName()+",工号:"+super.getId()+",工资:"
	+super.getSalary()+",奖金:"+this.prize);
	}
}
//测试工程师
class TestEngineer extends Employee{
	
	public TestEngineer(String name, int id, int salary) {
		super(name, id, salary);
	}

	@Override
	public void work() {
		System.out.println("我的工作是软件测试。");
	}

	@Override
	public void show() {
		System.out.println("姓名:"+super.getName()+",工号:"+super.getId()+",工资:"
	+super.getSalary());
	}
}
//项目经理
class ProjectManager extends Employee{
	
	private int prize;//奖金
	
		
	public ProjectManager(String name, int id, int salary, int prize) {
		super(name, id, salary);
		this.prize = prize;
	}

	@Override
	public void work() {
		System.out.println("我的工作是控制进度。");
	}

	@Override
	public void show() {
		System.out.println("姓名:"+super.getName()+",工号:"+super.getId()+",工资:"
	+super.getSalary()+",奖金:"+this.prize);
	}
}
			  



				  
4(接口)定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(),
然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法calculateArea(),
分别计算圆和矩形的面积。
public class Test04 {
	public static void main(String[] args) {
		MyCircle circle = new MyCircle(2);
		circle.calculateArea();
		MyRactangle ractangle = new MyRactangle(5, 8);
		ractangle.calculateArea();
	}
}

interface Area{
	void calculateArea();
}

class MyCircle implements Area{
	private double r;
	public MyCircle(double r) {
		this.r = r;
	}
	@Override
	public void calculateArea() {
		System.out.println(3.14*r*r);
	}	
}
class MyRactangle implements Area{
	private int length;
	private int width;
	public MyRactangle(int length, int width) {
		this.length = length;
		this.width = width;
	}
	@Override
	public void calculateArea() {
		System.out.println(length*width);
	}
}






5 .编写一个Shape接口,
具有一个draw 方法,
并编写三个类Triangle,Rectangle,Diamond都实现Shape 接口。
在3个类中分别实现draw方法打印如下星阵:
  *       ****      *
 ***      ****     ***
*****     ****      *
编写一个测试类具有一个测试方法,
使用Shape 参数,
方法体中调用Shape的draw 方法,打印出相应图形
public class Test05 {
	public static void main(String[] args) {
		Tria t = new Tria();
		t.draw();
		System.out.println("-------------");
		Rect r = new Rect();
		r.draw();
		System.out.println("-------------");
		Diamond d = new Diamond();
		d.draw();
	}
}

interface Shape{
	void draw();
}

class Tria implements Shape{
	@Override
	public void draw() {
		for (int i = 1; i <= 3; i++) {
			for (int j = i; j <= 2; j++) {
				System.out.print(" ");
			}
			for (int j = 1; j <= 2*i-1; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
class Rect implements Shape{
	@Override
	public void draw() {
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 4; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
class Diamond implements Shape{
	@Override
	public void draw() {
		System.out.println(" * ");
		System.out.println("***");
		System.out.println(" * ");
	}
}
				  

		  

 

你可能感兴趣的:(【JAVA】JAVA基础)