|
-
Mar 11th, 2010, 10:34 AM
#5
Fanatic Member
Re: Getting errors in this program...any ideas?
 Originally Posted by CarlMartin10
The more corrections I make, the more errors I get. I think I need to start this over from scratch.
I don't think so. From the look of things, the only thing that is currently holding you back is the spelling mistakes of your code.
Now that isn't the worst thing in the world - at least you seem to be understanding some concepts, which is the crucial part. As long as you realise that Java is heavily reliant on Object Oriented Programming approach i.e. every class is supposed to represent an object of some kind - the variables are the properties of that object, and the methods are the ways you can interact with an object.
To give you a small example, let's look at a ball as a possible object we can put into Java. A ball has a size and colour (for simplicity sakes), and in our example, we can interact with this object by picking it up, and dropping it.
In this example, the Java code could possibly look like this (there is strictly no right answer, as it depends completely on the situation):
java Code:
public class Ball
{
private int size;
private String colour;
public Ball()
{
size = 1;
colour = "red";
}
public Ball(int size, String colour)
{
this.size = size;
this.colour = colour;
}
public void pickUpBall()
{
//some code here that represents this Ball object being picked up
}
public void dropBall()
{
//some code here that represents this Ball object being picked up
}
}
That is how I'd represent it in Java, at the very least. Also, I have two constructors; the whole idea is that if this Ball object is called by another class, they can either mention the ball size and colour (using the constructor Ball(int size, String colour)), or I can set a default size (using the constructor Ball()). I can force whoever calls my object in their code to specify a size and colour by getting rid of the Ball() constructor or just force them to take the default one by deleting the Ball(int size, String colour) constructor.
I at least hope you understand this stuff, as for me, it really helps if I understand the core concepts behind the code. If you already knew this and I came across patronizing, then I'm very sorry for that. If you didn't know, and you want to try learning more, then try making a simple Java class that runs via the console and interacts with the Ball class code I've given (you will obviously need to add more to the class to make it interactive).
Good luck with learning Java though! It's nice to see someone actually actively learning a language by themselves.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|