Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA introductory course Download the starter project LabRectangleWithMenu.zip,

ID: 673326 • Letter: J

Question

JAVA introductory course

Download the starter project LabRectangleWithMenu.zip, unzip the file and open it in jGrasp.
There are two files: Rectangle.java and RectangleMenu.java.
Rectangle.java includes the class Rectangle. This class is already completed and should not be modified. RectangleMenu includes a method called menuPrompt and main.

Complete the main method by doing the following:

Create an instance of Rectangle and initialize it with length 5 and width 4

Create an integer variable called selection. No need to initialize it at this point.

The variable selection needs to be declared before the do-while loop because we want to use it in

the condition at the end of the do-while loop.

Create a do-while loop that does the following:

call the method menuPrompt to prompt the user with a menu
read in a selction from the user (What is a good place to declare the Scanner instance? ) use a switch statement to respond to the user selection

o e.g. if user selects area the area of the rectangle should be printed
o if the user selects 0 the program should print good bye
o if the user enters a value less than 0 or greater 3 the program should print invalid input

The do-while loop should continue until the user enters 0 to exit do-while loop switch statement:

DO WHILE LOOP

do

{

statements(s)

}while (expressions);

SWITCH STATEMENTS

switch (selection) {

.
.
. default:

break; }

ZIP rectangle

public class Rectangle
{
   // private field(s)
   private int length;
   private int width;
  
// constructor(s)
   public Rectangle (int l, int w)
   {
       setLength(l);
       setWidth(w);
   }
  
   // public method(s)
  
   public int getLength()
   {
       return length;
   }
  
   public int getWidth()
   {
       return width;
   }
  
   public void setLength(int newLength)
   {
       if (newLength >= 0)
           length = newLength;
   }

   public void setWidth(int newWidth)
   {
       if (newWidth >= 0)
           width = newWidth;
   }
  
   public int area()
   {
       return length * width;
   }
  
   public int perimeter()
   {
       return (length + width) * 2;
   }
}

ZIP RectagleMenu

import java.util.Scanner;
public class RectangleMenu
{
   public static void main(String[] args)
   {
  
   }
  
   private static void menuPrompt()
   {
   System.out.println("1 .. area");
       System.out.println("2 .. perimeter");
       System.out.println("3 .. length and width");
       System.out.println("0 .. exit");
       System.out.print("Your choice: ");
   }
}

Explanation / Answer

/* I have wrote Main Method for you. Please have a look.*/

import java.util.Scanner;
class Rectangle
{
    // private field(s)
    private int length;
    private int width;
  
   // constructor(s)
    public Rectangle (int l, int w)
    {
        setLength(l);
        setWidth(w);
    }
  
    // public method(s)
  
    public int getLength()
    {
        return length;
    }
  
    public int getWidth()
    {
        return width;
    }
  
    public void setLength(int newLength)
    {
        if (newLength >= 0)
            length = newLength;
    }

    public void setWidth(int newWidth)
    {
        if (newWidth >= 0)
            width = newWidth;
    }
  
    public int area()
    {
        return length * width;
    }
  
    public int perimeter()
    {
        return (length + width) * 2;
    }
}  
  
  
  
   public class RectangleMenu
   {
        public static void main(String[] args)
        {

// created object of class rectangle to access Rectangle class methods and variables
            Rectangle r=new Rectangle(5,4); // length and breadth initialize to 5 and 4 respectively.
           int selection;

// Scanner object must be created outside the do while loop because there is no mean to create Scanner class object inside the loop because everytime it created new object if it is placed inside the loop. But be informed that we need to read user input every time not need selection object every time. With one object we can read as many times we want to read.
           Scanner s=new Scanner(System.in);
          
           do

           {

// first times menu will be printed in front of user.
               menuPrompt();

// read the input given by user.
               selection=s.nextInt();

// vallidation of input whether it is in range or not.
               if(selection>=0 && selection<=3){

// If yes, then applying switch case
               switch (selection) {

// If user input is 0 then program quit

                  case 0:
                      System.out.println("Good Bye");
                      System.exit(0);
                    break;

// If user input 1 then Area is printed.

                  case 1:
                      System.out.println("The Area is "+r.area());
                    break;

// If user input 2 then Perimeter is printed.
                  case 2:
                      System.out.println("The perimater is "+r.perimeter());
                      break;

// If user input 3 then Length and breadth is printed.
                  case 3:
                      System.out.println("The Length is "+r.getLength() + "The Breadth is "+ r.getWidth());

                default:

// I did not mention anything here because nothing is mentioned in question about what to do in default case.

               break; }
               }
              
               else
                      System.out.println("Invalid input");
  

           }while (selection!=0);
      
          }
      
        private static void menuPrompt()
        {
           System.out.println("1 .. area");
            System.out.println("2 .. perimeter");
            System.out.println("3 .. length and width");
            System.out.println("0 .. exit");
            System.out.print("Your choice: ");
        }
   }