Need help on formulating a code for a JAVA program

Posted on 1998-05-27
Last Modified: 2010-03-30
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.
Question by:skilamb

Author Comment

ID: 1221633
Edited text of question

Expert Comment

ID: 1221634
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.

Author Comment

ID: 1221635
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1221636
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.

Expert Comment

ID: 1221637
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.
LVL 16

Accepted Solution

imladris earned 200 total points
ID: 1221638
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


Expert Comment

ID: 1221639
...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.?


Author Comment

ID: 1221640
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.  

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question