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...
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...
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...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to Light, LightOnCommand and LightOffCommand), AND (edited) Implement the CeilingFan class (with state, see p. 220), AND CeilingFanHighCommand (p. 221), CeilingFanMediumCommand, CeilingFanLowCommand, CeilingFanOffCommand (WITH Undo), AND TEST your CeilingFan classes (see pp. 222 and 223), AND Finally, implement and Test the MacroCommand class on pp. 224-227 EXAMPLE output for homework assignment…………… ======= The COMMAND PATTERN ============= guest house garage door is UP guest house garage...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps classes 2. In the CompareByPlayoffsAndSalary the compare method should list: (a) teams that made the playoffs last year before teams that did not. (b) If this is the same then it should list teams with lower salary first. 3. In the CompareByWinsLossesChamps list: (a) teams with more wins first. (b) If the same then list by teams with fewer losses. (c) If this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT