Question: Need help on following problem in JAVA: Write a class called TemperatureMonitoringSystem that contains two fields of type Thermometer. (Use the Thermometer class provided). Create

Need help on following problem in JAVA:

Write a class called TemperatureMonitoringSystem that contains two fields of type Thermometer. (Use the Thermometer class provided). Create an object called temperatureMonitor of this class and serialize it. Then in main write code to verify that the Thermometer instances in temperatureMonitor were serialized correctly.

Thermometer class:

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Scanner;

public class Thermometer implements Serializable { private static String type; private static int temperature;

public static void main(String[] args) { Thermometer thermometer = new Thermometer(); Scanner scanner = new Scanner(System.in); System.out.print("Mercury or Digital? "); type = scanner.nextLine(); System.out.print("Enter temperature: "); temperature = scanner.nextInt(); try { FileOutputStream fileOut = new FileOutputStream("thermometer.dat"); ObjectOutputStream objectOut = new ObjectOutputStream(fileOut); System.out.print(thermometer); objectOut.close(); System.out.print(thermometer); objectOut.writeObject(thermometer); objectOut.close(); FileInputStream fileIn; fileIn = new FileInputStream("thermometer.dat"); ObjectInputStream objectIn = new ObjectInputStream(fileIn); thermometer = (Thermometer) objectIn.readObject(); objectIn.close(); } catch (Exception e) { e.printStackTrace(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!