• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tiny programs from Building Java Programs book  RSS feed

 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dear Ranch Dudes,

I am working my way thru this Building Java Programs book, I think I know what they are talking about, they give a code snippet, I try to work it into a complete program and fail miserably each time.

Here is an example of one miserable failure.
It is a snippet designed to give a message after a particular SAT score:



Then I come along and think I can build the rest of the program around this, like this:



but all I get are error messages that I cannot begin to understand or correct because they don't seem to apply exactly to what the problem is (You KNOW this already!)

SatCalc.java:5 error: '.class' expected
rating(int totalSAT);
^

SatCalc.java:5: error: ';' expected
rating(int totalSAT);
^
2 errors

What I want to know is - is there a basic format that when you are given a method, you can EASILY work it into a complete program with main method.
Maybe it's me - need more CAFFEINE!

thanks in advance.
This is absolute torture - I am trying to work in my office at the same time; fielding complaints from angry clients; troubleshooting - etc.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it helps if you use the code tags. when entering your post, higlight your java and click the "Code" button you'd see above, and you get this:





 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That original snippet uses totalSAT as an argument to the rating method, but sets its value to 2200 without ever using whatever it was when rating first got called. No real method would ever work that way, so your book is, I think, misleading you from the start.

Go back to the point where you are calling the rating method from inside your main method and look at what you are passing in the parentheses there (Line 04 in Fred's reformatted version). What's that supposed to do? Your problem is at that point (for now).
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:That original snippet uses totalSAT as an argument to the rating method, but sets its value to 2200 without ever using whatever it was when rating first got called. No real method would ever work that way, so your book is, I think, misleading you from the start.

Go back to the point where you are calling the rating method from inside your main method and look at what you are passing in the parentheses there (Line 04 in Fred's reformatted version). What's that supposed to do? Your problem is at that point (for now).


I am the one who put the value of 2200 in there just to get it to test the various conditions - that's part of the problem I'm having in turning this bare methods from the book into programs.
The book I am using does not really give that much guidance to formatting programs or why things are done one way or another.

Let me see if I can just put the bare code from the book in here with highlighting as you describe:



the above is all they gave me and I am at a loss as to how to turn it into a working program. By setting totalSAT to an initial value of 559, I can get it to throw the exception, but I can't get it to print out the other return statement values.
Thanks so much.
Not getting anywhere fast here.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method from the book is fine. You modification of the method is "fine" too, insofar as it is valid Java code. But it's not very useful code.

However your real problem is not that method at all. Your problem is that you don't know how to call that method. Here's an example of how you would call it:



What you tried in your original code was something like declaring the method, rather than using it (which is what you wanted to do). What you tried wasn't valid Java code, so the compiler told you so. Not very helpfully, I must admit, but at least it pointed at the line of code which was wrong.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...so let's look at this code (which is basically what you have above, with some details hidden):



the main() method runs first. So what exactly is happening on line 4? you are calling the method rating...but...when you CALL a method, you don't declare what the parameter type is. You just pass it in. So really, it should be



But then there is another problem...

What exactly is totalSAT? You have not declared it anywhere. You have not given it a value. the compiler won't have a clue what it is. So you need to declare and initialize it BEFORE you use it:



There are still more issues...but try and work through these suggestions and see what happens...
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please give us more details of the book; maybe we can find a sample chapter and see whether we like it. I have added code tags to your first post.
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Please give us more details of the book; maybe we can find a sample chapter and see whether we like it. I have added code tags to your first post.


The book was recommended to me by a colleague - Building Java Programs: A Back to Basic Approach Third Edition Stuart Reges + Marty Stepp
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome, by the way :>)

Any way, I am at the point where I was at the beginning when I first posted, but backtracked to give history of how I got there.
I can get it to throw the exception, but if I use values other than <600 or >2400, none of the rating messages print out - and yes, I corrected the method to be public static String(rating) instead of int.
That was one of the many things I tried to get it to work.
So I am back to wondering if the method in the book is correct.

Here it is in a form that compiles fine but doesn't work (no rating message is sent as output - does it need a System.out.println in ADDTION to the "return?"):



I try all sorts of things that I hope will "fix" the program, but since I don't feel I have all the information I need . . . .
It is very hard to learn Java just from a book or tutorial because you can't ask anyone what they meant when they write something - either the code or the explanation.
You are right - I DON'T know how to call a method so that the information gets passed and it gives the expected output.
Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are VERY close...

right now, you call the method rating. It returns a String, but you don't save it, so it basically gets thrown away. So, save it:



Now you have the String the method returned, so you can print it from your main method:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jessica Stensrud wrote:I can get it to throw the exception, but if I use values other than <600 or >2400, none of the rating messages print out

Because you don't appear to print them out. All the method does is return a String; that doesn't mean it prints it out.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the book? It only got 3½ stars on the Amazon reviews. Now I know there are people who rubbish books on Amazon because they dislike the subject or the author or have other dishonest reasons, but I didn't see that in the reviews.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't find a sample chapter.
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jessica Stensrud wrote:I can get it to throw the exception, but if I use values other than <600 or >2400, none of the rating messages print out

Because you don't appear to print them out. All the method does is return a String; that doesn't mean it prints it out.

Winston


Hi Winston,
Yes, that is the book on Amazon. I liked it at first since it gave a lot of initial detail, but I feel it assumes a lot as the complexity grows - like that I'll know automagically that I need to save a return value and explicitly System.out.print that saved value if I want to test what I know so far (which doesn't feel like much) by incorporating those new lesson methods into program.
I can't get from point A (method they give) - Oh! I just saw a UFO circling the Moose Head - not just the usual fly! Very classy - nice job! - to point B - incorporate that method with appropriate output into complete program with main method.
Do YOU have a book or tutorial that you recommend that doesn't leave Greenhorns endlessly guessing how to alter everything they can think of (taking hours) because they don't know what they don't know???
Thanks so much, all of you - glad you're all there! Very helpful - not like my poor beleaguered colleagues who, in trying to be helpful, just type in their corrections without telling me what they're doing or why. Thanks for the info and - most importantly - the WHY.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best book I have come across is Core Java II by Horstmann and Cornell. That is the good news. The bad news is, Horstman and Cornell is completely unsuitable for beginners. A slightly old book now, but still good: Head First Java by Kathy Sierra and Bert Bates. Make sure to get the 2nd edition. You can probably pick up second hand copies at a good price.

Don't let people simply write corrections. Get them to explain why. Otherwise you will learn less than nothing. You learn the most by writing your own corrections.

General design principle, I think called single responsibility. It does one thing. An object represent one thing, and does all that one thing does. Don't make it do something from inside another object and don't make other objects do its work. Methods: they should do one thing. As a general rule of thumb, the smaller a method is, the better. Your method does two things: it returns a String and prints it out. It can be useful to print things while a method is running, so you can see how it works (or even how it goes wrong), but if you declare a method as returning a String, then its business is to create and return that String, not to show it. A method declared as returning something must return that something (one thing only). When that something is returned, you can do several things with it:
  • Display it (with a print... method)
  • Store it, maybe as what a variable points to
  • Manipulate it with another formula or method.
  • Ignore it ... in which case it disappears into a sort of cyber‑limbo, never to be seen again.
  • From what Fred writes (thank you Fred for all the effort you are putting into this) I think he thinks you are ignoring that return value.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jessica Stensrud wrote: . . . a UFO . . . not just the usual fly! . . .
    Aaaaaaaaaaaaaaaaaargh! They have landed



    Or maybe it is simply this.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!