Object-Oriented Programming/Validation/Java
main.java
edit/** This program demonstrates use of the Temperature class. **/
public class Main{
public static void main(String[] args){
var temperature = new Temperature();
temperature.setCelsius(37);
System.out.println(temperature.getFahrenheit());
temperature = new Temperature();
temperature.setFahrenheit(98.6);
System.out.println(temperature.getCelsius());
temperature = new Temperature();
System.out.println(temperature.toCelsius(98.6));
System.out.println(temperature.toFahrenheit(37));
try {
temperature = new Temperature();
temperature.setCelsius(-500);
}
catch(Exception exception) {
System.out.println(exception.getMessage());
}
try {
temperature = new Temperature();
temperature.setFahrenheit(-600);
}
catch(Exception exception) {
System.out.println(exception.getMessage());
}
try {
System.out.println(temperature.toCelsius(-700));
}
catch(Exception exception) {
System.out.println(exception.getMessage());
}
try {
System.out.println(temperature.toFahrenheit(-800));
}
catch(Exception exception) {
System.out.println(exception.getMessage());
}
}
}
Temperature.java
edit/** Temperature converter. Provides temperature conversion functions. Supports Fahrenheit and Celius temperatures.
Examples:
Temperature temperature = new Temperature();
temperature.setCelsius(37);
System.out.println(temp.getFarenheit());
temperature = new Temperature();
temperature.setFahrenheit(98.6);
System.out.println(temp.getCelcius());
temperature = new Temperature();
System.out.println(temp4.toCelsius(98.6));
System.out.println(temp4.toFahrenheit(37));
**/
public class Temperature {
private double _celsius;
private double _fahrenheit;
// Returns absolute zero Celsius.
public double getAbsoluteZeroCelsius() {
return -273.15;
}
//Returns absolute zero Fahrenheit.
public double getAbsoluteZeroFahrenheit() {
return -459.67;
}
// Returns Celsius value.
public double getCelsius() {
return this._celsius;
}
// Sets Celsius value.
public void setCelsius(double value) {
this._celsius = this.validateCelsius(value);
this._fahrenheit = this.toFahrenheit(value);
}
// Returns Fahrenheit value.
public double getFahrenheit() {
return this._fahrenheit;
}
// Sets Celsius value.
public void setFahrenheit(double value) {
this._fahrenheit = this.validateFahrenheit(value);
this._celsius = this.toCelsius(value);
}
// Converts Fahrenheit temperature to Celsius.
public double toCelsius(double fahrenheit) {
fahrenheit = validateFahrenheit(fahrenheit);
return (fahrenheit - 32) * 5.0 / 9.0;
}
// Converts Celsius temperature to Fahrenheit.
public double toFahrenheit(double celsius) {
celsius = validateCelsius(celsius);
return celsius * 9.0 / 5.0 + 32;
}
// Validates Celsius temperature.
// Throws error if Celsius temperature is below absolute zero.
private double validateCelsius(double celsius) {
if (celsius < this.getAbsoluteZeroCelsius()) {
throw new IllegalArgumentException(
"celsius must not be below absolute zero (" +
this.getAbsoluteZeroCelsius() + "). Received " + celsius);
}
return celsius;
}
// Validates Fahrenheit temperature.
// Throws error if Fahrenheit temperature is below absolute zero.
private double validateFahrenheit(double fahrenheit) {
if (fahrenheit < this.getAbsoluteZeroFahrenheit()) {
throw new IllegalArgumentException(
"fahrenheit must not be below absolute zero (" +
this.getAbsoluteZeroFahrenheit() + "). Received " + fahrenheit);
}
return fahrenheit;
}
}
Try It
editCopy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.