Get the Lab 10 program files from the "read only folder" and put them in your Lab 10 directory.
public class Demo { public static void main (String [] args) { int [] a; a = new int[4]; a[0] = 3; for (int i = 1; i < 4; i++) a[i] = a[0]; a[1] = 6; for (int i = 0; i < 4; i++) System.out.println(a[i]); } }Using paper and pencil, trace out what what happens. What will the output be?
1
Show us your trace and the anticipated output.
Change directory into Lab10 and go into the subdirectory MyArray. Edit the class Demo that contains the above code. Compile this program and see if your prediction agrees with the results.
Now, edit the Demo class and replace the lines
int[] a; a = new int[4];with
StringBuffer[] a; a = new StringBuffer[4];and also replace
a[0] = 3;with
a[0] = new StringBuffer("Sasparilla");Finally, replace
a[1] = 6;with
a[1].setCharAt(0, 'J');Predict what the output should be now. Draw a state-of-memory diagram to help you visualize what's happening. Then, run the program and see what you get.
2
Explain why the program behaves differently than the original program.
Notice that there are two versions of the List constructor. The main() method creates a List object using the constructor that takes no arguments and calls the printList() method on this object. Compile and run this program and observe the results.
Now, change your main() method to construct a List object using the constructor that takes a size. Use a size of 3 as the argument, but do not change anything else. Compile and run this program and observe the results.
3
Explain why the outputs of the printList()
method differ in these two examples.
Add a line to the end of your second List() constructor that asks the user to input strings into your data array by calling the readList() method. Do not change main(). When you run your program, you should be prompted for three input strings, after which the strings will be displayed on your terminal window.
Next, modify the printList() method so that each string in the data array is printed 4 words per line, with each word on a line separated from the previous word by one space. So, for example, if the following list of 14 words is used to populate the data array
early to bed and early to rise makes a person healthy wealthy and wiseThe output should appear as
early to bed and early to rise makes a person healthy wealthy and wiseYou will only need to make minor changes to the printList()method. Hint: The modulus operator may be helpful here.
Having made your changes to printList(), modify your main() method to construct a List object using the default constructor. Change the initial strings in the data array from their current values of "Do", "Re", "Mi" ... to "early", "to", "bed", ....
4
Show us your modifications and your running program.
position 0 1 2 3 4 data "Do" "Re" "Mi" "Fa" "So"first data[0] and data[4] should be exchanged to give
position 0 1 2 3 4 data "So" "Re" "Mi" "Fa" "Do"then data[1] and data[3] should be exchanged to give
position 0 1 2 3 4 data "So" "Fa" "Mi" "Re" "Do"and data[2] (because it is in the middle) should be left alone. This in-place reversal should also work for an array with an even number of elements, since for such an array there simply isn't any "middle" element.
Compile and run your program. Observe that something is wrong. Trace your program's behavior by carefully showing what happens in each iteration of the loop in reverseList(). Using your trace, find and fix the problem. Your fix should only modify reverseList() and should still do an in-place reversal. Do not modify any other parts of your program.
5
When you are ready, show us your fix and your running program.
6 Draw a state of memory diagram to explain what is happening.
Show us your diagram and explain.
Close project PassArray.
The program searches the list and reports on the youngest and oldest people in the list.
Next, you will be asked to enter a "Name to search:". To answer this question, enter either a new name, or one that you entered previously. The program reports the position in the list if found or that the name was not found. Run your program at least twice: once with a name to search that you know is in the array (and make sure the position the program reports is correct) and once with a name that you know is not in the array.
Change your program so that instead of searching for a name, you are asked to enter a "Name to delete:" and to delete the entry from the array.
First change the prompt to delete instead of search. Then add a section of code to Demo.java (after the code that searches for a particular person) that deletes that person from the array (only if the person was found). The deletion should be performed so that all the Persons following the deleted entry are moved to the previous positions in the array. For example, suppose the person array looks like the BEFORE picture below. After removing person B, the array should look like the AFTER picture.
In addition, modify Demo.java to display the names of persons in the array after the deletion has been performed.
Hint #1: You'll want to create a new array of the proper size to hold the reduced list.
Hint #2: There are (possibly!) two groups of references to be copied to the new array -- the group ahead of the item to be deleted, and the group following the item to be deleted. One simple approach is to treat the copying of each group as a separate task.
Hint #3: Don't forget to assign your person array to the new array after you have finished copying the correct elements.
7 Show us your program when you have made the revisions described above.
Now modify the section of code in Demo that finds the oldest and youngest person so that it finds the positions in the array of the oldest and youngest. Since you know their positions, you can modify your code to print information about the youngest and oldest in the following manner (for example):
Oldest : Joe is 66 years old and is at position 3 in the list. Youngest: Fran is 22 years old and is at position 1 in the list.Strip out the part of the code that searches for a name to delete.
8 Show us your program when you have made these revisions.
Note that the instance variable graphPoints is an array of Points. Conceptually, the result of this is as follows, but the points are not actually 'displayed':
Complete the code in the paint() method to draw lines connecting the points, in order to produce a grid as seen in the following diagram:
Note that you need to draw only 2 * (NUM_SQUARES + 1) lines, with each line connecting the two points on opposite sides of the grid.
We suggest using the following procedure. Use two sequential for loops, one to go through the rows and draw the horizontal lines between each pair of points
and one to go through the columns and draw the vertical lines between each pair of points.
The method for drawing a line is a method on the Graphics object that is a parameter to the paint() method. To draw a line from from Point p (with coordinates (p.x, p.y)) to Point q (with coordinates (q.x, q.y)) use
g.drawLine(p.x, p.y, q.x, q.y).Instead of 4 squares per row (as shown above), the graph paper will have 10 squares per row.