Question

I am working with to complete a TrafficSystem class in Java by using BlueJ. I have...


I am working with to complete a TrafficSystem class in Java by using BlueJ. I have stuck to answer this question below:

Write a private instance method cycleState() which takes no argument and
returns no value. This method cycles the value of state, from 1 to 2, from 2 to
3, from 3 to 4 and from 4.

Is there any body can help me to set cycleState() method to go from 1 to 2, then 2 to 3, then 3 to 4 and back to 1 again.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code in Java:

class TrafficSystem
{
private int state; //class variable
TrafficSystem() //default constructor
{
state=1;
}
private void cycleState() //function to cycle state from 1 to 2, 2 to 3, 3 to 4 and 4 to 1
{
state++;
if(state==5) //if state exceeds 4, we make it 1
state=1;
}
//function to test the function above by cycling and printing states for a given number of times
void displayTraffic(int time)
{
for(int i=1;i<=time;i++)
{
System.out.println("State at time "+i+" is "+state);
cycleState();
}
}
public static void main(String args[])
{
TrafficSystem t=new TrafficSystem();
t.displayTraffic(10);
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
I am working with to complete a TrafficSystem class in Java by using BlueJ. I have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi so I am currently working on a project for a java class and I am...

    Hi so I am currently working on a project for a java class and I am having trouble with a unit testing portion of the lab. This portion wants us to add three additional tests for three valid arguments and one test for an invalid argument. I tried coding it by myself but I am not well versed in unit testing so I am not sure if it is correct or if the invalid unit test will provide the desired...

  • I am working on my java but I keep getting this one wrong. Method Name: arrayContains...

    I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java project/class called ChangeUp. Create an empty int array of size 6. Create a method called populateArray that will pass an array, use random class to choose a random index and prompt the user to enter a value, store the value in the array. Create a method called printArray that prints the contents of the array using the for each...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java project/class called ChangeUp. Create an empty int array of size 6. Create a method called populateArray that will pass an array, use random class to choose a random index and prompt the user to enter a value, store the value in the array. Create a method called printArray that prints the contents of the array using the for each...

  • USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat....

    USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat. Follow the detailed steps below. This is based on Exercises 2.93 - 2.94 (6e) / 2.92 - 2.93 (5e) in the book, but with slight modifications, so be sure to follow the instructions below. Create a new BlueJ project named LastName-heater using your last name. Create a class named Heater At the top of the source code for Heater, add documentation like this: /**...

  • Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It...

    Java. i.Write a public instance method alignBody() that takes no arguments and returns no value. It should set the xPos and yPos of the body to the xPos and yPos of the StickFigure. ii.Write a public instance method alignLeg() that takes no argument and returns no value. It should set the xPos and yPos of the leg so it is centred immediately below the body of the StickFigure. public class StickFigure { /*Instance variables*/    private int xPos;//The horizontal position...

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • If anyone could give me some guidance for these questions. I know i'm chancing my arm...

    If anyone could give me some guidance for these questions. I know i'm chancing my arm but its worth a shot.... Thanks in advance i.Write a private instance method cycleState() which takes no argument and returns no value. This method cycles the value of state, from 1 to 2, from 2 to 3, from 3 to 4 and from 4 back to 1 again. (Note that the value of 0 for state is not part of this cycle). ii.Write a...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT