This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hello All, I am a newbie trying to learn thru doing exercises and reading from one study book.I have run into some troubles with one program that has me mentally stalled. Here is the problem I am working on.
The process of finding the largest value (i.e., the maximum of a group of values) is used frequently in computer applications.For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sell the most units wins the contest. Write a pseudocode program and then a Java application that inputs a series of 10 single-digit number as characters and determines and prints the largest of the number. Hint: Your program should use the following three variables: a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers been processed); b) number: The current digit input to the program; c) largest: The largest number found so far.
public static void main(String args[]) { String number; double sales; int counter; double largest; largest = 0; counter = 0;
number = JOptionPane.showInputDialog("Please enter the sales amount of item sold by Employee"); sales = Double.parseDouble(number);
while( counter!= 10) { counter = counter + 1; number = JOptionPane.showInputDialog("Please enter a single digit for the sales unit"); sales = Double.parseDouble(number);
}
JOptionPane.showMessageDialog(null," The largest number input was " + largest, "Paycheck", JOptionPane.INFORMATION_MESSAGE );
}
}
Now as you can see I don't have anything to calculate what the largest number is. THis is what my problem is. I can not think of a way to do this, because if I am reading this problem correct, it states to find the largest input from 10. The only way I can think of is if name those inputs number1, number2, number3,.....number10 and use if statements to find the largest, but I don't beleive this problem is asking for it to be calculated in that manner. Can someone please point me in the right direction?
Ali Khan
Greenhorn
Joined: Sep 27, 2005
Posts: 18
posted
0
Also, this problem is not to use arrays, as arrays are covered in a later chapter.
number1 = JOptionPane.showInputDialog("Please enter the sales amount of item sold by Employee"); sales1 = Double.parseDouble(number1);
if(sales1 > sales2) { largest = sales1; }
else { largest = sales2; } while( counter!= 10) { counter = counter + 1; number2 = JOptionPane.showInputDialog("Please enter a single digit for the sales unit"); sales2 = Double.parseDouble(number2);
}
JOptionPane.showMessageDialog(null," The largest number input was " + largest, "Paycheck", JOptionPane.INFORMATION_MESSAGE );
}
}
Ali Khan
Greenhorn
Joined: Sep 27, 2005
Posts: 18
posted
0
On 2nd thought, after careful review this does not solve it either, am I getting close? yeesh, I thought i had it
Ali, why don't you try this for a starting point: This needs some extra thought before it will be able to deal with the first number. You should be able to think of a couple of different ways that you could deal with that.
BTW, in your code, double is not the most approriate type for number and largest.
you never said you did this part. Kym did it for you. Believe it or not, this is probably the most critical part of ANY programming assignment/task/project. the first thing you should do is think about the problem, and not write ANY actual java code.
what you should do is take what Kym gave you, and break it down line by line. do you know how to "do something 10 times"
if so, great. you then look at the next line of the pseudocode. do you know how to "get the next number"? if the answer is no, then break it down.
in order to get the next number, i'd have to:
1) prompt the user, 2) get input from the keyboard 3) store is somewhere, perhaps only temporarily...
keep iterating over each line until you get to the point were you can look at your pseudocode, and say "oh... THAT is EASY..."
Never ascribe to malice that which can be adequately explained by stupidity.
Ali Khan
Greenhorn
Joined: Sep 27, 2005
Posts: 18
posted
0
Thanks! I got it this time How long have you studied Java? Ive graduated from my university but they only gave mostly c++ in their courses Now Im studying Java on my own, may I ask how long it took the 2 of you to learn Core Java?
Kenneth Albertson
Ranch Hand
Joined: Sep 18, 2005
Posts: 59
posted
0
Ali, I am a complete Java beginner and know almost nothing about the language. However I do have some experience programming in other languages. As fred point out, the problem that you were having was in your general approach to the programming problem, and was not related to the Java language. Would you really have found this problem any easier in C++ ?
Ali Khan
Greenhorn
Joined: Sep 27, 2005
Posts: 18
posted
0
No I don't believe I would have foudn it any easier in c++. The only experience I had of c++ was thru a classroom. I definately would not consider it as good experience. How long have you studied java?