Question: (Java Code)- please, Add a comment to the top of the program indicating whether you think it works in all cases described above, or if
(Java Code)- please, Add a comment to the top of the program indicating whether you think it works in all cases described above, or if there are some it has trouble processing (and why).
Now that we have the data in an array, we can calculate the statistics. Add the possibility of an optional fourth token on a line, found only in readings with precipitation, containing the duration of precipitation. This will be given in the format 1:43 where 1 is the number of hours and 43 is the number of minutes (may be one or two digits). The : may be omitted, meaning the value is just the number of hours (zero minutes), or the number of hours may be omitted. For example:
-10,3NW,15cm,4:38 5,15SW,8mm,2:8 8,8ENE,2mm,:25 -5,0,7cm,1 -3,0,3mm
Download the complete Weather Station program and add the following features to it:
Add hours and minutes instance variables to the Reading class. Zero values for both indicate no data (or no precipitation).
Add the code to the main program to parse the extra data on the line. Hint: once you have split the CSV line into tokens, you may want to take the fourth token (if it exists) and split it according to the position of the :.
Make sure you deal with the same situations as the original program: neither duration values can be negative, and use trim() to remove excess whitespace.
public class Activity3A {
public static void main(String[] args) {
Reading r1, r2, r3;
r1 = new Reading(-10, 27, "SSW", 0, false);
r2 = new Reading(-18, 6, "E", 2, true);
r3 = new Reading(4, 0, "", 3, false);
System.out.println(r1);
System.out.println("temp = " + r1.getTemperature() + " precip = " +
r1.getPrecipitation());
System.out.println(r2);
System.out.println("temp = " + r2.getTemperature() + " precip = " +
r2.getPrecipitation());
System.out.println(r3);
System.out.println("temp = " + r3.getTemperature() + " precip = " +
r3.getPrecipitation());
System.out.println(" End of processing.");
}
}
class Reading {
private int temperature; // in degrees C
private int windSpeed; // in km/h
private String windDirection; // e.g. N,SW,ESE
private int precipitation; // in mm
private boolean isSnow; // true if precipitation fell as snow
public Reading(int temperature, int windSpeed, String windDirection,
int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = precipitation;
this.isSnow = isSnow;
}
/*
public Reading(int temperature, int windSpeed, String windDirection) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = 0;
this.isSnow = false;
}
public Reading(int temperature, int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = 0;
this.windDirection = "";
this.precipitation = precipitation;
this.isSnow = isSnow;
}
*/
public int getTemperature() {
return temperature;
}
public int getPrecipitation() {
return precipitation;
}
public String getWindDirection() {
return windDirection;
}
public String toString() {
String result;
result = "temp: " + temperature + "C wind: " + windSpeed + windDirection +
" precipitation: " + precipitation;
if (isSnow) {
result += "cm snow";
} else {
result += "mm rain";
}
return result;
}
}
import java.io.*;
public class Activity3B {
public static void main(String[] args) {
BufferedReader input;
String line;
Reading[] readings = new Reading[100];
int size = 0;
try {
input = new BufferedReader(new FileReader("readings.txt"));
line = input.readLine();
while (line != null) {
// For now, just print the line
System.out.println(line);
line = input.readLine();
}
input.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
for (int i = 0; i < size; i++) {
System.out.println(readings[i]);
}
System.out.println(" End of processing.");
}
}
class Reading {
private int temperature; // in degrees C
private int windSpeed; // in km/h
private String windDirection; // e.g. N,SW,ESE
private int precipitation; // in mm
private boolean isSnow; // true if precipitation fell as snow
public Reading(int temperature, int windSpeed, String windDirection,
int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = precipitation;
this.isSnow = isSnow;
}
/*
public Reading(int temperature, int windSpeed, String windDirection) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = 0;
this.isSnow = false;
}
public Reading(int temperature, int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = 0;
this.windDirection = "";
this.precipitation = precipitation;
this.isSnow = isSnow;
}
*/
public int getTemperature() {
return temperature;
}
public int getPrecipitation() {
return precipitation;
}
public String getWindDirection() {
return windDirection;
}
public String toString() {
String result;
result = "temp: " + temperature + "C wind: " + windSpeed + windDirection +
" precipitation: " + precipitation;
if (isSnow) {
result += "cm snow";
} else {
result += "mm rain";
}
return result;
}
}
import java.io.*;
public class Activity3C {
public static void main(String[] args) {
BufferedReader input;
String line;
String[] tokens;
int temperature;
String numeric, direction;
int speed;
int precipitation;
String unit;
int separation;
Reading[] readings = new Reading[100];
int size = 0;
try {
input = new BufferedReader(new FileReader("readings.txt"));
line = input.readLine();
while (line != null) {
tokens = line.split(",");
temperature = Integer.parseInt(tokens[0].trim());
tokens[1] = tokens[1].trim();
separation = firstNonNumericPosition(tokens[1]);
numeric = tokens[1].substring(0, separation);
speed = Integer.parseInt(numeric.trim());
direction = tokens[1].substring(separation).trim();
if (tokens.length > 2) {
tokens[2] = tokens[2].trim();
separation = firstNonNumericPosition(tokens[2]);
numeric = tokens[2].substring(0, separation);
precipitation = Integer.parseInt(numeric.trim());
unit = tokens[2].substring(separation).trim();
} else {
precipitation = 0;
unit = "";
}
readings[size] = new Reading(temperature, speed, direction,
precipitation, unit.equalsIgnoreCase("cm"));
size++;
line = input.readLine();
}
input.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
for (int i = 0; i < size; i++) {
System.out.println(readings[i]);
}
System.out.println(" End of processing.");
}
public static int firstNonNumericPosition(String str) {
int pos = -1;
int i = 0;
while (pos < 0 && i < str.length()) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
pos = i;
} else {
i++;
}
}
return pos;
}
}
class Reading {
private int temperature; // in degrees C
private int windSpeed; // in km/h
private String windDirection; // e.g. N,SW,ESE
private int precipitation; // in mm
private boolean isSnow; // true if precipitation fell as snow
public Reading(int temperature, int windSpeed, String windDirection,
int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = precipitation;
this.isSnow = isSnow;
}
/*
public Reading(int temperature, int windSpeed, String windDirection) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = 0;
this.isSnow = false;
}
public Reading(int temperature, int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = 0;
this.windDirection = "";
this.precipitation = precipitation;
this.isSnow = isSnow;
}
*/
public int getTemperature() {
return temperature;
}
public int getPrecipitation() {
return precipitation;
}
public String getWindDirection() {
return windDirection;
}
public String toString() {
String result;
result = "temp: " + temperature + "C wind: " + windSpeed + windDirection +
" precipitation: " + precipitation;
if (isSnow) {
result += "cm snow";
} else {
result += "mm rain";
}
return result;
}
}
import java.io.*;
public class Activity3D {
public static void main(String[] args) {
BufferedReader input;
String line;
String[] tokens;
int temperature;
String numeric, direction = "";
int speed;
int precipitation = 0;
String unit = "";
int separation;
Reading[] readings = new Reading[100];
int size = 0;
try {
input = new BufferedReader(new FileReader("readings.txt"));
line = input.readLine();
while (line != null) {
tokens = line.split(",");
temperature = Integer.parseInt(tokens[0].trim());
tokens[1] = tokens[1].trim();
separation = firstNonNumericPosition(tokens[1]);
if (separation == 0 || (separation < 0 && Integer.parseInt(tokens[1]) != 0)) {
speed = -1;
} else {
if (separation < 0) {
speed = 0;
direction = "";
} else {
numeric = tokens[1].substring(0, separation);
speed = Integer.parseInt(numeric.trim());
direction = tokens[1].substring(separation).trim();
}
if (tokens.length > 2) {
tokens[2] = tokens[2].trim();
separation = firstNonNumericPosition(tokens[2]);
if (separation <= 0) {
precipitation = -1;
} else {
numeric = tokens[2].substring(0, separation);
precipitation = Integer.parseInt(numeric.trim());
unit = tokens[2].substring(separation).trim();
}
} else {
precipitation = 0;
unit = "";
}
}
if (speed < 0 || precipitation < 0) {
System.out.println("Error in input: " + line);
} else {
readings[size] = new Reading(temperature, speed, direction,
precipitation, unit.equalsIgnoreCase("cm"));
size++;
}
line = input.readLine();
}
input.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
for (int i = 0; i < size; i++) {
System.out.println(readings[i]);
}
System.out.println(" End of processing.");
}
public static int firstNonNumericPosition(String str) {
int pos = -1;
int i = 0;
while (pos < 0 && i < str.length()) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
pos = i;
} else {
i++;
}
}
return pos;
}
}
class Reading {
private int temperature; // in degrees C
private int windSpeed; // in km/h
private String windDirection; // e.g. N,SW,ESE
private int precipitation; // in mm
private boolean isSnow; // true if precipitation fell as snow
public Reading(int temperature, int windSpeed, String windDirection,
int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = precipitation;
this.isSnow = isSnow;
}
/*
public Reading(int temperature, int windSpeed, String windDirection) {
this.temperature = temperature;
this.windSpeed = windSpeed;
this.windDirection = windDirection;
this.precipitation = 0;
this.isSnow = false;
}
public Reading(int temperature, int precipitation, boolean isSnow) {
this.temperature = temperature;
this.windSpeed = 0;
this.windDirection = "";
this.precipitation = precipitation;
this.isSnow = isSnow;
}
*/
public int getTemperature() {
return temperature;
}
public int getPrecipitation() {
return precipitation;
}
public String getWindDirection() {
return windDirection;
}
public String toString() {
String result;
result = "temp: " + temperature + "C wind: " + windSpeed + windDirection +
" precipitation: " + precipitation;
if (isSnow) {
result += "cm snow";
} else {
result += "mm rain";
}
return result;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
