EBK JAVA PROGRAMMING
8th Edition
ISBN: 9781305480537
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 12, Problem 7PE
Program Plan Intro
Display list
Program plan:
Filename: “UsedCarException.java”
- Define “UsedCarException” class which extends from “Exception” class
- Define the constructor
- Call the super method.
- Define the constructor
Filename: “UsedCar.java”
- Define the “UsedCar” class
- Declare the required variables and set the values
- Define the default constructor
- Set the values
- Define the parameterized constructor
- Set the values
- Check “num” length is not equal to “VIN_NUM_LENGTH”
- Set the value
- Iterate “x” until it reaches “num” length
-
- Check “num” is not a digit
- Set the value
- Check “num” is not a digit
- Iterate “x” until it reaches “MALES” length
-
- Check “carMake” is not equal to “MAKES [x]”
- Set the value
- Check “carMake” is not equal to “MAKES [x]”
- Check “isGoodMake” is false
-
- Set the value
- Check “carYear” is less than “LOW_YEAR” or greater than “HIGH_YEAR or “miles” less than 0 or “pr” less than 0
-
- Set the value
- Check “isBad” is true
-
- Throw an exception
- Set the values
- Set the value
- Define the “getVin” method
- Set the values
- Define the “toString” method
- Return the values.
Filename: “ThrowUsedCarException.java”
- Define the class “ThrowUsedCarException”
- Define the “main” method
- Create an object for “UsedCar” class
- Declare the variables and set the required values to the variables
- Iterate “for” loop until it reaches length of the “UsedCar”
- Set the values to the object
- Iterate “for” loop until it reaches length of the “UsedCar”
- In “try” block, set the values
- In “catch” block, display the course and error
- Display the course values
- Define the “main” method
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
This is the question -
Create a UsedCarException class that extends Exception; its constructor receives a value for a vehicle identification number (VIN) that is passed to the parent constructor so it can be used in a getMessage() call. Create a UsedCar class with fields for VIN, make, year, mileage, and price. The UsedCar constructor throws a UsedCarException when the VIN is not four digits; when the make is not Ford, Honda, Toyota, Chrysler, or Other; when the year is not between 1997 and 2017 inclusive; or either the mileage or price is negative.
Write an application that establishes an array of at least seven UsedCar objects and handles any Exceptions. Display a list of only the UsedCar objects that were constructed successfully.
Here is the code I have -
public class ThrowUsedCarException {
public static void main(String[] args) {
// Write your code here
}
}
public class UsedCar {
String vin;
String make;
int year;
int mileage;
int…
Create a CourseException class that extends Exception and whose constructor receives a String that holds a college course’s department (for example, CIS), a course number (for example, 101), and a number of credits (for example, 3). Save the file as CourseException.java. Create a Course class with the same fields and whose constructor requires values for each field. Upon construction, throw a CourseException if the department does not consist of three letters, if the course number does not consist of three digits between 100 and 499 inclusive, or if the credits are less than 0.5 or more than 6. Save the class as Course.java. Write an application that establishes an array of at least six Course objects with valid and invalid values. Display an appropriate message when a Course object is created successfully and when one is not. Save the file as ThrowCourseException.java.
Create a CourseException class that extends Exception and whose constructor receives a String that holds a college course’s department (for example, CIS), a course number (for example, 101), and a number of credits (for example, 3). Save the file as CourseException.java. Create a Course class with the same fields and whose constructor requires values for each field. Upon construction, throw a CourseException if the department does not consist of three letters, if the course number does not consist of three digits between 100 and 499 inclusive, or if the credits are less than 0.5 or more than 6. Save the class as Course.java. Write an application that establishes an array of at least six Course objects with valid and invalid values. Display an appropriate message when a Course object is created successfully and when one is not. Save the file as ThrowCourseException.java.
Modify the CourseException class to extend RuntimeException class and identify the differences.
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- The following class maintains an account balance and returns aspecial error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to depositinvalid amounts and catches the exceptions that are thrownarrow_forward!!!!!!!!!!Below is The following class maintains an account balance and returns aspecial error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to depositinvalid amounts and catches the exceptions that are thrown/. Cosearrow_forwardIn Java Create a class for a runtime exception called InvalidRangeException. Then, re-write the constructor for the Airplane class so that it throws the InvalidRangeException if the range is a negative number.arrow_forward
- Write an application that displays a series of at least 10 student ID numbers (that you have stored in an array) and asks the user to enter a test letter grade for the student. Create an Exception class named GradeException that contains a static public array of valid grade letters ('A', 'B', 'C', 'D', 'F', and 'I'), which you can use to determine whether a grade entered from the application is valid. In your application, throw a GradeException if the user does not enter a valid letter grade. Catch the Grade Exception and then display an appropriate message. In addition, store an 'I' (for Incomplete) for any student for whom an exception is caught. At the end of the application, display all student IDs and grades. Save the files as GradeException.java and TestGrade.java.arrow_forwardIn this assignment, you will create a class that you will need for the upcoming Super Ghost project. Please do your best job on this assignment as early as possible. You will depend on the code in this assignment in your final Super Ghost Project. Create a class named MyIOManager that implements the accompanying interface IOManager. MyIOManager should adequately implement all methods in the IOManager such that it accepts and returns the defined parameters and throws the outlined exceptions correctly. When you submit your assignment to Grader Than only submit your MyIOManager.java file. Please read the programmatic documentation of the IOManager interface to gain an understanding of what each function should do. This assignment will test your knowledge of serializing and deserializing a binary file, implementing interfaces, and throwing exceptions.arrow_forwardGiven the following definition of the classes Person, Father, and Home,which option, if used to replace/* INSERT CODE HERE */,will cause the code to compilesuccessfully (select 3 options):class Person {}class Father extends Person {public void dance() throws ClassCastException {}}class Home {public static void main(String args[]) {Person p = new Person();try {((Father)p).dance();}//INSERT CODE HERE}}Question options: catch (NullPointerException e) {}catch (ClassCastException e) {}catch (Exception e) {}catch (Throwable t) {} catch (ClassCastException e) {}catch (NullPointerException e) {}catch (Exception e) {}catch (Throwable t) {} catch (ClassCastException e) {}catch (Exception e) {}catch (NullPointerException e) {}catch (Throwable t) {} catch (Throwable t) {}catch (Exception e) {}catch (ClassCastException e) {}catch (NullPointerException e) {} finally {}arrow_forward
- In preparation for a class Fraction, create a classNullDenominatorExceptionthat must be a checked exception whoseinstances are thrown if the denominator of an object of class Fraction is 0.arrow_forwardin java Define the class InvalidSideException, which inherits from the Exception class. Also define a Square class, which has one method variable -- an int describing the side length. The constructor of the Square class should take one argument, an int meant to initialize the side length; however, if the argument is not greater than 0, the constructor should throw an InvalidSideError. The Square class should also have a method getArea(), which returns the area of the square.Create a Driver class with a main method to test your classes. Your program should prompt the user to enter a value for the side length, and then create a Square object with that side length. If the side length is valid, the program should print the area of the square. Otherwise, it should catch the InvalidExceptionError, print "Side length must be greater than 0.", and terminate the program. screenshot shows outputarrow_forwardIn Java. Define the class InvalidSideException, which inherits from the Exception class. Also define a Square class, which has one method variable -- an int describing the side length. The constructor of the Square class should take one argument, an int meant to initialize the side length; however, if the argument is not greater than 0, the constructor should throw an InvalidSideError. The Square class should also have a method getArea(), which returns the area of the square.Create a Driver class with a main method to test your classes. Your program should prompt the user to enter a value for the side length, and then create a Square object with that side length. If the side length is valid, the program should print the area of the square. Otherwise, it should catch the InvalidExceptionError, print "Side length must be greater than 0.", and terminate the program. The words for output is highlighted in grey and yellow in the blue box. Class should be named Driver, as shown in blue box…arrow_forward
- Java question Write a Thermostat class such that a user of the Thermostat class can create an objectof Thermostat and set it to the desired temperature within a pre-specified range. If theuser tries set the temperature outside this range it should throw a TemperatureTooHighor TemperatureTooLow exception. Use inheritance to create an exception superclassTemperatureOutofRange and subclasses TemperatureTooHigh andTemperatureTooLow.Sample Tester code:Thermostat t = new Thermostat(0, 100);t. setTemp(50); // Should be OK.t.setTemp(150); // Should throw TemperatureTooHigh exception.t.setTemp(-50); // Should throw TemperatureToolLow exception.Write a Tester class to demonstrate throwing and catching of exceptions. Show that thecatch specifying the superclass catches the subclass exceptions. The order of exceptionhandlers is important. If you try to catch a superclass exception type before a subclasstype, the compiler would generate errors. Also show the re-throwing of exceptions.arrow_forwardJava Questions - (Has 2 Parts). Based on each code, which answer out of the choices "A, B, C, D, E" is correct. Each question has one correct answer. Thank you. Part 1 - Which of these keywords is used by the calling method to guard against the exception that is thrown by called method? A. tryB. throwC. throwsD. catchE. finally Part 2 - Which of the following is correct? A. All classes of Exception extend from Error.B. All classes of Error extend from Exception.C. All classes of Exception extend from Throwable.D. All classes of Throwable extend from Exception.E. All classes of Throwable extend from RuntimeException.arrow_forwardCreate a class named MyIOManager that implements the accompanying interface IOManager. MyIOManager should adequately implement all methods in the IOManager such that it accepts and returns the defined parameters and throws the outlined exceptions correctly. When you submit your assignment to Grader Than only submit your MyIOManager.java file. package homework;import java.io.FileNotFoundException;import java.io.IOException;public interface IOManager<T extends Object> {/*** Sets the file path that this IOManager will read and write an object from. This* file location will be used by {@link #read()} and {@link #write(Object)}* @param path The file location to be read and written to* @throws FileNotFoundException when the specified path can not be found.* @throws IllegalArgumentException When the specified path points to a folder/directory rather than a file*/public void setPath(String path) throws FileNotFoundException, IllegalArgumentException;/*** The file path that is…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning