• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to set this up in a single for loop?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys so my teacher wanted us to make this program that does the following ...


September 2015 was one of the hottest months on record for the City of Los Angeles. We are going to create a Java console application to see just how hot it was. In order to accomplish this challenge, I would like you to create an array of 30 elements to store each of the day's high temperatures in LA during the month of September. Note that the daily temperatures below are reported as whole numbers, just as they are among all forecasts, so please create an integer array.

Your application should prompt the user to enter all 30 days (high temperatures only), then report the highest temperature recorded during the month, the "average high" temperature and the most frequent high temperature (e.g. the mode).

///
Now the catch is that I have to create this program and all of its calculations, along with its data entry in one single for loop. Also, if there is more than one mode (meaning that more than one temperature is the most frequent), I'd like you to report the modes as follows:

[if there are exactly 2 modes]

September's most frequent high temps were Bimodal (two modes): 90 92

-or-

[if there are > 2 modes, it is called a "multi-modal" scenario]

September's most frequent high temps were Multi-modal (more than two modes): 88 90 92


//
Im so confused on arrays that i thought to myself well lets make this program in a basic foundational way, and so i did as my code below shows, but i had to separate it into separate for loops, and i just can't seem to understand how to combine everything under one single loop... can anyone guide me or give me some pointers ??



 
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that seriously a constraint given by your teacher for this problem? Can you please share exactly what your teacher wrote? I'd like to see if it's not just something that got lost in translation or interpretation. Otherwise, it's a horrible constraint.
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perform all data entry and calculations in one loop (it can be done!). Also, if there is more than one mode (meaning that more than one temperature is the most frequent), I'd like you to report the modes as follows:

[if there are exactly 2 modes]

September's most frequent high temps were Bimodal (two modes): 90 92

-or-

[if there are > 2 modes, it is called a "multi-modal" scenario]

September's most frequent high temps were Multi-modal (more than two modes): 88 90 92
 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are people who can kick themselves in the head, too. Just because it can be done doesn't mean you should. Shame on this teacher.

Have you been taught about Lists? Because that's the only way I can think of to do all that in just one loop.

Let me rephrase that: Can you use Lists? Are you allowed to use things you weren't specifically taught about in class? Is there an explicit instruction that says you CAN'T?
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't an explicit statement saying we can't. Ive tried looking things up online with arrays, but being an introductory class to java i still have some work to do when it comes to understanding the syntax of some of the techniques I've come across on stack overflow. I honestly would like to try and be able to follow what I'm coding.. otherwise its just copy and pasting :/
 
Mihai Gherghina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, to answer your question no we have not been told of lists from what i know. Unless its something like enumeration ?
 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Enumeration won't help. I suppose you could do it with arrays but Lists would be more convenient. The mode calculation is the trickiest because you have to track multiple modes potentially. It can be done with just arrays but I think it's beyond what I consider fair given the capabilities of most newbies.

Basically, you need to keep a histogram of temperatures. That is, you would track how many times each particular temperature was entered, then update a modeValue variable when necessary.

A Map would be the best structure to use for the histogram. The map key would be the temperature and the corresponding value would be the number of times it has been entered.

Here's an example:

Start with modeValue equal to 0
Day 1: 93 --- add to map(93, 1), 1st time 93 entered; modeValue is now 1
Day 2: 95 --- add to map(95, 1), modeValue remains 1
Day 3: 95 --- update map(95, 2), 2nd time 95 entered, modeValue is now 2
Day 4: 97 --- add map(97, 1), modeValue remains 2
Day 5: 97 --- update map(97, 2), 2nd time 97 entered, modeValue remains 2
Day 6: 99 --- add map(99, 1), modeValue still 2
Day 7: 97 --- update map(97, 3), modeValue is now 3
... And so on.

Since there's no stipulation that you can't use another loop to report the results, you just use another loop to iterate over the map and report any map entries that have a count equal to the modeValue that you tracked in the entry/calculation loop.

Does that make sense?

You would need to read up on java.util.Map and it's methods, in particular get, put, and containsKey.
 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although looking at your code, it looks like you already have a histogram in the frequencies array. You'd just have to make an assumption as to what your largest possible temperature would be instead of using largest+1 as you are doing now.

The reason I said "it's beyond the capabilities of most newbies" was because I was trying to translate what I would do with a Map to something that used an array instead. In hindsight, that was a bad idea and the more straightforward approach you use with the frequencies array is easier to understand even if it wastes a little memory.

The example I gave using a Map is basically what you're doing with that array; just figure out which part of that you can move inside that one loop.

Again, it's not a good idea to use that one loop but since your teacher seems to enjoy watching people kick themselves in the head then you'll just have to bite the bullet and do it the way he/she wants it to be done.
 
Junilu Lacar
Sheriff
Posts: 11496
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, your variable name, maxFrequency, is better than my modeValue, so stick with that name.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mihai Gherghina wrote:Now the catch is that I have to create this program and all of its calculations, along with its data entry in one single for loop.

I agree with Junilu; this is an artificial and rather pointless exercise - and definitely one you don't want to get in the habit of doing regularly.
However, since we're unable to read your teacher's mind, we can't really tell if s/he has an ulterior motive.

I also don't see any restriction that says you have to use only one method; just one loop.

So my advice, before you write any code, would be to go through the instructions and:

1. Break down everything you have to do inside the loop into tasks, and write a method for each one.

2. Make a list of all the information (totals, most common temperatures, etc) and store those in fields outside main().

Indeed, you might be better off to simply create an LATemps object that contains them as fields, and update them with instance methods - like getters and setters.
This page shows you a couple of ways you can do that.

Remember: just because you're only allowed one loop, doesn't mean you're only allowed one method.

HIH

Winston
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . I also don't see any restriction that says you have to use only one method; just one loop. . . .
Actually it says to add all the values and calculate the average in one loop; does that mean you have to calculate the mode in the same loop? I can't think offhand about a simple way to display modes in the one loop, because the modes do not actually exist until the loop has completed. Maybe I am mistaken there, however.

I would suggest you calculate the average (=arithmetical mean, because if I go into pedantic mode a mode is a kind of average, too) in the loop. You can easily enter values (by hand or from the file) and calculate the maximum, minimum and total (which you use to calculate the mean) all in one loop. Do that. Leave the modes out for the time being. Once you have got that working, consider adding the modes later.

public static final int DAYS_IN_SEP = 30;
?
Yes, I like it; that is how you should write a constant.
You can simplify lines 16-27 a lot with the printf method. It can even add the leading 0 for you. More details in these Java™ Tutorials sections:- 1 2. I think lines 16-27 ought to be a method in their own right, in which case the Scanner object should be a private field. And never close a Scanner reading from System.in. Not even if Eclipse calls it a resource leak.

I don't know whether you understood Junilu's suggestion about Maps, but I think you would do well to go through the loop with paper and pencil and eraser (the latter is very important) and try to work out modes by hand. Observe how you calculate modes. Once you have got that process worked out, then you should consider how to convert it to code. We can find more suggestions once you get that all worked out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!