Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# A class to encapsulate the concept of daily temperatures for a week

H. O.
Greenhorn
Posts: 1
So, these are the instructions:

1. Create a class encapsulating the concept of daily temperatures for a week. Prompt the user for 7 values; store the values in an array.

This class must include the following methods to manage the array: Accessors, mutators, toString and equals methods

Method to calculate number of days below freezing
Method to determine and return largest temperature change between any 2 consecutive days

2.Write a client class to create objects from the class in step 1. In the client class main, you need to create a second array using an initializer list containing the following data:
32, 45, 52, 66, 76, 53, 31

Write the following method calls in your main:

A. Call the method to calculate the number of days below and freezing for each of the arrays.
B. Use the equals method to compare the 2 arrays.
C. Call the method that determines and returns the largest temperature change for each of the arrays.
D. Use the toString method to print out each of the arrays.

______________________________________________________

This is what I have come up with so far:

import java.util.Scanner;

public class Weather
{

int [] tempWeather = new int [7];

Scanner scan = new Scanner(System.in);

//--------------
//Mutator method
//--------------

public void inputTemps()
{
for (int i=0; i<tempWeather.length; i++ )
{
System.out.print( "Enter temperature for day " + (i+1) + "/t" );

tempWeather[i] = scan.nextInt();
}
}

//---------------
//Accessor method
//---------------

public int [] getTempWeather()
{
int [] temp = new int [tempWeather.length];
for (int i = 0; i < tempWeather.length; i++)
{
temp[i] = tempWeather[i];
}
return temp;
}

//----------------------------------
//Method to calculate freezing temps
//----------------------------------

public int getFreezing()
{
int temp = 0;

for (int i=0; i<tempWeather.length; i++)
{
if ((tempWeather[i] < 32) || (tempWeather[i] == 32))
temp++;
}
return temp;
}

//---------------------------------------
//Method to calculate largest temp change
//---------------------------------------

public int getMaxChange()
{
int i = 0;
int max = tempWeather[0];
int temp = Math.abs(tempWeather[i]-tempWeather[i+1]);

for (i=1; i<tempWeather.length-2; i++)
{
if (temp > max)
max = temp;
}
return max;
}

//---------------
//toString method
//---------------

public String toString(int [] tempWeather)
{
for (int i=0; i<tempWeather.length; i++)
{
return "Temperatures:/t" + String.valueOf(tempWeather[i]);
}

}

}

--------------------------------------------------------------

I need help writing this program. I know for fact that my toString method is incorrect. If anyone could let me know what I've done wrong so far or what I've done right or guide me with the correct path I should take to finish this- anything at all really, I would greatly appreciate it! I've been trying to find the solution or anything that will help, but I haven't been too successful.

Maneesh Godbole
Saloon Keeper
Posts: 11257
16
"H.O"
Please check your private messages for an important administrative matter.