mercredi 26 août 2020

How do I add a condition on my main class

I was tasked to create a program that will let the user choose either to compute the area and perimeter of either a rectangle or triangle. However, I am having a problem with the perimeter part of triangle because the perimeter of a rectangle should be a+b>c or b+c> a, and this is the condition representation that I'd like to use: a+b>c, b+c>a, a+c>b,a!=0, b!=0, c!=0. By the way, I used abstract methods as it is what our professor taught us today. Here is my main class code:

public class Main {
  public static void main(String[]args) {
        Scanner in = new Scanner(System.in);
        int choice = 0;
        System.out.println("[1] Rectangle \n[2] Triangle\n Input your choice:");
        choice = in.nextInt();
        Rectangle obj = new Rectangle(0,0);
        Triangle obj1 = new Triangle(0,0,0,0,0);
    if (choice == 1)
    {
          System.out.println("You chose rectangle.");
          System.out.println("Please input width: ");
          obj.setData1(in.nextInt());
          System.out.println("Please input length: ");
          obj.setData2(in.nextInt());
          obj.computePerimeter();
          obj.computeArea();
          obj.display();
    }
    else if (choice == 2) 
    {

        System.out.println("You chose triangle.");
        System.out.println("Please input value for a: ");
        obj1.setData1(in.nextInt());
        System.out.println("Please input value for b: ");
        obj1.setData2(in.nextInt());
        System.out.println("Please input value for c: ");
        obj1.setC(in.nextInt());
        System.out.println("Please input value for base: ");
        obj1.setBase(in.nextInt());
        System.out.println("Please input value for height: ");
        obj1.setHeight(in.nextInt());
        obj1.computePerimeter();
        obj1.computeArea();
        obj1.display();
    
    } else{
       System.out.println("Invalid value.");
   }
  }
}

Aucun commentaire:

Enregistrer un commentaire