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
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
For this part, you will write a PostfixCalculator class that has methods for processing each possible...
For this part, you will write a PostfixCalculator class that has methods for processing each possible input. You will write a Tester class that reads a line of input from the user, with each symbol separated by a space, and prints out the numeric value of the top of the stack. If the user specifies an incomplete expression, print out the top of the stack and print out a message saying that the stack contains more than one item. If...
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...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
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...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT