Question

I need this in Java: /** * Class for storing the last ten integers added. *...

I need this in Java:

/**
* Class for storing the last ten integers added.
*
* Your class should be named LastTen.
*
* Your class should implement two public methods as described below:
* 1. void add(int newValue):
* add a new integer to the values that we are rememebring
* 2. int[] getLastTen():
* return the last ten values that were added using add, in any order.
* If fewer than ten values were added, you should return zeros in their
* place.
*
* Examples:
* 1. add(1), add(2)
* getLastTen: [1, 2, 0, 0, 0, ... ]
* 2. add(0), add(1), add(2), ... add(10)
* getLastTen: [1, 2, 3, 4, ... 10]
*
* Your class should also provide a constructor that takes no arguments.
*/

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

class LastTen {

    private int[] arr;
    private int index;

    public LastTen() {
        arr = new int[10];
        index = 0;
    }

    public void add(int n) {
        if(index < 10) {
            arr[index++] = n;
        } else {
            for(int i = 0; i < 9; ++i) {
                arr[i] = arr[i+1];
            }
            arr[9] = n;
        }
    }

    public int[] getLastTen() {
        return arr;
    }
}
Add a comment
Know the answer?
Add Answer to:
I need this in Java: /** * Class for storing the last ten integers added. *...
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
  • In this assignment, you are asked to implement a Java class named HugeInt for storing huge...

    In this assignment, you are asked to implement a Java class named HugeInt for storing huge integers and performing mathematical operations on them. This class must have a private variable of type LinkedList〈Byte〉 which stores digits of a huge integer and ten of the following public methods (choose ten methods out of 12): public void setValue (int value): gets an integer (like 1234) as input parameter and stores its digits in the linked list in the same order as they...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...

    Please use java code. Implement a test class named BatArray1Test that tests all the functionality of the following given 3 methods, including the invalid arguments: 1) commonEnd Given two arrays of ints, a and b, return true if they have the same first element or they have the same last element. This method should return false if either array is empty or null. 2) midThree Given an array of integers, return a new array of length 3 containing the elements...

  • To be written in JAVA We want you to write a Java class named Character WithStatus...

    To be written in JAVA We want you to write a Java class named Character WithStatus that can be used to keep track of random game effects status on a role playing game character. The following requirements specify what fields you are expected to implement in your class: - A String data field named name to store the character's name - An int data field named health to store the character's health point; at zero they are dead. - An...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

  • Suppose an antiques dealer decides to reuse class Checker, that you wrote last week, for a...

    Suppose an antiques dealer decides to reuse class Checker, that you wrote last week, for a program they are writing to keep track of fancy or antique checker pieces. Modify your checker game from last week as described below. Add a class FancyChecker that is a child of class Checker. In addition to the members and member functions of class Checker, class FancyChecker should have a member named material which stores strings such as "wood" or "plastic". FancyChecker should also...

  • in JAVA 24. Suppose that a class has an overloaded method named add with the following...

    in JAVA 24. Suppose that a class has an overloaded method named add with the following two implementations: double add (int x, double y) { return x + y; } double add (double x, int y) { return x + y + 1; } What, if anything, will be returned by the following method calls? A. add(3, 3.14) B. (3.14, 3) C. add (3, 3) D. add (3.14, 3.14) 29. Here is the code for a recursive method named mystery....

  • Question 6) A “Golomb ruler” is a collection of integers such that no two pairs of...

    Question 6) A “Golomb ruler” is a collection of integers such that no two pairs of values are the same distance apart. Eg: If the current ruler is {1, 2, 4}, then either 7 or 9 could be added to the ruler but not both. (Since 7 − 2 = 5 and 9 − 4 = 5). You might want to use Math.abs(n) to compute the absolute value of n and the sort(null) method on List to sort a list....

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