Chapter 1 - Introduction - Class Schedule



Missouri State University Spring 2010 CSC 131-2

Lab 13, April 20, 2010

Name: ______________________

Note: Besides this paper copy, this lab is also available on eccentric and online at If you need help with any exercise, raise your hand.

Please remember to save your work, upload what you have done to your folder on eccentric server (You may create a subdirectory named lab13), and turn in this report to the lab’s instructor. This lab is due at the start of the next lab on Tuesday, April 27.

There are nine checking points in this lab.

Polymorphism

|1. Assuming the following classes have been defined: |W What is the output produced by each statement below? If |

|public class First { |the statement produces more than one line of output, |

|public void method2() { |indicate the line breaks with slashes as in "a/b/c". If |

|System.out.println("First2"); |the statement causes an error, write either "compiler |

|} |error" or "runtime error" as appropriate. |

| | |

|public void method3() { |Statement |

|method2(); | |

|} |var1.method2(); |

|} | |

| |var2.method2(); |

|public class Second extends First { | |

|public void method2() { |var3.method2(); |

|System.out.println("Second2"); | |

|} |var4.method2(); |

|} | |

| |var5.method2(); |

|public class Third extends Second { | |

|public void method1() { |var6.method2(); |

|System.out.println("Third1"); | |

|super.method2(); |var1.method3(); |

|} | |

| |var2.method3(); |

|public void method2() { | |

|System.out.println("Third2"); |var3.method3(); |

|} | |

|} |var4.method3(); |

| | |

|public class Fourth extends First { |var5.method3(); |

|public void method1() { | |

|System.out.println("Fourth1"); |var6.method3(); |

|} | |

| | |

|public void method2() { | |

|System.out.println("Fourth2"); | |

|} | |

|} | |

| | |

|A and that the following variables have been defined: | |

| | |

|First var1 = new Second(); | |

|First var2 = new Third(); | |

|First var3 = new Fourth(); | |

|Second var4 = new Third(); | |

|Object var5 = new Fourth(); | |

|Object var6 = new Second(); | |

|2. Assuming the following classes have been defined: |W What is the output produced by each statement below? If the |

|public class Harry { |statement produces more than one line of output, indicate the |

|public void method1() { |line breaks with slashes as in "a/b/c". If the statement causes |

|System.out.println("harry 1"); |an error, write either "compiler error" or "runtime error" as |

|} |appropriate. |

| | |

|public void method2() { |Statement |

|method1(); System.out.println("harry 2"); | |

|} |var1.method1(); |

|} | |

| |var2.method1(); |

|public class Larry extends Harry { | |

|public void method1() { |var3.method1(); |

|System.out.println("larry 1"); | |

|super.method1(); |var4.method1(); |

|} | |

|} |var5.method1(); |

| | |

|public class Mary extends Larry { |var6.method1(); |

|public void method2() { | |

|System.out.println("mary 2"); |var1.method2(); |

|} | |

| |var2.method2(); |

|public void method3() { | |

|super.method1(); |var3.method2(); |

|System.out.println("mary 3"); | |

|} |var4.method2(); |

|} | |

| |var5.method2(); |

|public class Jerry extends Mary { | |

|public void method2() { |var6.method2(); |

|super.method2(); System.out.println("jerry 2"); | |

|} |var3.method3(); |

|} | |

|A and that the following variables have been defined: |var5.method3(); |

|Harry var1 = new Harry(); | |

|Harry var2 = new Larry(); | |

|Larry var3 = new Jerry(); | |

|Mary var4 = new Mary(); | |

|Mary var5 = new Jerry(); | |

|Object var6 = new Larry(); | |

Inheritance and Polymorphism

Issues with People and Dogs

Remember to Save your changes and to upload to your eccentric\upload\CSC 131\002\abc123s folder after each step.

Open the project named as Mammals. Examine the Demo1 class in the same project. Predict the output when you compile and run this program. Now compile and run it. How did the hair color get assigned?

3 Explain what happens when you run the Demo1 class.

Edit the Demo2 class in the same project. Predict the output when you compile and run this program and then do so. Make sure you understand its behavior, and then change the statement

Human [] human = new Human[SIZE];

to

Mammal [] human = new Human[SIZE];

and try to compile this program. What happens? What is the problem?

Leaving this change in place, change the statement

System.out.println(human[i].getSSN());

to

System.out.println(human[i].getHairColor());

and try to compile the program. What happens? To help you understand this, draw a picture of the array called human and think about what the objects in this array are declared to be.

4 Explain what happens when you make the above changes to Demo2.

Examine class Demo3. What do you think will happen when you compile and run it? Do so and run it several times. Is this an example of polymorphism?

5 Explain what happens when you run this program and whether or not it is an example of polymorphism.

Finally, examine class Demo4. What do you think will happen when you compile and run it? Do so and run it several times. Is this an example of polymorphism?

6 Explain what happens when you run this program and whether or not it is an example of polymorphism.

Inheritance Classes

7. Consider the task of representing types of tickets to school campus events. Each ticket has a unique number and a price. There are three types of tickets: walk-up tickets, advance tickets, and student advance tickets. Figure 1 illustrates the types;

• Walk-up tickets are purchased the day of the event and cost $50.

• Advance tickets purchased 10 or more days before the event cast $30, and advance tickets purchase fewer than 10 days before the event cost $40.

• Student advance tickets are sold at half the price of normal advance tickets: ones 10 days early cost $15, and fewer than 10 days early cost $20.

[pic]

Figure1: Inheritance hierarchy

Open project TestTickets, which implements one class called Ticket. Class Ticket serve as a superclass for all three types of tickets. It defines all common operations in the class. It has the following operations:

• The ability to construct a ticket by number

• The ability to ask for a ticket’s price.

• The ability to println a ticket object as a String. An example of string would be “Number: 17, Price: 50”.

Checking Point 7: implement a class called WalkupTicket to represent a walk-up event ticket. Walk-up tickets are also constructed by number, and they have a price of $50.

Checking Point 8: implement a class called AdvancedTicket to represent tickets purchased in advance. An advance ticket is constructed with a ticket number and with how many days in advance the ticket was purchased. Advance tickets purchased 10 or more days before the event cost $30, and advance tickets purchased fewer than 10 days before the event cost $40.

Checking Point 9: implement a class called StudentAdvancedTicket to represent tickets purchased in advance by students. A student advance ticket is constructed with a ticket number and with how many days in advance the ticket was purchased. Student advance tickets purchased 10 or more days before the event cost $15, and student advance tickets purchased fewer than 10 days before the event cost $20. When a student advance ticket is printed, the string should mention that the student must show his or her student ID(for example, “Number: 17, Price:15 (ID required) ”

You might use Client class to test your class definition. When you are ready, please save your program and upload it to your eccentric\upload\CSC 131\002\abc123s folder.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download