Java实验三

睡觉睡觉睡觉

13. 定义一个类A,该类有一个方法f,方法f可以输出英文字母,再定义一个类BA的子类,子类B不能重写父类A的方法f,子类有一个方法g,可以输出中文字符串。

   编写测试程序,创建B的对象,输出英文字母和中文字符串。

import java.util.*;

class A{
	void f(){
		System.out.println("Slow_Walker");
	}
}

class B extends A{
	void g(){
		System.out.println("软件工程");
	}
}

public class Test13 {
		public static void main(String args[]){
			B b=new B();
			b.f();
			b.g();
		}
}

14. 定义一个类SA,该类有一个方法f()public int f(int 1,int b),该方法返回ab的最大值。再定义该类的子类SB,要求子类重写父类的方法f,重写的方法将返回ab的最小公倍数。

   要求在重写的方法的方法体中首先调用被隐藏的方法返回ab的最大公约数m,然后将乘积(a*b/m返回。要求在测试程序中分别使用父类和子类创建对象,并分别调用方法f计算两个正整数的最大公约数和最小公倍数。

import java.util.*;

class SA{
	public int divisor(int a,int b){
		int r;
		while((r=a%b)!=0){
			a=b;
			b=r;
		}
		return b;
	}
	public int f(int a,int b){
		if(a>b)return a;
		else return b;
	}
}

class SB extends SA{
	public int f(int a,int b){
		int d;
		d=divisor(a,b);
		return a*b/d;
	}
}
public class Test14 {
	public static void main(String args[]){
		int a,b;
		Scanner in=new Scanner(System.in);
		a=in.nextInt();
		b=in.nextInt();
		SA sa=new SA();
		SB sb=new SB();
		System.out.println("最小公约数: "+sa.divisor(a,b)+" 最大公倍数: "+sb.f(a,b));
	}
}

16. 编写程序.定义类Vehlcles.属性包括sizecolorseats,还有一个构造方法和一个显示信息的方法。然后从此类中派生Car类,其中包括构造方法和显示信息的方法,并编  写程序进行测试。

import java.util.*;

class Vehicles{
	public String size,color,seats;
	public Vehicles(){
		this.size=null;
		this.color=null;
		this.seats=null;
	}
	public Vehicles(String size,String color,String seats){
		this.size=size;
		this.color=color;
		this.seats=seats;
	}
	public void display(){
		System.out.println(size+" "+color+" "+seats);
	}
}

class Car extends Vehicles{
	private String name;
	public Car(String name,String size,String color,String seats){
		this.name=name;
		this.size=size;
		this.color=color;
		this.seats=seats;
	}
	public void display(){
		System.out.println(name+" "+size+" "+color+" "+seats);
	}
}
public class Test16 {
		public static void main(String args[]){
			Car car=new Car("bycycle","small","black","big");
			car.display();
		}
}

17. 编写个矩形类MyRectangle,其中包括方法getHeigth(获得矩形的长度)getWidth方法(获得矩形的宽度)setHeigth方法(设置矩形的长度)setWidth方法(设置矩形的宽  度)getArea方法(求矩形的面积)showlnfo方法(显示矩形的格式),从矩形类派生出一个子类Mysquare代表正方形,并对gctArea()方法和showinfo()方法进行重写。并编  写程序进行测试。

import java.util.*;

class MyRectangle{
	public int height,width;
	public MyRectangle(){
		height=0;
		width=0;
	}
	public MyRectangle(int height,int width){
		this.height=height;
		this.width=width;
	}
	public void setHeight(int height){
		this.height=height;
	}
	public void setWidth(int width){
		this.width=width;
	}
	public int getHeight(){
		return height;
	}
	public int getWidth(){
		return width;
	}
	public int getArea(){
		return height*width;
	}
	public void showInformation(){
		System.out.println("长:"+height+" 宽:"+width);
	}
}

class Mysquare extends MyRectangle{
	public Mysquare(int a){
		height=a;
		width=a;
	}
	public void showInformation(){
		System.out.println("边长:"+height);
	}
}

public class Test17 {
		public static void main(String args[]){
			Mysquare mysquare=new Mysquare(10);
			mysquare.showInformation();
		}
}

19. 定义一个抽象类Area、两个Area的子类RectAreaRoundArea,以及一个实现类ImpleArea。要求如下

  (1)抽象类Area类中只包含一个抽象方法double area()

  (2)子类RoundArea类通过覆盖父类中的抽象方法area()来求圆的面积,另一个子类RectArea类通过覆盖父类中的抽象方法area()求长方形的面积。

  (3)圆的半径和长方形的边分别定义为子类RoundArea类和RectArea类的域,都为double娄型。

  (4ImpleArea类中创建对象,接收键盘输入,输入内容分别为圆的半径和长方形的边,并求出圆和长方形的面积,在屏幕上显示。

import java.util.*;

abstract class Area{
	abstract double area();
}

class RectArea extends Area{
	private double height,width;
	public RectArea(double height,double width){
		this.height=height;
		this.width=width;
	}
	public double area(){
		return height*width;
	}
}

class RoundArea extends Area{
	private double radius;
	public RoundArea(double radius){
		this.radius=radius;
	}
	public double area(){
		return radius*radius*Math.PI;
	}
}

public class Test19 {
		public static void main(String args[]){
			Scanner in=new Scanner(System.in);
			double h,w,r;
			h=in.nextDouble();
			w=in.nextDouble();
			r=in.nextDouble();
			RectArea rec=new RectArea(h,w);
			RoundArea rou=new RoundArea(r);
			System.out.printf("长方形面积: %.2f\n",rec.area());
			System.out.printf("圆形面积: %.2f\n",rou.area());
		}
}

20.编写一个Animal类,包含:

  (1)属性:private String type

  (2)构造方法;用于初始化私有属性type

  (3)方法:public string toString(),用于返回动物类型的信息public void sound(),用于输出动物的叫声信息。

21.编写一个Flyable接口,包含:

    方法:double flySpeed(),用于返回最大的飞行速度。

22.编写Giede(老鹰)类和Pigeon(鸽子)类,分别继承题16中的Animal类并实现20题中的F1yable接口。编写测试程序,显示输出老鹰和鸽子的叫声信息和最大飞行速度。

import java.util.*;

class Animal{
	public String type,voice;
	public Animal(){
		this.type=null;
		this.voice=null;
	}
	public Animal(String type,String voice){
		this.type=type;
		this.voice=voice;
	}
	public String toString(){
		return type;
	}
	public void sound(){
		System.out.println("叫声信息:"+voice);
	}
}

interface Flyable{
	public double flyable();
}

class Giede extends Animal implements Flyable{
	public Giede(String type,String voice){
		this.type=type;
		this.voice=voice;
	}	
	public double flyable(){
			return 50.0;
	}
}

class Pigeon extends Animal implements Flyable{
	public Pigeon(String type,String voice){
		this.type=type;
		this.voice=voice;
	}	
	public double flyable(){
		return 20.0;
	}
}

public class Test20_21_22 {
		public static void main(String args[]){
			Giede giede=new Giede("鹰","尖锐");
			Pigeon pigeon=new Pigeon("鸽子","和谐");
			System.out.println("动物类型: "+giede.type+" 动物声音: "+giede.voice+" 最大飞行速度: "+giede.flyable());
			System.out.println("动物类型: "+pigeon.type+" 动物声音: "+pigeon.voice+" 最大飞行速度: "+pigeon.flyable());
		}
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值