java学习--day12(抽象类与接口作业)

作业1

利用接口做参数,写个计算器,能完成±*/运算

//(1)定义一个接口Compute含有一个方法int calc(int one,int two);
//(2)设计四个类分别实现此接口,完成+-*/运算
//(3)设计一个类UseCompute,含有方法:public void useCom(Compute com, int one, int two)
//此方法要求能够:1.用传递过来的对象调用computer方法完成运算
//2.输出运算的结果
//(4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算
package 抽象类与接口;
	interface Compute{
		void calc(int one,int two);
	}
	class plus implements Compute{
		public void calc(int one, int two) {
			double num=one+two;
			System.out.println("两个数的和为"+num);
		}		
	}
	class subtract implements Compute{
		public void calc(int one, int two) {			
			double num=one-two;
			System.out.println("两个数相减的值为:"+num);
		}		
	}
	class ride implements Compute{
		public void calc(int one, int two) {		
			double num =one*two;
			System.out.println("两个数相乘的值为:"+num);
		}		
	}
	class divide implements Compute{
		public void calc(int one, int two) {
			double num =one/two;
			System.out.println("两个数相除的值为:"+num);
		}		
	}
	class UseCompute{
		public void useCom(Compute com,int one,int two) {
			System.out.println("两个数的和为"+(one+two));
			System.out.println("两个数相减为"+(one-two));
			System.out.println("两个数相乘为"+(one*two));
			System.out.println("两个数相除为"+(one/two));
		}
	}
	
public class Demo {
	public static void main(String[] args) {
		/*
		plus p=new plus();
		p.calc(10, 2);
		subtract s =new subtract();
		s.calc(10, 2);
		ride r =new ride();
		r.calc(10,2);
		divide d =new divide();
		d.calc(10,2);
		*/
		UseCompute u =new UseCompute();
		u.useCom(new plus(), 10, 2);		
	}
}

作业2

package 抽象类与接口;
	/*
	(1)定义一个接口CanFly,描述会飞的方法public void fly();
	(2)分别定义类飞机和鸟,实现CanFly接口。
	(3)定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,
	再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法,
	让飞机和鸟起飞。
	 */
	interface CanFly{
		public abstract void fly();
	}
	class Plane implements CanFly{
		public void fly() {
			System.out.println("飞机在天上飞");
		}
		
	}
	class Bird implements CanFly{
		public void fly() {
			System.out.println("小鸟也在飞");
		}					
	}
public class Demo2 {
	public static void main(String[] args) {
		CanFly plane = new Plane();
		CanFly bird = new Bird();
		makeFly(plane);
		makeFly(bird);
		
	}
	public static void makeFly(CanFly canFly) {
		canFly.fly();
	}
}

作业3

编写程序,求柱体的体积

package 抽象类与接口;
	/*
	(1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();
	(2)、为柱体设计类pillar,要求:
	a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。
	b)实现构造方法,对成员变量赋值。
	c)包含成员方法,计算柱体Pillar的体积。
	(3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,
	分别用圆形、矩形作为柱体的底面,并计算其体积。
	 */
	interface Geometry{
		public double getArea();
	}
	class Pillar{
		Geometry bottom;
		double height;
		public Pillar(Geometry bottom,double height) {
			this.bottom=bottom;
			this.height=height;
		}
		public double Volume() {
			return bottom.getArea()*this.height;			
		}
	}
	class Circle implements Geometry{
		double radius;
		public Circle(double radius) {
			this.radius=radius;
		}
		public double getArea() {
			return Math.PI*this.radius*this.radius;
		}
	}
	class Rect implements Geometry{
		double wide,length;
		public Rect(double wide,double length) {
			this.wide=wide;
			this.length=length;
		}
		public double getArea() {
			return wide*length;
		}
	}
public class Demo3 {
	public static void main(String[] args) {
		Pillar pillar;
		Geometry bottom;
		bottom =new Rect(10,5);		
		pillar=new Pillar(bottom,5);
		System.out.println("矩形的柱体体积:"+pillar.Volume());
		bottom=new Circle(5);
		pillar =new Pillar(bottom,5);
		System.out.println("圆形柱体的体积:"+pillar.Volume());
	}
}

作业4

import java.util.Scanner;

/*1.定义一个canAttack()接口,其中有一个attack()抽象方法
2.定义一个canMove()接口,其中有一个move()抽象方法
3.定义一个抽象类Weapon(),实现canAttack()和canMove()两个接口,但不实现其中的抽象方法。
4.定义三个类,Tank坦克,Airplane战斗机,Ship军舰,
都继承Weapon武器分别用不同的方式实现 Weapon 类中的抽象方法
5.写一个类Army,代表一支军队,这个类有一个属性是Weapon数组weapons(用来存储该军队所拥有的所有武器)该类还
提供一个构造方法,在构造方法里通过传一个int类型的参数来限定该类所能拥有的最大武器数量,并用这一大小
来初始化数组w。该类还提供一个方法addWeapon(Weapon weapon),
表示把参数weapon所代表的武器加入到数组weapons中。在这
个类中还定义两个方法attackAll()和moveAll(),让weapons数组中的所有武器攻击和移动
6.定义一个主类来实现这些步骤。
 */
	interface canAttack{
		void attack(); 
	}
	interface canMove{
		void move();
	}
	abstract class Weapon implements canAttack,canMove{
		public void attack() {};
		public void move() {};
	}
	class Tank extends Weapon{
		public void attack() {
			System.out.println("坦克在开炮...");
		}
		public void move() {
			System.out.println("坦克在行驶...");
		}
	}
	class Ship extends Weapon{
		public void attack() {
			System.out.println("军舰在攻击中...");
		}
		public void move() {
			System.out.println("军舰移动在海洋中...");
		}
	}
	class Airplane extends Weapon{
		public void attack() {
			System.out.println("战斗机在攻击中...");
		}
		public void move() {
			System.out.println("战斗机在天上飞行...");
		}
	}
	class Army{
		int j=0;  
	    int n;  
	    Weapon[] w;
	    public int getMax() {     
	        return n;   
	    }            
	    public void setMax(int n){     
	        this.n = n;    
	        }  
	    public Army(int n)  
	    {  
	        this.n = n;   
	        w = new Weapon[n];  
	        System.out.println("您最多拥有"+n+"个武器!!!");                
	    }
	    public void addWeapon(Weapon wa) {
	    	if(j<getMax()) {
	    		System.out.println("武器足够使用!"+"已增加"+(j+1)+"个武器");
	    		w[j]=wa;
	    		j++;
	    	}else {
	    		System.out.println("武器足够,不能增加武器!!!");
	    	}
	    }
	    public void attackAll() {
	    	System.out.println();
	    	System.out.println("所有武器准备战斗!!");
	    	for(int i=0;i<j;j++) {
	    		System.out.println((i+1)+"号");
	    		w[i].attack();
	    	}
	    }
	    public void moveAll(){
	    	System.out.println();
	    	System.out.println("所有武器准备移动");
	    	for(int i=0;i<j;i++) {
	    		System.out.println((i+1)+"号");
	    		w[i].move();
	    	}
	    }
	}
public class Demo4 {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);  
        System.out.println("请输入武器库容量");  
        int n = s.nextInt();  
        Army a = new Army(n);  
        System.out.println("输入yes添加武器,输入其他则不录入");  
        String m = s.next();//添加武器  
          
        while(m.equals("yes"))  
        {  
            System.out.println("1.坦克  2.战斗机  3.军舰");  
            System.out.println("请选择:");  
            int l=s.nextInt(); 
        if(l==1)  
        {  
            a.addWeapon(new Tank());  
        }  
        else if(l==2)  
        {  
            a.addWeapon(new Airplane());  
        }  
        else if(l==3)  
        {  
            a.addWeapon(new Ship());    
        }  
        else{  
            System.out.println("输入错误");  
        }  
        System.out.println("输入yes添加武器,输入其他则退出");  
        m = s.next();  
        }  
        a.moveAll();     
        a.attackAll();                  
	}

作业5

package 抽象类与接口;
/*1.定义一个接口IShape,该接口中包含两个成员:周长和面积;
	2.分别定义四个类,矩形类:Rectangle,三角形类:Triangle,
	平行四边形类:Parallelogram,梯形类Echelon,这四个类都实现接口IShape,
	同时各类拥有自己的私有属性,比如说矩形的属性长和宽,平行四边形的属性边长和高,
	三角形的属性三个边长和高,梯形的属性上底、下底、腰长和高等,给每个类添加相应的构造方法,
	使各私有属性都能获得相应的值。
	3.定义一个测试类TestShape,在该类中定义一个方法,只要调用该方法就能获得对应类型的周长和面积,
	然后在该类中进行相关测试。
	注:类中描述的成员除上述内容外,可通过自己的想法自行添加,也可不添加。
 * 
 */
interface IShape{
	int getPerimeter();//求周长
	 int getArea();//求面积
}
class Triangle implements IShape {
	 
    private int length;
 
    private int width;
 
    public Triangle(int length, int width) {
        this.length = length;
        this.width = width;
    }
    public int getPerimeter() {
        return (length+width)*2;
    }  
    public int getArea() {
        return length*width;
    }
}
class Rectangle implements IShape {	 
    private int line1;
    private int line2;
    private int line3;
    private int height1;
    private int height2;
    private int height3;
    public Rectangle(int line1, int line2, int line3, int height1, int height2, int height3) {
        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
        this.height1 = height1;
        this.height2 = height2;
        this.height3 = height3;
    }
    public int getPerimeter() {
        return line1+line2+line3;
    } 
    public int getArea() {
        return line1*height1/2;
    }
}
class Parallelogram implements IShape {	 
    private int line1;
    private int line2;
    private int height1;
    private int height2;
    public Parallelogram(int line1, int line2, int height1, int height2) {
        this.line1 = line1;
        this.line2 = line2;
        this.height1 = height1;
        this.height2 = height2;
    } 
    public int getPerimeter() {
        return (line1+line2)*2;
    }
    public int getArea() {
        return line1*height1;
    }
}
class Echelon implements IShape {	 
    private int topLine;
    private int bottomLine;
    private int line1;
    private int line2;
    private int height; 
    public Echelon(int topLine, int bottomLine, int line1, int line2, int height) {
        this.topLine = topLine;
        this.bottomLine = bottomLine;
        this.line1 = line1;
        this.line2 = line2;
        this.height = height;
    }
    public int getPerimeter() {
        return topLine+bottomLine+line1+line2;
    }
    public int getArea() {
        return (topLine+bottomLine)*height/2;
    }
}
public class Demo5{
	public static void main(String[] args) {
		Triangle triangle = new Triangle(4,5);		        
        Rectangle rectangle = new Rectangle(3,4,5,4,3,6);      
        Parallelogram parallelogram = new Parallelogram(6,7,4,5); 
        Echelon echelon = new Echelon(3,4,6,7,8);
        System.out.println("矩形周长:"+triangle.getPerimeter());
        System.out.println("矩形面积:"+triangle.getArea()); 
        System.out.println("三角形周长:"+rectangle.getPerimeter());
        System.out.println("三角形面积:"+rectangle.getArea());
        System.out.println("平行四边形周长:"+parallelogram.getPerimeter());
        System.out.println("平行四边形面积:"+parallelogram.getArea()); 
        System.out.println("梯形周长:"+echelon.getPerimeter());
        System.out.println("梯形面积:"+echelon.getArea());   
	}
}

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余生羁绊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值