M251 - Meeting 5 - Activity - Solution

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

M251 Meeting 5

Activity 1 – Robot Class


Part 1
You are required to develop a programmer-defined class that models the Robot class.
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. Create a project with the name M251_M5
2. Create a class Robot.
3. The class has two private instance variables, x and y; both are of the type integer.
4. The class has a zero-argument constructor that sets the values of x and y to
zero.
5. The class has two-argument constructor that sets the values of x and y, to given
values.
6. The class has public getter and a public setter methods for each of the instance
variables.
7. The class has a method moveNorth() that increments instance variable y by 1.
8. The class has a method moveSouth() that decrements instance variable y by 1.
9. The class has a method moveEast() that increments instance variable x by 1.
10. The class has a method moveWest() that decrements instance variable x by 1.
11. The class has a method moveTo(x,y) that sets the values of x and y, to given
values.
12. The class overrides the Object’s toString() method in order to return a string
representation of a item object similar to format given below:
X: 5 , Y: 2

Part 2
Write a class TestRobot_yourName to test the above class. It should contain the
following specifications:
1. Write the static main method.
2. Create an object of the type Robot, tetoo, which is initialized to the
following values: x=5, y=2, using two-argument constructor.
3. Create an object of the type Robot, detoo, using zero-argument constructor
then initialize its properties using appropriate setter methods as the following
values: x=3, y=10.
4. Invoke the method toString() on both tetoo and detoo to display their values using
Standard Output.
5. Move tetoo 3 steps east
6. Move detoo 2 steps south
7. Invoke the method toString() on both tetoo and detoo to display their values using
Standard Output
8. Write: what are the members of class of Robot (its attributes & methods), Explain
your answer.
M251 Meeting 5- Activity 2 – MagicRobot Class
Part 3
You are required to develop a programmer-defined class that models the Robot class.
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. In project M251_M5 create a class MagicRobot.
2. The class MagicRobot inherits Robot class.
3. The class has one private instance variables, visible of the type boolean.
4. The class has a zero-argument constructor that sets the value of visible to false.
5. The class has a method makeVisible() that sets the instance variable visible to
true.
6. The class has a method makeInVisible() that sets the instance variable visible to
false.
7. The class has a method isVisible() that returns the current value of the
instance variable visible.
8. The class overrides the Object’s toString() method in order to return a string
representation of a item object similar to format given below:
X: 5 , Y: 2, and it is visible

Part 4
Modify a class TestRobot_yourName to test the above class. It should contain the
following specifications:
9. Create an object of the type MagicRobot, medoo, using zero-argument
constructor then invoke the method toString() on medoo to display its values
using Standard Output
10. Initialize its properties using appropriate setter methods as the following values:
x=3, y=5 then invoke the method toString() on medoo to display its values using
Standard Output.
11. Make medoo visible, and then display its state usingStandardOutput.
12. Can you apply the following line
medoo.moveTo(9,11); Explain your answer
13. Can you apply the following lines
tetoo. makeVisible();
detoo.makeInvisible(); Explain your answer
14. Write: what are the members of class of Robot, Explain your answer.
15. Write: what are the members of class of MagicRobot, Explain your answer.
16. Explain when we use extends word
17. toString() methods in the two classes, is it consider as overloading or overriding
method, Explain your answer.
M251 Meeting 5- Activity 3 – SpeedRobot Class
Part 5:
You are required to develop a programmer-defined class that models the Robot shape.
Develop a class SpeedRobot 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. In project M251_M5 create a class SpeedRobot.
2. The class SpeedRobot inherits Robot class.
3. The class has one private instance variables, speed of the type integer which sets
the step size for moves.
4. The class has a zero-argument constructor and the class has a one-argument
constructor that sets the value of speed to a given value.
5. The class has a method setSpeed() that sets the instance variable speed to
a given value.
6. The class has a method getSpeed() that returns the current value of the
instance variable speed.
7. The class has a method moveNorth() that increments instance variable y by
the peed value.
8. The class has a method moveSouth() that decrements instance variable y by
the speed value.
9. The class has a method moveWest() that increments instance variable x by the
speed value.
10. The class has a method moveEast() that decrements instance variable x by
the speed value.
11. The class overrides the Object’s toString() method in order to return a
string representation of a item object similar to format given below:
X:5 , Y: 2,and its speed is 10
Part 6
Modify a class TestRobot_yourName to test the above class. It should contain the
following specifications:
18. Create an object of the type SpeedRobot, speedoo, using one-argument
constructor that sets the value of speed to 5.
19. Invoke the method toString() on speedoo to display its current attributes’
values using Standard Output.
20. Invoke the method moveNorth() on speedoo twice, and invoke the
method moveWest() on speedoo twice.
21. Set the speed of speedoo to 10, then invoke the method toString() on speedoo to
display its current attributes’ values usingStandardOutput.
22. Can you apply the following line
speedoo.moveTo(4,5); Explain your answer
23. Can you apply the following lines:
speedoo. makeVisible();
24. toString() methods in the two classes SpeedRobot & Robot, is it consider as
overloading or overriding method, Explain your answer
25. Write: what are the members of class of SpeedRobot, Explain your answer
M251 Meeting 5- Activity 4 – VariableSpeedRobot Class
Part 7
You are required to develop a programmer-defined class that models the Robot shape.
Develop a class VariableSpeedRobot 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. In project M251_M5 create a class VariableSpeedRobot.
2. The class VariableSpeedRobot inherits SpeedRobot class.
3. The class has a zero-argument constructor.
4. The class has a method moveNorth(newSpeed) that increments instance variable
y by the newSpeed value.
5. The class has a method moveSouth(newSpeed) that decrements instance variable
y by the newSpeed value.
6. The class has a method moveWest(newSpeed) that increments instance variable x
by the newSpeed value.
7. The class has a method moveEast(newSpeed) that decrements instance variable x
by the newSpeed value.

Part 8
Modify a class TestRobot_yourName to test the above class. It should contain the
following specifications:
26. Create an object of the type VariableSpeedRobot, vspeedoo, using
zero- argument constructor.
27. Invoke the method toString() on vspeedoo to display its current attributes’ values
using Standard Output.
28. Invoke the methods moveNorth() and moveEest() on vspeedoo, then invoke the
method toString() on vspeedoo to display its current attributes’ values using
Standard Output.
29. Set the speed of vspeedoo to 2,
30. Invoke the methods moveNorth() and moveEest() on vspeedoo, then invoke the
method toString() on vspeedoo to display its current attributes’ values using
Standard Output.
31. Invoke the methods moveNorth(10) and moveEest(10) on vspeedoo, then invoke
the method toString() on vspeedoo to display its current attributes’ values using
Standard Output.
32. Can you apply the following line
vspeedoo.moveTo(4,5); Explain your answer
33. Can you apply the following lines
vspeedoo. makeVisible();
34. Write: what are the members of class of VariableSpeedRobot, Explain
your answer
6 public class Robot
{
7 //part 3 instance
variables
8 private int x;
11 private int y;
12
13 //part 4 zero-argument constructor
14 public Robot ()
{
15 x = 0;
16 y = 0;
17 }
18
19 //5- Two-arguments
constructor
20 public Robot (int xPos, int yPos)
{
21 x = xPos;
22 y=
yPos; 23 }
24
25 // 6- Setter
methods
26 public void setX (int xPos)
{
27 x=
xPos; 28 }
29 public void setY (int yPos)
{
30 y=
yPos; 31 }
32 //6- Getter
Methods
33 public int getX ()
{
34 return
x;
35 }
36 public int getY ()
{
37 return
y;
38 }
39
40 // Class
methods
41 //
7
42 public void moveNorth ()
{
43 y = y + 1;
44 }
45 //
8
46 public void moveSouth
() {
47 y = y -
1;
48 }
49 //
9
50 public void moveEast ()
{
51 x = x + 1;
52 }
53 //
10
54 public void moveWest ()
{
55 x = x -
1;
56 }

// 11
58 public void moveTo (int xPos, int yPos)
{
59 x = xPos;
60 y=
yPos; 61 }
62
63 // 12
64 @Override
65 public String toString()
66 { String message ="";
67 message += " X: " + getX() +" Y: " + getY ();
68 return message;}
69 }// class
////////////////////////////////////////////////////////////////////////////////////////////////////

6 //-------------------Part 3------------------------------
7 // 2 inherits from Robot
8 public class MagicRobot extends
Robot 9 {
10 //3 instance variable
11 private boolean visible; // true or false
12
13 // 4- no-argument Constructor
14 public MagicRobot()
15 {
16 visible = false;
17 //or
18 makeInvisible();
19 }
20
21 // 5- Class methods
22 public void makeVisible ()
23 {
24 visible = true;
25 }
26 // 6
27 public void makeInvisible ()
28 {
29 visible = false;
30 }
31 // 7
32 public boolean isVisible ()
33 {
34 return visible;
35 }
36 // 8
37 public String toString()
38 { String message ="";
39 message += " X: " + getX() +" Y: " + getY () + " and ";
40 message += (isVisible()) ? " It is Visible " : " It is not Visible";
41 return message;}
42 }// class
////////////////////////////////////////////////////////////////////////////////////////////////////
6 //-----Part 5----------
// 2 inherits from Robot
7 public class SpeedRobot extends Robot // incomplete
8 {
9 // 3 instance variable
10 private int speed; // step size for moves
12
13 // 4 zero- argument constructor
14 public SpeedRobot ()
15 {
16 super(); // will be disscussed lated
17 }
18 // 4 one- argument constructor
19 public SpeedRobot (int
initialSpeed) 20 {
21 super(); // will be disscussed lated
22 speed =
initialSpeed; 23 }
24 // 5
25 public void setSpeed (int newSpeed)
26 {
27 speed = newSpeed;
28 }
29 // 6
30 public int getSpeed ()
31 {
32 return speed;
33 }
34 // new version overriding previous methods 7,8,9,10
35 @Override
36 public void moveNorth ()
37 {
38 setY (getY() +
speed); 39 }
40 @Override
41 public void moveSouth ()
42 {
43 setY (getY() -speed);
44 }
45 @Override
46 public void moveEast ()
47 {
48 setX (getX() +
speed); 49 }
50 @Override
51 public void moveWest ()
52 {
53 setX (getX() -
speed); 54 }
55 //11
56 public String toString()
57 { String message ="";
58 message += " X: " + getX() +" Y: " + getY () + " and its speed is " + speed;
59 return message;}
60 }// class
////////////////////////////////////////////////////////////////////////////////////////////////////
6 //-------------------Part 7------------------------------
7 // 2 inherits from Robot
8 public class VariableSpeedRobot extends SpeedRobot {
9
10 // 3 zero-argument constructor
11 public VariableSpeedRobot(){
12 super();
13 }
14 // new version overloading previous methods 4,5,6,7
15 public void moveNorth (int
newSpeed) 16 {
17 setY (getY() +
newSpeed); 18 }
19
20 public void moveSouth (int newSpeed)
21 {
22 setY (getY() - newSpeed);
23 }
24
25 public void moveEast (int
newSpeed) 26 {
27 setX (getX() + newSpeed);
28 }
29
30 public void moveWest (int newSpeed)
31 {
32 setX (getX() -
newSpeed); 33 }
34
35 }
36
6 //-------------------Parts 2,4,6,8 ------------------------------
7
8 public class TestRobot {
9 //------------------------Part 2-----------------
10 // 1
11 public static void main (String []args)
12 {
13 // 2
14 Robot tetoo = new Robot(5,2);
15
16 // 3
17 Robot detoo = new Robot();
18 detoo.setX(3);
19 detoo.setY(10);
20
21 // 4
22 System.out.println("tetoo, " +tetoo.toString());
23 System.out.println("detoo, " +detoo.toString());
24
25 // 5
26 tetoo.moveEast();
27 tetoo.moveEast();
28 tetoo.moveEast();
29
30 // 6
31 detoo.moveSouth();
32 detoo.moveSouth();
33
34 // 7
35 System.out.println("tetoo, " +tetoo.toString());
36 System.out.println("detoo, " +detoo.toString());
37
38 //--------------------Part 4 ---------------------------
39 // 9- create an object
40 MagicRobot medoo = new MagicRobot();
41 // invoke toString
42 System.out.println("medoo ="
+medoo.toString()); 43
44 //10 setter
45 medoo.setX(3);
46 medoo.setY(5);
47 System.out.println("medoo ="
+medoo.toString()); 48
49 // 11 visible
50 medoo.makeVisible();
51 System.out.println("medoo ="
+medoo.toString()); 52
53 // 12 yes we can
54 medoo.moveTo(9,11);
55
56 // 13 NO we can not
57 //tetoo. makeVisible();
58 // detoo.makeInvisible();
59
60 //--------------------Part 6 ---------------------------
61 //18 create an object
62 SpeedRobot speedoo = new
SpeedRobot(5); 63
64 //19
65 System.out.println("speedoo =" +speedoo.toString());
66
67 // 20
68 speedoo.moveNorth();
69 speedoo.moveNorth();
70 speedoo.moveWest();
71 speedoo.moveWest();
72
73 //21
74 speedoo.setSpeed(10);
75 System.out.println("speedoo =" +
speedoo.toString()); 76
77 // 22 yes we can
78 speedoo.moveTo(4,5);
79
80 // 23 No we can not
81 //speedoo. makeVisible();
82
83 //--------------------Part 8 ---------------------------
84 //26 create an object
85 VariableSpeedRobot vspeedoo = new VariableSpeedRobot();
86
87 // 27
88 System.out.println("Vspeedoo =" +vspeedoo.toString());
89
90 //28
91 vspeedoo.moveEast();
92 vspeedoo.moveNorth();
93 System.out.println("Vspeedoo =" +vspeedoo.toString());
94
95 //29
96 vspeedoo.setSpeed(2);
97
98 //30
99 vspeedoo.moveEast();
100 vspeedoo.moveNorth();
101 System.out.println("Vspeedoo =" +vspeedoo.toString());
102
103 //31
104 vspeedoo.moveEast(10);
105 vspeedoo.moveNorth(10);
106 System.out.println("Vspeedoo =" +vspeedoo.toString());
107
108 //32 yes we can
109 vspeedoo.moveTo(4,5);
110
111 //33 No we can not
112 //vspeedoo. makeVisible();
113 }
114 }// class
115 /*
116 * the output will be:
117 tetoo, X: 5 Y: 2
118 detoo, X: 3 Y: 10
119 tetoo, X: 8 Y: 2
120 detoo, X: 3 Y: 8
121 medoo = X: 0 Y: 0 and It is not Visible
122 medoo = X: 3 Y: 5 and It is not Visible
123 medoo = X: 3 Y: 5 and It is Visible
124 speedoo = X: 0 Y: 0 and its speed is 5
125 speedoo = X: -10 Y: 10 and its speed is 10
126 Vspeedoo = X: 0 Y: 0 and its speed is 0
127 Vspeedoo = X: 0 Y: 0 and its speed is 0
128 Vspeedoo = X: 2 Y: 2 and its speed is 2
129 Vspeedoo = X: 12 Y: 12 and its speed is 2
130 */
131
132

You might also like