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

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...
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
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:...
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.
In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming...
In python. Write a program that takes 2 string inputs and calculates the Hamming Distance. Hamming distance between two strings is the number of positions at which the corresponding symbols are different. The program should output an integer representing this distance. For example a = XXWWZZ b = ZZWWXX answer = 4 More examples: "Phone" and "PHOONE" = 3 "God" and "Dog" = 2 "Dog" and "House" = 4
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values.
 in Coral Simulator  Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. If the input is 7 15 3, the output is: largest: 15 smallest: 3 Your program should define and call two functions: Function LargestNumber(integer num1, integer num2, integer num3) returns integer largestNum Function SmallestNumber(integer num1, integer num2, integer num3) returns integer smallestNum The function LargestNumber should return the largest number of the...
Write a function myfn6 which takes as inputs vector u and value a, and output as...
Write a function myfn6 which takes as inputs vector u and value a, and output as vector w with its elements being “True, ” or “False, ”(w = [True, False, False, …, True]). Such that “True, ” means a is in u and “False, ” means a is not in u. Test your code for u = [0, -3, 1, 1, 2, 2, 6, 2] and a = 9, a = 1 and a = 2. Copy your code together...
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT