Question
Kirkland Grace Hospital is less worried about low temperatures than high temperatures. They want to have an option where a limit can be created but
Kirkland Grace Hospital is less worried about low temperatures than high temperatures. They want to have an option where a limit can be created but only one of the high or low needs to be configured. MedCo's programmers get together and decide on the following changes:
- Limit needs to have two new Boolean instance variables: doLow and doHigh. These will default to true.
- A new constructor needs to be added to the Limit class that takes the doHigh, high, doLow, and low arguments.
- Four new methods are needed for Limit to enable or disable the high or low check. These methods should return false if something changed and true if not. For example:
if (stdLimit.disableLow()) System.out.println("Nothing changed!");
- Analog will need an new addLimit() method that should pass in the ID, printStr, doHigh, high, doLow, low values as arguments. Example call would be:
- addLimit("HIGH", "High", true, 39.0, false, 0);
Submission:
Submit both your source files (Analog.java Limit.java (and LimitState.java if in its own file) ) with code for all of the Methods discussed above and the Methods from Stages I, II, and III. Also submit the Test Code - preferably in a separate .java file
STAGE 3 CODE:
/**
*
* @author
*
*/
enum LimitState {
NORMAL, LOW, HIGH
}
public class Limit {
// Attributes for id and printStr
private String id;
private String printStr;
// Attributes for high and low temperatures
private double high;
private double low;
// Attributes for text labels with defaults
private String highText = "Over High";
private String lowText = "Under Low";
private String normalText = "In Range";
// attribute for deadband with default
private double deadband = 0.1;
/*
* variable for current state of the Limit. This should be an enum with values
* for NORMAL, LOW, HIGH. It should start out as NORMAL.
*/
LimitState currentState = LimitState.NORMAL;
/**
* Init Method (Constructor) with no arguments
*/
public Limit() {
id = "STD";
printStr = "";
high = 37.6;
low = 36.5;
}
/**
* An Init Method that passes the high and low arguments,
*
* @param high
* @param low
*/
public Limit(String id, String printStr, double high, double low) {
this.id = id;
this.printStr = printStr;
this.high = high;
this.low = low;
}
public String getId() {
return id;
}
public String getPrintStr() {
return printStr;
}
// Setter Methods for high, low, and both,
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public double getLow() {
return low;
}
public void setLow(double low) {
this.low = low;
}
//getter for deadband
public double getDeadband() {
return deadband;
}
//setter for deadband
public void setDeadband(double deadband) {
this.deadband = deadband;
}
//setter for hightext
public void setHighText(String highText) {
this.highText = highText;
}
//setter for lowtext
public void setLowText(String lowText) {
this.lowText = lowText;
}
//setter for normaltext
public void setNormalText(String normalText) {
this.normalText = normalText;
}
//setter for all three texts
public void setTexts(String highText, String lowText, String normalText) {
setHighText(highText);
setLowText(lowText);
setNormalText(normalText);
}
public String getHighText() {
return highText;
}
public String getLowText() {
return lowText;
}
public String getNormalText() {
return normalText;
}
/**
* Print (toString) method to display the settings,
*/
public void print() {
System.out.println("Below are the Settings");
System.out.println("High Temprature " + high);
System.out.println("Low Temprature " + low);
}
/**
* A CheckLimit method that is passed the temperature
*
* @param temp
* @return LimitState.NORMAL, LimitState.LOW, LimitState.HIGH
*/
public LimitState checkLimit(double temp) {
// Check temperature and set message according to the limits.
// if currentState is HIGH, also consider temp > high - deadband
if (temp > high || (currentState == LimitState.HIGH && temp > high - deadband)) {
currentState = LimitState.HIGH;
// if currentState is LOW, also consider temp < low + deadband
} else if (temp < low || (currentState == LimitState.LOW && temp < low + deadband)) {
currentState = LimitState.LOW;
} else {
currentState = LimitState.NORMAL;
}
return currentState;
}
}
import java.util.ArrayList;
/**
*
* @author
*
*/
public class Analog {
// list of limits
private ArrayList limits = new ArrayList();
// constructor adds default limit
public Analog() {
limits.add(new Limit());
}
/**
* An Init Method that passes the high and low arguments,
*
* @param high
* @param low
*/
public void addLimit(String id, String printStr, double high, double low) {
Limit lastLimit = limits.get(limits.size()-1);
//if high is lower than last limit or low is higher than last limit then throw exception
if (high < lastLimit.getHigh() || low > lastLimit.getLow())
throw new IllegalArgumentException();
limits.add(new Limit(id, printStr, high, low));
}
//delete limit with id
public boolean deleteLimit(String id) {
//loop through all limits and check id
for (int i = 0; i < limits.size(); i++) {
if (id.equalsIgnoreCase(limits.get(i).getId())) {
//remove if found
limits.remove(i);
return true;
}
}
return false;
}
//checks all limits and logs to file
public void checkAnalog(double temp, String filePath, String time) {
// Declare string variable to store the message
String message = "";
// boolean to store if there's any change of state
boolean stateChange = false;
// boolean to store if a message has been generated
boolean messageGenerated = false;
// loop through limits starting at the last position (most critical)
for (int i=limits.size()-1; i >= 0; i--) {
Limit limit = limits.get(i);
//get previous and current state
LimitState prevState = limit.currentState;
LimitState newState = limit.checkLimit(temp);
//if there's a state change
if (prevState != newState)
stateChange = true;
//if there's a state change, and message has not yet been generated
//log if new state is not normal, else if it's the STD limit, then log the normal
if (stateChange && !messageGenerated && (newState != LimitState.NORMAL || i==0)) {
if (newState == LimitState.HIGH) {
message = time + (" Alarm " + limit.getPrintStr()).stripTrailing() + ": Temperature is " + temp + "C " + limit.getHighText() + " Limit of " + limit.getHigh() + "C";
} else if (newState == LimitState.LOW) {
message = time + (" Alarm " + limit.getPrintStr()).stripTrailing() + ": Temperature is " + temp + "C " + limit.getLowText() + " Limit of " + limit.getLow() + "C";
} else {
message = time + " Return: Temperature is " + temp + "C " + limit.getNormalText();
}
//the message is from the most critical limit, don't generate again
messageGenerated = true;
}
}
if (messageGenerated) {
// Write message to a file
try {
FileOutputStream outputStream = new FileOutputStream(filePath, true);
byte[] strToBytes = (message+" ").getBytes();
outputStream.write(strToBytes);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.text.DecimalFormat;
/**
* This is the Test class to MedCo's automatic thermometer that displays and
* updates a patient's temperature every 10 seconds.
*
* @author
*
*/
public class TemperatureLimitTest {
public static void main(String[] args) {
// simulate the clock
// Always show two digits.
DecimalFormat formatter = new DecimalFormat("00");
Analog analog = new Analog();
analog.addLimit("HIGH", "High", 39.0, 36.2);
analog.addLimit("CRIT", "Critical", 40.0, 36.1);
/*
* Simulate the clock.
*
* The clock starts at 1:00:00 PM and ticking till 3:00:00 PM
*/
for (int hours = 1; hours <= 3; hours++) {
/*
* For every minute in the hour of the clock
*/
for (int minutes = 0; minutes <= 59; minutes++) {
/*
* For every 10 seconds in the minute of clock
*/
for (int seconds = 0; seconds <= 59; seconds += 10) {
// Create formatted time string
String time = formatter.format(hours) + ":" + formatter.format(minutes) + ":"
+ formatter.format(seconds) + "PM";
// Create nd store random temperature
double temp = 35 + (Math.random() * (42 - 35));
// Invoke checkLimit() method of Limit class
analog.checkAnalog(temp, "tempFile.txt", time);
}
}
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Limitjava This class represents a temperature limit with attributes like id printStr high low deadband and currentState It includes constructors to in...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started