Lab 8: 1D Arrays!
Goals and Objectives
The main goal this lab is for you to get a more practice
with Java's array storage structures.
You will work more with loops to gain a better appreciation for
their power and capabilities.
Task
You will complete methods in a simple class that perform some simple array operations.
Background
Almost every program of note uses an array structure to store information.
A String can be represented using an array of char.
Remember that arrays and for loops are BFFs.
Introductions
Introduce yourself to your lab partner(s).
Tell your lab partner(s) your favoritve fall foods.
Have you had any yet?
Download the lab
Download the source file
ArrayStuff.java.
It
contains the stubs of several static methods. Save it directly to your folder with your
other lab files and open it in DrJava or jGRASP.
/**
* A collection of methods related to int[].
*
* Here the shorthand list[i..j] refers to the elements:
* list[i], list[i+1], ..., list[j], if i < j;
* list[i], if i = j;
* no elements if i > j.
*/
public class ArrayStuff {
/**
* Print list[i..j] on one line, separated by spaces and surrounded by [ and ].
* @param list the sorted array.
* @param i the index of the start of the range to be printed.
* @param j the index of the end of the range to be printed.
* Example: [ 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 ]
*/
public static void print(int[] list, int i, int j) {
System.out.println();
}
/**
* Sum list[i..j] = list[i] + list[i+1] + ... + list[j]
* @param list the sorted array.
* @param i the index of the start of the range to be summed.
* @param j the index of the end of the range to be summed.
* @return the sum items in list (or 0 if j>i)
*/
public static int sum(int[] list, int i, int j) {
return 0;
}
/**
* Return whether k is in list[i..j].
* Precondition: list[i..j] is sorted.
* @param list the sorted array.
* @param i the index of the start of the range to be searched.
* @param j the index of the end of the range to be searched.
* @param k the number to search for.
*/
public static boolean contains(int[] list, int i, int j, int k) {
return false;
}
/**
* Insert k into list where it belongs.
* Precondition: list[0..i-1] is sorted, and list[i] is unused.
* Postcondition: list[0..i] is sorted.
* @param list the sorted array.
* @param i the number of items in list.
* @param k the number to add to list.
*/
public static void insert(int[] list, int i, int k) {
}
/**
* Remove k from list[0..i-1].
* Precondition: list[0..i-1] is sorted, and k is an element of list[0..i-1].
* Postcondition: list[0..i-2] is sorted.
* Postcondition: list[i-1]=0 and is unused
* @param list the sorted array.
* @param i the number of items in list.
* @param k the number to remove from list.
*/
public static void remove(int[] list, int i, int k) {
}
/**
* Return true if the two arrays contain the same elements, in the same order
* arrays must be equal i length
* @param array1 the first array of ints
* @param array2 the second array of ints
* @return true if the two arrays contain the same elements, in the same order.
*/
public static boolean equalArrays(int[] array1, int[] array2) {
return false;
}
/**
* Returns a clone of the input array
* @param array1 an array of ints
* @return an array of ints that is a clone of the original
*/
public static int [] clone(int[] array) {
return new int[0];
}
/**
* Replaces every value of the array with the value 0
* @param array1 an array of ints
*/
public static void zero(int[] array) {
return;
}
/**
* Test the various methods in this class.
* Don't change anything below!
*/
public static void main(String[] args) {
/*
* The following line creates an int[] object of length 2 with 11 and
* 13 in it. Cool, huh? It's called an "array initializer".
*/
int[] a= new int[] {11, 13};
print(a, 0, 1); // Result: [11 13]
print(a, 0, 0); // Result: [11]
System.out.println(sum(a, 0, 1)); // Result: 24
System.out.println(sum(a, 0, 0)); // Result: 11
System.out.println(sum(a, 1, 1)); // Result: 13
System.out.println(contains(a, 0, 1, 11)); // Result: true
System.out.println(contains(a, 1, 1, 11)); // Result: false
System.out.println(contains(a, 2, 1, 11)); // Result: false
a= new int[10];
int count = 0;
a[count++]= 1;
a[count++]= 3;
a[count++]= 5;
print(a, 0, count-1); // Result: [1 3 5]
insert(a, count, 2);
count++;
print(a, 0, count-1); // Result: [1 2 3 5]
insert(a, count, 0);
count++;
print(a, 0, count-1); // Result: [0 1 2 3 5]
insert(a, count, 7);
count++;
print(a, 0, count-1); // Result: [0 1 2 3 5 7]
remove(a, count, 7);
count--;
print(a, 0, count-1); // Result: [0 1 2 3 5]
remove(a, count, 0);
count--;
print(a, 0, count-1); // Result: [1 2 3 5]
remove(a, count, 2);
count--;
print(a, 0, count-1); // Result: [1 3 5]
a= new int[10];
a[0]= 1;
remove(a, 1, 1);
print(a, 0, 0); // Result: []
int[] b;
a = new int[] {1, 2, 3, 4, 5};
b = new int[] {1, 2, 3, 4};
System.out.println(equalArrays(a,b)); // false
a = new int[] {1, 2, 3};
b = new int[] {1, 2, 3};
System.out.println(equalArrays(a,b)); // true
a = new int[] {};
b = new int[] {};
System.out.println(equalArrays(a,b)); // true
a = new int[] {1, 2, 3, 4, 5};
b = clone(a); // Result: [ 1 2 3 4 5 ]
print(b,0,b.length-1); // Result: [ 1 2 3 4 5 ]
System.out.println(a==b); // false
System.out.println(equalArrays(a,b)); // true
a = new int[] {1, 2, 3, 4, 5};
zero(a);
print(a,0,a.length-1); // Result: [ 0 0 0 0 0 ]
}
}
Getting Started
In this lab, you will practice writing loops. You should start off with s1 driving and s2 navigating.
Complete the methods in ArrayStuff.java. You should change driver/navigator roles
after each method is completed.
There are several independent methods in this lab.
These are listed in roughly easiest to hardest, but if you get stuck on
a part, you should consider trying the next step.
Have Fun!
Copyright © 2019, David A. Reimann. All rights reserved. |
|