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

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;    ...
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...
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 :...
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...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree. Car Class Write a class named Car that...
// the language is java, please implement the JOptionPane Use method overloading to code an operation...
// the language is java, please implement the JOptionPane Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods and an output method as follows: • computeObject(double radius) – compute the area of a circle • computeObject(double radius, double height) – compute area of a cylinder • computeObject(double radiusOutside, double radiusInside, double height) – compute the volume of a cylindrical object • output() use of JOptionPane to display instance field(s) and the result...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT