214k views
1 vote
Create a public class called Last4Ordered. You should expose two public methods: add: Adds a value, does not return a value. last: Returns an array containing the last 4 values that were added in the order they were added. You do not need a constructor, but you can add an empty one if you need. Until 4 values have been added, you should return 0s in their place.

1 Answer

5 votes

Final answer:

Create a public class called Last4Ordered that has two public methods: add and last. The add method adds values to an array and the last method returns the last four values added. Use an array to store the last four values and update it accordingly.

Step-by-step explanation:

To implement the Last4Ordered class, you can use an array to store the last four values added. Create an instance variable last4 as an array of size 4, initialized with all zeros. The add method should take a value as a parameter and update the array by shifting values to the left and adding the new value at the end. The last method should simply return the last4 array.

Learn more about Last4Ordered class

User Doug Watkins
by
7.3k points

No related questions found