Question

Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions.

import java.util.Scanner;

public class Area {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter code(C for circle, R for rectangle, S for square): ");
        char code = in.next().charAt(0);

        if(code == 'C') {
            System.out.print("Enter radius: ");
            double radius = in.nextDouble();
            System.out.println("Area of circle is " + (Math.PI * radius * radius));
        } else if(code == 'R') {
            System.out.print("Enter width: ");
            double width = in.nextDouble();
            System.out.print("Enter height: ");
            double height = in.nextDouble();
            System.out.println("Area of rectangle is " + (width * height));
        } else {
            System.out.print("Enter length of side: ");
            double side = in.nextDouble();
            System.out.println("Area of square is " + (side * side));
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class Area {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter code(C for circle, R for rectangle, S for square): ");
        char code = in.next().charAt(0);

        switch (code) {
            case 'C':
                System.out.print("Enter radius: ");
                double radius = in.nextDouble();
                System.out.println("Area of circle is " + (Math.PI * radius * radius));
                break;
            case 'R':
                System.out.print("Enter width: ");
                double width = in.nextDouble();
                System.out.print("Enter height: ");
                double height = in.nextDouble();
                System.out.println("Area of rectangle is " + (width * height));
                break;
            default:
                System.out.print("Enter length of side: ");
                double side = in.nextDouble();
                System.out.println("Area of square is " + (side * side));
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Please, modify the code below to use the Switch Statements in Java instead of “if” statements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • 2.1.1: Area of a Circle w/ Ra... SUBMIT CONTINUE RUN CODE TEST CASES ASSIGNMENT DOCS GRADE...

    2.1.1: Area of a Circle w/ Ra... SUBMIT CONTINUE RUN CODE TEST CASES ASSIGNMENT DOCS GRADE MORE Description Write a method that returns the area of a circle given the radius 1 import java.util.Scanner; 2. public class Main { 3. public static void main(String[] args) { Scanner io- new Scanner(System.in); System.out.println("Inout the radius of the circle: "); 6 double radius - io.nextDouble 7 System.out.println("Perimeter is -" + (2 radius - Math.PD)): 8 System.out.println("Area is - " . (Math.PI. radius radius)):...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • Java programming: The following java code is supposed to solve a linear equation of the form...

    Java programming: The following java code is supposed to solve a linear equation of the form aX+b = 0 where a and b are real numbers. However, there are some errors, nd and x them using a combination of exceptions and control statements. Hint. What could go wrong in the execution of this program? /*********************************************/ // here is the code I have: import java.util.*; public class Solver{ public static void main(String[] args){ System.out.println("Let us solve an equation of the form...

  • please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){...

    please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...

  • Area Program – Build a Java Class to Calculate the area of a Figure(call it “Area.java”....

    Area Program – Build a Java Class 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. If they enter a “C”, proceed to calculate the area of the Circle using values read in for radius. If they enter an “S”, then calculate the area of a Square using values read in for side. If they...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT