programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# 12 value comparison.

Daniel Brackett
Greenhorn
Posts: 8
Hi

I am looking for help on a school project where I have to compare 12 average monthly temperatures and return the month with the hottest temp. How's the code looking is it going to be able do what I asked of it? Do I need to create a new MonthTemp Object in my tester class(not included) each time a enter a new value? PS I cannot use arrays.

Zeeshan Sheikh
Ranch Hand
Posts: 144
1. Why loop is running only 3 times?

2. You already initialized max_temp = -100? why it could be any number

3. If you create 12 objects then you have to compare 12 objects.

4. Logic: get input from user; first input assign it max_temp, 2nd input, compare it with max_temp if 2nd input > max_temp then max_temp = 2nd input & then finally output max_temp.

Campbell Ritchie
Marshal
Posts: 56562
172
I presume the -100 was in order to get the initial value smaller than its true value. I think there is an alternative method; you can start by setting the maximum equal to the first month’s temperature and then iterate the remainder of the array. Of course, you will suffer an Exception if you try that technique on a 0-length array.

Daniel Brackett
Greenhorn
Posts: 8
import java.util.*;

public class TempChecker3
{
private int cm;
private double max_temp;

Scanner s = new Scanner(System.in);
public void highMonth()
}
double max_temp;
for (int cm = 0; cm < 12; cm++)
{
double temp = s.nextDouble();
if (cm == 0)
{
max_temp = temp;
max_month = cm;
}
else if (temp > max_temp)
{
max_temp = temp;
max_month = cm;
}

}
}
}

Zeeshan Sheikh
Ranch Hand
Posts: 144
what's the question Daniel?

Henry Wong
author
Sheriff
Posts: 23295
125
Daniel Brackett wrote:

Also, this is a bad cut-n-paste, as this code won't compile.

Henry

Sarah Mitchell
Greenhorn
Posts: 17
Ok I have a question regarding this !
Why doesn't this class have a main() method ? will it ever work like this ?

Zeeshan Sheikh
Ranch Hand
Posts: 144
• 1
Daniel hasn't posted all the classes, as per his above post "tester class(not included)".

Sarah Mitchell
Greenhorn
Posts: 17
Zeeshan Sheikh wrote:Daniel hasn't posted all the classes, as per his above post "tester class(not included)".

Oh I see, thanks for the clarification.

I managed to come up with this, it might help to give some idea :

Good Luck

 Consider Paul's rocket mass heater.