Solved

Need help on formulating a code for a JAVA program

Posted on 1998-05-27
8
209 Views
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.
0
Comment
Question by:skilamb
8 Comments
 

Author Comment

by:skilamb
ID: 1221633
Edited text of question
0
 
LVL 1

Expert Comment

by:froderik
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.
0
 

Author Comment

by:skilamb
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.
0
 
LVL 1

Expert Comment

by:froderik
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:rprice032897
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.
0
 
LVL 16

Accepted Solution

by:
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];
if(smax>max)
{   max=stock[1];
    smax=stock[0];
}
for(int i=2; i<5; ++i)
{   if(stock[i]>max)
    {   smax=max;
         max=stock[i];
    }
    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;
if(stock[smax]>stock[max])
{   max=1;
    smax=0;
}
for(int i=2; i<5; ++i)
{    if(stock[i]>stock[max])
     {   smax=max;
          max=i;
     }
     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

0
 
LVL 1

Expert Comment

by:dufort
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.?

0
 

Author Comment

by:skilamb
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.  
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now