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
} }
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
Get step-by-step solutions from verified subject matter experts
