• Post Reply Bookmark Topic Watch Topic
  • New Topic

12 value comparison.  RSS feed

 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;

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


Scanner s = new Scanner(System.in);
//read 12 values
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
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the question Daniel?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Brackett wrote:


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

Henry
 
Sarah Mitchell
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel hasn't posted all the classes, as per his above post "tester class(not included)".
 
Sarah Mitchell
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!