Java Programming (MindTap Course List)
9th Edition
ISBN: 9781337397070
Author: Joyce Farrell
Publisher: Cengage Learning
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 12, Problem 6PE
Program Plan Intro
Display course and appropriate message
Program plan:
Filename: “CourseException.java”
- Define “CourseException” class which extends from “Exception” class
- Define the constructor
- Call the super method.
- Define the constructor
Filename: “Course.java”
- Define the “Course” class
- Declare the required variables and set the values
- Define the default constructor
- Set the values
- Define the parameterized constructor
- Set the values
- Check the length of the department
- Append the string to the “msg” variable
- Check the “num” value is less than “LOW_NUM” or greater than “HIGH_NUM”
- Append the string to the “msg” variable
- Check “msg” is not equal to empty string.
- Throw an exception.
- Define “toString” method
- Return the value
Filename: “ThrowCourseException.java”
- Define the class “ThrowCourseException”
- Define the “main” method
- Create an object for “Course” class
- Declare the variables and set the required values to the variables
- Iterate “for” loop until it reaches length of the “course”
- Set the values to the object
- Iterate “for” loop until it reaches length of the “course”
- 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
Trending nowThis is a popular solution!
Students have asked these similar questions
In Java
Attached
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.
JAVA Problem – CycleFileInput
Revisit the Cycle class . Modify your application such that the properties will be read from a text file called “Cycle.txt”.
Directions
Examine your application for the class called Cycle.
Add an appropriate throws statement in the main method.
Create a reference to a File class with the appropriate name of a text file (Cycle.txt). Note: Cycle.txt was created in the previous assignment, CycleFileOutput.
In your code, check that the text file does exist.
Input the values from the file to memory.
Close the file.
public class Cycle {
// Declear integer instance variable
private int numberOfWheels;
private int weight;
// Constructer declear class
public Cycle(int numberOfWheels, int weight ) {
this.numberOfWheels = numberOfWheels;
this.weight = weight;
}
// String method for output
public String toString() {
String wheel = String.valueOf(this.numberOfWheels);
String load = String.valueOf(this.weight);
return("No of wheels in cycle = " +…
Chapter 12 Solutions
Java Programming (MindTap Course List)
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
- In 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_forwardJava Programming: Below is the lexer, shank and token files along with the shank.txt file. The shank file is the main method file. The lexer must break up the input text stream into lexemes and return a token object for each one in the shank.txt file. Make sure to fix the errors in the lexer.java file and show the complete code for lexer.java.There must be no error in the code at all. Run the whole code and show the output which the shank.txt must be printed out in the terminal. Attached is the rubric. Lexer.java package mypack; import java.util.HashMap;import java.util.List;import mypack.Token.TokenType; public class Lexer { private static final int INTEGER_STATE = 1;private static final int DECIMAL_STATE = 2;private static final int IDENTIFIER_STATE = 3;private static final int SYMBOL_STATE = 4;private static final int ERROR_STATE = 5;private static final int STRING_STATE = 6;private static final int CHAR_STATE = 7;private static final int COMMENT_STATE = 8; private static final…arrow_forwardwrite a java programme: Vehicle class:First create an abstract Vehicle class that contains code common to all of the vehicles.1. Vehicle class has a color, IDnumber, and a model.2. Throw an exception named “InputNotCorrectException” if the IDnumber is negative.3. The accessor and mutator methods for all three data fields.4. Also include a void wheels() method which is declared abstract. Runnable interface:1.Deaclare an abstract method named HowToRun() which has a return type of void. Car class:Car class is a subclass of Vehicle which implements the Runnable interface.1. Write a class called Car which extends the Vehicle class and implements the Runnable interface.2. Override the equals method in the Object class. Two Car objects are equal if their models are thesame.3. In the Car class override the wheels() method to display “A car has four wheels”.4. A method named toString() that returns a string description for the car.5. Override the HowToRun() method to display “A car runs with…arrow_forward
- Write a Month class that holds information about the month. Write exception classes for the following error conditions:• A number less than 1 or greater than 12 is given for the month number.• An invalid string is given for the name of the month.Modify the Month class so that it throws the appropriate exception when either of these errors occurs. Demonstrate the classes in a program.arrow_forwardWrite a program to create a class” Fan “to represents features of a fan as follows: “Fan “should contain following details fanType, manufacturer, model, isOn. Use constructor to initialize data members of the Fan class. Create function on() and off() to switch on and off the fan respectively. Create a function speedup() is to increase the speed of the fan by 1 if speed of the Fan greater than 5 it should throw a custom exception. Create another function speeddown() is to decrease the speed of the fan by 1 if speed of the Fan less than 0 it should throw a custom exception. Make a menu driven program.arrow_forwardWrite the source code of the exception FlightException.arrow_forward
- Programming Problem 3 – CycleFileInput Revisit the Cycle class in Module 3. Modify your application such that the properties will be read from a text file called “Cycle.txt”. Directions Examine your application for the class called Cycle. Add an appropriate throws statement in the main method. Create a reference to a File class with the appropriate name of a text file (Cycle.txt). Note: Cycle.txt was created in the previous assignment, CycleFileOutput. In your code, check that the text file does exist. Input the values from the file to memory. Close the file.arrow_forwardJAVA Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an "IllegalArgumentException". Demonstrate the class in a program.arrow_forwardWrite an application TestStudent that prompts for and reads a student name. It then creates a Student object. It then prompts for and reads the number of quizzes n that the student has taken. It then prompts for and reads the scores of the student in n quizzes and adds each to the totalScore of the student using addQuiz() method. Finally, the application prints the student object using the toString() method, and it also prints his quiz average. The main method must handle java.util.InputMismatchException, java.lang.IllegalArgumentException and java.lang.Unsupported OperationException.arrow_forward
- Modify the attached code: Create a new class called CalculatorWithMod. This class should be a sub class of the base class Calculator. This class should also have an additional method for calculating the modulo. The modulo method should only be seen at the sub class, and not the base class! Include exception handling for instances when dividing by 0 or calculating the modulo with 0. You would need to use throw, try, and catch. The modulo (or "modulus" or "mod") is the remainder after dividing one number by another.Example: 20 mod 3 equals 2Because 20/3 = 6 with a remainder of 2 Refer also to the attached sample outputs:arrow_forwardThis 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…arrow_forwardDevelop an exception class named InvalidMonthException that extends the Exception class. Instances of the class will be thrown based on the following conditions: The value for a month number is not between 1 and 12 The value for a month name is not January, February, March, … December Develop a class named Month. The class should define an integer field named monthNumber that holds the number of a month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods: A no-argument constructor that sets the monthNumberto 1. An overloaded constructor that accepts the number of the month as an argument. The constructor should set the monthNumberfield to the parameter value if the parameter contains the value 1 – 12. Otherwise, throw an InvalidMonthException exception back to the caller. The exception should note that the month number was incorrect. An overloaded constructor that accepts a string containing the name of the month,…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