CSci 2101 Data Structures: Lab 8

Problem 1: Passing arrays to methods

Draw two memory pictures for the following program: the first one right before the method m1 finishes, the other one right before m2 finishes. The pictures should show a and b in the method and arr1 and arr2 in main.

What will be printed by this program? Write it on your memory picture. Run the program to check your answer, modify your picture if needed.



public class Lab8Prob1 {
    public static void main(String [] args) {
	int [] arr1 = {1, 2, 3};
	int [] arr2 = {6, 7, 8};
	m1(arr1, arr2);
	System.out.println("after m1:");
	print(arr1);
	print(arr2);
	m2(arr1, arr2);
	System.out.println("after m2:");
	print(arr1);
	print(arr2);
    }

    public static void m1(int [] a, int [] b) {
	for (int i = 0; i < a.length; ++i) {
	    int temp = a[i];
	    a[i] = b[i];
	    b[i] = temp;
	}
	System.out.println("in m1:");
	print(a);
	print(b);
	// Memory picture 1
    }

    public static void m2(int [] a, int [] b) {
	int [] temp = a;
	a = b;
	b = temp;
	System.out.println("in m2:");
	print(a);
	print(b);
	// Memory picture 2
    }

    public static void print(int [] items) {
	// print array elements all on one line
	for (int i = 0; i < items.length; ++i) {
	    System.out.print(items[i] + " ");
	}
	System.out.println();
    }

}

Problem 2: writing array methods

Write a static method that takes two arrays of strings as parameters. The method has a loop that goes through the arrays until the end of the shorter one. If the string in the first array is alphabetically before the string at the same index in the second array, then the two strings get switched, otherwise they don't. Print out the two arrays in main after the call to the method.

Problem 3: exploring inheritance

Use the example we studied in class for this problem. Create a separate directory (folder) for this problem and save all the four files there. Important: you need to recompile all the classes before testing any features. To do this, remove all the .class files by typing

rm *.class
and then compile Test.java.

Problem 4

Is it possible to call a private method of a superclass in its subclass? What about a protected method? A public one? Write a program that tests it (you may use the Vehicle/Car/Airplane classes). Submit the program and write down the results (in comments or on the Wiki).

Problem 5

Write a program to answer the following questions (you may use any of the Vehicle/Car/Airplane classes). Explain in comments what works and what doesn't. Comment out the code that doesn't work.

The following questions refer just to a single class, they don't deal with inheritance:

Problem 6

Consider the same three situations as in Problem 5, but now one of the two methods is in the subclass, the other one is in the superclass. If you call a method on an instance of the subclass, which method would be called: the method of the subclass or the method of the superclass that has the same name?

Problem 7, extra credit

Come up with other features of inheritance that you would like to test. Write the program(s) to test these features. Clearly write the question that you are trying to answer, the results you have observed, and the answer based on your result.

Important: make sure that you submit only one copy of each file. If you need to submit multiple copies for different problems, rename the file before the submission.


This is a lab from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.