Work on Static methods continue to practice to write the code for th.docx
1. Work on Static methods continue to practice to write the code
for the data type class Learn what is static members in data
type class and how to write static methods Learn how to access
static members of data type class from main() REQUIREMENT
STATEMENT Provide the application that helps users to
calculate the area of the following list of shapes. Square
Rectangle Triangle Circle Trapezoid Rhombus Kite
Parallelogram For each shape, you can ask for all necessary
information of the selected shape to calculate its area from the
keyboard. The result should be displayed in the following
format, for example with selected shape is Rectangle Shape:
RECTANGLE Length = 20.56 Width = 8.42 Area = 173.12
The application should allow users to continue using it until
they want to exit WHAT WE NEED TO KNOW TO DO THE
PROGRAM Manage the menu with the do., while loop to
redisplayed the menu after finishing one shape How to write
the switch statement How to format the output in columns and
with 2 decimal digits How to create the static method in data
type class How to access static method of data type class from
main() The formula to calculate the area of all above shapes
PSUEDO-CODE You have to provide the pseudo code of
main() at the top of driver class or in the word document with
part1 HOW TO DO THE LAB Create a project named
SU2016LAB6_PART2_YourLastName then add two classes in
Class SU2016LAB6_ShapeArea_yourLastName that includes all
the static methods that receive the information input from
main(), calculate the area of required shapes and return its area.
Driver class SU2016LAB6_AccessStaticMethod_yourLastName:
this class include main() where you can provide the menu to
allow users to select the shapes to calculate the area. For each
shape, ask for and read the necessary information to calculate
the area from the keyboard, then call the static method of class
SU2016LAB6_ShapeArea_yourLastName to pass the
information in to get the area. After that, display the result as
requested format
2. Solution
Area.java(main method)
import java.text.DecimalFormat;
import java.util.Scanner;
public class Area {
public static void main(String args[]){
int option;
double area=0.0;
do{
System.out.println("1 - Square 2 - Rectangle 3 -
Triangle 4 - Circle 5 - Trapezoid 6 - Rhombus 7 - Kite 8 -
Parallelogram -1 quit Choose :");
option = new Scanner(System.in).nextInt();
switch(option){
case 1 : area = Calculate.square(); break;//accessing
static methods from main
case 2 : area = Calculate.rectangle(); break;
case 3 : area = Calculate.triangle(); break;
case 4 : area = Calculate.circle(); break;
case 5 : area = Calculate.trapezoid(); break;
3. case 6 : area = Calculate.rhombus(); break;
case 7 : area = Calculate.kite(); break;
case 8 : area = Calculate.parallelogram(); break;
}
if(option!=-1){
DecimalFormat df = new DecimalFormat("#.##"); //
formating to 2 decimals
System.out.println("Area : "+ df.format(area)+"
");
}
}while(option !=-1);
}
}
Static method class(calculate.java)
class Calculate{
static Scanner s = new Scanner(System.in);
public static double square(){
System.out.println("Enter the side");
double side = s.nextDouble();
return side*side;
}
public static double rectangle(){
System.out.println("Enter the length");
double len = s.nextDouble();
4. System.out.println("Enter the width");
double wid = s.nextDouble();
return len*wid;
}
public static double triangle(){
System.out.println("Enter the base");
double base = s.nextDouble();
System.out.println("Enter the height");
double height = s.nextDouble();
return 0.5*base*height;
}
static final double PI = 3.1415;
public static double circle(){
System.out.println("Enter the radius");
double radius = s.nextDouble();
return radius*radius*PI;
}
public static double trapezoid(){
System.out.println("Enter the Base-1");
double base1 = s.nextDouble();
System.out.println("Enter the Base-2");
double base2 = s.nextDouble();
System.out.println("Enter the height");
double height = s.nextDouble();
return (0.5*(base1+base2))*height;
5. }
public static double rhombus(){
System.out.println("Enter the diagonal-1");
double d1 = s.nextDouble();
System.out.println("Enter the diagnol-2");
double d2 = s.nextDouble();
return 0.5*d1*d2;
}
public static double kite(){
System.out.println("Enter the diagonal-1");
double d1 = s.nextDouble();
System.out.println("Enter the diagnol-2");
double d2 = s.nextDouble();
return 0.5*d1*d2;
}
public static double parallelogram(){
System.out.println("Enter the base");
double base = s.nextDouble();
System.out.println("Enter the height");
double height = s.nextDouble();
return base*height;
}
}