Question: Questions at the bottom after the code. Here is the code I have so far: ProgammableThermostat.jave File: package programmable.thermostat; import java.io.EOFException; import java.io.File; import java.io.FileInputStream;

Questions at the bottom after the code.

Here is the code I have so far:

ProgammableThermostat.jave File:

package programmable.thermostat;

import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.util.ArrayList; import java.util.Scanner; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException;

public class ProgrammableThermostat {

//1st method public static String retrieveData(String url_str) throws ProtocolException, IOException { //Need to string info to initialize variable for later use String info = null; //Here we have the http of the site that was given to us URL url; try{ url = new URL(url_str); }catch (MalformedURLException e){ System.out.println("New URL failed"); return ""; } //Forms an http connection to the URL called conn and opens it HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //sets get request type conn.setRequestMethod("GET"); //Opens connection to API conn.connect(); //Gets the response code for format int responseCode = conn.getResponseCode(); //if response code not 200 throws runtime exception if(responseCode != 200) throw new RuntimeException("HttpResponseCode: " +responseCode); /* else the scanner opens the URL stream and reads in the code line by line*/ else { /*opens url stream and reads each wordf line by line Until it reaches the end of the document and then it closes And returns info */ Scanner sc = new Scanner(url.openStream()); while(sc.hasNext()) { info += sc.nextLine(); } sc.close(); } if(info.substring(0, 4).equals("null")){ return info.substring(4); } return info; } //2nd method public static JSONObject rawToJSON(String info) throws ParseException{ //Parsed the data JSONParser parse = new JSONParser(); //puts the data into an object JSONObject object = (JSONObject)parse.parse(info); //returns said object return object; } //3rd method public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException, org.json.simple.parser.ParseException, ClassNotFoundException { //call RetrieveData method to get JSON from web String info = retrieveData("http://media.capella.edu/BBCourse_Production/IT4774/temperature.json"); //Grab the JSONObject thermostatInfo and convert that to readable language JSONObject thermostatInfo = rawToJSON(info); //gets the information from the user when they want the temp to change File file = new File("Schedule.txt"); //puts all the temps in a arraylist ArrayList dayandtime = new ArrayList(); dayandtime.add(new ScheduledTemp("Monday", 1300)); dayandtime.add(new ScheduledTemp("Tuesday", 1545)); dayandtime.add(new ScheduledTemp("Thursday", 1800)); dayandtime.add(new ScheduledTemp("Saturday", 1252)); dayandtime.add(new ScheduledTemp("Sunday", 200)); //Serialize the collection of times and days FileOutputStream fo = new FileOutputStream(file); ObjectOutputStream output = new ObjectOutputStream(fo); for (ScheduledTemp st : dayandtime) { output.writeObject(st); } output.close(); fo.close(); //deserialize the collection of times and days FileInputStream fi = new FileInputStream(file); ObjectInputStream input = new ObjectInputStream(fi); ArrayList readday = new ArrayList(); try { while (true) { ScheduledTemp st = (ScheduledTemp)input.readObject(); readday.add(st); } } catch (EOFException ex) { } for (ScheduledTemp st : readday) System.out.println(st); //get data for results array System.out.println("Items in result array!"); System.out.println("Identifier: " + thermostatInfo.get("identifier")); System.out.println("Name: " + thermostatInfo.get("name")); System.out.println("Thermostat Time: " + thermostatInfo.get("thermostatTime")); System.out.println("utcTime: " + thermostatInfo.get("utcTime")); JSONObject runtime = (JSONObject) thermostatInfo.get("runtime"); System.out.println("Actual Temperature: " + runtime.get("actualTemperature")); System.out.println("Actual Humidity: " + runtime.get("actualHumidity "));

} }

ScheduledTemp.java File

package programmable.thermostat;

import java.io.Serializable;

public class ScheduledTemp implements Serializable { private String dayOfWeek; private double timeOfDay; public ScheduledTemp(String dayOfWeek, double timeOfDay) { this.dayOfWeek = ""; this.timeOfDay = 0; } public String getDayOfWeek() { return this.dayOfWeek; } public String setDayOfWeek(String newDay) { return (this.dayOfWeek = newDay); } public double getTimeOfDay() { return this.timeOfDay; } public double setTimeOfDay(double newTime) { return (this.timeOfDay = newTime); } @Override public String toString() { return String.format("%s\t%f", this.dayOfWeek, this.timeOfDay); } }

So here is my question:

I have to Add Java classes to the ProgrammableThermostat project that read/write programmable temperature settings and times to disk. It must include appropriate file I/O error handling that addresses issues such as "file not found" and other typical errors.

So basically its:

  • Create a Java class with methods that read and write user programmed settings to and from a file.
  • Write appropriate file I/O error handling to identify code errors.

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!