I have to code a program that utilizes method overloading to calculate the surface area and volume of a cylinder, sphere, cone and triangular prism. I need to take input from the user and then display the correct surface area and volume.
An example of what the program should do is:
Input your shape’s “a” value: none
Input your shape’s “b” value: none
Input your shape’s “c” value: none
Input your shape’s “h” value: none
Input your shape’s “l” value: none
Input your shape’s “r” value: 5
Input your shape’s “s” value: none
Would you like your answer rounded?: yes
To what decimal place?: 0
---------------------------------------------------------------------
Your shape is a sphere!
It’s volume is 524 units cubed.
It’s surface area is 314 units squared.
I've coded the majority of the program, but the issue I'm having now is whenever I finish inputting the data, I get the error: "Exception in thread "main" java.util.NoSuchElementException: No line found". I suspect the issue is with the if statements I've used to filter through what the user has given me, as each one can have the same exceptions multiple times. So if I want to calculate the volume of a sphere the only input I need is to that of the radius, r, and all others are none. But all others are none as well for a cylinder except for the radius, r, and the height, h. Any help would be much appreciated.
This is my code so far:
import java.util.*;
class Main {
String a = "none"; String b = "none"; String c = "none"; String h = "none"; String r = "none"; String s = "none"; String l = "none";
String round;
double aP; double bP; double cP; double hP; double rP; double sP; double lP;
public static void main(String[] args) throws Exception{
new Main();
}//end main
public Main() throws Exception{
input();
surfaceAreaPrint();
volumePrint();
}//end Main
/**
*
*/
public void input() throws Exception{
Scanner sc = new Scanner(System.in);
System.out.print("");
System.out.print("Input your shape’s “a” value: ");
a = sc.nextLine();
System.out.print("Input your shape’s “b” value: ");
b = sc.nextLine();
System.out.print("Input your shape’s “c” value: ");
c = sc.nextLine();
System.out.print("Input your shape’s “h” value: ");
h = sc.nextLine();
System.out.print("Input your shape’s “r” value: ");
r = sc.nextLine();
System.out.print("Input your shape’s “s” value: ");
s = sc.nextLine();
System.out.print("Input your shape’s “l” value: ");
l = sc.nextLine();
System.out.print("Would you like your answer rounded?: ");
round = sc.nextLine();
System.out.print("");
System.out.print("");
sc.close();
parse();
}//end input
public void parse() throws Exception{
System.out.println(a);
aP = Double.parseDouble(a);
bP = Double.parseDouble(b);
cP = Double.parseDouble(c);
hP = Double.parseDouble(h);
rP = Double.parseDouble(r);
sP = Double.parseDouble(s);
lP = Double.parseDouble(l);
//cylinder
if(a == "none" && b == "none" && c == "none" && s == "none" && l != "none") {
surfaceAreaPrint();
return;
}
//sphere
else if (a.equalsIgnoreCase("none") && b.equalsIgnoreCase("none") && c.equalsIgnoreCase("none") && s.equalsIgnoreCase("none") && h.equalsIgnoreCase("none") && s.equalsIgnoreCase("none") && l.equalsIgnoreCase("none")){
surfaceAreaPrint();
return;
}
//cone
else if (a.equalsIgnoreCase("none") && b.equalsIgnoreCase("none") && c.equalsIgnoreCase("none") && s.equalsIgnoreCase("none") && h.equalsIgnoreCase("none") && l.equalsIgnoreCase("none")){
surfaceAreaPrint();
return;
}
//traingular prism
else if (r.equalsIgnoreCase("none") && s.equalsIgnoreCase("none")){
surfaceAreaPrint();
return;
}
}//end parse
public void surfaceAreaPrint() throws Exception{
Main s = new Main();
System.out.println(s.surfaceArea(hP, rP));
System.out.println(s.surfaceArea(rP));
System.out.println(s.surfaceArea(hP, rP, sP));
System.out.println(s.surfaceArea(aP, bP, cP, lP, hP));
}//end surfaceAreaPrint
public void volumePrint() throws Exception{
Main s = new Main();
System.out.println(s.surfaceArea(hP, rP));
System.out.println(s.surfaceArea(rP));
System.out.println(s.surfaceArea(hP, rP));
System.out.println(s.surfaceArea(bP, lP, hP));
}//end volumePrint
//surface area for cylinder
public double surfaceArea(double hP, double rP){
return (2.0 * Math.PI * Math.pow(rP, 2.0)) + (2.0 * Math.PI * rP * hP);
}//end surfaceArea
//surface area for sphere
public double surfaceArea(double rP){
return (4.0 * Math.PI * Math.pow(rP, 2.0));
}//end surfaceArea
//surface area for cone
public double surfaceArea(double hP, double rP, double sP){
return (Math.PI * Math.pow(rP, 2.0)) + (Math.PI * rP * sP);
}//end surfaceArea
//surface area for traingular prism
public double surfaceArea(double aP, double bP, double cP, double lP, double hp){
return (bP * lP) + (aP * hP) + (bP * hP) + (cP* hP);
}//end surfaceArea
//volume for cylinder
public double volume(double hP, double rP){
return (Math.PI * Math.pow(rP, 2.0) * hP);
}//end volume
//volume for sphere
public double volume(double rP){
return ( 4.0 / 3.0 ) * Math.PI * Math.pow(rP, 3.0);
}//end volume
//volume for cone
public double volume(double hP, double rP, double sP){
return (1.0 / 3.0 ) * Math.PI * Math.pow(rP, 2.0) * hP;
}//end volume
//volume for traingular prism
/**
* calculates volume for traingular prism
* @param times The number of times the die is to be rolled.
* @return The results of rolling a 6-sided die.
*/
public double volume(double aP, double bP, double cP, double lP, double hp){
return (1.0 / 2.0 ) * bP * lP * hP;
}//end volume
}//end Main