In: Computer Science
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.
The Instance Variables
Constructing the Methods
Create the following methods:
? All of the methods below will be in the class Area.
menu()
setFirstDimension() (Mutator)
setSecondDimension() (Mutator)
areaOfRectangle()
areaOfTriangle()
displayOutput()
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 :