In: Computer Science
Use the class definition below to answer the following questions.
[Total 8 Marks]
public class TrafficLight {
String stopLight = "red";
String waitLight;
String goLight;
public void setStopLight(String colour) {
stopLight = colour;
}
public String getGreenLight() {
return goLight;
}
}
Question 21
Not yet answered
Marked out of 1.00
Flag question
Question text
D3a - [1 Mark]
How many field attributes are there in the TrafficLight class?
Answer:
Question 22
Not yet answered
Marked out of 1.00
Flag question
Question text
D3b - [1 Mark]
Name a field attribute for this class.
Answer:
Question 23
Not yet answered
Marked out of 1.00
Flag question
Question text
D3c - [1 Mark]
What is the name of the method that is an accessor?
Answer:
Question 24
Not yet answered
Marked out of 1.00
Flag question
Question text
D3d - [1 Mark]
What is the name of the method that is a mutator?
Answer:
Question 25
Not yet answered
Marked out of 1.00
Flag question
Question text
D3e - [1 Mark]
What is the output type for the setStopLight method
Answer:
Question 26
Not yet answered
Marked out of 3.00
Flag question
Question text
D3f - [3 Marks]
Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?
Given code:
public class TrafficLight {
String stopLight = "red";
String waitLight;
String goLight;
public void setStopLight(String colour) {
stopLight = colour;
}
public String getGreenLight() {
return goLight;
}
}
.
Question 21
How many field attributes are there in the TrafficLight class?
3
.
Question 22
Name a field attribute for this class.
stopLight
.
Question 23
What is the name of the method that is an accessor?
getGreenLight
Explanation:
accessor is a method which returns value of attribute of an object, here getGreenLight returns value of goLight.
.
Question 24
What is the name of the method that is a mutator?
setStopLight
Explanation:
mutator is a method which modifies value of attribute of an object, here setStopLight modifies value of stopLight.
.
Question 25
What is the output type for the setStopLight method
void
Explanation:
because setStopLight is an mutator method, it doesn't output something ie void.
.
Question 26
Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?
TrafficLight() {
stopLight = "red";
waitLight = "yellow";
goLight = "green";
}
.