Question: Help with Java please: Create a new Class that is an Exception Call the base (super) constructor with the same message, but append -

Help with Java please:

  1. Create a new Class that "is an" Exception
    1. Call the base (super) constructor with the same message, but append " - my exception" to the text
  2. Create a main function in a driver class
    1. Inside a loop, call a function to prompt the user to input miles driven and gallons used
      1. If either number is negative, do not add the values to the totals
      2. If something other than an integer is entered, assume the user intended to display the output and exit the program.
      3. After each pass of the loop, display "------------", even after the user has exited by typing in a string
  3. Display miles per gallon calculation
    1. This should be a separate function

Don't forget to consider the case when the very first value the user enters is a string

Sample

Enter miles driven on this tank: 527 Enter number of gallons: 23 ------------ Enter miles driven on this tank: 403 Enter number of gallons: 16 ------------ Enter miles driven on this tank: -200 Negative value detected - my exception ------------ Enter miles driven on this tank: 494 Enter number of gallons: 21 ------------ Enter miles driven on this tank: 475 Enter number of gallons: -12 Negative value detected - my exception ------------ Enter miles driven on this tank: 488 Enter number of gallons: foo ------------ Average miles per gallon: (527+403+494)/(23+16+21) = 23

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!