Skip to content

mpc-csis/csis10a-lab-12

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csis10a-lab-12

Getting started

Clone or download this lab. It can be used via Maven and your favorite editor or in BlueJ.

Part A

In this exercise we will practice working with arrays. The main method is already stubbed out for you. You must implement the methods ex1 through ex7 as described below. You can run your program using mvn exec:java "-Dexec.mainClass=Lab12".

problem1()

  1. Declare an array of int called stuff,
  2. and (on same line) add initialize it to set up stuff with the following data: {1, 5, 2, 9, 8, 4, 0, 3}
  3. Display "3. The length of the stuff array is: " followed by the length of the array. Don't type the number. Use the length data field for the array you created.

Expected result:

-- Problem 1 ----------
3. The length of the stuff array is: 8
0:1  1:5  2:2  3:9  4:8  5:4  6:0  7:3

problem2()

  1. Declare a 4-element array of char called word
  2. and (on same line) initialize word with the following data: {'W', 'a', 'i', 't'}
  3. Display "6. The length of the word array is: " followed by the length of the array. Don't type the number. Use the length data field.

Expected Result:

-- Problem 2 ----------
6. The length of the word array is: 4
0:W  1:a  2:i  3:t  

problem3()

  1. Declare a 10 element array of double called moreStuff
  2. On separate lines, set the first 5 elements of the array to these values: 1.1, 2.2, 3.3, 4.4, 5.5 9.Display "9. The length of the moreStuff array is: " followed by the length of the array. Don't type the number. Use the length data field.
  3. Write a method (below main) named displayArray that will display an array of type double[].
  4. Use the method to display the moreStuff array.
  5. Change the third element from 3.3 to 1000.
  6. Change the last element to 99.
  7. Display the array again beginning on a new line.

Expected result:

-- Problem 3 ----------
9. The length of the moreStuff array is: 10
0:1.1  1:2.2  2:3.3  3:4.4  4:5.5  5:0.0  6:0.0  7:0.0  8:0.0  9:0.0  
0:1.1  1:2.2  2:1000.0  3:4.4  4:5.5  5:0.0  6:0.0  7:0.0  8:0.0  9:99.0

problem4()

There is a 6 element array of int called data, already set to [0 1 2 3 4 5]. Using array assignment:

  1. Put a 10 in the first position of the array.
  2. put a 27 in the last position of the array.
  3. display the contents of position 2 of the array.
  4. then input a value from keyboard and store in the 5th spot (the 4th array position).

Expected Result:

-- Problem 4 -----------
2
Enter an integer: 7
0:10  1:1  2:2  3:3  4:7  5:27  

problem5()

  1. Make a 12-element double array called samples.
  2. Make an int variable called index and set it to 3.
  3. Put a 10 in the samples array at position index.
  4. put an 11 in the array at the position immediately following index (use index + 1 in square brackets).
  5. put a 7 in the array at two positions before index ( use index - 2 in square brackets).
  6. display the samples array

Expected Result:

-- Problem 5 ----------
0:0.0  1:7.0  2:0.0  3:10.0  4:11.0  5:0.0  6:0.0  7:0.0  8:0.0  9:0.0  10:0.0  11:0.0  

problem6()

Shallow Copying Arrays.

  1. Create an int array named array1 with the values 2, 4, 6, 8, 10.
  2. Create an int array named array2 and set it equal to array1. (int[] array2 = array1;)
  3. Change position 0 of array2 to 99.
  4. Display array1 and array2. Position 0 of both arrays changed. Why? If you do not know the answer, you'll get no credit for this lab.

Expected Result:

-- Problem 6 ----------
0:99  1:4  2:6  3:8  4:10  
0:99  1:4  2:6  3:8  4:10  

problem7()

Deep Copying Arrays

  1. Below main, create a new method called cloneArray that takes an array of integers as a parameter, creates a new array that is the same size as the parameter, copies the elements from the first array into the new one, and then returns a reference to the new array.
  2. Back in the Problem 7 section in main, create an int array named array3 with the values 1, 2, 3, 4, 5.
  3. Declare an int array array4 and assign it the clone of array3, using your cloneArray method.
  4. Display array3 and array4. They should have the same values.
  5. change element 0 of array4 to 99.
  6. display array3 and array4. array4's first element is 99, but array3's first element did not change.

Expected Result:

-- Problem 7 ----------
0:1  1:2  2:3  3:4  4:5  
0:1  1:2  2:3  3:4  4:5  
0:1  1:2  2:3  3:4  4:5  
0:99  1:2  2:3  3:4  4:5  

Part B

Now that you know how arrays work in Java, we’re going to work with the BoxBug classes from lab 10 and write a class DancingBug that "dance" by making different turns before each move. The DancingBug constructor has an integer array as a parameter. The integer entires in the array represent how many times the bug turns before it moves. For example, an array entry of 5 repressents a turn of 225 degrees (recall that one turn is 45 degrees). When a dancing bug acts, it should turn the number of times gien by the current array entry, then act like a Bug. In the next move, it should use the next entry in the array. After carrying out the last turn in the array, it should start again with the intial array value so that the dancing bug continually repeats the same turning pattern.

The DancingBugRunner should create an array and pass it as a parameter to the DancingBug constructor.

Start by modifying the code for BoxBug and BoxBugRunner provided (rename them to DancingBug etc), then modify them according to the instructions embedded in the files. Finally, add 4 DancinBugs to the Grid – make a line dance!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages