This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.)