Java实验一 类的设计与对象使用

Java实验一 类的设计与对象使用_第1张图片

package ex1;

import java.util.Scanner;

class Circle{
	private double radius = 0;
	
	public Circle() {
		
	}
	
	public Circle(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return radius*radius*Math.PI;
	}
}


class Trapezoid{
	private double length1 = 0;
	private double length2 = 0;
	private double high = 0;
	
	public Trapezoid() {
		
	}
	
	public Trapezoid(double length1, double length2, double high){
		this.length1 = length1;
		this.length2 = length2;
		this.high = high;
	}
	
	public double getArea() {
		return (length1+length2)*high/2;
	}
}

public class Test {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		System.out.println("-----Class Circle Test-----");
		System.out.println("Please input the radius:");
		double radius = stdIn.nextDouble();
		Circle[] circleTest = new Circle[2];
		circleTest[0] = new Circle();
		circleTest[1] = new Circle(radius);
		System.out.println("The area of the circle1 is: " + circleTest[0].getArea());
		System.out.println("The area of the circle2 is: " + circleTest[1].getArea());
		
		System.out.println("-----Class Trapezoid Test-----");
		System.out.println("Please input the length1, length2, high in order:");
		double[] a = new double[3];
		for(int i=0; i<a.length; i++) {
			a[i] = stdIn.nextDouble();
		}
		Trapezoid trapezoidTest = new Trapezoid(a[0], a[1], a[2]);
		System.out.printf("The area of the Trapezozid is: %.2f", trapezoidTest.getArea());

	}

}

运行结果
Java实验一 类的设计与对象使用_第2张图片


更新
四种实现方式
1.

package ex1;	//第一次试验

import java.util.Scanner;


//继承实现

//抽象类Shape
abstract class Shape{
	abstract public double getArea();
}

//Circle继承Shape
class Circle extends Shape{
	private double radius = 0;
	
	public Circle() {
		
	}
	
	public Circle(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return radius*radius*Math.PI;
	}
}

//Trapezoid继承Shape
class Trapezoid extends Shape{
	private double length1 = 0;
	private double length2 = 0;
	private double high = 0;
	
	public Trapezoid() {
		
	}
	
	public Trapezoid(double length1, double length2, double high){
		this.length1 = length1;
		this.length2 = length2;
		this.high = high;
	}
	
	public double getArea() {
		return (length1+length2)*high/2;
	}
}

//测试类
public class Test {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		System.out.println("-----Class Circle Test-----");
		System.out.println("Please input the radius:");
		double radius = 0.0;
		do {
			radius = stdIn.nextDouble();
			if(radius < 0) {
				System.out.println("radius must > 0, please input again:");
			}
		}while(radius < 0);
		
		Circle[] circleTest = new Circle[2];
		circleTest[0] = new Circle();
		circleTest[1] = new Circle(radius);
		System.out.println("The area of the default circle1 is: " + circleTest[0].getArea());
		System.out.println("The area of the circle2 is: " + circleTest[1].getArea());
		
		System.out.println("-----Class Trapezoid Test-----");
		System.out.println("Please input the length1, length2, high in order:");
		double length1 = 0.0;
		double length2 = 0.0;
		double high = 0.0;
		do {
				length1 = stdIn.nextDouble();
				length2 = stdIn.nextDouble();
				high = stdIn.nextDouble();
				if(length1<0 || length2<0 || high<0) {
					System.out.println("Wrong!!! Please input again:");
				}
		}while(length1<0 || length2<0 || high<0);

		Trapezoid trapezoidTest = new Trapezoid(length1, length2, high);
		System.out.printf("The area of the Trapezozid is: %.2f", trapezoidTest.getArea());
		stdIn.close();
	}

}
package ex1;

import java.util.Scanner;


//接口实现
interface Shape1{
	public double getArea();
}

class Circle1 implements Shape1{
	private double radius = 0;
	
	public Circle1() {
		
	}
	
	public Circle1(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return radius*radius*Math.PI;
	}
}

class Trapezoid1 implements Shape1{
	private double length1 = 0;
	private double length2 = 0;
	private double high = 0;
	
	public Trapezoid1() {
		
	}
	
	public Trapezoid1(double length1, double length2, double high){
		this.length1 = length1;
		this.length2 = length2;
		this.high = high;
	}
	
	public double getArea() {
		return (length1+length2)*high/2;
	}
}

public class Test1 {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		System.out.println("-----Class Circle Test-----");
		System.out.println("Please input the radius:");
		double radius = 0.0;
		do {
			radius = stdIn.nextDouble();
			if(radius < 0) {
				System.out.println("radius must > 0, please input again:");
			}
		}while(radius < 0);
		
		Circle1[] circleTest = new Circle1[2];
		circleTest[0] = new Circle1();
		circleTest[1] = new Circle1(radius);
		System.out.println("The area of the default circle1 is: " + circleTest[0].getArea());
		System.out.println("The area of the circle2 is: " + circleTest[1].getArea());
		
		System.out.println("-----Class Trapezoid Test-----");
		System.out.println("Please input the length1, length2, high in order:");
		double length1 = 0.0;
		double length2 = 0.0;
		double high = 0.0;
		do {
				length1 = stdIn.nextDouble();
				length2 = stdIn.nextDouble();
				high = stdIn.nextDouble();
				if(length1<0 || length2<0 || high<0) {
					System.out.println("Wrong!!! Please input again:");
				}
		}while(length1<0 || length2<0 || high<0);

		Trapezoid1 trapezoidTest = new Trapezoid1(length1, length2, high);
		System.out.printf("The area of the Trapezozid is: %.2f", trapezoidTest.getArea());
		stdIn.close();

	}

}
package ex1;

import java.util.Scanner;

//直接用两个类实现

//圆类
class Circle2 {
	private double radius = 0;
	
	public Circle2() {
		
	}
	
	public Circle2(double radius) {
		this.radius = radius;
	}
	
	public double getArea() {
		return radius*radius*Math.PI;
	}
}

//梯形类
class Trapezoid2 {
	private double length1 = 0;
	private double length2 = 0;
	private double high = 0;
	
	public Trapezoid2() {
		
	}
	
	public Trapezoid2(double length1, double length2, double high){
		this.length1 = length1;
		this.length2 = length2;
		this.high = high;
	}
	
	public double getArea() {
		return (length1+length2)*high/2;
	}
}

public class Test2 {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		System.out.println("-----Class Circle Test-----");
		System.out.println("Please input the radius:");
		double radius = 0.0;
		do {
			radius = stdIn.nextDouble();
			if(radius < 0) {
				System.out.println("radius must > 0, please input again:");
			}
		}while(radius < 0);
		
		Circle2[] circleTest = new Circle2[2];
		circleTest[0] = new Circle2();
		circleTest[1] = new Circle2(radius);
		System.out.println("The area of the default circle1 is: " + circleTest[0].getArea());
		System.out.println("The area of the circle2 is: " + circleTest[1].getArea());
		
		System.out.println("-----Class Trapezoid Test-----");
		System.out.println("Please input the length1, length2, high in order:");
		double length1 = 0.0;
		double length2 = 0.0;
		double high = 0.0;
		do {
				length1 = stdIn.nextDouble();
				length2 = stdIn.nextDouble();
				high = stdIn.nextDouble();
				if(length1<0 || length2<0 || high<0) {
					System.out.println("Wrong!!! Please input again:");
				}
		}while(length1<0 || length2<0 || high<0);

		Trapezoid2 trapezoidTest = new Trapezoid2(length1, length2, high);
		System.out.printf("The area of the Trapezozid is: %.2f", trapezoidTest.getArea());
		stdIn.close();
	}

}

package ex1;

import java.util.Scanner;

//一个Shape类用多态实现
//重载getArea()函数

class Shape3{
	public Shape3() {
		
	}
	
	public double getArea(double radius) {
		return Math.PI*radius * radius;
	}
	
	public double getArea(double length1, double length2, double high) {
		return (length1+length2)*high/2;
	}
}

public class Test3 {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		System.out.println("-----Class Circle Test-----");
		System.out.println("Please input the radius:");
		double radius = 0.0;
		do {
			radius = stdIn.nextDouble();
			if(radius < 0) {
				System.out.println("Please input again:");
			}
		}while(radius < 0);
		
		Shape3 a = new Shape3();
		System.out.println("The area of the circle1 is: " + a.getArea(radius));
		
		System.out.println("-----Class Trapezoid Test-----");
		System.out.println("Please input the length1, length2, high in order:");
		double length1 = 0.0;
		double length2 = 0.0;
		double high = 0.0;
		do {
				length1 = stdIn.nextDouble();
				length2 = stdIn.nextDouble();
				high = stdIn.nextDouble();
				if(length1<0 || length2<0 || high<0) {
					System.out.println("Wrong!!! Please input again:");
				}
		}while(length1<0 || length2<0 || high<0);

		Shape3 b = new Shape3();
		System.out.printf("The area of the Trapezozid is: %.2f", b.getArea(length1, length2, high));
		stdIn.close();
	}

}

结果
Java实验一 类的设计与对象使用_第3张图片

你可能感兴趣的:(Java)