In Java need to write a program in main(): • getFootValue() returns a double val
ID: 3798504 • Letter: I
Question
In Java need to write a program in main():
• getFootValue() returns a double value that is entered.
• getInchValue() returns a double value that is entered.
convertToCentimeters() returns a Double value using the calculated results from the getFootValue() and getInchValue() which will be used as the input values for the convertToCentimeters() method
After: Need to write exceptions if the values are invalid and these values are checked and handled by a try/catch block.
• NegativeNumberException (if the foot or inch inputvalue are negative)
• NonDigitNumberException (if the foot or inch input value are non-numeric)
In addition to checking for the NegativeNumberException, also check for a NumberFormatException for the foot or inch values that were entered. When catching the NumberFormatException, you will then throw your own NonDigitNumberException which will be caught by the try/catch block.
Run examples:
Enter the foot value: 2
Enter the inch value: 12
Your result = 91.44
Enter the foot value: Kate
A Non-Digit foot value has been entered.
Please enter the values again.
Enter the foot value: 2
Enter the inch value: Hello
A Non-Digit inch value has been entered.
Please enter the values again.
Enter the foot value: 2
Enter the inch value: 12
Your result = 91.44
Explanation / Answer
ConvertToCentimetersTest.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class ConvertToCentimetersTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double cm = -1;
while(cm==-1){
cm = convertToCentimeters(scan);
if(cm!=-1){
System.out.println("Your result = " +cm);
}
else{
System.out.println("Please enter the values again.");
}
scan.nextLine();
}
}
public static double convertToCentimeters(Scanner scan){
double centimeters = -1;
try{
double foot = getFootValue(scan);
double inche = getInchValue(scan);
double totalInches = foot * 12 + inche;
centimeters = totalInches* 2.54;
}catch(NegativeNumberException e1){
System.out.println(e1);
}
catch(NonDigitNumberException e2){
System.out.println(e2);
}
return centimeters;
}
public static double getFootValue(Scanner scan) throws NegativeNumberException, NonDigitNumberException{
try{
System.out.println("Enter the foot value: ");
double foot = scan.nextDouble();
if(foot <= 0){
throw new NegativeNumberException ("A Negative foot value has been entered");
}
return foot;
}
catch(InputMismatchException e){
throw new NonDigitNumberException ("A Non-Digit foot value has been entered.");
}
}
public static double getInchValue(Scanner scan)throws NegativeNumberException, NonDigitNumberException{
try{
System.out.println("Enter the inche value: ");
double inche = scan.nextDouble();
if(inche <= 0){
throw new NegativeNumberException ("A Negative inche value has been entered");
}
return inche;
}
catch(InputMismatchException e){
throw new NonDigitNumberException ("A Non-Digit inche value has been entered.");
}
}
}
NegativeNumberException.java
public class NegativeNumberException extends Exception{
String errorMsg ;
public NegativeNumberException(String s){
this.errorMsg = s;
}
public String toString(){
return (errorMsg ) ;
}
}
NonDigitNumberException.java
public class NonDigitNumberException extends Exception{
String errorMsg ;
public NonDigitNumberException(String s){
this.errorMsg = s;
}
public String toString(){
return (errorMsg ) ;
}
}
Output:
Enter the foot value:
Kate
A Non-Digit foot value has been entered.
Please enter the values again.
Enter the foot value:
2
Enter the inche value:
Hello
A Non-Digit inche value has been entered.
Please enter the values again.
Enter the foot value:
2
Enter the inche value:
12
Your result = 91.44
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.