• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to write a program that gives greatest number and least number  RSS feed

 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you calling that an applet? I can see no sign of applets there. Why have you written a run() method? Does ConsoleProgram implement Runnable? You appear not to be using any features from ConsoleProgram; why not write the program the conventional way with a main() method.
Your code is confusing with two local variables called num and numb.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am supposed to use the ConsoleProgram but i found a different algorithm to the problem so i did not use it and i called it an applet because that was the only time when i ran the code on an applet and something appeared and my i did not spend much thought on  variables names.  
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you supposed to use console program? I do not think it is helping you at all. Please don't say applet when you are not using an applet.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie if you suggest i should forget about stanford can you at least tell me where i can learn more about java than on Stanford because i have tried meany different ways to learn java but  non of them provided me the amount of knowledge that Stanford have given me. what do i do?
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh and i do not know why i am supposed to use ConsoleProgram but Stafford's assignments required it for some reason.
 
Carey Brown
Saloon Keeper
Posts: 2439
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will fail for

You are very close though.
 
Knute Snortum
Sheriff
Posts: 3438
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code seems okay in general (there are some problems, but not major ones).  Did you have a question about the code?
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie can you tell me where i can learn java more than Stanford since you suggested i should not use stanford.
 
Sergiu Dobozi
Ranch Hand
Posts: 100
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Alphones wrote:Campbell Ritchie can you tell me where i can learn java more than Stanford since you suggested i should not use stanford.


I think more modern approaches can be found on this website and in the books that this website recommends:
https://www.tutorialspoint.com/java/java_useful_resources.htm
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you are not doing Stanford's assignments.

Try thisThat code shou‍ld compile and run on any Java® installation. Eclipse shou‍ld have no difficulty with it. Once you have got it running, delete line 11 and you code in the run() method. I suggest you do this:-Now you will get red marks on Eclipse because that code won't compile. Click each red mark and you get a dropdown list of options to correct it, including create method.... Click that. Now you need to fill in those three methods. You will probably need smallest and largest fields.
Now, you will have a more object‑oriented result than you had previously. You shou‍ld be designing the code step by step; having those three methods will make it easier to go step by step. If you use Eclipse it will organise the imports for you, as I said earlier.
ScreenshotEclipse.jpg
[Thumbnail for ScreenshotEclipse.jpg]
Eclipse showing option to create printResults method
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi i appreciate the help but i cant seem to learn java from reading i learn most from seeing, hearing and doing. if you or any other person out there can inform me about a place that have video tutorials and assignments for each tutorial or couple tutorials.Tat would be appreciated
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now what ...

 
Sergiu Dobozi
Ranch Hand
Posts: 100
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Alphones wrote:Now what ...


Now you create the smallest and largest fields and write the logic behind each step. It's going to be a fun ride, don't worry.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you create the smallest and largest fields and write the logic behind each step. It's going to be a fun ride, don't worry.


what do you mean by Smallest and larges fields and write the logic behind each step
could you show me an example of that.
 
Sergiu Dobozi
Ranch Hand
Posts: 100
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Alphones wrote:
Now you create the smallest and largest fields and write the logic behind each step. It's going to be a fun ride, don't worry.


what do you mean by Smallest and larges fields and write the logic behind each step
could you show me an example of that.


I mean you should define two fields named Smallest and Largest.
The logic behind steps means the code inside each method. You should basically replace the  // TODO Auto-generated method stub with your own code in each method. For example in printResults() you will write the code that prints out the results generated in the previous methods.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry i am a real beginner to java so when you say "you will write the code that prints out the results generated in the previous methods." i have no idea how to do that i just started java a few weeks ago i consider it a miracle that when somebody talks about a scanner or a if statement or a for loop or while loop i know half of what they are talking about.   
 
Sergiu Dobozi
Ranch Hand
Posts: 100
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Alphones wrote:i am sorry i am a real beginner to java so when you say "you will write the code that prints out the results generated in the previous methods." i have no idea how to do that i just started java a few weeks ago i consider it a miracle that when somebody talks about a scanner or a if statement or a for loop or while loop i know half of what they are talking about.   


Okay so then your first step before you even consider solving the problem is understanding how to write methods, what they are...same goes for basic stuff: learn about if statements and loops. Then you can come back with real knowledge to solve this problem and post your solution in this thread.
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is good
Change the how many numbers method to read how many from the keyboard. It will print, “How many numbers do you want?” and you enter 7 and it will return 7. You can use the same code you had earlier with a Scanner or you can use this Stanford class, which appears to be much more useful than the Stanford code you have been using.
Add the following line to the run() method:-
System.out.printf("You are going to use %d numbers.%n", count);
* * * * * * * * *
Now remove that last print line and get the printResults method working. As I said, you will need two fields, and until you have the middle method working, it will simply print 0, 0. Or -2147483648, 2147483647. But don't worry about that. You will sort that out with the middle method.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok than can you tell me where i can learn about basics(beside orcal.)
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:. . . Now you create the smallest and largest fields and write the logic behind each step.
Agree Exactly what he shou‍ld do.
It's going to be a fun ride, don't worry.
It will also be much quicker.
 
Henry Wong
author
Sheriff
Posts: 22646
112
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think the problem here is that you are trying to solve a Java problem -- and it isn't. It is a logic problem. You need to understand the logic before you can put it to code.

In other words, if I write down a bunch of numbers on a piece of paper, can you tell me what is the greatest number and smallest number?  Can you describe how were you able to obtain the greatest number and smallest number? And finally, can you describe the procedure to a five year old child, to find the greatest number and smallest number?

I think, once you are able to accomplish that on paper, should you try convert that to pseudo code, and ultimately to Java. Otherwise, you are just putting random code snippets together, with the hope that the end result works.

Henry
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Alphones wrote:ok than can you tell me where i can learn about basics(beside orcal.)
Where's orcal?

You have been given a link to the Java™ Tutorials, which show how to write a method. You shou‍ld be able to find out about methods in any Java® book. But Eclipse has already written much of the method for you. I hope you used my naughty trick with the red marks.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay but i want a place where i can learn java reliably for free where would that be for example how did you(Henry Wong)learn how to code in java.
 
Knute Snortum
Sheriff
Posts: 3438
85
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are a visual learner, I'd try Googling beginning java video tutorials and picking one.  This series looks promising.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i did use your trick and it helped thanks Campbell Ritchie.
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and how have you got on with the first and last methods?
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not good sense i am not sure what each method is supposed to do and how to write that in code. 
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One method asks how many numbers you are going to enter, for example 7. The other prints the values of largest and smallest.

Edit: You probably have code to do that in the old version of your program.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i will try that
 
John Joe
Ranch Hand
Posts: 200
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I'm out of the topic . How can we edit our post as I saw some post can be edited by the user

This message was edited 1 time. Last update was at 7 minutes ago by xxx
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way John Joe i have seen your post earlier and wanted to ask one thing how much did the book help vs. the one semester at the university.
 
John Joe
Ranch Hand
Posts: 200
2
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny Alphones wrote:by the way John Joe i have seen your post earlier and wanted to ask one thing how much did the book help vs. the one semester at the university.

I think it would be better if we discuss at here or private message. 
 
Junilu Lacar
Sheriff
Posts: 10678
140
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you supposed to use console program? I do not think it is helping you at all. Please don't say applet when you are not using an applet.

As I pointed out in a previous reply, OP is extending this https://cs.stanford.edu/people/eroberts/jtf/javadoc/student/acm/program/ConsoleProgram.html which is, in fact, a subclass of Applet.
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I found that out later. Although the lectures are good, I don't like that console program class. That is another reason: the number of interfaces it implements and its superclass.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie, I tried following your instructions as good as i could and it lead me to this.
What do i do next?

 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 27‑33 shou‍ld all be one method. The return statement goes last. If you put that lot together into one method, you shou‍ld be all right. You will have to return something other than 0.
 
Campbell Ritchie
Sheriff
Posts: 53532
126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Push ctrl‑A ctrl‑I on Eclipse to get the indentation right. Note where lines 31 and 32 are. Actually, you might change the Scanner to a field, but it will need a different name.
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the variable count that is in line 11 wont reach the method ReadHowManyNumbers am i right
 
Danny Alphones
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would i change the scanner to a field
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!