第二次课记录
//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)访问数组中元素的值