Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application from scratch where you are to write in Java the following m

ID: 3800350 • Letter: W

Question

Write an application from scratch where you are to write in Java

the following methods to be used by the main():
getFootValue() returning a Double value that is entered
getInchValue() returning 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

Next: The Exceptions that will be thrown if the values are
invalid and these values are to be checked and handled
by a try/catch block…

Write the following Exceptions:

• NegativeNumberException (if the foot or inch input value 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 incorrectly (For example: I enter a letter for an inch or foot value). When catching the NumberFormatException, you will then throw your own NonDigitNumberException which will be caught by the try/catch block.

Sample I/O

Enter the foot value: 2
Enter the inch value: 12
Your result = 91.44 cm // 2ft,12 inches converted to CM

Enter the foot value: Bill
A non-digit foot value has been entered Please enter the values again.
Enter the foot value: 2
Enter the Inch value: Blah
A non-digit inch value has been entered Please enter the values again.

Explanation / Answer

// Convertor.java


import java.util.Scanner;;

public class Convertor {

   static Scanner sc = new Scanner(System.in);
   public static double getFootValue() throws NonDigitNumberException, NegativeNumberException
   {
       System.out.print("Enter the foot value: ");
       double d;
       String foot = sc.nextLine();
       try
       {
           d = Double.parseDouble(foot);
       }
       catch(NumberFormatException n)
       {
           throw(new NonDigitNumberException("foot"));
       }
      
       if (d < 0)
       {
           throw(new NegativeNumberException("foot"));
       }
      
       return d;
   }
  
   public static double getInchValue() throws NonDigitNumberException, NegativeNumberException
   {
       System.out.print("Enter the inch value: ");
       double d;
       String inch = sc.nextLine();
       try
       {
           d = Double.parseDouble(inch);
       }
       catch(NumberFormatException n)
       {
           throw(new NonDigitNumberException("inch"));
       }
      
       if (d < 0)
       {
           throw(new NegativeNumberException("inch"));
       }
      
       return d;
   }
  
   public static double convertToCentimeters(double foot, double inch)
   {
       return (foot*12 + inch)*2.54;
   }
  
   public static void main(String[] args)
   {
       double foot = 0;
       double inch = 0;
       try{
           foot = getFootValue();
           inch = getInchValue();
       }
       catch(NonDigitNumberException d)
       {
           System.out.println(d.getMessage());
       }
       catch(NegativeNumberException n)
       {
           System.out.println(n.getMessage());
       }
      
       System.out.println("Your result = " + convertToCentimeters(foot, inch) + " cm");
   }
  
}

//NonDigitNumberException .java


public class NonDigitNumberException extends Exception {
   public NonDigitNumberException(){
       super("A non-digit foot value has been entered Please enter the values again.");
}
  
   public NonDigitNumberException(String message){
   super("A non-digit " + message + " value has been entered Please enter the values again.");
}
}

// NegativeNumberException.java


public class NegativeNumberException extends Exception{

   public NegativeNumberException(){
       super("A non negative value has been entered Please enter the values again.");
}
  
   public NegativeNumberException(String message){
   super("A non negative " + message + " value has been entered Please enter the values again.");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote