Question

In: Computer Science

THE LANGUAGE IS JAVA Download the class ArrayManager.java and fill out the method shells with the...

THE LANGUAGE IS JAVA

Download the class ArrayManager.java and fill out the method shells with the specifications below. After creating the methods 2) through 5), test them by running the application. The main method allows users to enter a command and, when necessary, additional command specifications.

1) a method displayArray:

  • accepts as a parameter an int array named data
  • prints the array elements to the console, separated by commas and spaces, with the entire array enclosed in braces, e.g., the array [0, 1, 2] should display as {0, 1, 2}

2) a method expandIntArray:

  • accepts as a parameter an int array named data
  • creates a new int array twice as large as data
  • copies every int value from data into the same index in the new array
  • the other indexes in the new array should be zeroes
  • returns the new array

3) a method shrinkIntArray:

  • accepts as a parameter an int array named data
  • creates a new int array half as large as data
  • for every index in the new array, copies the corresponding int value from data into it
  • returns the new array

4) a method insertValue:

  • accepts as parameters an int array named data, an int named value, and an int named index
  • creates a new array one element larger than data
  • for every index < index, copies the value from data into the new array
  • copies value into the new array's index element
  • for every index > index, copies the value from data into the new array's next index, i.e., copies from index x in data to index x+1 in the new array
  • returns the new array

5) a method removeValue:

  • accepts as parameters an int array named data and an int named index
  • creates a new array one element smaller than data
  • for every index < index, copies the value from data into the new array
  • for every index > index, copies the value from data into the new array's previous index, i.e., copies from index x in data to index x-1 in the new array
  • returns the new array

HERE'S "ArrayManager.java" code:

import java.util.Scanner;

public class ArrayManager {
        int[] data = new int[0];
        
        public static void main(String[] args) {
                ArrayManager am = new ArrayManager();
                while(true) {
                        System.out.println("please enter a command: display, expand, shrink, insert, remove, expand");
                        String command = new Scanner(System.in).next();
                        switch(command) {
                                case "display": am.displayArray(am.data); break;
                                case "expand": am.data = am.expandIntArray(am.data); break;
                                case "shrink": am.data = am.shrinkIntArray(am.data); break;
                                case "insert": System.out.println("please enter the new value");
                                int value = new Scanner(System.in).nextInt();
                                System.out.println("please enter the insertion index");
                                int insert = new Scanner(System.in).nextInt();
                                am.data = am.insertValue(am.data, value, insert); break;
                                case "remove": System.out.println("please enter the removal index");
                                int delete = new Scanner(System.in).nextInt();
                                am.data = am.removeValue(am.data, delete); break;
                        }
                }
        }

        void displayArray(int[] data) {
                
        }
        
        int[] expandIntArray(int[] data) {
                int[] data2 = null;
                return data2;
        }
        
        int[] insertValue(int[] data, int value, int index) {
                int[] data2 = null;
                return data2;
        }
        
        int[] removeValue(int[] data, int index) {
                int[] data2 = null;
                return data2;
        }

        int[] shrinkIntArray(int[] data) {
                int[] data2 = null;
                return data2;
        }
}

Solutions

Expert Solution

GIVEN BELOW ARE THE COMPLETED VERSIONS OF THE AFOREMENTIONED FUNCTIONS:

void displayArray(int[] data) {

for(int i=0; i<data.length(); i++)
      { System.out.println(data[i]);       
        }

int[] expandIntArray(int[] data) {

   int len= 2* data.length();
                int[] data2 = new int [len];

   for(int i=0; i<len; i++)
   {
       if(!i<data.length())
           data2[i]=0;
       else
           data2[i]=data[i];
   }
                return data2;
        }

   int[] shrinkIntArray(int[] data) {
   int len=data.length()/2;
                int[] data2 = new int [len];
   for(int i=0; i<len; i++)
   {
       data2[i]=data[i];
   }
                return data2;
        }

int[] insertValue(int[] data, int value, int index) {
                int len= data.length()+1;
                int[] data2 = new int[len];
  
   for(int i=0; i<(len-1); i++)
   {
       if(i<index)
           data2[i]=data[i];
       else if(i>index)
           data2[i+1]=data[i];
   }
       data2[index]=value;
                return data2;
        }

   int[] removeValue(int[] data, int index) {
                int len= data.length()-1;
                int[] data2 = new int [len];
   for(int i=0; i<len; i++)
   {
       if(i<index)
           data2[i]=data[i];
       else if(i>index)
           data2[i]= data[i-1];
   }

                return data2;
        }


Related Solutions

Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry {...
Language java Rewrite the method getMonthusing the "this" parameter CODE: import java.util.Scanner; public class DateSecondTry { private String month; private int day; private int year; //a four digit number. public void writeOutput() { System.out.println(month + " " + day + ", " + year); } public void readInput() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter month, day, and year."); System.out.println("Do not use a comma."); month = keyboard.next(); day = keyboard.nextInt(); year = keyboard.nextInt(); } public int getDay() { return day;...
***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class...
***JAVA LANGUAGE, NO CHANGES NEEDED FOR THE Movie CLASS. USE THIS AS A REFERENCE*** public class Movie { private String title; private int length; private static int numMovies = 0; public Movie() { this("", 0); } public Movie(String title, int length) { this.title = title; this.length = length; ++numMovies; } public String getTitle() { return this.title; } public int getLength() { return this.length; } public static int getNumMovies() { return numMovies; } public void setTitle(String title) { this.title = title;...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
The programming language is Java. Write the code you would use to fill the variable cubed...
The programming language is Java. Write the code you would use to fill the variable cubed with the value stored in x to the third power.
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Also please discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT