• Post Reply Bookmark Topic Watch Topic
  • New Topic

capturing maximum value in a file based on data in another arraylist  RSS feed

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think if I'm asking my question clearly but I'll explain it
I have two arraylist one contains hour and the other one contains temperature(already separated from a xml file into different arraylists). I need to calculate the average per hour. So I have a method that goes through time array and separate the hour from minutes and seconds. Now I'm trying to go through a loop and calculate the average while the hours are equal and choose the greatest value and whenever the time changes everything resets and calculation starts again. I'm really confused as it has to be separated from interface but I can't even get the logic right. my current code which is not working is as below


I have tried many ways to get this right and then convert it to a method but the logic doesn't work as abviously I'm doing something wrong.
I just need to print for eg. (max value "80" in hour 21) (max value "70" in hour 22)
Before I turn it in a method I need to get the logic right as I'm really confused and the more I focse on it the more confused I get.

Thanks for your help in advance
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:. . . I have two arraylist one contains hour and the other one contains temperature . . .
That sounds like a mistake. I think you shou‍ld start by creating a WeatherRecord class which incorporates both temperature and time. It is probably worthwhile creating some method of comparing two WeatherRecord instances to see which is “bigger”. You can read about ordering objects in the Java™ Tutorials. Once you have done that, you will be able to work out ways of capturing the WeatherRecord instance with the highest temperature. I think you will have to write down how you are going to use the time to average the temperatures across the day. Once you have the algorithm worked out, turning it into code shou‍ld be quite easy.

I don't know what a Sensor is in line 8.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also tried the code bellow and similar codes

But the problem is if let's say there are hours (21,22,23) in arraylist it prints it out as
21
22
23
21
22
23
21
22
23
repeated values
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
saeid jamali wrote:. . . I have two arraylist one contains hour and the other one contains temperature . . .
That sounds like a mistake. I think you shou‍ld start by creating a WeatherRecord class which incorporates both temperature and time. It is probably worthwhile creating some method of comparing two WeatherRecord instances to see which is “bigger”. You can read about ordering objects in the Java™ Tutorials. Once you have done that, you will be able to work out ways of capturing the WeatherRecord instance with the highest temperature. I think you will have to write down how you are going to use the time to average the temperatures across the day. Once you have the algorithm worked out, turning it into code shou‍ld be quite easy.

I don't know what a Sensor is in line 8.

I to do calculations for light, temperature, vcc and etc per hour. I have a class Sensor and all these sensor readings are declared there with getter and setter. When I read the files I separate Those Which I need in a arraylist so I can do calculations on them. I have three different data file format so I think this is the best way as it was suggested by our tutor.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:I also tried the code bellow and similar codes . . .
That looks like guessing. You can try 1000000 guesses, and there is a good chance that one will be correct. Or you can think through the problem and get a correct solution straight away.
I disagree about multiple Lists being a good way to organise data. If you have wind speed, sunshine, (what's vcc) rain, etc., those can all be fields in a WeatherRecord object and they can all be searched on. You can find weather records with the brightest sunshine, coldest frost, wettest rain, warmest temperature, etc. for the day or week like that. Once you have worked it out for temperature, you can use a similar technique for all the other features.
You say the logic doesn't work. Have you written down what the logic is?
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
saeid jamali wrote:I also tried the code bellow and similar codes . . .
That looks like guessing. You can try 1000000 guesses, and there is a good chance that one will be correct. Or you can think through the problem and get a correct solution straight away.
I disagree about multiple Lists being a good way to organise data. If you have wind speed, sunshine, (what's vcc) rain, etc., those can all be fields in a WeatherRecord object and they can all be searched on. You can find weather records with the brightest sunshine, coldest frost, wettest rain, warmest temperature, etc. for the day or week like that. Once you have worked it out for temperature, you can use a similar technique for all the other features.
You say the logic doesn't work. Have you written down what the logic is?

So far I have a class that contains variables like light, temperature etc and each have a getter and setter so when I read a file I create a new object and then add each value using setter and then I add the object to my list(Is this what you are saying??) for my calculations I have a diferent list and I add data to the list using getter and pass the list to a method to do the calculation. The reason is I'm using GUI and I need to keep user interface and the logic separate. So which part can be improved and how?
Thank you
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:. . . when I read a file I create a new object and then add each value using setter and then I add the object to my list(Is this what you are saying??)
Yes
for my calculations I have a diferent list . . .
Nononono. Use the getXXX methods in the original List. Don't try duplicating data like that. I am not convinced that you need setXXX methods if you can set all the fields via the constructors. Better to make your WeatherRecord objects immutable. Also, if you ever return the List, return an unmodifiable view of it. Otherwise there is a risk of your giving the List, temperature, rainfall, etc. too wide an access and there is a risk of the data changing while you are not watching. The thing about, “I have a diferent list ... I'm using GUI,” doesn't look correct to me, I am afraid.
Consider copying all the contents of the List into a Map that maps time/date to readings. Read about Maps in the Java™ Tutorials here and here.
Once you have one list, write down the formula/algorithm you are using to average the temperatures; once you have that formula in nice simple writing you will probably find it easy to implement as code. Do everything you need with temperatures. Create a WeatherStation class which encapsulates the list of readings and has methods like getTemperatureForTime and getAverageTemperatureForDay. You will need those methods for use with your GUI later.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your guidance guys I'm more clear on what I'm going to do now. Here is one more question. When We are asked to keep User Interface and the logic separate then How can You keep something like the code below separate from user interface

There is a lot of outputs that is printed out. Is it ok if I keep something like this in my user interface? Or can I use it a method and every time I have an output I just add it to a list and at the end return the list? or is there any other way?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method should do one thing well.  A class should be a related set of data and methods.  So first ask yourself, does your method do one thing well?  What is the one thing?  Where does that fit into a class?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A WeatherStation instance would contain a list of WeatherRecord instances in date/time order.
A WeatherStation class might contain these methods
Then in your user interface
Each method does one thing well.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does this make any sense?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a lot easier to understand the code when you post the entire class.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:It's a lot easier to understand the code when you post the entire class.

It's almost the entire class just ignored the variables


That code works perfectly if it's in user interface (main class) but I don't know how to use it as a method so I can call it from User interface class. I tried to comment out the system.out.print and instead set values and add them to a list.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the complete class, correctly indented.  Personally, I'm not going to take the time trying to stitch together your code to see if it's correct or not if you're not willing to post complete, well-indented code.  I think others feel that way too.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Post the complete class, correctly indented.  Personally, I'm not going to take the time trying to stitch together your code to see if it's correct or not if you're not willing to post complete, well-indented code.  I think others feel that way too.

You are right.
The code below is the one that works fine the way I want in the user interface

The code below is what I'm trying to make as a method but it is not working

And this is how I'm trying to call it

I have never came across a method that needs to output like a few things and has a lot of if statements.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static List<Sensor> findMaxPerHour(List<Double> array) {
You have a List but you call it "array", this is confusing. The name "array" does not indicate what it is a List of. Later you compare the contents to "hour" but hour is an int and your List is a Double. What data does the List hold?

Says it returns List<Sensor> but in the body you declare "list" as a Datalist. Why not just List<sensor> list = new ArrayList<>()?
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:public static List<Sensor> findMaxPerHour(List<Double> array) {
You have a List but you call it "array", this is confusing. The name "array" does not indicate what it is a List of. Later you compare the contents to "hour" but hour is an int and your List is a Double. What data does the List hold?

Says it returns List<Sensor> but in the body you declare "list" as a Datalist. Why not just List<sensor> list = new ArrayList<>()?

The list has a data temperature which decimal but hour is integer like 18, 20, 21, 22. I have a datalist class and I'm getting my our information out of that clasa as it already contains hours at his point.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, here you are comparing temperature to hour (?)
if(array.get(i) != hour ){
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Then, here you are comparing temperature to hour (?)
if(array.get(i) != hour ){

Thanks for finding the first mistake, it should be

but the main problem still remains the same.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are headed down the wrong path as others have mentioned. You need a class that holds a time and a temperature. Example:

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I think you are headed down the wrong path as others have mentioned. You need a class that holds a time and a temperature. Example:


I have them in my Sensor class

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My time format is like 2010/5/4 21:57:34 so I have to read the file as string and split it again and again until I get to ours only as I need only the hour and I added it to an arraylist that holds only hour without date and minutes/seconds
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing Sensor code helps.

Why are all the data types "String"?
Why isn't "millis" a "long", for instance?
"millis", "stamp", and "dateTime" seems to be different formats of date and time. If this is the case then there should only be one variable to hold date-time.

you should provide a constructor that sets all the fields and do away with the setters.

public static List<Sensor> findMaxPerHour(List<Double> array) {
Should take a List<Sensor> as an argument, not List<Double>. A static utility method should get all of its data from arguments and not rely on static data from other classes.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java class "LocalDateTime" has the ability to parse and format date-time information. It also has a getHour() method.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Seeing Sensor code helps.

Why are all the data types "String"?
Why isn't "millis" a "long", for instance?
"millis", "stamp", and "dateTime" seems to be different formats of date and time. If this is the case then there should only be one variable to hold date-time.

You should provide a constructor that sets all the fields and do away with the setters.

public static List<Sensor> findMaxPerHour(List<Double> array) {
Should take a List<Sensor> as an argument, not List<Double>. A static utility method should get all of its data from arguments and not rely on static data from other classes.

I'm reading three different file format and some of them like csv needs to read variable as string and then I parse them to another datatype after splitting them. I'm using setters in other classes which are my file readers.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So considering all of these. is there any way I can use that code as a method separate from my user interface?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Not that code. But having a method that takes a list of Sensor data and returns another list of Sensor data with only one Sensor instance per hour representing the max temperature in the hour is definitely do-able.

Here's how to parse a date-time

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:I'm using setters in other classes which are my file readers.

Then your design is flawed. Your file reader should gather all the values and then construct a Sensor instance passing in all the values at once. No setters needed.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor should be as simple as

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you saying that I can't find the maximum value per hour because of the way I'm parsing datetime? what is the way of having a method that finds max value per hour?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:


No, what I'm saying is have a LocalDateTime field instead of millis, stamp, and datetime.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
saeid jamali wrote:


No, what I'm saying is have a LocalDateTime field instead of millis, stamp, and datetime.

They are different data
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:
Carey Brown wrote:
saeid jamali wrote:


No, what I'm saying is have a LocalDateTime field instead of millis, stamp, and datetime.

They are different data

Which one are you using to get the "hour" from? Do you then need any of the others?
Just because a file has that data doesn't mean you need to store it in its native form.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote my own method to find the hour. I have a variable in Sensor class which gets the hour value from a method
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:I wrote my own method to find the hour. I have a variable in Sensor class which gets the hour value from a method

I assume the method is relying on one of: millis, stamp, or dateTime to compute the hour.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
saeid jamali wrote:I wrote my own method to find the hour. I have a variable in Sensor class which gets the hour value from a method

I assume the method is relying on one of: millis, stamp, or dateTime to compute the hour.

datetime
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:
Carey Brown wrote:
saeid jamali wrote:I wrote my own method to find the hour. I have a variable in Sensor class which gets the hour value from a method

I assume the method is relying on one of: millis, stamp, or dateTime to compute the hour.

datetime

So, do you really need to keep millis and stamp around? If so, what will they be used for?
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
saeid jamali wrote:
Carey Brown wrote:
saeid jamali wrote:I wrote my own method to find the hour. I have a variable in Sensor class which gets the hour value from a method

I assume the method is relying on one of: millis, stamp, or dateTime to compute the hour.

datetime

So, do you really need to keep millis and stamp around? If so, what will they be used for?

just printing their values with no calculation.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example of what a Max method might look like if it was a static method in the (ideal) Sensor class. The output list would only have one Sensor instance per hour.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:
Carey Brown wrote:So, do you really need to keep millis and stamp around? If so, what will they be used for?

just printing their values with no calculation.

So, if you have several Sensor items in an hour, which item's millis and stamps will you keep?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!