|
RHS |
Test02
Class: 1st i Name : ___________________________________________________________________________________________
Question 01
What is an instance variable?
Answer: _______________________________________________________________________________________________________________________
_______________________________________________________________________________________________________________________________
_______________________________________________________________________________________________________________________________
Question 02
Below is a class, MobilePhone, to represent a mobile phone. It holds two instance variables, phoneNumber (a String) and PINcode (an int). For phoneNumber there is an accessor (getter) and a mutator (setter) called getPhoneNumber() and setPhoneNumber().
You should create the accessor and mutator for PINcode. Please fill in the code after the comments in the class below!
class MobilePhone {
/** Creates a new instance of MobilPhone with default values
*/
public MobilePhone() {
this.phoneNumber = "No number set!";
this.PINcode = 1234;
}
/** Creates a new instance of MobilPhone with values from parameters
* @param phoneNumber the number for the phone
* @param PINcode an integer to unlock the phone
*/
public MobilePhone(String phoneNumber, int PINcode) {
this.phoneNumber = phoneNumber;
this.PINcode = PINcode;
}
/** Sets (overwrites) the value for the phoneNumber
* @param phoneNumber the new value for the phoneNumber
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/** Reads and returns the value for the phoneNumber
*/
public String getPhoneNumber() {
return phoneNumber;
}
/** Sets (overwrites) the value for the phoneNumber
* @param phoneNumber the new value for the phoneNumber
*/
/** Reads and returns the value for the phoneNumber
*/
private String phoneNumber;
private int PINcode;
}
Question 03
Show how the method toString() could be implemented for the class MobilePhone (see above):
Question 04
Assume that the String variable st holds the value “My mom is sweet”.
What is the value of len after the expression int len = st.length(); has been executed?
Answer: __________________________________________________________
What is the value of st.substring(1,5)?
Answer: __________________________________________________________
What is the value of st.substring(7);
Answer: __________________________________________________________
Question 05
Assume that the following lines are executed:
int age = 12;
int heigth = 140;
What will be printed on the screen after the following lines of code has been executed?
if (age > 12)
System.out.println(“Statement 01”);
else
if ((heigth > age) && (age != 13)) {
System.out.println(“Statement 02”);
}
else
System.out.println(“Statement 03”);
System.out.println(“Statement 04”);
Answer: __________________________________________________________
_________________________________________________________________
_________________________________________________________________