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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

615 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