Java程序设计:选实验3 包及继承应用

发布时间:2024年01月17日

(1).?2023年杭州亚运会共设多种球类比赛项目,包括足球、篮球、排球、乒乓球、羽毛球、网球、橄榄球等,这些都可以称之为“球”。请通过分析,抽象它们所共有的性质,定义一个抽象类Ball。

package 选实验3;

//定义一个抽象类球
abstract class Ball
{
	int TotalMedal;
	int Gold;
	int Silver;
	int Copper;
	String BallName;
	public Ball(String n,int g,int s,int c)
	{
		BallName = n;
		Gold = g;
		Silver = s;
		Copper = c;
		TotalMedal = g+s+c;
	}
	abstract public void display();
}

//定义乒乓球子类
class PingPong extends Ball
{
	public PingPong(String n,int g,int s,int c) {
		super(n,g,s,c);
	}
	@Override
	public void display()
	{
		System.out.println("中国队在"+BallName+"中共获得"+TotalMedal+"枚奖牌:");
		System.out.println("金牌:"+Gold+"枚;银牌:"+Silver+"枚;铜牌:"+Copper+"枚。");
	}
}

//定义羽毛球球子类
class Badminton extends Ball
{
	public Badminton(String n,int g,int s,int c) {
		super(n,g,s,c);
	}
	@Override
	public void display()
	{
		System.out.println("中国队在"+BallName+"中共获得"+TotalMedal+"枚奖牌:");
		System.out.println("金牌:"+Gold+"枚;银牌:"+Silver+"枚;铜牌:"+Copper+"枚。");
	}
}

//定义网球球子类
class Tennis extends Ball
{
	public Tennis(String n,int g,int s,int c) {
		super(n,g,s,c);
	}
	@Override
	public void display()
	{
		System.out.println("中国队在"+BallName+"中共获得"+TotalMedal+"枚奖牌:");
		System.out.println("金牌:"+Gold+"枚;银牌:"+Silver+"枚;铜牌:"+Copper+"枚。");
	}
}

public class Test_ball {
	public static void main(String[] args) {
		//声明乒乓球对象
		Ball pingpong;
		pingpong = new PingPong("乒乓球",6,2,1);
		pingpong.display();
		
		//声明羽毛球对象
		Ball badminton;
		badminton = new Badminton("羽毛球",4,3,2);
		badminton.display();
		
		//声明网球对象S
		Ball tennis;
		tennis = new Tennis("网球",5,3,1);
		tennis.display();
	}
}

(2).编写一个抽象类Animal,它具有三个抽象方法moving, eating和yelling。分别实现这个抽象类的三个子类Tiger、Parrot和Dolphin。实现一个测试类,在测试类的main方法中分别使用这三个子类创建对象实体,然后通过它们的上转型对象变量调用moving()方法、eating()方法和yelling()方法。

package 选实验3;

//定义一个抽象类Animal
abstract class Animal
{
	//三个抽象方法moving, eating和yelling
	abstract public void moving();
	abstract public void eating();
	abstract public void yelling();
}

//定义Tiger子类
class Tiger extends Animal
{
	@Override
	public void moving()
	{
		System.out.println("老虎在草原上奔跑。");
	}
	@Override
	public void eating()
	{
		System.out.println("老虎在吃猪肉。");
	}
	@Override
	public void yelling()
	{
		System.out.println("老虎叫道:嗷呜!");
	}
}

//定义Parrot子类
class Parrot extends Animal
{
	@Override
	public void moving()
	{
		System.out.println("鹦鹉在天空中飞行。");
	}
	@Override
	public void eating()
	{
		System.out.println("鹦鹉在吃小米");
	}
	@Override
	public void yelling()
	{
		System.out.println("鹦鹉叫道:嘤咦!");
	}
}

//定义Dolphin子类
class Dolphin extends Animal
{
	@Override
	public void moving()
	{
		System.out.println("海豚在海洋中畅游。");
	}
	@Override
	public void eating()
	{
		System.out.println("海豚在吃小鱼");
	}
	@Override
	public void yelling()
	{
		System.out.println("海豚叫道:吱啾!");
	}
}

public class Test_animal {
	public static void main(String[] args) {
		
		Animal animal;
		
		//声明Tiger子类的对象实体tiger,animal为tiger的上转型变量
        Tiger tiger;
        tiger = new Tiger();
        animal = tiger;
        animal.moving();
        animal.eating();
        animal.yelling();
        
        //声明Parrot子类的对象实体parrot,animal为parrot的上转型变量
        Parrot parrot;
        parrot = new Parrot();
        animal = parrot;
        animal.moving();
        animal.eating();
        animal.yelling();
        
        //声明Dolphin子类的对象实体dolphin,animal为dolphin的上转型变量
        Dolphin dolphin;
        dolphin = new Dolphin();
        animal = dolphin;
        animal.moving();
        animal.eating();
        animal.yelling();
	}
}

(3).?一个五维向量由五个分量组成。五维向量的相加、相减和点乘等价于对应五个分量的相加、相减和相乘。比如两个五维向量(5,2,1,8,10)和(3,-1,0,-4,1),它们的和为(8,1,1,4,11),它们的差为(2,3,1,12,9),它们的点乘为(15,-2,0,-32,10)。编写一个接口Computable,它具有三个抽象方法add、minus和elementwiseProduct。编写一个Vector类,通过Computable接口实现五维向量的相加、相减和点乘。

package 选实验3;

//通过Computable接口实现五维向量的相加、相减和点乘
public class Vector implements Computable
{
	
	int v,w,x,y,z;
	
	Vector(int a,int b,int c,int d,int e)
	{
		v = a;
		w = b;
		x = c;
		y = d;
		z = e;
	}
	
	@Override
	public Vector add(Vector a)
	{
		Vector c = new Vector(0, 0, 0, 0, 0);
		c.v = this.v + a.v;
		c.w = this.w + a.w;
		c.x = this.x + a.x;
		c.y = this.y + a.y;
		c.z = this.z + a.z;
		return c;
	}
	
	@Override
	public Vector minus(Vector a)
	{
		Vector c = new Vector(0, 0, 0, 0, 0);
		c.v = this.v - a.v;
		c.w = this.w - a.w;
		c.x = this.x - a.x;
		c.y = this.y - a.y;
		c.z = this.z - a.z;
		return c;
	}
	
	@Override
	public Vector elementwiseProduct(Vector a)
	{
		Vector c = new Vector(0, 0, 0, 0, 0);
		c.v = this.v * a.v;
		c.w = this.w * a.w;
		c.x = this.x * a.x;
		c.y = this.y * a.y;
		c.z = this.z * a.z;
		return c;
	}
	
	public void display()
	{
		System.out.println("("+v+","+w+","+x+","+y+","+z+")");
	}
}
package 选实验3;

//定义一个Computable接口
public interface Computable {
	
     //三个抽象方法add,minus,elementwiseProduct
     abstract Vector add(Vector A);
     abstract Vector minus(Vector A);
     abstract Vector elementwiseProduct(Vector A);
}


package 选实验3;
public class Test_vector {
	public static void main(String[] args) {
		  
		  //声明两个Vector对象测试五维向量的相加,相减,点乘
          Vector vector1 = new Vector(5, 2, 1, 8, 10);
          Vector vector2 = new Vector(3, -1, 0, -4, 1);
          vector1.display();
          vector2.display();
          
          //临时变量vector3
          Vector vector3 = new Vector(0, 0, 0, 0, 0);
          
          //测试加法
          System.out.println("两者相加后为:");
          vector3 = vector1.add(vector2);
          vector3.display();
          
          //测试减法
          System.out.println("两者相减后为:");
          vector3=vector1.minus(vector2);
          vector3.display();
          
          //测试点乘
          System.out.println("两者点乘后为:");
          vector3=vector1.elementwiseProduct(vector2);
          vector3.display();
	}
}

(4).?Comparable接口定义了compareTo方法,用于比较两个对象,自行查找资料了解Comparable接口。设计一个GeometricObject类,实现Comparable接口,同时声明抽象方法getArea计算几何图形的面积。设计两个子类Circle和Rectangular,分别继承自GeometricObject。分别创建一个Circle对象和一个Rectangular对象,利用GeometricObject的compareTo方法比较两个对象的大小。

package 选实验3;

//定义一个GeometricObject类
class GeometricObject implements Comparable<GeometricObject>
{
	public GeometricObject() {}
	double getArea() {return 0;};
	@Override
	public int compareTo(GeometricObject o)
	{
		if(this.getArea()>o.getArea())
		{
			return 1;
		}
		else if(this.getArea()<o.getArea())
		{
			return -1;
		}
		else 
		{
			return 0;
		}
	}
}

//定义Circle子类
class Circle extends GeometricObject
{
	double radius;
	public Circle(double r) {
		radius = r;
	}
	@Override
	double getArea()
	{
		return 3.1415926*radius*radius;
	}
}

//定义Rectangular子类
class Rectangular extends GeometricObject
{
	double height,width;
	public Rectangular(double h,double w) {
		height = h;
		width = w;
	}
	@Override
	double getArea()
	{
		return height*width;
	}
}

public class Test_compare {

	public static void main(String[] args) {
		
        //创建一个Circle对象和一个Rectangular对象
		Circle circle = new Circle(3);
		Rectangular rectangular = new Rectangular(4,7);
		
		System.out.printf("圆的面积为%.3f\n", circle.getArea());
		System.out.printf("矩形的面积为%.3f\n",rectangular.getArea());
		
		System.out.println(circle.compareTo(rectangular));
		int check=circle.compareTo(rectangular);
		if(check==1)
		{
			System.out.println("圆大!");
		}
		else if(check==-1)
		{
			System.out.println("矩形大!");
		}
		else 
		{
			System.out.println("一样大!");
		}
	}
}

(5).?编写bin2Dec(String binaryString)方法,将一个二进制字符串转换为十进制数。(1)实现bin2Dec方法,在字符串不是一个二进制字符串时抛出NunberFormatException异常。编写测试程序,提示用户以字符串形式输入一个二进制数字并显示其对等的十进制数字。如果方法抛出异常,则显示“不是二进制数字”。(2)定义一个名为BinaryFormatException的自定义异常,实现bin2Dec方法,在字符串不是一个二进制字符串时抛出BinaryFormatException异常,重复步骤(1)的测试。

package 选实验3;

import java.util.Scanner;

public class Text_abnormal {

	public static int bin2Dec(String binaryString)
	     throws NumberFormatException
	{
		//使用正则表达式检查输入字符串是否为二进制数字
		if(!binaryString.matches("^[01]+$"))
		{
			throw new NumberFormatException("不是二进制数字");
		}
		
		//将二进制字符串转换为十进制数
		int decimal = 0;
		int length = binaryString.length();
		int sum2 = 1;//累积乘2值
		for(int i=length-1;i>=0;i--)
		{
			//将字符转换为数字
			int temp = binaryString.charAt(i)-'0';
			if(i!=length-1)sum2*=2;
			decimal += temp*sum2;
		}
		return decimal;
	}
	
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		
		try
		{
			System.out.print("请输入一个二进制数字: ");
            String binaryString = reader.nextLine();
            int decimal = bin2Dec(binaryString);
            System.out.println("对应的十进制数字为: " + decimal);
		}
		catch (NumberFormatException e) 
		{
			System.out.println("不是二进制数字");
		}
		reader.close();
	}
}
package 选实验3;

import java.util.Scanner;

class BinaryFormatException extends RuntimeException
{
    public BinaryFormatException() 
    {
        super("不是二进制数字");
    }
}

public class Test_abnormal2 {

	public static int bin2Dec(String binaryString)
	    throws BinaryFormatException
	{
		//使用正则表达式检查输入字符串是否为二进制数字
		if(!binaryString.matches("^[01]+$"))
		{
			throw new NumberFormatException("不是二进制数字");
		}
		
		//将二进制字符串转换为十进制数
		int decimal = 0;
		int length = binaryString.length();
		int sum2 = 1;//累积乘2值
		for(int i=length-1;i>=0;i--)
		{
			//将字符转换为数字
			int temp = binaryString.charAt(i)-'0';
			if(i!=length-1)sum2*=2;
			decimal += temp*sum2;
		}
		return decimal;
	}
	
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		
		try
		{
			System.out.print("请输入一个二进制数字: ");
            String binaryString = reader.nextLine();
            int decimal = bin2Dec(binaryString);
            System.out.println("对应的十进制数字为: " + decimal);
		}
		catch (BinaryFormatException e) 
		{
			System.out.println("不是二进制数字");
		}
		reader.close();

	}

}

文章来源:https://blog.csdn.net/gyeolhada/article/details/135598179
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。