Question

In: Computer Science

Write a program which takes 3 inputs values as dimensions of a cuboid. Do the following-...

Write a program which takes 3 inputs values as dimensions of a cuboid. Do the following-

1. Verify it forms a cuboid (non zero, non negative and non-integer inputs).

2. Show the longest and shortest edges.

3. Calculate the surface area of the square made by the two short edges.

4. Calculate the volume of the cuboid.

Refer the following to learn about the cuboid (generally this is called the domain knowledge)

https://www.math-only-math.com/cuboid.html
Volume and Surface Area of Cuboids and Cubes ( GMAT / GRE / CAT / Bank PO / SSC CGL)
Provide value adding comments in the code wherever necessary.
Write test cases covering as many scenarios as possible.
Maintain a tabular format with columns - Test#, Scenario, test steps, expected result, Actual result.

java code required

JAVA CODE

Solutions

Expert Solution

JAVA PROGRAM

///////////////////////Cuboid.java////////////////////////////////

import java.util.Arrays;
import java.util.Scanner;

public class Cuboid {
  
   private int length,breadth,height;
  
   public Cuboid(int length,int breadth, int height){
       this.length = length;
       this.breadth = breadth;
       this.height = height;
       System.out.println("Cuboid is Created!");
   }
   public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
       int length=0,breadth=0,height=0;
       boolean isValid = true;
       try{
           System.out.println("Enter length of cuboid:");
           length = sc.nextInt();
           if(length <=0){
               System.out.println("Invalid Length provided!");
               isValid = false;
           }
       }catch(Exception e){
           System.out.println("Invalid length provided");
           isValid = false;
       }
      
       if(isValid){
           try{
               System.out.println("Enter breadth of cuboid:");
               breadth = sc.nextInt();
               if(breadth <=0){
                   System.out.println("Invalid breadth provided!");
                   isValid = false;
               }
           }catch(Exception e){
               System.out.println("Invalid breadth provided");
               isValid = false;
           }
       }
      
       if(isValid){
           try{
               System.out.println("Enter height of cuboid:");
               height = sc.nextInt();
               if(height <=0){
                   System.out.println("Invalid height provided!");
                   isValid = false;
               }
           }catch(Exception e){
               System.out.println("Invalid height provided");
               isValid = false;
           }
       }
      
       if(!isValid){
           return;
       }
       Cuboid c = new Cuboid(length, breadth, height);
       int[] edges = {length,breadth,height};
       Arrays.sort(edges);
       System.out.println("Shortest edge is: "+edges[0]);
       System.out.println("Longest edge is: "+edges[2]);
      
       //calculate surface area formed by 2 shortest edge(2 of them are the shortest edge only)
       int surfaceAreaSquare = c.calculateSurfaceArea(edges[0],edges[0]);
       System.out.println("Surface area of the square formed by the shortest edges("+edges[0]+ "unit): "
       +surfaceAreaSquare+" square unit");
      
       //calculate surface area formed by 2 short edges (shortest, next shortest)
       int surfaceArea = c.calculateSurfaceArea(edges[0],edges[1]);
       System.out.println("Surface area formed by 2 short edges("+edges[0]+"unit and "+edges[1]+"unit ) : "
       +surfaceArea+" square unit");
      
       int volume = c.calculateVolume();
       System.out.println("Volume of the cuboid is: "+ volume + " cubic unit");
   }
  
   /**
   * returns volume of the cuboid
   * @return
   */
   private int calculateVolume(){
       return length*breadth*height;
   }
  
   /**
   * calculate surface area
   * @param edge1
   * @param edge2
   * @return
   */
   private int calculateSurfaceArea(int edge1,int edge2){
       return edge1*edge2;
   }
  
  

}

==============================================

OUTPUT

==============================================

Run1

Enter length of cuboid:
11
Enter breadth of cuboid:
15
Enter height of cuboid:
19
Cuboid is Created!
Shortest edge is: 11
Longest edge is: 19
Surface area of the square formed by the shortest edges(11unit): 121 square unit
Surface area formed by 2 short edges(11unit and 15unit ) : 165 square unit
Volume of the cuboid is: 3135 cubic unit

Run2

Enter length of cuboid:
15
Enter breadth of cuboid:
27
Enter height of cuboid:
-45
Invalid height provided!

Run3
Enter length of cuboid:
12
Enter breadth of cuboid:
klh
Invalid breadth provided


================================================

ASSUMPTION

================================================

If length,breadth and height of the cuboid are different, then none of the surfaces for the cuboid wil be square.

Hence for the point#3 , two versions are given while testing.

#1. surfacearea of the square is calculated by considering only the smallest edge

#2. surface area is calculated for the rectangle formed by by 2 shortest edges.


Related Solutions

Write a C++ program that takes input from the keyboard of the 3 dimensions of a...
Write a C++ program that takes input from the keyboard of the 3 dimensions of a room (W,L,H in feet) and calculates the area (square footage) needed to paint the walls only. Use function overloading in your program to pass variables of type int and double. You should have two functions: one that accepts int datatypes and one that accepts double datatypes. Also assume a default value of 8 if the height parameter is omitted when calling the functions.
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
Please write this program in C++ Write a program that           - a user-defined class Cuboid...
Please write this program in C++ Write a program that           - a user-defined class Cuboid which has the following elements:                    - default constructor (has no parameters)                    - constructor that has 3 parameters (height, length, width)                    - data members                              - height, length, width                    - member functions                              - to set the value of each of the data members - 3 functions                              - to get the value of each of the data members - 3...
python program You are going to write a program that takes two inputs: A string representing...
python program You are going to write a program that takes two inputs: A string representing a list of names of the following form: first name 1, last name 1, (nick name 1); first name 2, last name 2, (nick name 2); ... A string representing part of or the complete family name. You are going to output all nick names for those names where there is a partial or complete match of the family name. If no match was...
3. Write a C++ program that takes in the name of a store, and the following...
3. Write a C++ program that takes in the name of a store, and the following details for 4 employees working at the store; their first name, last name, number of hours they worked that week and how much they are paid per hour. Your program should output the name of the store, along with each employee's full name and how much they earned that week in the manner below. Monetary values should be format to 2 decimal places. Also...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
Write a program that inputs the values of three Boolean variables, a, b, and c, from...
Write a program that inputs the values of three Boolean variables, a, b, and c, from a “cin” operator (user gives the values be sure to prompt user for what they have to give!). Then the program determines the value of the conditions that follow as true or false. 1. !(a&&b&&c) && ! (a||b||c) 2. !(a||b)&&c Output should include the values of a,b,c ie 0 or 1 in the patterns that follow reflecting the Boolean logic being tested. Where “True”...
In C write a program that asks the user for the dimensions of 3 sides of...
In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle 1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions:...
Write a program to calculate the area and circumference of a cuboid. Use printf to display...
Write a program to calculate the area and circumference of a cuboid. Use printf to display 2 digits after decimal point in all the results Program should be in java language.
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT