public class CityWeatherV2 { // private instance variables - three arrays private String[] months;
Question:
public class CityWeatherV2 {
// private instance variables - three arrays
private String[] months;
private double[] temperature;
private double[] precipitation;
// constructor with parameters
public CityWeatherV2(String[] m, double[] t, double[] p) {
months = m;
temperature = t;
precipitation = p;
}
// setters and getters for each private instance variable
//Getters
public String[] getMonth(String[] months) {
return months;
}
public double[] getTemperature(double[] temperature) {
return temperature;
}
public double[] getPrecipitation(double[] precipitation) {
return precipitation;
}
//Setters
public void setMonth(String[] m) {
months = m;
}
public void setTemperature(double[] t) {
temperature = t;
}
public void setPrecipitation(double[] p) {
precipitation = p;
}
// method to calculate the average of temperature
public double calcAverageAnnualTemperature(double[] t) {
double total = 0;
double average = 0.0;
//for loop to add the values
for (int i = 0; i < t.length; i++) {
total = total + t[i];
}
average = total / t.length;
return average;
}
// method to calculate total precipitation array
public double calcTotalAnnualPrecipitation(double[] p) {
double total = 0;
//for loop to add the values
for (int i = 0; i < p.length; i++) {
total = total + p[i];
}
return total;
}
// method to calculate temperature in Celsius (current index of temperature array passed as a parameter) - to be completed in 6.02
public double calcFahrenheitToCelsuis(double celsius, double fahrenheit) {
celsius = ((fahrenheit - 32) * 5) / 9;
return celsius;
}
// method to calculate precipitation in centimeters (current index of precipitation array passed as a parameter) - to be completed in 6.02
public double calcInchesToCentimeters(double inches, double centimeters) {
centimeters = inches * 2.54;
return centimeters;
}
}
the above is the object class
_
_
_
_
import java.util.Scanner;
public class CityWeatherTesterV2
{
// method to print results (formatting output will be done in 6.02)
public String formattedTable() {
//Output: display table of weather data including average and total
System.out.println();
System.out.println(" Weather Data");
System.out.println(" Location: " + city +", " + state);
System.out.println("Month Temperature (" + temperatureInput + ") Precipitation (" + precipitationInput + ")");
System.out.println();
System.out.println("***************************************************");
//for( int index = 0; index < temperature.length; index++)
//{
// use printf to format
//}
System.out.println("Average: " + " Total: ");
}
public static void main (String [ ] args)
{
//Declare and initialize variables
Scanner in = new Scanner(System.in); //will be used in 6.02
String city = "Orlando"; //choose a city from the table provided
String state = "Florida"; //choose a city from the table provided
String [] month = {"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec." };
double [] orlandoTemperature = {60.9, 62.6, 67.4, 71.5, 81.2, 82.4, 82.5, 75.3, 68.8, 63.0};//complete initialization of months array
double [] orlandoPrecipitation = {2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};
String tempLabel = "F"; //initialize to F
String precipLabel = "in."; //initialize to in
//input to decide F/C and in/cm - to be completed in 6.02
System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): ");
String temperatureInput = in.next();
System.out.print("Choose the precipitation scale (i = inches, c = centimeters): ");
String precipitationInput = in.next();
System.out.println();
//instantiate AnnualWeatherV1 object
CityWeatherV2 annualWeatherData = new CityWeatherV2(month, orlandoTemperature, orlandoPrecipitation);
//conditions & method call to convert temp, if needed - to be completed in 6.02
annualWeatherData.calcFahrenheitToCelsuis();
if(temperatureInput.equalsIgnoreCase("F")) {
for(double tempData: orlandoTemperature) {
System.out.println(tempData);
}
}
else if(temperatureInput.equalsIgnoreCase("C")) {
for(double tempData: orlandoTemperature) {
System.out.println(tempData);
}
}
//conditions & method call to convert prec, if needed - to be completed in 6.02
annualWeatherData.calcInchesToCentimeters();
if(precipitationInput.equals("i")) {
for(double precData: orlandoPrecipitation) {
System.out.println(precData);
}
}
else if(precipitationInput.equals("c")) {
for(double precData: orlandoPrecipitation) {
System.out.println(precData);
}
}
//method call to calculate the average temperature
annualWeatherData.calcAverageAnnualTemperature();
//method call to calculate the total precipitation
annualWeatherData.calcTotalAnnualPrecipitation();
//method call to print results (hint: Need parameters for month, temperature, precipitation arrays, data labels and the average temperature, total precipitation variables)
formattedTable();
}//end main
}//end class
the above is the tester class
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch