I Had to findThe hottest and coldest months of the year.
The average temperature for the year
The difference between the hottest and coldest months of the year.
The average temperature of any given month. The month is specified by input values: month (1,…,12) . Reject invalid input values (less than 1 and greater than 12 for month).
Things seem to be better but now I'm getting all my exception message and also java.lang.ArrayIndexOutOfBoundsException: 13 error messahe
Thanks
class InvalidMonthException extends Exception {
public InvalidMonthException(String message) {
super(message);
}
}
//Object that holds the temperatures and the methods
class Temperatures {
//The array to hold the temperatures
private double temperatures[];
public Temperatures() {
//Create an array of doubles to hold the temperatures
temperatures = new double[12];
}
public Temperatures( double temps[] ) {
// create an array of doubles to hold the temperatures, initially set to the values given
temperatures = new double[12];
for (int i = 0; i < 12; i++) {
temperatures[i] = temps[i];
}
}
// Method to return the hottest month of the year
public double getHottest() {
// find the largest entry
double theMax = temperatures[0];
for (int i = 0; i < 12; i++) {
// if we've found a larger temperature, then keep track of it
if ( temperatures[i] <= theMax ) {
theMax = temperatures[i];
}
}
return theMax;
}
// Method to return the coldest month of the year
public double getColdest() {
// find the smallest entry
double theMin = temperatures[0];
for (int i = 0; i < 12; i++) {
// if we've found a smaller temperature, then keep track of it
if ( temperatures[i] <= theMin ) {
theMin = temperatures[i];
}
}
return theMin;
}
//Method to return the average temperature for the year
public double getAvgYearTemp() {
// sum up all the entries in the array
double total = 0;
for (int i = 0; i < 12; i++) {
total += temperatures[i];
}
// compute the average and return it
return total / 12.0;
}
//Method to return the difference between the hottest and the coldest months
public double getHotColdDiff() {
return getHottest() - getColdest();
}
// Method to return the average temp for the specified month, month is 1...12
double getMonthAvgTemp(int month) throws InvalidMonthException {
if ( (1 <= month) && (month <= 12) ) {
return temperatures[month-1];
} else {
throw new InvalidMonthException( month + "is an invalid month. Need to be between 1 and 12.");
}
}
// set the average temp for the specified month, month is 1...12
public void setMonthAvgTemp(int month, double t) throws InvalidMonthException {
if ( (1 >= month) && (month <= 12) ) {
temperatures[month-1] = t;
} else {
throw new InvalidMonthException( month + "is an invalid month. Need to be between 1 and 12.");
}
}
}
public class Program2 {
public static void main(String args[]) {
// setup the temperature object with some values
double initialTemps[] = {10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,80.0,70.0,60.0};
Temperatures t = new Temperatures(initialTemps);
// print out the temperatures, try two invalid months (0 and 13)
for (int i = 0; i <= 13; i++) {
try {
System.out.println("Month" + i + t.getMonthAvgTemp(i) );
} catch (Exception e) {
System.out.println(e);
}
}
// call the methods to summarize the data
System.out.println("Hottest month" + t.getHottest() );
System.out.println("Coldest month" + t.getColdest() );
System.out.println("Difference between hot and cold " + t.getHotColdDiff() );
System.out.println("Average Temp." + t.getAvgYearTemp() );
}
}
This post has been edited by summer291: 19 August 2008 - 11:47 AM

New Topic/Question
Reply



MultiQuote



|