Matthew Brown wrote:You're still overloading rather than overriding, though. Try adding an @Override annotation and that will become clear. The signature needs to be equals(Object), not equals(Pixel). You should be able to compare a Pixel to any object (returning false if it isn't a Pixel). It also needs to cope with being compared to null (again, returning false).
The class overrides equals(Object o) in java.lang.Object to compare the instance values of two Pixel objects and return true if they are equal, and false otherwise.
Winston Gutkowski wrote:
Looks pretty good to me; 'fraid I haven't check your UML diagram tonight because it's late, but if it's up with the rest of your stuff, I'm sure it'll be fine (and if it's not, you'll be told - and that's how you learn).
Other observations: Don't forget that one of the major signs of a good programmer is how "readable" your code is.
1. (in the words of Einstein) make everything as simple as possible; but no simpler.
2. Stick to standard coding practises - most importantly: Classes and interfaces start with a capital letter; methods and fields names with lowercase.
3. Good documentation keeps your program from the garbage bin for longer.
HIH
Winston
Unit 1 Activity 1
open NetBeans IDE and start applying the following activity
Part 1
You are required to develop a programmer-defined class that models the Robot object. Develop a class Robot to the following specifications. No error checking is required in the constructor or methods. You may assume that they will only be invoked using sensible values.
1. The class has two private instance variables, x and y; both are of the type int.
2. The class has a no-argument constructor that sets the values of x and y to 1.
3. The class has two-argument constructor that sets the values of x and y, to given values xPos, yPos.
4. The class has the public getter methods getX() & getY() for each of the instance variables, the getter methods return the current value of x and y.
5. The class has the public setter methods setX() & setY() that set the value of x and y with the new values xPos, yPos
The class has the following public methods
a- moveNorth() that increases the value of y by 1
b- moveSouth() that decreases the value of y by 1
c- moveEast() that increases the value of x by 1
d- moveWest() that decreases the value of x by 1
e- moveTo() which changes the position of x and y by given values xPos and yPos
f- The public method toString() that returns a string representation of the record, giving the x, y as follows:
Robot1, x= 5, y= 2
Part 2
Write a class TestRobot_yourName to test the above class. It should contain the following specifications:
1. Create an object of the type Robot, robot1, which is initialized to the following values: x=5 , y=2 , using two-argument constructor.
2. Create an object of the type Robot, robot2, using no-argument constructor then initialize its properties using appropriate setter methods as the following values: x=3, y=10
3. Increase the value of x of robot1 tree times
4. Decrease the value of y of robot1 2 times
5. Invoke the method toString() on both robot 1 and robot 2 to display their values using Standard Output.
6. Provide a screen shoot for the output
Part 3
You are required to develop a programmer-defined class that models the MagicRobot object which is a special kind of Robot class. It inherits all Robot class features. Develop a class MagicRobot to the following specifications. No error checking is required in the constructor or methods. You may assume that they will only be invoked using sensible values.
1. The class has one private instance Boolean variable visible.
2. The class has the following methods
a- makeVisible () that alter the value of visible to true
b makeInisible () that alter the value of visible to false
c- isVisible () that returns the current state of variable visible
3. The class has a no-argument constructor that sets the value of visible to false using - makeInvisible() method
Part 4
Add the following part to class TestRobot_yourName to test the above class. It should contain the following specifications:
1. Create an object gandalf of the type MagicRobot,
2. Invoke the method toString() on gandalf to display its values using Standard Output.
3. Check the response of Gandalf to the following methods moveNorth(), moveSouth(), moveEast(), moveWest(), moveTo(), does it response, Explain Why?
4. Now apply this sentence robot1.makeInvisible(); does it work with you, Explain Why
5. Draw the UML diagram that shows the relation of the two classes Robot and MagicRobot
All best of luck,
Course coordinator
3. Check the response of Gandalf to the following methods moveNorth(), moveSouth(), moveEast(), moveWest(), moveTo(), does it response, Explain Why?
4. Now apply this sentence robot1.makeInvisible(); does it work with you, Explain Why?
5. Draw the UML diagram that shows the relation of the two classes Robot and MagicRobot
Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.
Jeff Verdegan wrote:
Campbell Ritchie wrote:Maybe we should bring this thread back to its official topic.
I think that ball is in Sarah's court now. We haven't heard form her in a while, but as far as I can tell, all her questions have been answered.
Jeff Verdegan wrote:
Sarah Mitchell wrote:
4- And here, I was able to modify your code to get you what you want :
Please don't do that. This site is NotACodeMill.(⇐click) Doing somebody's work for him does not help him to learn. As it says on the topics page: "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."
Collin Sampson wrote:Thanks for the help. I both wrote it out and looked at the code provided. Makes sense now.
initialization = size -1
boolean expression = numStars >= 0
change = numStars-- (one less star on each line)
Campbell Ritchie wrote:Welcome to the Ranch
Careful with books; some books are very good, others not so good. We have some book reviews which we hope will help you.
fred rosenberger wrote:I think of calling a method as asking a question. "hey...What time is it?" or "What is the sine of 27.43 degrees"? or "what is the result set when you query THIS database with THAT sql query?"
Note that each and every question can only return one answer. I can't say "What time is it and what is Fred's dog's name?" and expect to get back "8:30 a.m. and Mamie". One question, one answer.
Now, that answer can be a rather complicated single thing...the aforementioned result set may contain hundreds or thousands of records, but they are all contained in one object (which may then have methods for accessing those records).
The reason you write methods rather than just 'in-lining' the calculation everywhere is that the methods can actually be rather complicated. You may have a method that connects to the database, passes the query, gets the results, formats them, and a bunch of other things. You don't want to have the same basic 200 lines of code written over and over again. Plus, if you write it in a single method, you can test that method extensively. Once you know it works, you KNOW it works, and you don't have to worry about it anymore. If you re-wrote they method by hand a bunch of times, you increase the changes of introducing an error somewhere.
Winston Gutkowski wrote:
Sarah Mitchell wrote:Ok, so if I made such a code, should I assign the formula's outcome to the variable 'result' by using the equal sign '=' ? I mean something like this :
Pretty much; although getting the answer may take more than just one line.
and in this case should we also use return as well ?
Yes. Any method that is defined to return a value other than void must contain at least one return statement.
but then how will it know in which variable to store the outcome if I have more than one variable ?
Because you tell it which one to return. Despite all the advances of the last 50 years, computers are still basically stupid - they do exactly what they're told.
HIH
Winston
Zeeshan Sheikh wrote:Daniel hasn't posted all the classes, as per his above post "tester class(not included)".
Jeff Verdegan wrote:Also, did you study these, or their equivalents in some other tutorial or book? If not, you should, and if you did, what parts didn't you understand?
http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
As for this:why use the command return when I can just use ( variable = value or formula )
Two points:
1) If you're talking inside the method called, then sometimes its result is to just set the value of a member variable. However that's a different scenario. When you do that, you're changing the state of an object, and you want that state to persist as long as that object exists, unless somebody comes along and changes it again. For instance, in a Student object, I might store a currentGPA value. That stays with the student until something comes along to change it. Returning a value, on the other hand, a way of providing the result of some operation or querying the state of the object without actually changing it. For instance, a Person object might have a private Date birthDate member variable. If I want to know that person's age in seconds at a particular instant in time, I might call person.getAgeInSeconds(some time). The method will then use the birthDate and the given time to calculate the age. It wouldn't make sense to update the state of the Person object for that, as a person's age in seconds as of some arbitrary instant is not an attribute of that person.
2) When you do variable = forumula, if that "formula" is, for instance, x = Math.sin(y), we couldn't do that unless the sin() method returned a value.