Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Java Code) Now that we have the data in an array, we can calculate the statisti

ID: 3905130 • Letter: #

Question

(Java Code)

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:

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.

Submit your complete modified program by the due date specified in the course schedule. 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).

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;

}

}

Explanation / Answer

*****************************************************

Reading.java

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 int hours = 0;

                private int minutes = 0;

                private boolean isSnow; // true if precipitation fell as snow

                public Reading(int temperature, int windSpeed, String windDirection,

                int precipitation, boolean isSnow, int hours, int minutes) {

                                this.temperature = temperature;

                                this.windSpeed = windSpeed;

                                this.windDirection = windDirection;

                                this.precipitation = precipitation;

                                this.isSnow = isSnow;

                                this.hours = hours;

                                this.minutes = minutes;

                }

                /*

                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 getTime() {

                                return hours + ":" + minutes;

                }

                public String toString() {

                                String result;

                                result = "temp: " + temperature + "C wind: " + windSpeed + windDirection +

                                " precipitation: " + precipitation ;

                                if (isSnow) {

                                                result += "cm snow";

                                } else {

                                                result += "mm rain";

                                }

                                result += (hours != 0 && minutes != 0 ? (" Time: " + hours + ":" + minutes) : "");

                                return result;

                }

}

*****************************************************

Activity3A.java

public class Activity3A {

public static void main(String[] args) {

                Reading r1, r2, r3;

                r1 = new Reading(-10, 27, "SSW", 0, false, 1, 43);

                r2 = new Reading(-18, 6, "E", 2, true, 0, 0);

                r3 = new Reading(4, 0, "", 3, false, 5, 55);

                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.");

}

}

*****************************************************

Activity3B.java

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.");

}

}

*****************************************************

Activity3C.java

import java.io.*;

public class Activity3C {

public static void main(String[] args) {

                BufferedReader input;

                String line;

                String[] tokens;

                String[] times;

                int temperature;

                int hours;

                int minutes;

                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 = "";

                                                }

                                               

                                                if(tokens.length > 3) {

                                                                times = tokens[3].split(":");

                                                                hours = Integer.parseInt(times[0].trim());

                                                                minutes = Integer.parseInt(times[1].trim());

                                                } else {

                                                                hours = 0;

                                                                minutes = 0;

                                                }

                                               

                                                readings[size] = new Reading(temperature, speed, direction,

                                                precipitation, unit.equalsIgnoreCase("cm"), hours, minutes);

                                                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;

                }

}

*****************************************************

Activity3D.java

import java.io.*;

public class Activity3D {

public static void main(String[] args) {

                BufferedReader input;

                String line;

                String[] tokens;

                String[] times;

                int temperature;

                int hours = 0;

                int minutes = 0;

                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(tokens.length > 3) {

                                                                                times = tokens[3].split(":");

                                                                                hours = Integer.parseInt(times[0].trim());

                                                                                minutes = Integer.parseInt(times[1].trim());

                                                                } else {

                                                                                hours = 0;

                                                                                minutes = 0;

                                                                }

                                                }

                                                if (speed < 0 || precipitation < 0) {

                                                                System.out.println("Error in input: " + line);

                                                } else {

                                                                readings[size] = new Reading(temperature, speed, direction,

                                                                precipitation, unit.equalsIgnoreCase("cm"), hours, minutes);

                                                                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;

}

}