我刚刚完成了以下程序。现在我们被要求在snipet中创建以下代码,这些代码已经呈现在一个从驱动程序调用的类中,但我不知道它的外观,工作方式或术语需要做什么此
/**
* Will Calculate the Area of Given Shapes.
*
* @author Adrian Miranda
* @version (a version number or a date)
*/
import java.util.Scanner;
public class Area_Shapes
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
int Shape;
String A = "The Area is ";
double Area;
double Base;
double Height;
double q1;
double q2;
double radius;
double Length;
char response;
do
{
System.out.println(" Enter A Shape 1 = triangle 2 = square 3 = rhombus 4 = circle 5 = rectangle: ");
Shape = stdIn.nextInt();
if (Shape == 1)
{
System.out.println("Enter Base: ");
Base = stdIn.nextDouble();
System.out.println("Enter Height: ");
Height = stdIn.nextDouble();
Area = (Base * Height)/ 2 ;
System.out.println( A + Area);
System.out.println(" Enter Another Shape? (y/n): ");
response = stdIn.next() .charAt(0);
}
else if (Shape == 2)
{
System.out.println("Enter length ");
Length = stdIn.nextDouble();
Area = Length * Length;
System.out.println( A + Area);
System.out.println(" Enter Another Shape? (y/n): ");
response = stdIn.next() .charAt(0);
}
else if (Shape == 3)
{
System.out.println("Enter q1: ");
q1 = stdIn.nextDouble();
System.out.println("Enter q2: ");
q2 = stdIn.nextDouble();
Area = q1 * q2 * 0.5 ;
System.out.println( A + Area);
System.out.println(" Enter Another Shape? (y/n): ");
response = stdIn.next() .charAt(0);;
}
else if (Shape == 4)
{
System.out.println("Enter radius ");
radius = stdIn.nextDouble();
Area = radius * Math.PI * radius;
System.out.println( A + Area);
System.out.println(" Enter Another Shape? (y/n): ");
response = stdIn.next() .charAt(0);
}
else if (Shape == 5)
{
System.out.println("Enter Base: ");
Base = stdIn.nextDouble();
System.out.println("Enter Height: ");
Height = stdIn.nextDouble();
Area = (Base * Height) ;
System.out.println( A + Area);
System.out.println(" Enter Another Shape? (y/n): ");
response = stdIn.next() .charAt(0);
}
else
{
System.out.println("error, invalid shape, please enter a square triangle or rhombus.");
System.out.println(" Enter Another Shape? (y/n}: ");
response = stdIn.next() .charAt(0);;
}
}while (response == 'y');
}
}
答案 0 :(得分:2)
从概念上讲,您应该从main(...)
类中删除Area_Shapes
方法。在Area_Shapes
中创建一个接受必要参数的构造函数。创建一个具有ShapesDriver
方法的新类(例如main(...)
)。收集驱动程序中的信息后,实例化Area_Shapes
类。
我实际上会创建多个Shape类而不是将所有内容混合在一起的单个类,但这是更大的重构(但更像OO)。我还会创建一个计算和显示结果的方法,而不是在构造函数中完成工作。
尽管如此,基本的概念方法(编辑:为驱动程序类添加了一个示例循环)
public class Area_Shapes {
public Area_Shapes(int shape, double l1, double l2)
{
double area;
switch (shape) {
case 1:
area = (l1 * l2) / 2;
System.out.println("A triangle has an area of: " + area);
break;
...
}
}
}
驱动程序类
public class Driver {
public static void main(String args[]) {
boolean keepGoing = true;
do {
//collect shape information
System.out.println("Enter your shape (1 = triangle ...");
shape = stdIn.nextInt();
// based upon the shape, collect the inputs
double inp1, inp2;
switch (shape) {
case 1:
// inp1 is the base, inp2 is the height
System.out.println("Enter Base: ");
inp1 = stdIn.nextDouble();
System.out.println("Enter Height: ");
inp2 = stdIn.nextDouble();
break;
} //switch shape
// create a shape area instance; this calculates the
// area and outputs the answer
Shape_Area sa = new Shape_Area(shape, inp2, inp2);
// prompt the user to enter another shape
if (user_wants_to_stop) {
keepGoing = false;
}
} while (keepGoing)
} // main()
}