File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Program Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program Problem" Watch "Program Problem" New topic
Author

Program Problem

Ali Khan
Greenhorn

Joined: Sep 27, 2005
Posts: 18
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.



Here is what I have so far.

import javax.swing.JOptionPane;

public class Ch4_15 {


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
Also, this problem is not to use arrays, as arrays are covered in a later chapter.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19059
    
  40

c) largest: The largest number found so far.


The way the question is worded gives you a hint on how to find the largest number.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ali Khan
Greenhorn

Joined: Sep 27, 2005
Posts: 18
Thanksss!
I believe I have it!


import javax.swing.JOptionPane;

public class Ch4_15 {


public static void main(String args[])
{
String number;
double sales1;
double sales2;
int counter;
double largest;
String number1;
String number2;
sales1 =0;
sales2 = 0;
largest = 0;
counter = 0;

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
On 2nd thought, after careful review this does not solve it either, am I getting close? yeesh, I thought i had it
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11494
    
  16

Write a pseudocode program


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..."


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ali Khan
Greenhorn

Joined: Sep 27, 2005
Posts: 18
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
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
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program Problem