第二次課記錄
//Box.java
public class Box implements Geometry {
private double width;
private double length;
private double height;
private double scale;
private double vol;
public Box(double w, double l, double h,double s){
width = w;
length = l;
height = h;
scale = s;
vol = volume();
}
private double volume() {
// TODO Auto-generated method stub
return width*scale*height*scale*length*scale;
}
public double getVolume(){
return vol;
}
public String toString(){
return "Volume of Box:"+vol;
}
}
//Cylinder.java
public class Cylinder implements Geometry {
private double height;
private double radius;
private double scale;
private double vol;
public Cylinder(double h, double r, double s){
height = h;
radius = r;
scale = s;
vol = volume();
}
private double volume() {
// TODO Auto-generated method stub
return Math.PI*radius*radius*height*scale*scale*scale;
}
public double getVolume(){
return vol;
}
public String toString(){
return "Volume of Cylinder:"+vol;
}
}
//sphere.java
public class Sphere implements Geometry {
private double radius;
private double scale;
private double vol;
public Sphere(double r, double s){
radius = r;
scale = s;
vol = volume();
}
private double volume() {
// TODO Auto-generated method stub
return 4*Math.PI*radius*scale*scale*scale/3;
}
public double getVolume(){
return vol;
}
public String toString(){
return "Volume of Sphere:"+vol;
}
}
- 在主函數中初始化原始資料,然後在方法中實作對于對象的處理:1.求所有幾何體體積的合2.求出幾何體中最大的體積3.實作使用者輸入幾何體的參數,輸出幾何體的體積
- 如果不适用接口,在求解幾何體體積之和和最大體積的時候需要根據不同的類來寫結構相同的代碼,增加了代碼的重複量,接口中有三個類中通用的方法,可以簡化代碼量
import java.util.Scanner;
import java.util.Vector;
public class GeometryManager {
public static void main(String[] args) {
// TODO Auto-generated method stub
//initialize geometries.
Vector<Geometry> list;
list = new Vector<Geometry>();
list.add(new Box(10,10,10,1));
list.add(new Box(10,20,10,10));
list.add(new Box(4,2,1,10));
list.add(new Cylinder(10,20,0.1));
list.add(new Cylinder(10,5,1));
list.add(new Cylinder(8,10,10));
list.add(new Sphere(15,0.1));
list.add(new Sphere(20,1));
list.add(new Sphere(8,1));//implements的功能
System.out.println("initialization finished.");
//Task 1: 計算所有幾何體的體積之和,并控制台輸出結果;
countVolume(list);
//Task 2: 從所有幾何體中挑選出體積最大的幾何體,控制輸出結果
selectMaximal(list);
//Task 4: 使用者輸入需要計算的幾何體以及相應的參數,計算出相應的結果
AnswerUser();
}
public static void countVolume(Vector<Geometry> list){
double vol=0;
int size = list.size();
for(int i=0;i<size;i++)
vol += list.get(i).getVolume();
System.out.printf("%f", vol);
}
public static void selectMaximal(Vector<Geometry> list)
{
int size = list.size();
double max = 0;
int k=0;
for(int i=0;i<size;i++)
if(list.get(i).getVolume()>max)
{
max = list.get(i).getVolume();
k=i;
}
String str = list.get(k).toString();
System.out.printf("\n%s",str);
}
public static void AnswerUser()
{
Scanner in = new Scanner(System.in);
while(true)
{
String str = in.nextLine();
int len = str.length();
//提取出首字母,體積的類型
String item;
item = str.substring(0,1);
str=str.substring(2, len-1);
//建立一個字元串數組
String[] strs = str.split(",");
len = strs.length;
//分情況讨論
Vector<Geometry> list;
list = new Vector<Geometry>();
if(item.equals("B"))
{
//将提取出來的字元串轉換為數字
double a = Double.parseDouble(strs[0]);
double b = Double.parseDouble(strs[1]);
double c = Double.parseDouble(strs[2]);
double d = Double.parseDouble(strs[3]);
list.add(new Box(a,b,c,d));
double result = list.get(0).getVolume();
System.out.printf("%f",result);
}
else if (item.equals("C"))
{
double a = Double.parseDouble(strs[0]);
double b = Double.parseDouble(strs[1]);
double c = Double.parseDouble(strs[2]);
list.add(new Cylinder(a,b,c));
double result = list.get(0).getVolume();
System.out.printf("%f",result);
}
else if (item.equals("S"))
{
double a = Double.parseDouble(strs[0]);
double b = Double.parseDouble(strs[1]);
list.add(new Sphere(a,b));
double result = list.get(0).getVolume();
System.out.printf("%f",result);
}
}
}
}
- vector是一個動态數組,是以才可以使用.get(index)通路數組中元素的值