如何将我创建的以下 Java 代码添加到驱动程序并调用 method/class?
How do I make the following Java code I created to a driver and call method/class?
我最近刚刚完成了以下程序。现在我们被要求在我提供的代码片段中将以下代码制作成从驱动程序调用的 class,但我不知道它的外观、工作方式或执行此操作所需的术语。
/**
* 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');
}
}
从概念上讲,您应该从 Area_Shapes
class 中删除 main(...)
方法。在 Area_Shapes
中创建一个接受必要参数的构造函数。创建一个具有 main(...)
方法的新 class(例如,ShapesDriver
)。收集完driver中的信息后,实例化Area_Shapes
class.
我实际上会创建多个 Shape classes 而不是一个 class 将所有东西混在一起,但这是更大的重构(但更多 OO-like)。我还会制作一种方法来计算和显示结果,而不是在构造函数中进行工作。
尽管如此,基本概念方法(编辑:向 driver class 添加了一个示例循环)
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;
...
}
}
}
Driver Class
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()
}
我最近刚刚完成了以下程序。现在我们被要求在我提供的代码片段中将以下代码制作成从驱动程序调用的 class,但我不知道它的外观、工作方式或执行此操作所需的术语。
/**
* 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');
}
}
从概念上讲,您应该从 Area_Shapes
class 中删除 main(...)
方法。在 Area_Shapes
中创建一个接受必要参数的构造函数。创建一个具有 main(...)
方法的新 class(例如,ShapesDriver
)。收集完driver中的信息后,实例化Area_Shapes
class.
我实际上会创建多个 Shape classes 而不是一个 class 将所有东西混在一起,但这是更大的重构(但更多 OO-like)。我还会制作一种方法来计算和显示结果,而不是在构造函数中进行工作。
尽管如此,基本概念方法(编辑:向 driver class 添加了一个示例循环)
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;
...
}
}
}
Driver Class
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()
}