Sarah Mitchell

Greenhorn
+ Follow
since Mar 04, 2012
Sarah likes ...
Eclipse IDE Chrome Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
11
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sarah Mitchell

Uffff, this toke a lot of my time, although your inputs were crystal clear but I just couldn't see it at that time =(
Plus I think this is the first time I actually do ( object casting ) !

Anyways, here is my final code, it's working just fine. Thank God !






Thank you guys, I'm really glad that all of you were nice enough to help me out. Appreciated =)






Sarah,
11 years ago

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).



Oops o_O
I thought it's supposed to be like this :S

So, now when I changed the argument to (Object o) I got an error for all the instances I have (x, y and myColor) !!!
How can I resolve this ??

I don't know how to use the @Override thingy =(
11 years ago
Thank you everybody, I got it working now because of your help =)
This is how it looks now if you're interested :

11 years ago
Hello CR,

How is everybody today ?

Guys, I'm having a little bit of a challenge overriding the .equal method available in java.lang.object, it's driving me crazy and I can't do it, please check my work and advise me =)

I have 2 classes and an Interface as following :







What's required from me at this point is :

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.



Also, in the same so called equal method, when I tried to simply write the code like this :


I got an error saying that the method must return a boolean value !!
I had to declare a boolean variable and use it, and the error was still appearing until I added the else statement, so why it didn't just accept the return true; statement ?

I hope this is not too much








Thanks,
Sarah
11 years ago

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



Hello Win,

First, thanks for your replay and support as usual. Second, I tried to organize and write my code in a very clear way, but if there is any part that you feel is not very clear please let me know so I can re-write it in a better way in the future. And about the standards coding practices while naming classes and methods, I'm ok in this I guess and I used it in my code as you can see. About the documentation I didn't understand, do you mean notes inside the code to explain the usage of different areas ? should I add a note to each and every method and other functions in the code ? or just in case I need to explain something and stuff ?




Sarah,
12 years ago
Hello nice people,

I just finished my very first assignment in Java, and I was wondering if you guys could please take a look at my work and let me know if there are any observations ..
First, these are the requirements :

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



And these are my codes/answers :

Part1 ( Robot class ) :



Part2 ( TestRobot_yourName class ) :



Part3 ( MagicRobot class ) :



Part4 ( Questions ) :

3. Check the response of Gandalf to the following methods moveNorth(), moveSouth(), moveEast(), moveWest(), moveTo(), does it response, Explain Why?


Does work, Because the subclass MagicRobot inherited all the protocols and messages of the super class Robot.

4. Now apply this sentence robot1.makeInvisible(); does it work with you, Explain Why?


Doesn't work, Because the objects created from a super class doesn't have all the methods available in the subclasses. In this case the object robot1 can't invoke the method makeInvisible() which in only available in the subclass MagicRobot().

5. Draw the UML diagram that shows the relation of the two classes Robot and MagicRobot


http://i39.tinypic.com/21b2kbc.png



I am really sorry, I know this is too much, but I'm too excited for being able to write code and answer questions about Java so I thought I might share some of this excitement ^_^
12 years ago
Campbell, Winston & Jeff,

From the bottom of my heart THANK YOU. Being humbled, patient, dealing with positive attitude and feeling obligated to help, this is what makes projects like this really valuable. And as individuals, I don't know what to say about you guys, you are really great and very nice people. Again thank you, and I wish I can repay you sometime.
12 years ago
Well, I'm not expert but I know for sure that you can use Boolean as a variable or object, but you cannot use boolean since it's a reserved word for the variable class of primitive data type !

and since you asked for an example code, I made this for you, it's the simplest thing I could come up with :



as you can see, I used the class boolean to define a variable named Boolean, and in the same way you could used Boolean as an object( I think, lol).

I hope this helps in clearing thins up a little bit .





Good Luck
12 years ago

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.



I'm still here, I was reading everything my eyes catch in the last tow days, but honestly this is still complicated, when I try to link everything together I get totally lost !
For example I was taking a look at some code :



and I got confused by the following :



I mean what is this public Account() ? it's not a class header of course, it's not variables deceleration, it's not a method(at least I don't think so), so what is it ?
As far as I know it's only about assigning some values to the variables, but in this case why did they use ' this. ' ? what is that exactly ? why did we have to use it in this case? why didn't they just say holder = ""; or balance = 0.0; ? and what other cases it must be used in ?

Winston, fred, Jeff and Campbell. Thank you all for helping me and I'm so sorry if I'm asking a lot of questions that you guys know for sure it will be answered after reading some tutorials, but the fact is after taking a look at some websites including Oracle website, every tutorial and every website has it's own approach, and reading from different resources is confusing, so when I asked my tutor he advised us to stick to the class books at least for now. We submitted a request to the course coordinator to see if there is any chance to change the current books with something a little bit more organized and better explained but since she didn't replay until now I'm not very optimistic about it, so I'm trying to read from the books and that's where I need some help from the virtual world. It's been only 3 weeks for me with Java!
12 years ago

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."



Oops. I'm sorry I thought I'm helping !
Won't happen in the future, promise >.<


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)



Yes, but this only goes for the second half of the triangle, because if we didn't add the -1 there will be a duplication for the row in the middle, and I guess we don't want this. Right ?
12 years ago
1- For such a program there must be a condition for a minimum number since we won't be able to draw a triangle if the user's input was 1. And also you should enforce the range from 1(or 2) - 50 by code so the user cannot get out of it. ( Check point 5 bellow for more details ).

2- I suggest using a JOptionPane to ask for the user's input, it would look a lot more professional .

3- For the loops you don't need anymore variables than just x, y.

4- And here, I was able to modify your code to get you what you want :



My advice would be to look at the code only, then try to re-create it yourself to get the maximum benefit and learn, otherwise you won't learn so much.

5- I made another version of the code one with if statement to ensure that the users enters a number >= 2 && <= 50. Just in case you're interested .




I hope I helped. Good Luck =)
12 years ago

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.



Unfortunately, the book is not really our choice since it's mandatory for our class. But thank you so much for the link, it's gonna com in handy when I plan to get a 'real' book, lol.


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.



Got your point, thanks for explaining .


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



OMG, I totally forgot about what I learned in the very beginning. the void methods doesn't return any value but other types does, and that's where we need to use arguments !
uffff, I kinda hate myself for forgetting this. If it wasn't for you I would be still lost about this a little bit. Again thank you so much Winston.
12 years ago

Zeeshan Sheikh wrote:Daniel hasn't posted all the classes, as per his above post "tester class(not included)".


Oh I see, thanks for the clarification.

I managed to come up with this, it might help to give some idea :



Good Luck
12 years ago
Ok I have a question regarding this !
Why doesn't this class have a main() method ? will it ever work like this ?
12 years ago

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.



Well, they actually give us like 12 small books for this course, they had this idea that by dividing it to small many books it's better than just giving us one big book, but actually it's more confusing now. So I got some e-books and I started reading here and there, I learned some very basic stuff but whenever I have a question I can't ask it in class, the proff always goes like "Don't rush into things, we're still not quite there yet", so it's really no use.

Thanks for the links tho, it seems like a very good and easy tutorial.
12 years ago