Question

In: Computer Science

Build a Java class file to calculate the area of a figure (call it Area.java). The...

Build a Java class file to calculate the area of a figure (call it Area.java). The figure may be a circle or a square or a rectangle. The user will enter the type of figure they want through a Scanner. It they enter “C”, proceed to calculate the area of a Circle using values read in for radius. If they enter an “S”, then calculate the area of the circle using values read in for side. If they enter an “R”, then calculate using values read in for height and width. Use nested if statements for this lab.
Formulas are:
Circle: Area = 3.14*radius*radius
Square: Area= side*side
Rectangle: Area = Height*Width
3)Modify code for #2 to use the Switch Statements instead of “if” statements to make the decisions.


CIST 2371

Solutions

Expert Solution

Please find the code below with if statements:

-----------------------------------------------------------------------------------

import java.util.Scanner;
public class Area
{   static String ch;
    Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
   Main ob=new Main();
   ob.input();
   if(ch=="C")
        {
          
            ob.circ();
        }
        if(ch=="S")
        {
            ob.squa();
        }
        if(ch=="R")
        {
            ob.rect();
        }  
      
   }
    void input()
    {
        System.out.println("Enter 'C' to calculate Area of Circle");
        System.out.println("Enter 'S' to calculate Area of Square");
        System.out.println("Enter 'R' to calculate Area of Rectangle");
        ch=sc.nextLine();
       
     
      
    }
    void circ()
    {
        System.out.println("Enter the Radius");
        int r=sc.nextInt();
        double ar1=3.14*r*r;
        System.out.println("Area of circle is :"+ar1);
    }
    void squa()
    {
        System.out.println("Enter the Side");
        int s=sc.nextInt();
        int ar2=s*s;
        System.out.println("Area of Square is :"+ar2);
    }
    void rect()
    {
        System.out.println("Enter the Length");
        int l=sc.nextInt();
        System.out.println("Enter the Breadth");
        int b=sc.nextInt();
        int ar3=l*b;
        System.out.println("Area of Rectangle is :"+ar3);
    }
  
  
}


----------------------------------------------------------------------------------------------------------

Please find the Switch statement code below:
----------------------------------------------------------------------------------------------------------

import java.util.Scanner;
public class Area
{   static String ch;
    Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
   Main ob=new Main();
   ob.input();
   switch(ch)
   {
   case "C":
        ob.circ();
        break;
     case "S":
            ob.squa();
        break;
     case "R":
            ob.rect();
        break;  
   default:

System.out.println("Wrong choice entered, Re-enter choice");

ob.input();

break;
   }
    }
    void input()
    {
        System.out.println("Enter 'C' to calculate Area of Circle");
        System.out.println("Enter 'S' to calculate Area of Square");
        System.out.println("Enter 'R' to calculate Area of Rectangle");
        ch=sc.nextLine();
       
     
      
    }
    void circ()
    {
        System.out.println("Enter the Radius");
        int r=sc.nextInt();
        double ar1=3.14*r*r;
        System.out.println("Area of circle is :"+ar1);
    }
    void squa()
    {
        System.out.println("Enter the Side");
        int s=sc.nextInt();
        int ar2=s*s;
        System.out.println("Area of Square is :"+ar2);
    }
    void rect()
    {
        System.out.println("Enter the Length");
        int l=sc.nextInt();
        System.out.println("Enter the Breadth");
        int b=sc.nextInt();
        int ar3=l*b;
        System.out.println("Area of Rectangle is :"+ar3);
    }
  
  
}


Related Solutions

User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
in java: Write a class responsible for storing data in a text file. The class should...
in java: Write a class responsible for storing data in a text file. The class should include a method that accepts a string and appends it to a file.
1. Data in Java: Primitives and Objects In this section, we’ll build a class that is...
1. Data in Java: Primitives and Objects In this section, we’ll build a class that is composed of both primitives and objects. Java has classes to help in converting from primitives to objects (for example an int to an Integer) and vice-versa. Let’s start by building a small class used to represent a single concept such as a Car or Vehicle. Make a new Java project and and create a new class called “Car”. Cars should define primitives for things...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing a list of integers. The following public methods are provided: ? IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0. ? void randomize()—fills the list with random integers between 1 and 100, inclusive. ? void print()—prints the array elements and indices ? int search(int target)—looks for value target in the list using a linear (also called sequential) search algorithm. Returns...
Circle Class (This is in JAVA) /** * Defines a basic shape with just area *...
Circle Class (This is in JAVA) /** * Defines a basic shape with just area * * @author Jo Belle * @version 0.2 (10/05/2020) */ public class Shape{ private double area; public Shape(){ area = 0.0; } public Shape( double a ){ this.area = a; } public void setArea( double a ){ area = a; } public double getArea(){ return area; } public String toString(){ return "Shape:\n\tarea: " + area; } } /** * Create a simple Circle object *...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method that does the following: Prompt the user to enter a number of inches Read the number of inches entered from the console Convert the inches into an equivalent number of miles, yards, feet, and inches. Output the results to the console. For example, if a user entered 100000 inches, the program would output: 100000 inches is equivalent to: Miles: 1 Yards: 1017 Feet: 2...
Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named...
Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file). The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all of the instance variables Three “Get” methods which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT