Questions for Discussion (Week 8)
Question 1
In this question, your task is to write the class Module with the following specification:
· It contains the following instance variables:
ü String moduleCode: the module code (e.g. “CS1101”)
ü int maxStudents: the maximum number of students who can take this module.
ü int numStudents: the current number of students in this module.
Besides, the class has several methods:
· 3 accessors: getModuleCode(), getmaxStudents() and getNumStudents()
· 3 mutators: setModuleCode(String), setMaxStudents(int), and setNumStudents(int)
· the method addStudents (int num): this method will add additional num students into the module (you may need to check some conditions before the addition, and output the suitable error messages if errors occur). What should the return type of this method be?
(a) After you have written the Module.java program, download the ModuleDriver.java program and run it. Compare the output with the given output in the file ModuleOutput.txt.
(b) At the moment, we provide a display(Module mod) method in ModuleDriver.java. After thinking about it, we think it is more appropriate to have a display() method in the Module class instead, and we should remove the display(Module mod) method in ModuleDriver. How would you then call the display() method in ModuleDriver?
Question 2
Given the following programs, what is the output?
public class Foo
{
private int x = 10;
public int f(int a)
{
int x = 7;
x += a;
return x;
}
public int g(int a)
{
x += a;
return x;
}
}
}
public class FooDriver
{
public static void main (String[] args)
{
Foo foo = new Foo();
int x = 5;
System.out.println (foo.f(x));
System.out.println (x);
System.out.println (foo.g(x));
System.out.println (foo.g(x));
}
}
Question 3
Goal: Parameter passing of objects; memory depiction of objects creation; some knowledge about the Garbage Collector.
You are given the following two classes: Ball and SoccerPlayer.
public class Ball {private int x = 0;
private int y = 0;
public void move (int newX, int newY) {
this.setX( this.getX() + newX );
this.setY( this.getY() + newY );
}
public int getX() {
...
}
public int getY() {
...
}
public void setX(int x) {
...
}
public void getY(int y) {
...
}
}
public class SoccerPlayer {
public void kick(Ball ball, int x, int y) {
ball.move(x, y);
}
}
Then you write this PlaySoccer driver class.
public class PlaySoccer {
public static void main (String[] args) {
Ball ball = new Ball();
SoccerPlayer sp = new SoccerPlayer();
// What is this output?
System.out.println( ball.getX() + ", " + ball.getY() );
sp.kick(ball, 10, 5);
sp.kick(ball, 8, 12);
// Now what is the output?
System.out.println( ball.getX() + ", " + ball.getY() );
// Do we need these?
ball = null; sp = null;
}
}
What is the output? Trace the programs and explain using a memory diagram.
Question 4
What is the output of this program?
public class Vehicle {private int topSpeed;
Vehicle() {}
void setTopSpeed(int topSpd) {
topSpeed = topSpd;
}
int getTopSpeed() {
return topSpeed;
}
int changeTopSpeed1(int topSpeed) {
topSpeed = topSpeed;
return this.topSpeed;
}
int changeTopSpeed2(int topSpeed) {
this.topSpeed = topSpeed;
return this.topSpeed;
}
void changeTopSpeed3(int topSpeed) {
topSpeed = topSpeed;
}
void UpdateMyTopSpeed(int topSpeed) {
topSpeed = 300;
}
}
public class VehicleDriver {
public static void main(String args[]) {
Vehicle Car = new Vehicle();
Car.setTopSpeed(200);
Car.changeTopSpeed3(230);
System.out.println(Car.getTopSpeed());
System.out.println(Car.changeTopSpeed1(210));
System.out.println(Car.getTopSpeed());
System.out.println(Car.changeTopSpeed2(220));
System.out.println(Car.getTopSpeed());
int myTopSpeed=155;
Car.UpdateMyTopSpeed(myTopSpeed);
System.out.println(myTopSpeed);
}
}
- 4 of 5 -