• Post Reply Bookmark Topic Watch Topic
  • New Topic

My very first assignment in Java, did I do ok ?  RSS feed

 
Sarah Mitchell
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ^_^
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarah Mitchell wrote: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 ..

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
 
Sarah Mitchell
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!