Question

In: Computer Science

IN JAVA. I have the following code (please also implement the Tester to test the methods...

IN JAVA.

I have the following code (please also implement the Tester to test the methods )

And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements.

After it should decrease the size of the array, and after return and save the integer.

package ourVector;

import java.util.Scanner;

public class ourVector {
        private int[] A;
        private int size;
        
        public ourVector() {
                A = new int[100];
                size = 0;
        }
        
        public ourVector(int s) {
                A = new int[s];
                size = 0;
        }
        public int size() {
                return size;
        }
        public int capacity() {
                return A.length;
        }
        public boolean isEmpty() {
                if(size == 0)
                        return true;
                else 
                        return false;
        }
        public String toString() {
                if(this.isEmpty())
                        return "[]";
                String st = "[";
                for(int i = 0; i < size-1; i++) {
                        st += A[i] + ", ";
                }
                
                st += A[size -1 ] + "]";
                return st;
        }
        public void add(int e) {
                
                A[size] = e;
                
                size++;
        }

Solutions

Expert Solution

Program Code Screenshot

Sample Output :

Program Code to Copy

class ourVector {
    private int[] A;
    private int size;

    public ourVector() {
        A = new int[100];
        size = 0;
    }

    public ourVector(int s) {
        A = new int[s];
        size = 0;
    }

    public int size() {
        return size;
    }

    public int capacity() {
        return A.length;
    }

    public boolean isEmpty() {
        if (size == 0)
            return true;
        else
            return false;
    }

    public String toString() {
        if (this.isEmpty())
            return "[]";
        String st = "[";
        for (int i = 0; i < size - 1; i++) {
            st += A[i] + ", ";
        }

        st += A[size - 1] + "]";
        return st;
    }

    public void add(int e) {
        A[size] = e;
        size++;
    }

    public int remove(){
        //Remove first element
        int ans = A[0];
        //Shift all elements to the left
        for(int i=0;i<size-1;i++){
            A[i] = A[i+1];
        }
        //Reduce the size
        size--;
        return ans;
    }
}

class Main{
    public static void main(String[] args) {
        ourVector v = new ourVector();
        System.out.println("Capacity is "+v.size()+" and Initial size is "+v.capacity());
        System.out.println("After adding 5 numbers");
        for(int i=0;i<5;i++){
            v.add(i+1);
        }
        System.out.println(v);
        System.out.println("Size is "+v.size());

        System.out.println("After removing "+v.remove());
        System.out.println(v);
        System.out.println("Size is "+v.size());
    }
}

Related Solutions

This code has to be in java (I code in eclipse). Also these instructions have to...
This code has to be in java (I code in eclipse). Also these instructions have to be followed exactly because if not my output won't match the expected out ( this will be uploaded on zybooks). Write a program that asks the user for their age in days. The program will compute the person's age in years (you can assume that all years have 365 days) and then prints one of the following messages: If the user is 1 year...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Python - Please include running time of push(), pop(), and top() methods and tester code for...
Python - Please include running time of push(), pop(), and top() methods and tester code for all methods. Design a stack ADT using a single queue as an instance variable, and only constant additional local memory within the method bodies. What is the running time of the push(), pop(), and top() methods for your design? Implement your modified stack ADT in Python, including tester code to test all its methods.
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Objective in JAVA (Please show output and have the avergae number of checks also.): Implement both...
Objective in JAVA (Please show output and have the avergae number of checks also.): Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times.  Each time the search is conducted the number of checks (IE number of times the loop is ran or the number of times the recursive method is called) needs...
Language: Java I have written this code but not all methods are running. First method is...
Language: Java I have written this code but not all methods are running. First method is running fine but when I enter the file path, it is not reading it. Directions The input file must be read into an input array and data validated from the array. Input file format (500 records maximum per file): comma delimited text, should contain 6 fields: any row containing exactly 6 fields is considered to be invalid Purpose of this code is to :...
Please fill in the code where it says to implement Methods needed to implement include: insert,...
Please fill in the code where it says to implement Methods needed to implement include: insert, find, remove, and toIndex // // STRINGTABLE.JAVA // A hash table mapping Strings to their positions in the the pattern sequence // You get to fill in the methods for this part. // public class StringTable {    private LinkedList<Record>[] buckets; private int nBuckets; // // number of records currently stored in table -- // must be maintained by all operations // public int...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT