天天看点

Homework20191107作业

作业

1.

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.

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.

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.

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 .

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

}

}

继续阅读