Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Help with Arrays

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am having a hard time with java syntax. I can do this on paper and in a flow chart, but I'm having a hard time grasping the java language. Basically this is what I need to do:

Write a program that prompts a user for a list of 5 prices. The program is then to compute and display the sum of all prices, the average of the prices, and all prices that are higher than the calculated average. Additionally, I need to break my code out by using the following methods: sumArray, aveArray, and highPrices. Can anyone please give me a hand with this? It would be greatly apprciated!

Heres what I have:



Needless to say, this does not compile, let alone let me enter much of anything.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the 5 integers into an int array like this.



assign the prices to the int array like this:



As for the functions.



I did not give you the full code intentionally to have you solve it mostly on your own. Happy coding.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonathon,

Welcome to java ranch

Hi Sebastian Janisch int prices = new int[5];



this will not work and initialize your int array... it declare as ...



 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nishan Patel wrote:

this will not work and initialize your int array... it declare as ...





Darn it... It was late
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would avoid using arrays, and use collections instead. What if your requirements change and you're given 4 numbers, or 6, or 897?

To do this, you would code PriceArray to have an add(int) method. PriceArray would then need a private List used to store the values. Note that collections must store objects, not primitives, so the add method would need to box the passed value as an Integer.

Or, because of the advent of generics, you could simply subclass ArrayList<Integer>. If you did this, your sumArray method would look like this:
(By the way, I think I'd name those methods getSum(), getAverage() and getHighPrices(), but I guess that's a matter of taste.)
 
Marshal
Posts: 69771
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Max Rahder wrote:I would avoid using arrays, and use collections instead. . . .

But he has been set this as an exercise in using arrays
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

But he has been set this as an exercise in using arrays
Jonathon: Tell your teacher that good encapsulation means he shouldn't care whether or not you actually use an array! ;)
 
Campbell Ritchie
Marshal
Posts: 69771
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Max Rahder wrote:Jonathon: Tell your teacher that good encapsulation means he shouldn't care whether or not you actually use an array! ;)

And the teacher will say, "not using an array when I told you to use arrays to learn array syntax means a mark of 0."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic