Create a program in Java for the TrafficLight class using a simple counter that
ID: 3861123 • Letter: C
Question
Create a program in Java for the TrafficLight class using a simple counter that is advanced in each call to next. If the traffic light was initially green, the counter has values 0 1 2 3 4 5 6 ... . If the traffic light was initially red, the counter has values 2 3 4 5 6 7 8 ... . Compute the current color and the number of reds, using integer division and remainder.
Complete the following file:
/**
A simulated traffic light.
*/
public class TrafficLight
{
private int steps;
/**
Constructs a green traffic light.
*/
public TrafficLight()
{
. . .
}
/**
Constructs a traffic light.
@param initialColor the initial color "green", "yellow", or "red"
*/
public TrafficLight(String initialColor)
{
. . .
}
/**
Moves this traffic light to the next color.
*/
public void next()
{
steps++;
}
/**
Returns the current color of this traffic light.
@return the current color
*/
public String getColor()
{
. . .
}
/**
Counts how often this traffic light has been red.
@return the number of times this traffic light has been red
*/
public int getReds()
{
. . .
}
}
Explanation / Answer
/**
A simulated traffic light.
*/
public class TrafficLight
{
private int steps;
/**
Constructs a green traffic light.
*/
public TrafficLight()
{
steps = 0;
}
/**
Constructs a traffic light.
@param initialColor the initial color "green", "yellow", or "red"
*/
public TrafficLight(String initialColor)
{
if ("green" == initialColor)
steps = 0;
else if ("red" == initialColor)
steps = 2;
else
steps = 1;
//. . .
}
/**
Moves this traffic light to the next color.
*/
public void next()
{
steps++;
}
/**
Returns the current color of this traffic light.
@return the current color
*/
public String getColor()
{
int rem = steps%3;
String color;
if (rem == 0)
{
color = "green";
}
else if (rem == 1)
{
color = "yellow";
}
else
{
color = "red";
}
//. . .
return color;
}
/**
Counts how often this traffic light has been red.
@return the number of times this traffic light has been red
*/
public int getReds()
{
int count = 0;
count = steps/3;
if (steps%3 == 2)
count++;
return count;
}
public static void main(String args[])
{
TrafficLight objTrafficLight = new TrafficLight("green");
for (int i = 0; i < 50; i++)
{
System.out.println(objTrafficLight.getColor());
System.out.println(objTrafficLight.getReds());
objTrafficLight.next();
}
}
}
/**
A simulated traffic light.
*/
public class TrafficLight
{
private int steps;
/**
Constructs a green traffic light.
*/
public TrafficLight()
{
steps = 0;
}
/**
Constructs a traffic light.
@param initialColor the initial color "green", "yellow", or "red"
*/
public TrafficLight(String initialColor)
{
if ("green" == initialColor)
steps = 0;
else if ("red" == initialColor)
steps = 2;
else
steps = 1;
//. . .
}
/**
Moves this traffic light to the next color.
*/
public void next()
{
steps++;
}
/**
Returns the current color of this traffic light.
@return the current color
*/
public String getColor()
{
int rem = steps%3;
String color;
if (rem == 0)
{
color = "green";
}
else if (rem == 1)
{
color = "yellow";
}
else
{
color = "red";
}
//. . .
return color;
}
/**
Counts how often this traffic light has been red.
@return the number of times this traffic light has been red
*/
public int getReds()
{
int count = 0;
count = steps/3;
if (steps%3 == 2)
count++;
return count;
}
public static void main(String args[])
{
TrafficLight objTrafficLight = new TrafficLight("green");
for (int i = 0; i < 50; i++)
{
System.out.println(objTrafficLight.getColor());
System.out.println(objTrafficLight.getReds());
objTrafficLight.next();
}
}
}
/**
A simulated traffic light.
*/
public class TrafficLight
{
private int steps;
/**
Constructs a green traffic light.
*/
public TrafficLight()
{
steps = 0;
}
/**
Constructs a traffic light.
@param initialColor the initial color "green", "yellow", or "red"
*/
public TrafficLight(String initialColor)
{
if ("green" == initialColor)
steps = 0;
else if ("red" == initialColor)
steps = 2;
else
steps = 1;
//. . .
}
/**
Moves this traffic light to the next color.
*/
public void next()
{
steps++;
}
/**
Returns the current color of this traffic light.
@return the current color
*/
public String getColor()
{
int rem = steps%3;
String color;
if (rem == 0)
{
color = "green";
}
else if (rem == 1)
{
color = "yellow";
}
else
{
color = "red";
}
//. . .
return color;
}
/**
Counts how often this traffic light has been red.
@return the number of times this traffic light has been red
*/
public int getReds()
{
int count = 0;
count = steps/3;
if (steps%3 == 2)
count++;
return count;
}
public static void main(String args[])
{
TrafficLight objTrafficLight = new TrafficLight("green");
for (int i = 0; i < 50; i++)
{
System.out.println(objTrafficLight.getColor());
System.out.println(objTrafficLight.getReds());
objTrafficLight.next();
}
}
}
/**
A simulated traffic light.
*/
public class TrafficLight
{
private int steps;
/**
Constructs a green traffic light.
*/
public TrafficLight()
{
steps = 0;
}
/**
Constructs a traffic light.
@param initialColor the initial color "green", "yellow", or "red"
*/
public TrafficLight(String initialColor)
{
if ("green" == initialColor)
steps = 0;
else if ("red" == initialColor)
steps = 2;
else
steps = 1;
//. . .
}
/**
Moves this traffic light to the next color.
*/
public void next()
{
steps++;
}
/**
Returns the current color of this traffic light.
@return the current color
*/
public String getColor()
{
int rem = steps%3;
String color;
if (rem == 0)
{
color = "green";
}
else if (rem == 1)
{
color = "yellow";
}
else
{
color = "red";
}
//. . .
return color;
}
/**
Counts how often this traffic light has been red.
@return the number of times this traffic light has been red
*/
public int getReds()
{
int count = 0;
count = steps/3;
if (steps%3 == 2)
count++;
return count;
}
public static void main(String args[])
{
TrafficLight objTrafficLight = new TrafficLight("green");
for (int i = 0; i < 50; i++)
{
System.out.println(objTrafficLight.getColor());
System.out.println(objTrafficLight.getReds());
objTrafficLight.next();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.