Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program Problem

 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this problem is not to use arrays, as arrays are covered in a later chapter.
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..."
 
Ali Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic