• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Java arraylist finding min and max value  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment deadline is getting nearer and I am struggling with creating method that is able to find min and max values from my user input arraylist.
Basically we have 3 different classes "Property", "InsuranceInfoContainer" and Main class. "InsInfoContainer" class is supposed to "store" arraylist information and contains methods (ie method for min and max)
I have spent most of my day just trying to figure out how to do it and testing different ways.
It would be greatly appreciate if someone would be able to give me tips or help me with this (Getting kinda desperate at this point ;) )





Property class:

InsuranceInfoContainer class


Main class

Adding
If anyone would like to point me towards some good tutorial videos / resources on java
I find our lecture material to be extremely hard to understand and quite cluttered
Would love to learn java properly but I simply can't do it from our lecture materials

 
Jake Longman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Completely forgot to mention
We need to find min and max of "InsInfo" value
sorry if posting on this again like this isn't allowed
didn't see edit button and figured that it is quite important part to know what min / max value I have to find

 
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you create a new variable 'insInfoC ' which is empty, you already have 'list' which has been properly populated. To get min & max you need to go through 'list'.

Note that there are two accepted ways to initialize your 'min' and 'max' variables prior to entering your loop:
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your use of the variable name 'list' is very misleading here. People will be expecting that it implements 'List', which it does not. It would have been better if you named it 'insInfoContainer' (starting with a lower case letter), or just 'container'.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In InsInfoContainer, this constructor is never used.
Also, nowhere in your project is 'value' ever used.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'insInfoC' would have been better named 'propertyList', which is what it is. Then 'getinsInfoC()' would be 'getPropertyList()'.
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your class 'Property' you have 'Type', 'Location', and 'InsInfo'. These are all variables and so should begin with a lower case letter. Same for the parameter names in your constructor.

The variable name 'insInfo' doesn't really tell you what the data is used for. What kind of 'info'?

You will, of course, need the getter() methods. I question whether you need the setters though. Once a Property is constructed  will these values ever need to change?
 
Jake Longman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:In your class 'Property' you have 'Type', 'Location', and 'InsInfo'. These are all variables and so should begin with a lower case letter. Same for the parameter names in your constructor.

The variable name 'insInfo' doesn't really tell you what the data is used for. What kind of 'info'?

You will, of course, need the getter() methods. I question whether you need the setters though. Once a Property is constructed  will these values ever need to change?



Thank you!
I tried to implement most of your suggestions into my code.
Thought I am still unable to get min max work. I believe I have some sort of method / code for it "working"
I am just unable to make it read from my arraylist (It prints

What comes to stuff like
"this.value = value;"
"Why do you create a new variable 'insInfoC'"
Most of these things are failed attempts to do min max method
as I am getting really tired I keep forgetting to remove these old "tests"

Currently only thing I need for completing this assignment is to get min max working.
I am hoping that after this assignment I am able to start reading some better java materials than lecture material.




 
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The algorithm usually goes like this:

if the list is empty:
   Report an error "No items in list" and exit

Assume max/min is the first element

for each element in list:
   if element is greater/less than max/min
        max/min is current element

return max/min
 
Junilu Lacar
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using streams is even easier, by the way. Google for how to get max min of list in Java for examples. You might not have gotten that far in class though and might be expected to use the more traditional way with a for-loop.
 
Junilu Lacar
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Don't get greedy. You're trying to solve two different problems with one for-loop.  Good programmers will separate different tasks into different blocks. Even better yet, you'd define two different methods, one for min and one for max.

Since you're still learning, it would behoove you to not inflict pain on yourself and follow good programming practice.

Ideally, you'd just write this code:

findMaxInsurance() is a method that is focused solely on finding the max value. findMinInsurance() is a method that is focused solely on finding min value. You don't have to complicate things by mashing the two tasks into one block of code.

 
Junilu Lacar
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this statement is kind of headscratcher:

Either you're just guessing or you don't understand what's happening or both. What you're doing is this:

// calls the toString() method of the i-th Property object in the list
low = Integer.parseInt(insInfoC.get(i).toString())  

// try to parse that value as an Integer
low = Integer.parseInt(insInfoC.get(i).toString())  

Since the value you get back from toString() is not an integer value (it's Type + " " + Location + " " + InsInfo, which is NOT an integer), then your call to parseInt is guaranteed to fail.
 
Jake Longman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone
Currently I have managed to get it working
it isn't exactly what our teacher wants, they want us to input number and only show values that are bigger or lower than inputted number
but I think this is enough for me to pass this assignment (hopefully)
 
Marshal
Posts: 6684
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jake Longman wrote:it isn't exactly what our teacher wants, they want us to input number and only show values that are bigger or lower than inputted number
but I think this is enough for me to pass this assignment (hopefully)


Hi Jake and welcome to the Ranch. I'm slightly about a different thing - but completely in a friendly/fellow way.

I really wish you to change the way you think about your studies. This assignment isn't really about passing a module, but about understanding the concept(s), and how to solve problem(s) in a correct way, well at least it should produce an expected output.

Always set for yourself a maximum bar, always. Don't aim to get a passing mark, aim to get 100% for that and every other assignment, morevoer, aim to impress your professor by achieving more than he/she might expect from you.

At the beginning programming is indeed very difficult and could even discourage. But you need to crack it throught since and at the beginning, because all the concepts you are going to see in further assignments - will be related.

So my suggestion is, if you still have time - try to solve problem so it would behave in an expected manner. If you don't have time - do exactly the same, simply because that's crucial for further understanding.
 
We noticed he had no friends. So we gave him this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!