Need help on formulating a code for a JAVA program

Hi, I am new to JAVA and programming. I am teaching myself JAVA, so I am not that good and am trying to write this particular program. First, I will give the details of the requirements of the program. I know for many people it might be easy, but I am just a fresh programmer and I just want to use the suggestions as a reference. The following are the requirements for a program that I am working on. There are two parts to this program.

Part A:

Write a JAVA program(Application or Applet) to do the following:
1) Take in five stock prices (expressed as decimal numbers such as 32.75) from the user.
2) Display the items accepted for input prior to any processing. Label this output as Echoprint.
3)Only one input are should be used (and reused) for input.
4) After the 5th item has been accepted, the program should print the highest and the second highest stock price among the five inputs. This output should be labeled: Max and Secondmax.
5)The program should be capable of accepting an indefinite stream of input and the user should indicate termination of input by entering a stock price of -1.

Part B:
1)Modify the program above to accept a stock symbol (like Ntsc for Netscape and etc) followed by the 5 stock prices.
2) Print the Highest number and the Second highest number preceded by the stock symbol.

I do know some Visual Basic and kind of know how to find the Min. and Max.  numbers from a given set of numbers. I also know how to do bubble sort. But, you see, I  cannot do it in a single pass. I have to do the above program in a single pass.
I need help with Part A: #4 and Part B. Any feedback would be greatly appreciated.

I am using Microsoft J++ 1.1 under Windows 95 environment.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skilambAuthor Commented:
Edited text of question
Sounds like a homework to me so I will not give any specific answers.

For A4 look at java.lang.Math for max methods. For B you should be able to use very much the same approach as for A. The problem might be to pair the stock symbol string with the stock price. I would recommend that you introduce a new class for that, which you can use to access the information about stock symbols and prices.
skilambAuthor Commented:
Thanx, froderik, for the comments. You are right. It is a homework problem which was given to a friend of mine who took an introductory JAVA course about a year ago. I am using his book and the assignments his teacher gave to study on my own. Obviously, he was not able to solve the problem in a single pass and he won't tell him the solutions. Once again, thanx for the comments.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

I wouldn't mind answering any other questions about Java, object orientation and whatever you might have. If you are studying on your own you might need help especially if you are new to object orientation and programming.
If you're learning Java, use a real Java compiler.  VJ++ is about as far from pure java as you can get.  Download JavaPureCheck and run it against your .class files for a surprise.
for A#4 how about:

Assume stock prices are in a float array named stock.

float max=stock[0];
float smax=stock[1];
{   max=stock[1];
for(int i=2; i<5; ++i)
{   if(stock[i]>max)
    {   smax=max;
    else if(stock[i]>smax)smax=stock[i];
//now max contains the greatest and smax the second.

To accomplish B you will have to use indirection in order to be able to show the associated symbols. That is, keep track of the max's with indexes, rather than directly. Now assume additionally that the stock symbols are contained in a string array called name:

int max=0;
int smax=1;
{   max=1;
for(int i=2; i<5; ++i)
{    if(stock[i]>stock[max])
     {   smax=max;
     else if(stock[i]>stock[smax])smax=i;
// Now max contains the index of the greatest stock, and
// smax that of the next. So the value of the greatest stock is
// in stock[max], and its name is in name[max]; ditto for smax


Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
...and by the way, why are people continually talking about Bubble Sort? This is the worst sort method of all! It has a complexity of strict n^2. How about Insertion Sort (simplest, and quite fast), or whatever else? Why do they teach Bublle Sort in schools? I know some engineers who are certain this is a good method and the only one out there.

For your problem, as shown by imladris, it can be done in two passes. For small instances of a problem (like, finding the two or three or even four highest values), straightforward methods are more efficient that general ones, like sorting.

I agree with reprice on VJ++... forget Micro$oft products for a while! You'd better use the Sun compiler with a good development tool, such as Visual Cafe.

For a great book on Java: "Java in a Nutshell" is one of my favorites: inexpensive, easy to understand, easy to carry (!).

One last point. Don't forget that in Java, arrays are of fixed length. If you don't know how many items you will have to keep, better use a Vector. BUT with vectors you have to do type casts, which your students won't like t all! Maybe an array with a limit of, say, 100 items.?

skilambAuthor Commented:
Thank you Mr. Imladris for answering my question. I really appreciate it a lot. I also would like to thank everybody else who have provided input to my question. I am currently using  my friend's book "Java: How to Program: An intro to Visual J++ by Deitel and Deitel". He said that this is a pretty good book for beginners. But once I learn the basics, I will take Mr. Dufort's & Mr. Rprice's suggestions into serious consideration. Mr. Dufort, they still emphasize a lot on the bubble sort in many colleges. Actually, that is the only sorting method they taught me in an introductory class to Visual Basic. How does insertion sort work if you don't mind me asking? Once again I thank Mr. Imladris and everybody else for providing input to my problem.  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.