java - 如何将我创建的以下 Java 代码写入驱动程序并调用方法/类?

标签 java driver call

我最近刚刚完成了以下程序。现在,我们被要求在我提供给从驱动程序调用的类中的片段中制作以下代码,但我不知道它会是什么样子,它会如何工作,或者需要执行的术语这个。

/**
 * 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 类中删除 main(...) 方法。在 Area_Shapes 中创建一个接受必要参数的构造函数。创建一个具有 main(...) 方法的新类(例如 ShapesDriver)。收集驱动程序中的信息后,实例化 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()
}

关于java - 如何将我创建的以下 Java 代码写入驱动程序并调用方法/类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378057/

相关文章:

java - RESTEasy注解扫描找不到资源(Tomcat下)

java - 如何将存储库部分移动到父 build.gradle 配置?

java - 这段 Java 代码有点问题。初学者可能会与 C# 混合一些东西

python - 在 Windows 7 中与 USB 设备通信并仍然使用预先存在的驱动程序?

java - 在哪里可以找到适用于 32 位 Windows 的 JDK?

Java程序和mySQL连接问题: No suitable driver found

linux-kernel - I2C 和 SPI 驱动程序

java - 我可以在 Java 中调用带有字段值的方法吗?

Python 函数调用真的很慢

java - 打开联系人后如何直接调用电话