In: Computer Science
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.