Question
Exception Handling and Input/Output Streams Instructions: Part 1: Exception Handling (40 points) Create a Java class called TemperatureConverter that includes the following methods: celsiusToFahrenheit(double celsius):
Exception Handling and Input/Output Streams
Instructions:
Part 1: Exception Handling (40 points)
Create a Java class called TemperatureConverter that includes the following methods:
celsiusToFahrenheit(double celsius): This method should accept a temperature value in Celsius and return its equivalent in Fahrenheit.
fahrenheitToCelsius(double fahrenheit): This method should accept a temperature value in Fahrenheit and return its equivalent in Celsius.
Implement exception handling in the TemperatureConverter class as follows:
If the input temperature in celsiusToFahrenheit is below absolute zero (-273.15°C), throw a custom TemperatureBelowAbsoluteZeroException.
If the input temperature in fahrenheitToCelsius is below absolute zero (-459.67°F), throw the same custom TemperatureBelowAbsoluteZeroException.
Catch these exceptions in your main program and display appropriate error messages.
Part 2: Throwing Custom Exceptions (30 points)
Create a custom exception class called TemperatureBelowAbsoluteZeroException that extends the RuntimeException class. This exception should be thrown when a temperature below absolute zero is encountered.
Modify the TemperatureConverter class from Part 1 to throw the TemperatureBelowAbsoluteZeroException when temperatures below absolute zero are encountered in either method.
In your main program, demonstrate the use of the TemperatureBelowAbsoluteZeroException by calling the methods in the TemperatureConverter class with inputs that would result in temperatures below absolute zero. Catch and handle this custom exception by displaying appropriate error messages.
Part 3: Input/Output Streams (30 points)
Create a Java program that performs the following tasks:
Read data from an input text file named "input_temperatures.txt" containing temperature values in Celsius, one value per line.
Convert each Celsius temperature to Fahrenheit using the TemperatureConverter class.
Write the converted temperatures (in Fahrenheit) to an output text file named "output_temperatures.txt", one value per line.
Implement error handling for file I/O operations as follows:
If the "input_temperatures.txt" file is not found, throw a custom FileNotFoundException and catch it to display an error message.
If any other I/O errors occur during reading or writing, throw a custom FileIOException and catch it to display an error message.
Step by Step Solution
3.43 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
Answer Heres the implementation for each part of the instructions Part 1 Exception Handling public c...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started