Homework20191107

作业

1./*(多态)定义榨汁机JuiceMachine 有榨汁方法makeJuice,传入相应的水果。

如果传入的是Apple 输出   "流出苹果汁"

    传入的是Orange 输出  "流出橙汁"

   传入的是Banana 输出  "流出香蕉酱"*/

public class Test01 {

 

public static void main(String[] args) {

JuiceMachine jm = new JuiceMachine();

Apple a = new Apple();

jm.makeJuice(a);

Orange o = new Orange();

jm.makeJuice(o);

Banana b = new Banana();

jm.makeJuice(b);

}

 

}

 

//榨汁机类

class JuiceMachine{

//多态的使用,传入参数

public void makeJuice(Fruit f) {

f.flow();

}

}

 

//抽象水果类

abstract class Fruit {

public abstract void flow();

}

 

//橘子类

class Orange extends Fruit{

public void flow() {

System.out.println("流出橙汁");

}

}

 

//苹果类

class Apple extends Fruit{

public void flow() {

System.out.println("流出苹果汁");

}

}

 

//香蕉类

class Banana extends Fruit{

public void flow() {

System.out.println("流出香蕉酱");

}

}    

2./*(多态)已知形状类  拥有一个计算面积的方法

子类 矩形、圆形、三角形  

覆盖形状类的计算面积的方法 分别计算矩形、圆形、三角形的面积

已知一个Object类型数组 装有 五个(各个图形的个数不定)

矩形、圆形、三角形的对象

计算这五个对象的面积总和

 

设计形状类 和它的三个子类,完成上面的工作,  

提示: 三角形面积等于   

半周长*(半周长-边1)*(半周长-边2)*(半周长-边2) 的积  开根号

(海伦定理)

如何开根号,请自行查阅Math类*/

public class Test02 {

public static void main(String[] args) {

Object[] object = new Object[5];

Rectangle r1 = new Rectangle(2,3);

Rectangle r2 = new Rectangle(2,3);

Triangle t1 = new Triangle(3,4,5);

Triangle t2 = new Triangle(6,8,10);

Circle c1 = new Circle(4);

object[0] = r1;object[1] = r2;object[2] = t1;

object[3] = t2;object[4] = c1;

double sum = 0;

for(int n = 0; n < object.length; n++) {

if(object[n] instanceof Rectangle ) {

Rectangle r = (Rectangle)object[n];

sum += r.area();

}else if(object[n] instanceof Circle) {

Circle c = (Circle)object[n];

sum += c.area();

}else if(object[n] instanceof Triangle) {

Triangle t = (Triangle)object[n];

sum += t.area();

}

}

System.out.println(sum);

}

}

 

 

 

//形状类

abstract class Shape{

//计算面积的抽象方法

public abstract double area();

}

 

//矩形类

class Rectangle extends Shape{

double length;

double width;

 

public Rectangle(double length, double width) {

super();

this.length = length;

this.width = width;

}

 

public double area() {

return length*width;

}

}

 

//圆形类

class Circle extends Shape{

double radius;

public Circle(double radius) {

super();

this.radius = radius;

}

public double area() {

return Math.PI*Math.pow(radius, 2);

}

}

//三角形类

class Triangle extends Shape{

double long1;

double long2;

public Triangle(double long1, double long2, double long3) {

super();

this.long1 = long1;

this.long2 = long2;

this.long3 = long3;

}

double long3;

public double area() {

double sum = (long1 + long2 + long3)/2;

return Math.sqrt(sum*(sum-long1)*(sum-long2)*(sum-long3));

}

}

  

 

3./*(抽象类)  雇员类(Employee-抽象类):

 * 包含抽象方法work() 和抽象方法 show()

work()方法表示 工作内容

show()方法表示 员工属性的介绍

 

程序员类:属性(姓名、工号、工资、奖金),行为(工作:软件开发)

测试工程师:属性(姓名、工号、工资),行为(工作:软件测试)

项目经理类:属性(姓名、工号、工资、奖金),行为(工作:控制进度)

 

要求:子类在实现时,用System.out.println()在控制台输出

例如: 程序员 work() 输出:"软件开发"

          show() 输出:姓名为xxx 工号为xxx  ......*/

public class Test03 {

public static void main(String[] args) {

Programmer p = new Programmer("熊落落",2016034,15000,3000);

p.work();

p.show();

Tester t = new Tester("李鹏鹏",2016035,17000);

t.work();

t.show();

Manager m = new Manager("崔美娟",2016036,30000,6000);

m.work();

m.show();

}

}

 

 

//抽象类 雇员类

abstract class Employee{

String name;

int id;

double salary;

//抽象方法work()

public abstract void work();

//抽象方法show()

public abstract void show();

}

 

 

//程序员类

class Programmer extends Employee{

double bonus;

public void work() {

System.out.println("软件开发");

}

public Programmer() {}

public Programmer(String name,int id, double salary,double bonus) {

super();

this.name = name;

this.id = id;

this.salary = salary;

this.bonus = bonus;

}

 

public void show() {

System.out.println("姓名为" + name + "  工号为" + id+ "  工资为" + salary+ "  奖金为" + bonus);

}

}

 

 

//测试工程师类

class Tester extends Employee{

public void work() {

System.out.println("软件测试");

}

public Tester() {}

public Tester(String name,int id, double salary) {

super();

this.name = name;

this.id = id;

this.salary = salary;

}

public void show() {

System.out.println("姓名为" + name + "  工号为" + id+ "  工资为" + salary);

}

}

 

//项目经理类

class Manager extends Employee{

double bonus;

public Manager() {}

public Manager(String name,int id, double salary,double bonus) {

super();

this.name = name;

this.id = id;

this.salary = salary;

this.bonus = bonus;

}

public void work() {

System.out.println("控制进度");

}

public void show() {

System.out.println("姓名为" + name + "  工号为" + id+ "  工资为" + salary+ "  奖金为" + bonus);

}

}      

4./*(接口)定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(),

然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法calculateArea(),

分别计算圆和矩形的面积。*/

public class Test04 {

public static void main(String[] args) {

MyCircle mc = new MyCircle(4);

double area1 = mc.calculateArea();

System.out.println(String.format("%.2f", area1));

 

MyRectangle mr = new MyRectangle(3,4);

double area2 = mr.calculateArea();

System.out.println(area2);

 

}

}

 

//接口Area

interface Area{

public abstract double calculateArea();

}

 

//MyCircle类

class MyCircle implements Area{

double radius;

public double calculateArea() {

return Math.PI*Math.pow(radius, 2);

}

public MyCircle() {

super();

// TODO Auto-generated constructor stub

}

public MyCircle(double radius) {

super();

this.radius = radius;

}

}

 

//MyRectangle类

class MyRectangle implements Area{

double longth;

double width;

public MyRectangle() {

super();

// TODO Auto-generated constructor stub

}

public MyRectangle(double longth, double width) {

super();

this.longth = longth;

this.width = width;

}

public double calculateArea() {

return longth*width;

}

}

 

5 ./*编写一个Shape接口,

具有一个draw 方法,

并编写三个类Triangle,Rectangle,Diamond都实现Shape 接口。

在3个类中分别实现draw方法打印如下星阵:

  *       ****      *

 ***      ****     ***

*****     ****      *

编写一个测试类具有一个测试方法,

使用Shape 参数,

方法体中调用Shape的draw 方法,打印出相应图形*/

public class Test05 {

 

public static void main(String[] args) {

System.out.println("三角形:");

Triangle t = new Triangle();

test(t);

System.out.println("长方形:");

Rectangle r = new Rectangle();

test(r);

System.out.println("菱形:");

Diamond d = new Diamond();

test(d);

 

}

public static void test(Shape shape) {

if(shape instanceof Triangle) {

Triangle t = (Triangle)shape;

t.draw();

}else if(shape instanceof Rectangle) {

Rectangle r = (Rectangle)shape;

r.draw();

}else if (shape instanceof Diamond) {

Diamond d = (Diamond)shape;

d.draw();

}

}

 

}

 

 

//Shape接口

interface Shape{

public abstract void draw();

}

 

//Triangle类

class Triangle implements Shape{

public void draw() {

for(int n1 = 1; n1 <= 3; n1++) {

for(int n2 = 3-n1; n2 > 0;n2--) {

System.out.print(" ");

}

for(int n3 = 1; n3 <= 2*n1-1; n3++) {

System.out.print("*");

}

System.out.println();

}

}

}

 

//Rectangle类

class Rectangle implements Shape{

public void draw() {

for(int n1 = 1; n1 <= 3;n1++)

{

for(int n2 = 1; n2 <= 4;n2++)

{

System.out.print("*");

}

System.out.println();

}

}

}

 

//Diamond类

class Diamond implements Shape{

public void draw() {

for(int n1 = 1; n1 <= 2; n1++) {

for(int n2 = 2-n1; n2 > 0; n2--) {

System.out.print(" ");

}

for(int n3 = 1; n3 <= 2*n1-1;n3++) {

System.out.print("*");

}

System.out.println();

}

System.out.println(" *");

}

}

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值