aspose file tools*
The moose likes Beginning Java and the fly likes Java noob needs help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java noob needs help" Watch "Java noob needs help" New topic
Author

Java noob needs help

Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
In my java assignment my job is to make a Horse class. I have to give the horse characteristics such as behavior, size, name, etc. and I'm just plain stuck. Here is where I am so far.

I am using Jgrasp to compile and run.

Here is my code:



When I compile I get these errors


Horse.java:29: ';' expected
public void drink()
^
Horse.java:31: class, interface, or enum expected
System.out.println( name + "is drinking");
^
Horse.java:32: class, interface, or enum expected
{
^
Horse.java:34: class, interface, or enum expected
public void sleep()
^
Horse.java:37: class, interface, or enum expected
}
^
Horse.java:39: class, interface, or enum expected
public void walk()
^
Horse.java:42: class, interface, or enum expected
}
^
Horse.java:44: class, interface, or enum expected
public void trot()
^
Horse.java:47: class, interface, or enum expected
}
^
Horse.java:49: class, interface, or enum expected
public void canter()
^
Horse.java:52: class, interface, or enum expected
}


I just need to be pointed in the right direction as to what I'm doing wrong.


I am for the time being trying to get it to run and say 'steve' for ex. is walking, steve is trotting, steve is eating. Very simple but I know i'm messing up some where and I think it is with the strings.

I apologize if some of the code is confusing I have to eventually make it private instead of public and then do a HorseTest class and try to access the private fields through there and run it.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29247
    
139


Try removing this code. Methods/private fields get declared in a class not a main method. A main method is for launching your program and will probably create a Horse object.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Jeanne Boyarsky wrote:
Try removing this code. Methods/private fields get declared in a class not a main method. A main method is for launching your program and will probably create a Horse object.


Hi Jeanne,

I removed that and now my code looks like this



I am still getting the error
Horse.java:47: class, interface, or enum expected


For each method (public void ' ' etc.)

I am not entirely sure why

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

First fix line 17, which I'm not sure why it's not complaining about: if "steve" is supposed to be a string, it should be in quotes. It *looks* like you mean to be passing the name in the constructor, however, so I'm not sure where that spurious steve is coming from.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

And please, please get off on the right foot by either: naming variables so that they don't require comments, or having the comments be meaningful. Don't store a horse's age in its size, nor breed in its color. Giving things meaningful names is critical to reasoning about code!
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Try to reverse the bracket at line 30. It should be { instead of }.

I would suggest you to use any modern IDE (Eclispe, NetBeans, both are free) so that they show you compile errors on spot.

And also try to get a good beginners book about Java that would explain you concepts of class, method, method body etc., and go over it before jumping immediately into the assignment. I started my Java with Core Java book, and it did not go very smooth I have to say, I am not sure whether that book is indeed for beginners. Try Thinking in Java or Head First Java.


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Look carefully at the braces in the drink() method. Pay attention to the difference between '{' and '}'.
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Ahh yes the brackets in the drink() method screwed me over pretty bad I fixed it. Thanks a lot.

Oh and david sorry the //comments were already there as a starting point from our instructor I just blatantly wrote code in front of them without matching them my fault.


The only error I get now was because I put an 's' in the size = where it should be a number.

When I compile it it works but now when I run the program it says this

java.lang.NoSuchMethodError: main
Exception in thread "main"


I'm guessing it's just because I don't have main class to test it. I still don't get why it won't say 'name' is eating, drinking etc.



Here is the fixed code



thank you so much for the help so far
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

Sunny Sarow wrote:Ahh yes the brackets in the drink() method screwed me over pretty bad I fixed it. Thanks a lot.

Oh and david sorry the //comments were already there as a starting point from our instructor I just blatantly wrote code in front of them without matching them my fault.


The only error I get now was because I put an 's' in the size = where it should be a number.

When I compile it it works but now when I run the program it says this

java.lang.NoSuchMethodError: main
Exception in thread "main"


I'm guessing it's just because I don't have main class to test it. I still don't get why it won't say 'name' is eating, drinking etc.






You cannot run the Horse class now because you do not have a main method(! method, not class) there. The stub of main methods is


Also remember that because main method is static, it cannot access instance methods of the class, something that they try to actually trick you with in SCJP exam This means that you cannot call eat() drink() etc. methods from withing your main method, but you have to first create an instance of Horse, and then call these methods on it.



Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Elchin Asgarli wrote:
Sunny Sarow wrote:Ahh yes the brackets in the drink() method screwed me over pretty bad I fixed it. Thanks a lot.

Oh and david sorry the //comments were already there as a starting point from our instructor I just blatantly wrote code in front of them without matching them my fault.


The only error I get now was because I put an 's' in the size = where it should be a number.

When I compile it it works but now when I run the program it says this

java.lang.NoSuchMethodError: main
Exception in thread "main"


I'm guessing it's just because I don't have main class to test it. I still don't get why it won't say 'name' is eating, drinking etc.






You cannot run the Horse class now because you do not have a main method(! method, not class) there. The stub of main methods is


Also remember that because main method is static, it cannot access instance methods of the class, something that they try to actually trick you with in SCJP exam This means that you cannot call eat() drink() etc. methods from withing your main method, but you have to first create an instance of Horse, and then call these methods on it.






My instructor has forced us to make the Horse class private so it cannot be accessed by anyone else. This is my final code below for the Horse class.



Now I am making a second .java file called HorseTest which will be my main and here is my start so far


The reason being so is my instructor wants me to access the private fields through the main java file using accessors so my guess is there won't be any 'name' is eating/walking etc. until I finish this

thank you

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The accessors should exist in the Horse class, not HorseTest.
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
okay so this is my Horse class so far



it compiles but won't run because I dont have a main

and I'm working on my main HorseTest here




In the horse class if I need to make more strings public like 'eat' for example.

Do I add 'string e' along with the other strings (string n, int s, string c, string b) and then add 'eat = e' under that? I've tried but it says cannot find symbol.


Also for the HorseTest im trying to make 2 horses, one h1 the other h2 but I am stuck.

the code I wrote down from class says something like

Horse h1 = new Horse("ed", 100, "grey"); for example but it gives me cannot find symbol.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How are you compiling it? Are you providing a classpath? Have you read any of the documentation either from your class or on the web about how to do these things?
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
I am using Jgrasp I have both java files saved in the same directory and I've been told it should work from there.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'll have to post the actual source (minus the extraneous blank lines, please--make it as easy as possible for us to help you) and the exact error and line number.
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Sorry ok I will try to post my problem as clearly as possible.

I am typing up my main and currently this is it.



I am trying to create a new Horse object called h1 and trying to pass through the name, size and color. When I try to compile it says java:6 cannot find symbol. It is pointing at



I am not sure what I have typed wrong in the above line. I am creating a new Horse called h1.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's no constructor that takes a String, int, String.
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Does the constructor need to me in the main? I have it in the Horse one not HorseTest . . that may be the problem?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, the problem is that in your Horse class you've defined a constructor that takes (String, int, String, String) and in your HorseTest.main you're trying to call one that takes (String, int, String), which doesn't exist.

Do you have a Java book?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(And what are all those extra strings for in the Horse class?)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Sunny Sarow wrote:Does the constructor need to me in the main? I have it in the Horse one not HorseTest . . that may be the problem?


No. Again, there's no Horse constructor that takes a String, int, String for parameters like you're trying to call it. The Horse constructor that you have in your code above takes String, int, String, String for parameters in that order:

Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
You are the man, you were right I went back and changed it and added the behavor I missed.


The extra strings I am still stuck on. You saw in the original constructor I had String n for name, int for the size etc. I need to get all those strings into that constructor.

Can I just add String e then eat = e below? I will copy the constructor so you dont have to scroll back up.



After String b, I was wondering if I can continue adding String e etc. for the rest of the strings then below that type eat = e etc.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why not just try it?

But... I'm not sure what they're for. You have methods called things like "eat": what's the string for?
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Here let me post my complete code. As of right now everything works and is good to go. I think the purpose of having those extra strings was to call it through the main java file 'HorseTest' because they were private.

Horse:



HorseTest



and the end result is this


Ed is eating
Steve is eating
Ed is drinking
Steve is drinking
Ed is sleeping
Steve is sleeping
Ed is walking
Steve is walking
Ed is trotting
Steve is trotting
Ed is cantering
Steve is cantering



I think i did it
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But those strings aren't used, and don't do anything. I'm talking about the String properties of the Horse class, like "eat" and "trot" etc.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Oh my god!
Sunny, if you have just started learning Java, please start by reading a book first...try the "Head First Java".
Also, try to learn thinking in Java first before you even begin to write the code. OOP is all about thinking first, the code semantics are really secondary.

"To study any language, there are 3 things you need to master:
i) How the language is structured(grammar)
ii) How to name things you want to talk about(vocabulary)
iii) The customary and effective way to say everyday things(usage)" -- Joshua Bloch

So really first "learn" to think in the language, and its usage will come naturally as you use it more often.


P.S.: I am not sure if quoting Joshua Bloch is violation of any terms, if it is, its completely unintentional. Please let me know I will edit it.

-Ninad


Don't walk as if you rule the world, walk as if you don't care who rules it...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
. . . and nobody has noticed yet that your thread title is so vague, or does not consist entirely of real words?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java noob needs help
 
Similar Threads
preparing for SCJP 5
Polymorphism
OverRiding a private method
Confused in function overriding
Unable to understand reason for ClassCastException