Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help on formulating a code for a JAVA program

Posted on 1998-05-27
8
Medium Priority
?
215 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
[X]
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
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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
 
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 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

688 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