1. 程式人生 > >Java實驗一 類的設計與物件使用

Java實驗一 類的設計與物件使用

問題描述:編寫一個程式計算梯形和圓形的面積

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()); } }

執行結果
image

更新
四種實現方式
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(