THINK FIRST·CODE LATER

← All labs

Utility class and object counter

Problem

Write a class Temperature with:

  • a private field celsius;
  • a static counter of how many Temperature objects were created, readable with public static int getCount();
  • a method toFahrenheit() returning celsius * 9 / 5 + 32;
  • a static method fahrenheitToCelsius(double f) returning (f - 32) * 5 / 9.

TemperatureApp reads two numbers: a value in Celsius and a value in Fahrenheit. It creates two Temperature objects (with the Celsius value and with the converted Fahrenheit value) and prints:

In Fahrenheit: 77.00
In Celsius: 37.00
Objects created: 2

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below.

l12-1-data-1.txt

25 98.6
What your program must print for l12-1-data-1.txt
In Fahrenheit: 77.00

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l12-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content l12-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.