Question

M al you wille to print: • The area of the box if the user gives valid sizes Invalid size given, otherwise . For example, i
31.24 Chapter 7: Area of a box (Methods) • You live in a world where box dimensions can have only three possible values: smal

in java
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query, do comment. Please rate answer. Thanks

Code:

import java.util.Scanner;

public class Main

{

    //function to return int correspond to the size

    public static int convert_to_number(String size)

    {

        switch (size) {

            case "small":

                return 1;

            case "medium":

                return 2;

            case "large":

                return 3;

            default:

                return -1;

        }   

  }

   

    //function to compute the product, if any of the dimension is negative return -1, else return the product

    public static int compute_product(int length,int width,int height)

    {

        if(length < 0 || width < 0 || height <0)

           return -1;

       

        return length*width*height;

    }

   

    //display the result

    public static void PrintOutput(int product)

    {

        if(product == -1)

                                 System.out.println("invalid size given");

                             else

                                 System.out.println("The area of the box is : " + product);

    }

   

              public static void main(String[] args) {

                             int length, width,height;

                            

                             Scanner scnr = new Scanner(System.in);

                             System.out.print("Please enter the first dimension of a box (small, medium , large) only: ");

                             length = convert_to_number(scnr.next());

                            

                             System.out.print("Please enter the second dimension of a box (small, medium , large) only: ");

                             width = convert_to_number(scnr.next());

                            

                             System.out.print("Please enter the third dimension of a box (small, medium , large) only: ");

                             height = convert_to_number(scnr.next());

                            

                             PrintOutput(compute_product(length,width,height));

                            

              }

}

============screen shot of the code===

Main.java import java.util.Scanner; 3 public class Main 4- 4 //function to return int correspond to the size public static in

Output:

input Please enter the first dimension of a box (small, medium, large) only: small Please enter the second dimension of a box

input Please enter the first dimension of a box (small, medium, large) only: big Please enter the second dimension of a box (

Add a comment
Know the answer?
Add Answer to:
in java M al you wille to print: • The area of the box if the...
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
  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • 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...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

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