## 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.
• Write a method getAltitutde() of the class Airplane. Test it on the variable `plane` in Test.java to make sure it works.
• What happens if you typecast Airplane to Vehicle and then call the method getAltitude() on the result? Write the answer on the Wiki page or in comments.
• In main create an array of 5 elements of class Vehicle. Do not initialize it at this point (it will contain nulls). Make sure your program compiles and runs.
• Create some new elements of class Vehicle, some new elements of class Car and some new elements of class Airplane. Which of these can you store in the array? Write your answer on the Wiki page or in comments.
• Store a mix of Cars, Vehicles, and Airplanes in the array. Make sure there are no nulls left in the array. Write a loop to go through the array and call a print() method on each element. Which print methods get called for each element? Write your answer on the Wiki page or in comments.
• Suppose you create a new Airplane object and assign the result to a variable of a class Vehicle:
NOTE: the following code has been corrected
``````
Vehicle v = new Airplane("Airbus",100,1000,3000);
``````
Can you call the Vehicle methods on this variable? Can you call the Airplane methods? Can you typecast v to Airplane?
• Create an array of Airplanes. Is it possible to store anything other than an airplane in the array? Can you write a loop that calls Vehicle methods on each element of the array? Can you call Airplane methods?
• Is it possible to typecast a Vehicle to a Car? A Car to a Vehicle? A Car to an Airplane? Write down what kinds of errors you get if it's not possible.

### 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:

• is it possible for a class to have two methods with the same name but with different number of parameters (for instance, one method takes one integer, and the other one takes two)? If yes, does the program call the right method?
• is it possible for a class to have two methods with the same name, the same number of parameters, but with different types of parameters (for instance, one method takes an integer, and the other takes a String or a boolean)? Do not use two types which are automatically converted to each other, such as int and double
• is it possible for a class to have two methods with the same name, the same number and type of parameters, but with different types of return values (for instance, one is void, the other one returns an integer). If it's possible, will the program call the right method?

### 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.