Question

In: Computer Science

CIT 149 JAVA 1 programming question. The Assignment This program will display a menu to the...

CIT 149 JAVA 1 programming question.

The Assignment

This program will display a menu to the user from which they can choose an option to find the area of a rectangle, the area of a triangle, or to quit.
The user will make the selection and will be prompted to enter the height and width, or the base and height, depending upon which selection they made. The program will then calculate the appropriate area and display the results.
This program will be in two classes, each in a separate file. One will contain the main method and the other will contain instance variables and methods which make up the program.

? Specifications

There are several ways in which this program could be written. However, in order to grade for following specifications and to have a clear path to a solution, it is required that you write your program using the specifications below.

  • Use two separate files with a single class in each one.
    Name the first file (and class) Area. This is the one that will have all of the methods other than the main method. Name the second file AreaRun. This file will contain the class that has the main method and the logic which controls the program.

The Instance Variables

  • In the class called Area, you will need to create the following instance variables. Each of these Variables will be private.
    • area
    • firstDimension
    • secondDimension

Constructing the Methods

Create the following methods:

? All of the methods below will be in the class Area.

  1. menu()

    • This method will display a menu with three options (see example above):
        1. Area of a Rectangle
        1. Area of a Triangle
        1. Quit
    • This method will return the user’s selection from the menu (int of 1, 2, or 3).
    • This method will contain a loop which will hold the user until a valid selection is made (see menu example above).
    • This method does not have parameters.
  2. setFirstDimension() (Mutator)

    • This method will accept one argument which will be a double.
    • This method will set the value of the instance variable called firstDimension in the Area class.
    • This method will contain a test of the argument coming in, to see that it is > 0. If it is less than zero, a message will be displayed and the program will end (see example above).
  3. setSecondDimension() (Mutator)

    • This method will accept one argument which will be a double and will set the instance variable secondDimension in the Area class.
      • This method will contain a test of the argument coming in, to see that it is > 0. If it is less than zero, a message will be displayed and the program will end (see example above).
    • This method will not return a value.
  4. areaOfRectangle()

    • This method will be a void method and does not have parameters. It will simply calculate the area of a rectangle:
      • area = firstDimension * secondDimension
      • Use the same variable names as given above.
  5. areaOfTriangle()

    • This method will be a void method and does not have parameters. It will simply calculate the area of a rectangle:
    • area = (firstDimension * secondDimension) / 2
    • Use the same variable names as given above.
  6. displayOutput()

    • This method will has three parameters: the menu selection (1 or 2), the firstDimension value, and the secondDimension value.
    • This method will display the first and second dimensions, and the calculated area of the rectangle or triangle.
    • Use if_ and else statements to test the menu selection variable passed to this method to see if the user selected the rectangle or triangle. Then make the output conform to the output specifications below.

Solutions

Expert Solution

Area.java :

//import package
import java.util.*;
//Java class
public class Area {
//instance variables
   private double area;
   private double firstDimension;
   private double secondDimension;
  
   //creating object of Scanner class
   Scanner sc=new Scanner(System.in);
   //method menu
   public int menu()
   {
       int choice=0;
       while(choice!=1 && choice!=2 && choice!=3)
       {
       //displaying menu to the user
       System.out.println("1.Area of a Rectangle\n2.Area of a Triangle\n3.Quit");
       System.out.print("Enter choice : ");//asking user choice
       choice=sc.nextInt();//reading choice
      
       }
       return choice;//return selection
   }
   //setter method
   public void setFirstDimension(double fd)
   {
       //checking value of fd
       if(fd>0)
       {
           //if value of fd is greater than 0
           this.firstDimension=fd;//set
       }
       else {
           System.out.println("First Dimension should be greater than 0");
      
       }
      
   }
   //setter method
       public void setSecondDimension(double sd)
       {
           //checking value of sd
           if(sd>0)
           {
               //if value of sd is greater than 0
               this.secondDimension=sd;//set
           }
           else {
               System.out.println("Second Dimension should be greater than 0");
           }
          
       }
       //method to calculate area of rectangle
       public void areaOfRectangle()
       {
           //calculate area of rectangle
           this.area=this.firstDimension*this.secondDimension;
       }
       //method to calculate area of triangle
       public void areaOfTriangle()
       {
           //calculate area of Triangle
           this.area = (this.firstDimension * this.secondDimension) / 2;
       }
       //method to display output
       public void displayOutput(int choice,double fd,double sd)
       {
           //checking choice
           if(choice==1)
           {
               //display area of rectangle
               System.out.println("Area of Rectangle with height "+this.firstDimension+" and width "+this.secondDimension+" is "+this.area);
           }
           else if(choice==2)
           {
               //display area of triangle
               System.out.println("Area of Triangle with base "+this.firstDimension+" and height "+this.secondDimension+" is "+this.area);
           }
       }
}
**************************

AreaRun.java :

//import package
import java.util.*;
//Java class
public class AreaRun {
   //entry point of the program , main() method
   public static void main(String[] args) {
       //creating object of Scanner class
       Scanner sc=new Scanner(System.in);
       //creating object of Area class
       Area a=new Area();
       while(true) {
       //calling method to display menu
       int selection=a.menu();
       //checking value of selection
       if(selection==1)
       {
           //to calculate area of Rectangle is selected then
           //asking user to enter height
           System.out.print("Enter height of Rectangle : ");
           int height=sc.nextInt();//reading height
           a.setFirstDimension(height);//setting height as first dimension
           //asking user to enter width
           System.out.print("Enter width of Rectangle : ");
           int width=sc.nextInt();//reading width
           a.setSecondDimension(width);//setting width
           //calling method to calculate area of rectangle
           a.areaOfRectangle();
           //calling method to display area of rectangle
           a.displayOutput(1,height,width);
          
       }
       else if(selection==2)
       {
           //to calculate area of Triangle is selected then
           //asking user to enter base
           System.out.print("Enter base of Triangle : ");
           int base=sc.nextInt();//reading base
           a.setSecondDimension(base);//setting base
           //asking user to enter height
           System.out.print("Enter height of Triangle : ");
           int height=sc.nextInt();//reading height
           a.setFirstDimension(height);//setting height as second dimension
           //calling method to calculate area of Triangle
           a.areaOfTriangle();
           //calling method to display area of Triangle
           a.displayOutput(2,base,height);          
       }
       else if(selection==3)
       {
           //if want to quit the program
           break;
       }
       }
   }

}
==========================

Output :


Related Solutions

CIT 149 JAVA 1 program question? Write a program that will use static methods as described...
CIT 149 JAVA 1 program question? Write a program that will use static methods as described in the specifications below. Utilizing the if and else statements, write a program which will calculate a commission based on a two-tiered commission plan of 3% and 7% paid on amounts of $15,000 or less, or over $15,000 in monthly sales by a sales force paid on a commission basis. Use the output specifications below. ? Specifications There will be two classes (separate files)...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain...
JAVA 1 PROGRAMMING QUESTION In this program you will be writing a class that will contain some methods. These will be regular methods (not static methods), so the class will have to be instantiated in order to use them. The class containing the main method will be provided and you will write the required methods and run the supplied class in order to test those methods. ? Specifications There are two separate files in this program. The class containing the...
The program is to be called CarClub The application will display menu as below 1. load...
The program is to be called CarClub The application will display menu as below 1. load cars 2. display all cars 3. search car 4 count cars older the 30 years 5 exit Please enter your option: Load car menu should read input data from text file name (car.txt) Car class have 4 data members which are. Car make, car model, car year and car price In addition If both car year and price were not provided, year has a...
Write a C++ program to run a menu driven program with the following choices: 1) Display...
Write a C++ program to run a menu driven program with the following choices: 1) Display the grades 2) Adjust grade 3) Display Average for each student 4) Display number of student with at least a B 5) Quit requirements: 1. Write a function called getValidGrade that allows a user to enter in an integer and loops until a valid number that is >= 0 and <= 100 is entered. It returns the valid value. 2. Write a function called...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT