QuestionQuestion

Transcribed TextTranscribed Text

In lecture, we described several common operations that can be performed on arrays. In this assignment you will implement a class, called Doub 1 earray! 1. This class defines and implements these and other operations on double arrays. The goals of this assignment are: 1. to become comfortable with defining a class and using it 2. become more comfortable with reading javadoc-created documentation and using it to guide implementation 3. developing code to test your implementation as you write it This is a much more complicated program than #1, so it will require you to start to develop some of your own debugging skills. You will also modify a program called Doub leArrayMain to both write tests for your class and interactively manipulate instances of the Doub leArray! 11 class. TASK 1 : The DoubleArray 11 class You are being provided a skeleton of the DoubleArray 11 class. It has been commented using Javadoc-style comments. 1. Download DoubleArray 11.java 2. Generate the documentation using the javadoc command-line tool, to see the details of each constructor and public method. See (A-section) lecture slides for an example of how to run the javadoc command. 3. Go through the signature of all the methods defined in the Javadoc and carefully read method description to understand its operation on the internal array 4. Implement all methods correctly. 5. Put your name in the file after the existing Javadoc @ author tag (replace YOUR NAME HERE) When you Implement Doub leArray! 11, You may NOT: a) Change the signature of any public method b) Change the return type of any public method c) Add any new public methods. You may add as many private methods as you desire d) Add any public instance or class variables. All class/instance variables should declared private e) Use any other pre-defined classes (e.g., Arrays, ArrayList, ) that provide similar functionality. Your implementation of DoubleArray11 should not have any other import statements than what has been provided to you in the skeleton You are allowed to add any number of your own private methods and variables. As you look at the Doub LeArray11 definition, it should be clear that: a. An instance of the class internally stores an array of doubles. This instance variable makes up a key component of the state of an DoubleArray11 object. b. The class must also define methods that manipulate an instance's internally-stored array. C. For this assignment, the capacity of the internal array may be larger than the number of active elements. The first size elements of the internal array are considered active. d. You should also note, that in the skeleton, boolean methods always return false. That is clearly incorrect in the full implementation. Those return values in the skeleton are present so that the initial code will compile. e. Note the return types of the methods. For clarity, we will list the constructors and methods defined in poubleArray11 with a brief description. In grading your assignment, we will write our test programs to utilize only your implemented DoubleArray11 class to test its functionality. The following table illustrates a portion of the Javadoc that was generated after Step 2: E to feath E 7.0 Methoo Summary All Methods Static Mathods Instance Medhods Modifier and cithearray. Scuble[] getArray() internalstered Arroy ant java. Jouble getthe static int arthall: staredir insert(inb index, double element] Insort From the table we can see that methods that return boolean should return true if method was successful, false if an error would occur For example, if you attempted to insert at improper index is out of bounds, you should return false. Note : Your DoubleArray11 implementation should never generate a runtime error. It must not print any error messages to the screen (though you will find such error messages useful during debugging). That means that you will need to check your arguments for valid values. Be sure to check that object references passed to your methods are not null. In the case of a constructor, if passed a null object pointer, you should create a valid instance in which the internal array has no capacity to store elements. Some hints for solving Task 1: 1. You may find an internal helper method that copies the contents of one array into another to be useful. 2. If the insert method is invoked, and your internal array is full, you should create a new internal array with expanded capacity. Don't forget to copy the original elements to your new internal array. TASK 2 : The DoubleArray11Main Program This is a java program and therefore defines a main ( ) method. You are being provided a DoubleArrayMain class that allows you to utilize/test instances of DoubleArray 11. You will extend the DoubleArrayMain class to allow testing of all of the methods in DoubleArray 11. You are being provided a partially-completed Doub leArrayMain program. Don't forget to put your name in the file after the existing Javadoc @author tag (replace YOUR NAME HERE) The main method contains a loop that processes commands that create and use methods of the DoubleArray 11 class. Each command is a string input by the user, which is then interpreted by the loop, and causes some action to happen. The start of the interpreter sets up some initial variables, and the first thing the loop does is print a > character and wait for input: DoubleArrayl currentArray = new DoubleArray11() ; Scanner scan = new Scanner (System.in) ; String cmd = "I" ; while (true) { System.err.print(">"); cmd = scan.nextLine() i String[] parts = cmd.split("\\s+"); The input is stored in cmd, and then it is split apart on whitespace. The expression cmd. split ("llst") returns an array of strings, where each string returned is a fragment of cmd separated by whitespace from the others. So, for example "set 10 1. 7" split (") \s+") would produce the array {"set", "10", "1.7"}. The rest of the loop processes the array produced: if (parts. length == 0) { continue; } switch (parts [0]) { case "show" : System. out println (currentArray. toString ()); continue, case "create": if (parts.length != 2 | I lisInt (parts [1]) ) { printCreateUsage i continue; } currentArray = new DoubleArray11 (Integer parseInt (parts [1]) ) ; continue; In particular, it matches the string in the first position in the array to see what command to run. In this snippet, we see that if the array starts with "show," the current array will be printed (using its toString method). If the array starts with "create," the interpreter expects the array to have a second element (index 1), which is an integer. If it doesn't, it prints an error message and goes back to the top of the loop. If it does, it uses that as an argument to the constructor for DoubleArrayll This will have the effect up updating the currentArray for future iterations of the loop. You should study this loop in detail so you understand how it works, and ask questions about it on Piazza and in lab to help. You will use these commands in two ways. First, you will extend the set of commands to cover all the methods on DoubleArray 11. Second, you will use the commands to test your implementation and make sure it does what you expect. Adding Commands There are several commands already implemented that call methods, print values, and otherwise manipulate and inspect the array. You will add several more commands, using the examples given to develop the code that calls out to a method. These commands should not print anything, just have the indicated effect: delete n -- deletes the item at index n in the current array using the delete method insert n d -- adds the element d at index n by using the insert method reverse- - from start stop -- uses the reverse method to reverse the array from start to stop (which should be valid indices) reverse -- uses the reverse method to reverse the whole array swap n m -- uses the swap method to switch the values in n and m These commands should print their answer: min -- uses the min method to print the min max -- uses the max method to print the max size -- uses the getSize method to print the current size of the array Using Commands to Test Running the program DoubleArrayMain after compiling it will allow you to quickly test particular methods. For example, you might have an interaction like this: $ java DoubleArrayMain > show [] > create 5 > append 2.3 > show [ 2.30] > append 4.4 > show [ 2.30, 4.40] This would cause the toString() method to be called 3 times (once for each use of show), the constructor to be called once (for create), and append to be called twice. You could be confident in the basic behavior of append and the constructor after doing this, though there may be more cases to consider. It's useful, once you've written a good test, to have a way to verify that it doesn't break later. One way you can do this is by using a feature of the shell (the thing running in your terminal), to save a test and then run it later. You can create a file with one command per line, ending in exit, for example the test above would be written as: show create 5 append 2.3 show append 4.4 show exit Then you can use the < shell operator to send that file to a program instead of user input (assuming the file was saved in test. txt): $ java DoubleArrayMain < test.txt This has the effect of running DoubleArrayMain while treating the lines in test.txt as though you had typed them yourself. Each use of scan.nextLine () will read a full line of the input file and process it as user input. This makes it so you can save the above test in a file, and re-run it easily without having to retype all the commands. It's also useful to be able to run a number of tests at once, if you've saved several different files. We've given you some support for doing this with the script run tests. If you run the command: $ /run tests The script will look for pairs of files named testinputl txt, testoutputl ixt, testinput2.txt, testoutput2.txt, and so on. It will run the main program on the inputs in the files named testinput, and compare the output to the corresponding testoutput file. That is, DoubleArrayMa when reading testinput is expected to give output identical to the testoutput file. The run tests program will report a success if they match, and a test error if they don't. For example, success would look like: $ /run tests - Success for test tests/testinput1.tx 1 tests passed, 0 failed while failure would look like: $ ./run tests Test tests/testinputl.txt failed, expected: [ 2.30] [ 2.30, 4.40] but got: [] [] 0 tests passed, 1 failed We've provided one test to get you started, and starting out, this kind of failure is what you'll see. You are required to submit test files along with your submission, and you will be graded on whether or not your tests are correct and thorough. Your tests, when taken together, should cover every command at least once in a meaningful way. "Meaningful" means, for example, that you if you want to test append, you should use it and then later use show to make sure that the element is present. You might use append without show to check that e.g. size works correctly, but you should also include a meaningful test for append. You should also check for error conditions - for example, test for getElement on an empty array, or for inserting at an invalid index.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/**
* Provide a set of Array functions on an Array of doubles
* @author YOUR NAME HERE
* @version
*/
public class DoubleArray11
{
public final int EXPANSION=10;
// This is internal array where you store double values
private double [] internal;

// This is the number of filled (utilized slots in the array)
// internal.length may be > size.
// only indices 0..(size-1) store "active" elements
private int size;

// You may want to add other instance variables here.
private static int nArrays = 0;

/**
   * 0-argument constructor.
   * initial capacity of internal double array is set to 0
   */
public DoubleArray11()
{
    size = 0;
    internal = new double[size];
    nArrays++;
}

/**
   * create an instance where the internal storage has a capacity to
   * to store upto capacity doubles
   * @param capacity initial capacity of this instance
   */
public DoubleArray11(int capacity)
{
    size = 0;
    internal = new double[capacity];
    nArrays++;
}

/**
   * Create the internal array to be the same size as the argument
   * copy element-by-element the argument array to internal
   * @param dArray array of elements to copy
   */
public DoubleArray11(double [] dArray)
{
    size = dArray.length;
    internal = new double[size];
    for (int i = 0; i < size; i++) {
      internal[i] = dArray[i];
      nArrays++;
    }
}

/* Make a string representation */
/**
   * Pretty Print -- Empty String "[]"
   *                  else "[e1, e2, ..., en]"
   *                  only print the first size elements
   */
@Override
public String toString()
{
    // note: String...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$75.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats