Question

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

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.

Homework Answers

Answer #1

// 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.");
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT