Java实验四

第一题:

import java.util.*;
class test411{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		double side1 = input.nextDouble();
		double side2 = input.nextDouble();
		double side3 = input.nextDouble();
		String c=input.next();
		boolean filled  = input.nextBoolean();
		Triangle t1 = new Triangle(side1,side2,side3);
		t1.setColor(c);
		t1.setFilled(filled);
		System.out.println("\t[Area: " + t1.getArea()+ " Perimeter: "+ t1.getPerimeter() + " Color: "+ t1.getColor()+ " Filled?: " + t1.getFilled() + "\t]");
	}
}
class GeometricObject{
		private String color="white";
		private boolean filled;
	
		public GeometricObject{}
		public String toString(){
			return "\tcolor:"+color+ "and filled:"+filled;
		}
		public void setColor(String color){
			this.color=color;
		}
		public void setFilled(boolean filled){
			this.filled=filled;
		}
		public String getColor(){
			return color;
		}
		public boolean getFilled(){
			return filled;
		}
	}
class Triangle extends GeometricObject{
		private double side1;
		private double side2;
		private double side3;
	
		public Triangle(){
			
			side1=side2=side3=1.0;
		}
		public Triangle(double side1,double side2,double side3){
			
			this.side1=side1;
			this.side2=side2;
			this.side3=side3;
		}
		public double getSide1(){
			return side1;
		}
		public double getSide2(){
			return side2;
		}
		public double getSide3(){
			return side3;
		}
		public double getArea(){
			double s=(side1+side2+side3)/2;
			return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
		}
		public double getPerimeter(){
			return side1+side2+side3;
		}
		public String toString(){
			return "Triangle: side1 = " + side1 + "side2 = " + side2 + " side3 = " + side3;
		}
	}

第二题:

import java.util.*;
class test4110{
	public static void main(String[] args){
		Tiger t1 = new Tiger();
		Eagle e1 = new Eagle();
		Scanner input = new Scanner(System.in);
		String a = input.nextLine();
		String b = input.nextLine();
		String e=e1.chushihua1(b);
		String t=t1.chushihua0(a);
		System.out.println(t+":    "+t1.shout()+"    "+t1.run()+"    ");
		System.out.println(e+":    "+e1.shout()+"    "+e1.run()+"     "+e1.fly());
	}
}
abstract class Animal{
	protected String describe;
	protected abstract String shout();
}
interface CanFly{
	public String fly();
}
class Tiger extends Animal{
	public String chushihua0(String a){
		describe=a;
		return describe;
	}
	public String shout(){
		return "Tiger shout";
	}
	public String run(){
		return "Tiger run";
	}
}
class Eagle extends Animal implements CanFly{
	public String chushihua1(String b){
		describe=b;
		return describe;
	}
	public String shout(){//chongxie
		return "Elage shout";
	}
	public String run(){
		return "Elage run";
	}
	@Override
	public String fly(){
		return "Elage flying";
	}
}

第二题修改

import java.util.*;
class test4110{
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		String a = input.nextLine();
		String b = input.nextLine();
		Animal t1 = new Tiger(a);//父类引用子类
		Animal e1 = new Eagle(b);
		System.out.println(a+"    "+t1.shout()+"    "+t1.run()+"    ");
		System.out.println(b+"    "+e1.shout()+"    "+e1.run()+"     "+e1.fly());
	}
}
abstract class Animal{
	protected String describe;
	protected abstract String shout();
	
	public abstract String run();
	public abstract String fly();//因为父类引用子类造成的抽象方法。多态
}
interface CanFly{
	public String fly();
	public abstract String shout();	//多态
	public String run();
}
class Tiger extends Animal{
	public Tiger(String a){
		this.describe=a;
	} 
	public String shout(){
		return "Tiger shout";
	}
	public String run(){
		return "Tiger run";
	}
	public String fly(){
		return " ";
	}
}
class Eagle extends Animal implements CanFly{

	public Eagle(String a){
		this.describe=a;
	} 
	public String shout(){
		return "Elage shout";
	}
	public String run(){
		return "Elage run";
	}
	@Override
	public String fly(){
		return "Elage flying";
	}
}

正式实验题目

import java.util.*;
class Household{ 
    private boolean on;
    private String brand;
    
    public Household(String a){
        this. brand=a;
    }
    public boolean getOn(){
        return on;
    }
    public void setOn(boolean on){
        this.on=on;
    }
    public String getBrand(){
        return brand;
    }
}
class TV extends Household{
    private int channel=1;
    private int volumnLevel=1;
    public TV(String a){
	super(a);
    }
    public void setChannel(int channel){
        this. channel=channel;
    }
    public void setVolumnLevel(int volumnLevel){
        this. volumnLevel=volumnLevel;
    }
    public int getChannel(){
        return channel;
    }
    public int getVolumnLevel(){
        return volumnLevel;
    }
    public String toString(){
        return "TV:    "+"brand:"+super.getBrand()+"    Channel:"+channel+"    Column level:"+volumnLevel;                            
    }
}
class WashingMachine extends Household{
    private int capacity;
    public WashingMachine(String a){
		super(a);
	}
    
    public int getCapacity(){
        return capacity;
    }
    public void setCapacity(int capacity){
        this. capacity=capacity;
    }
    public String toString(){
        return "WashingMachine:    brand:"+super.getBrand()+"     Capacity:"+capacity;
    }
}
class smartTV extends TV{
    public smartTV(String a){
		super(a);
	}
    public String toString(){
        return "smart TV:    brand:"+super.getBrand()+"    Channel:"+super.getChannel()+"    Column level:"+super.getVolumnLevel();
    }
}
class TestTV{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String a = input.nextLine();
        TV v = new TV(a);
        v.setChannel(3);
        v.setVolumnLevel(6);
        smartTV tv = new smartTV(a);
        WashingMachine wm = new WashingMachine(a);
         wm.setCapacity(9);
        System. out. println(v.toString());
        System. out. println(tv.toString());
        System. out. println(wm.toString());
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值