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; } }
1 :How many field attributes are there in the TrafficLight class?
2 :Name a field attribute for this class.
3 :What is the name of the method that is an accessor?
4 :What is the name of the method that is a mutator?
5 :What is the output type for the setStopLight method ?
6 :Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?
Ques 1:
There are 3 attributes in the TrafficLight class.
Explanation:
First one is stopLight, Second one is waitLight and Third one is goLight.
Ques 2:
One field attribute is goLight.
Explanation:
There are a total of 3 attributes present inside the class.
One of which is named as goLight.
Ques 3:
The method which is an accessor is getGreenLight
Explanation:
getGreenLight is a method which returns the value of the variable goLight.
Ques 4:
The method which is a mutator is setStopLight
Explanation:
setStopLight is a method which can be used to set the value of the String stopLight to the passed value color.
Ques 5:
The return type of setStopLight method is void.
Explanation:
void is the return type of the setStopLight method which means that it cannot return anything.
Ques 6:
Code:
public TrafficLight()
{
stopLight = "red";
waitLight = "yellow";
goLight = "green";
}
Explanation:
Here is the constructor above which sets the value of stopLight to red, waitLight to yellow and goLight to green.
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!