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

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...
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.
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the data and methods for the application. Scenario/Information: If you carry a balance on a credit card, it can be nice to see how long it would take to payoff the card. For this scenario, you should design a Java application the prints a credit card payment schedule. Inputs for your program should include: Customer’s name (first and last), the account number (as an integer),...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
//Using Java language Write a copy instructor for the following class. Be as efficient as possible....
//Using Java language Write a copy instructor for the following class. Be as efficient as possible. import java.util.Random; class Saw {    private int x;    private Integer p; //------------------------------------------ public Saw() { Random r = new Random();        x = r.nextInt();        p = new Integer(r.nextInt()); } }
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
Java Language Add a recursive method to the program shown in the previous section that states...
Java Language Add a recursive method to the program shown in the previous section that states how many nodes does the stack have. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack() { Node aPointer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT