Date post: | 27-Dec-2015 |
Category: |
Documents |
Upload: | srinivasa-helavar |
View: | 4 times |
Download: | 0 times |
Lab 5
AdvancedClass Features
Objectives
Upon completion of this lab, you should be able to:
l Apply static class members to resolve a design decision.
l Create abstract classes and interfaces, and explore the polymorphicproperties of these types of components
Lab 5-1Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 1)
Lab
Exercise 1: Applying Static Members to a Design (Level 1)
In this exercise, you apply static class members to resolve a design decision. TheBanking Project currently uses a concrete class to represent the concept of a bank,which contains the set of customers for the bank. The project team has decidedthat this is a risky design because it would be possible to instantiate multipleBank objects each with the potential to contain different sets of customers.
The design team has decided to make the Bank class a utility class. A utility classis one that is not instantiated and all of its members are static. Figure 5-1 showsthe new design for the Bank class. Your job is to program these changes to theBank class and to all of the classes that use the Bank class.
Figure 5-1 UML Diagram of the Bank Utility Class
This exercise contains the following sections:
l Task 1 – Modifying the Bank Class
l Task 2 – Modifying the CustomerReport Class
l Task 3 – Deleting the Current TestReport Class
l Task 4 – Copying the TestReport Class
l Task 5 – Compiling the TestReport Class
l Task 6 – Running the TestReport Program
com.mybank.domain
-customers : Customer[]
+addCustomer(f:String, l:String)
+getCustomer(index:int):Customer
-numberOfCustomers : int
+getNumOfCustomers():int
-Bank()
0..*
Bank
«constructors»
«methods»
serves
Customer Account1has-a
5-2Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 1)
Preparation
There is no preparation required for this exercise.
Tool Reference – Tool references used in this exercise:
l Java Development: Java Application Projects: Opening Projects
l Java Development: Other Files: Deleting Files
l Java Development: Java Classes: Modifying Java Classes: Copying JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Compiling JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Executing JavaPrograms
For this exercise, you work in the BankPrj project in the projects directory.
123
Demonstration – The demonstration for this exercise can be found in thedemos/07_class2/exercise1 directory.
Task 1 – Modifying the BankClass
In this task, you open the BankPrj project in the projects directory, andmodify the Bank class in the com.mybank.domain source package of theBankPrj project. All the members (both instance variables and methods) shouldbe changed to static, as shown in Figure 5-1 on page Lab 5-2. Also, move theoriginal variable initialization code from the constructor to either a static block oron the static variable declarations.
Task 2 – Modifying the CustomerReportClass
In this task, you modify the CustomerReport class source file in thecom.mybank.report source package of the BankPrj project. The updatedCustomerReport class uses the Bank class as a utility class.
Advanced Class Features Lab 5-3Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 1)
Lab
Task 3 – Deleting the Current TestReportClass
In this task, you delete the current TestReport class in thecom.mybank.test source package of the BankPrj project.
Task 4 – Copying the TestReportClass
In this task, you copy the TestReport.java file from theresources/07_class2 directory to the com.mybank.test source package ofthe BankPrj project.
Task 5 – Compiling the TestReportClass
In this task, you compile the TestReport class.
Task 6 – Running the TestReportProgram
In this task, you run the TestReport program. The output should be similar tothe output from previous tests as shown in Exercise 2, Task 5 of Lab 6 (Level 1).
5-4Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 2)
Exercise 1: Applying Static Members to a Design (Level 2)
In this exercise, you apply static class members to resolve a design decision. TheBanking Project currently uses a concrete class to represent the concept of a bank,which contains the set of customers for the bank. The project team has decidedthat this is a risky design because it would be possible to instantiate multipleBank objects each with the potential to contain different sets of customers.
The design team has decided to make the Bank class a utility class. A utility classis one that is not instantiated and all of its members are static. Figure 5-1 onpage Lab 5-2 shows the new design for the Bank class. Your job is to programthese changes to the Bank class and to all of the classes that use the Bank class.
This exercise contains the following sections:
l Task 1 – Modifying the Bank Class
l Task 2 – Modifying the CustomerReport Class
l Task 3 – Deleting the Current TestReport Class
l Task 4 – Copying the TestReport Class
l Task 5 – Compiling the TestReport Class
l Task 6 – Running the TestReport Program
Preparation
There is no preparation required for this exercise.
Tool Reference – Tool references used in this exercise:
l Java Development: Java Application Projects: Opening Projects
l Java Development: Other Files: Deleting Files
l Java Development: Java Classes: Modifying Java Classes: Copying JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Compiling JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Executing JavaPrograms
For this exercise, you work in the BankPrj project in the projects directory.
Advanced Class Features Lab 5-5Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 2)
Lab
123
Demonstration – The demonstration for this exercise can be found in thedemos/07_class2/exercise1 directory.
Task 1 – Modifying the BankClass
In this task, you modify the Bank class in the com.mybank.domain sourcepackage of the BankPrj project. All the members (both instance variables andmethods) should be changed to static, as shown in Figure 5-1 on page Lab 5-2.
Complete the following steps:
1. Open the BankPrj project in the projects directory if you have closedit.
2. Open the Bank class in the com.mybank.domain source package of theBankPrj project.
3. Change all instance variables to static.
4. Move the original variable initialization code from the constructor to eithera static block or on the static variable declarations.
5. Change the constructor to be private and remove the body of theconstructor.
6. Change all methods to static.
Task 2 – Modifying the CustomerReportClass
In this task, you modify the CustomerReport class source file in thecom.mybank.report source package of the BankPrj project. The updatedCustomerReport class uses the Bank class as a utility class.
Complete the following steps:
1. Remove the bank instance variable and the getBank and setBankmethods.
2. Modify the generateReport method to use the static methods from thenew Bank utility class design.
5-6Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 2)
Task 3 – Deleting the Current TestReportClass
In this task, you delete the current TestReport class in thecom.mybank.test source package of the BankPrj project.
Task 4 – Copying the TestReportClass
In this task, you copy the TestReport.java file from theresources/07_class2 directory to the com.mybank.test source package ofthe BankPrj project.
Task 5 – Compiling the TestReportClass
In this task, you compile the TestReport class.
Task 6 – Running the TestReportProgram
In this task, you run the TestReport program. The output should be similar tothe output from previous tests as shown in Exercise 2, Task 5 of Lab 6 (Level 1).
Advanced Class Features Lab 5-7Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 3)
Lab
Exercise 1: Applying Static Members to a Design (Level 3)
In this exercise, you apply static class members to resolve a design decision. TheBanking Project currently uses a concrete class to represent the concept of a bank,which contains the set of customers for the bank. The project team has decidedthat this is a risky design because it would be possible to instantiate multipleBank objects each with the potential to contain different sets of customers.
The design team has decided to make the Bank class a utility class. A utility classis one that is not instantiated and all of its members are static. Figure 5-1 onpage Lab 5-2 shows the new design for the Bank class. Your job is to programthese changes to the Bank class and to all of the classes that use the Bank class.
This exercise contains the following sections:
l Task 1 – Modifying the Bank Class
l Task 2 – Modifying the CustomerReport Class
l Task 3 – Deleting the Current TestReport Class
l Task 4 – Copying the TestReport Class
l Task 5 – Compiling the TestReport Class
l Task 6 – Running the TestReport Program
Preparation
There is no preparation required for this exercise.
Tool Reference – Tool references used in this exercise:
l Java Development: Java Application Projects: Opening Projects
l Java Development: Other Files: Deleting Files
l Java Development: Java Classes: Modifying Java Classes: Copying JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Compiling JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Executing JavaPrograms
For this exercise, you work in the BankPrj project in the projects directory.
5-8Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 3)
123
Demonstration – The demonstration for this exercise can be found in thedemos/07_class2/exercise1 directory.
Task 1 – Modifying the BankClass
In this task, you modify the Bank class in the com.mybank.domain sourcepackage of the BankPrj project. All the members (both instance variables andmethods) should be changed to static, as shown in Figure 5-1 on page Lab 5-2.
Complete the following steps:
1. Open the BankPrj project in the projects directory if you have closedit.
2. Open the Bank class in the com.mybank.domain source package of theBankPrj project.
3. Change all instance variables to static.
private static Customer[] customers; private static int numberOfCustomers;
4. Move the original variable initialization code from the constructor to eithera static block or on the static variable declarations.
static { customers = new Customer[10]; numberOfCustomers = 0; }
5. Change the constructor to be private and remove the body of theconstructor.
private Bank() { // this constructor should never be called }
6. Change all methods to static.
public static void addCustomer(String f, String l) { int i = numberOfCustomers++; customers[i] = new Customer(f, l); } public static int getNumOfCustomers() { return numberOfCustomers; } public static Customer getCustomer(int customer_index) { return customers[customer_index]; }
Advanced Class Features Lab 5-9Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 3)
Lab
Task 2 – Modifying the CustomerReportClass
In this task, you modify the CustomerReport class source file in thecom.mybank.report source package of the BankPrj project. The updatedCustomerReport class uses the Bank class as a utility class.
Complete the following steps:
1. Remove the bank instance variable and the getBank and setBankmethods.
2. Modify the generateReport method to use the static methods from thenew Bank utility class design.
public void generateReport() {
// Print report header System.out.println("CUSTOMERS REPORT"); System.out.println("================");
// For each customer... for ( int cust_idx = 0; cust_idx < Bank.getNumOfCustomers(); cust_idx++ ) { Customer customer = Bank.getCustomer(cust_idx);
// and so on...}
}
Task 3 – Deleting the Current TestReportClass
In this task, you delete the current TestReport class in thecom.mybank.test source package of the BankPrj project.
Task 4 – Copying the TestReportClass
In this task, you copy the TestReport.java file from theresources/07_class2 directory to the com.mybank.test source package ofthe BankPrj project.
5-10Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 1: Applying Static Members to a Design (Level 3)
Task 5 – Compiling the TestReportClass
In this task, you compile the TestReport class.
Task 6 – Running the TestReportProgram
In this task, you run the TestReport program. The output should be similar tothe output from previous tests as shown in Exercise 2, Task 5 of Lab 6 (Level 1)..
Advanced Class Features Lab 5-11Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 1)
Lab
Exercise 2: Working With Interfaces and Abstract Classes(Level 1)
In this exercise, you create abstract classes and interfaces, and explore thepolymorphic properties of these types of components. You create a hierarchy ofanimals that is rooted in an abstract class Animal. Several of the animal classesimplement an interface called Pet. Figure 5-2 shows a UML class diagram of theanimal classes that you create.
Figure 5-2 Animal and Pet Hierarchy
This exercise contains the following sections:
l Task 1 – Creating the Pet Interface
l Task 2 – Creating the Animal Classes
l Task 3 – Creating the TestAnimals Class
l Task 4 – Compiling the TestAnimals Class
l Task 5 – Running the TestAnimals Program
Animal
+walk():void
+eat():void
«constructors»
«methods»
#Animal(legs:int)
#legs : int
Fish
+getName():String
«constructors»
«methods»
+Fish()
-name : String
Cat
«constructors»+Cat(n:String)
-name : String
Pet
+getName():String
+setName(n:String):void
+play()
«interface»
+setName(n:String):void
+play():void
{abstract}
+Cat()
+walk():void
+eat():void
+getName():String
«methods»
+setName(n:String):void
+play():void
+eat():void
Spider
«constructors»+Spider()
«methods»+eat():void
5-12Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 1)
Preparation
There is no preparation required for this exercise.
Tool Reference – Tool references used in this exercise:
l Java Development: Java Application Projects: Opening Projects
l Java Development: Java Classes: Creating Java Classes
l Java Development: Java Classes: Modifying Java Classes: Compiling JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Executing JavaPrograms
For this exercise, you work in the InterfaceProject project in theexercises/07_class2/exercise2 directory.
123
Demonstration – The demonstration for this exercise can be found in thedemos/07_class2/exercise2 directory.
Task 1 – Creating the Pet Interface
Complete the following steps:
1. Open the InterfaceProject project in theexercises/07_class2/exercise2 directory.
2. Create the Pet interface with the following characteristics:
Class Name: Pet
Project: InterfaceProject
Location: Source Packages
Package: default package
The Pet interface must satisfy the UML diagram in Figure 5-1 on page Lab 5-2.
Advanced Class Features Lab 5-13Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 1)
Lab
Task 2 – Creating the Animal Classes
Complete the following steps:
1. Create the Animal class with the following characteristics:
Class Name: Animal
Project: InterfaceProject
Location: Source Packages
Package: default package
The Animal class must satisfy the UML diagram in Figure 5-1 on page Lab 5-2.The action methods (walk and eat) print a statement to standard output thatreflects the animal. For example, the walk method for the Animal class mightsay something similar to This animal walks on 4 legs, where 4 is thevalue of the legs instance variable.
2. Create the Spider, Cat, and Fish classes in the Source Packages of theInterfaceProject project to satisfy the UML diagram in Figure 5-1 onpage Lab 5-2.
Task 3 – Creating the TestAnimalsClass
Complete the following steps:
1. In this task, you create the TestAnimals class with the followingcharacteristics:
Class Name: TestAnimals
Project: InterfaceProject
Location: Source Packages
Package: default package
2. Add the main method to create and manipulate instances of the classes youcreated previously.
Start with:
Fish d = new Fish();Cat c = new Cat("Fluffy");Animal a = new Fish();Animal e = new Spider();Pet p = new Cat();
5-14Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 1)
Experiment by:
l Calling the methods in each object
l Casting objects
l Using polymorphism
l Using super to call superclass methods
Task 4 – Compiling the TestAnimalsClass
In this task, you compile the TestAnimals class.
Task 5 – Running the TestAnimalsProgram
In this task, you run the TestAnimals program.
Advanced Class Features Lab 5-15Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 2)
Lab
Exercise 2: Working With Interfaces and Abstract Classes(Level 2)
In this exercise, you create abstract classes and interfaces, and explore thepolymorphic properties of these types of components. You create a hierarchy ofanimals that is rooted in an abstract class Animal. Several of the animal classeswill implement an interface called Pet. Use Figure 5-2 on page Lab 5-12 as areference. You experiment with variations of these animals, their methods, andpolymorphism.
This exercise contains the following sections:
l Task 1 – Creating the Pet Interface
l Task 2 – Creating the Animal Classes
l Task 3 – Creating the TestAnimals Class
l Task 4 – Compiling the TestAnimals Class
l Task 5 – Running the TestAnimals Program
Preparation
There is no preparation required for this exercise.
Tool Reference – Tool references used in this exercise
l Java Development: Java Application Projects: Opening Projects
l Java Development: Java Classes: Creating Java Classes
l Java Development: Java Classes: Modifying Java Classes: Compiling JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Executing JavaPrograms
For this exercise, you work in the InterfaceProject project in theexercises/07_class2/exercise2 directory.
123
Demonstration – The demonstration for this exercise can be found in thedemos/07_class2/exercise2 directory.
5-16Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 2)
Task 1 – Creating the Pet Interface
Complete the following steps:
1. Open the InterfaceProject project in theexercises/07_class2/exercise2 directory.
2. Create the Pet interface with the following characteristics:
Class Name: Pet
Project: InterfaceProject
Location: Source Packages
Package: default package
The Pet interface must satisfy the UML diagram in Figure 5-1 on page Lab 5-2.
Task 2 – Creating the Animal Classes
Complete the following steps:
1. Create the Animal class.
a. Create the Animal class with the following characteristics:
Class Name: Animal
Project: InterfaceProject
Location: Source Packages
Package: default package
b. Declare a protected integer instance variable called legs, whichrecords the number of legs for this animal.
c. Define a protected constructor that initializes the legs instancevariable.
d. Declare an abstract method eat.
e. Declare a concrete method walk that prints out something about howthe animals walks (include the number of legs).
Advanced Class Features Lab 5-17Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 2)
Lab
2. Create the Spider class.
a. Create the Spider class with the following characteristics:
Class Name: Spider
Project: InterfaceProject
Extends: Animal
Location: Source Packages
Package: default package
b. Define a no-arg constructor that calls the superclass constructor tospecify that all spiders have eight legs.
c. Implement the eat method.
3. Create the Cat class.
a. Create the Cat class with the following characteristics:
Class Name: Cat
Project: InterfaceProject
Extends: Animal
Implements: Pet
Location: Source Packages
Package: default package
b. This class must include a String instance variable to store the nameof the pet.
c. Define a constructor that takes one String parameter that specifiesthe cat’s name. This constructor must also call the superclassconstructor to specify that all cats have four legs.
d. Define another constructor that takes no parameters. Have thisconstructor call the previous constructor (using the this keyword)and pass an empty string as the argument.
e. Implement the Pet interface methods.
f. Implement the eat method.
4. Create the Fish class.
a. Create the Fish class with the following characteristics:
Class Name: Fish
Project: InterfaceProject
Extends: Animal
5-18Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 2)
Implements: Pet
Location: Source Packages
Package: default package
b. This class must include a String instance variable to store the nameof the pet.
c. Define a no-arg constructor that calls the superclass constructor tospecify that fish do not have legs.
d. Implement the Pet interface methods.
e. Override the walk method. This method should call the super methodand they print a message that fish do not walk
f. Implement the eat method.
Task 3 – Creating the TestAnimalsClass
Complete the following steps:
1. In this task, you create the TestAnimals class with the followingcharacteristics:
Class Name: TestAnimals
Project: InterfaceProject
Location: Source Packages
Package: default package
2. .Add the mainmethod to create and manipulate instances of the classes youcreated previously.
Start with:
Fish d = new Fish();Cat c = new Cat("Fluffy");Animal a = new Fish();Animal e = new Spider();Pet p = new Cat();
Experiment by:
l Calling the methods in each object
l Casting objects
l Using polymorphism
l Using super to call superclass methods
Advanced Class Features Lab 5-19Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 2)
Lab
Task 4 – Compiling the TestAnimalsClass
In this task, you compile the TestAnimals class.
Task 5 – Running the TestAnimalsProgram
In this task, you run the TestAnimals program.
5-20Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 3)
Exercise 2: Working With Interfaces and Abstract Classes(Level 3)
In this exercise, you create a hierarchy of animals that is rooted in an abstractclass Animal. Several of the animal classes implement an interface called Pet.Use Figure 5-2 on page Lab 5-12 as a reference. You experiment with variationsof these animals, their methods, and polymorphism.
This exercise contains the following sections:
l Task 1 – Creating the Pet Interface
l Task 2 – Creating the Animal Classes
l Task 3 – Creating the TestAnimals Class
l Task 4 – Compiling the TestAnimals Class
l Task 5 – Running the TestAnimals Program
Preparation
There is no preparation required for this exercise.
Tool Reference – Tool references used in this exercise:
l Java Development: Java Application Projects: Opening Projects
l Java Development: Java Classes: Creating Java Classes
l Java Development: Java Classes: Modifying Java Classes: Compiling JavaClasses
l Java Development: Java Classes: Modifying Java Classes: Executing JavaPrograms
For this exercise, you work in the InterfaceProject project in theexercises/07_class2/exercise2 directory.
123
Demonstration – The demonstration for this exercise can be found in thedemos/07_class2/exercise2 directory.
Advanced Class Features Lab 5-21Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 3)
Lab
Task 1 – Creating the Pet Interface
Complete the following steps:
1. Open the InterfaceProject project in theexercises/07_class2/exercise2 directory.
2. Create the Pet interface with the following characteristics:
Class Name: Pet
Project: InterfaceProject
Location: Source Packages
Package: default package
The Pet interface must satisfy the UML diagram in Figure 5-1 on page Lab 5-2.
public interface Pet {public String getName();public void setName(String n);public void play();
}
Task 2 – Creating the Animal Classes
Complete the following steps:
1. Create the Animal class.
a. Create the Animal class with the following characteristics:
Class Name: Animal
Project: InterfaceProject
Location: Source Packages
Package: default package
a. Declare a protected integer instance variable called legs, whichrecords the number of legs for this animal.
protected int legs;
b. Define a protected constructor that initializes the legs instancevariable.
protected Animal(int legs) {this.legs = legs;
}
5-22Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 3)
c. Declare an abstract method eat.
public abstract void eat();
d. Declare a concrete method walk that prints out something about howthe animals walks (include the number of legs).
public void walk() {System.out.println("This animal walks on " + legs + " legs.");
}
2. Create the Spider class.
a. Create the Spider class with the following characteristics:
Class Name: Spider
Project: InterfaceProject
Extends: Animal
Location: Source Packages
Package: default package
a. The Spider class extends the Animal class.
public class Spider extends Animal {// more code here
}
b. Define a no-arg constructor that calls the superclass constructor tospecify that all spiders have eight legs.
public Spider() {super(8);
}
c. Implement the eat method.
public void eat() {System.out.println("The spider eats a fly.");
}
3. Create the Cat class.
a. Create the Cat class with the following characteristics:
Class Name: Cat
Project: InterfaceProject
Extends: Animal
Implements: Pet
Location: Source Packages
Package: default package
Advanced Class Features Lab 5-23Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 3)
Lab
b. This class must include a String instance variable to store the nameof the pet.
private String name;
c. Define a constructor that takes one String parameter that specifiesthe cat’s name. This constructor must also call the superclassconstructor to specify that all cats have four legs.
public Cat(String n) {super(4);name = n;
}
d. Define another constructor that takes no parameters. Have thisconstructor call the previous constructor (using the this keyword)and pass an empty string as the argument.
public Cat() {this("");
}
e. Implement the Pet interface methods.
public String getName() {return name;
}public void setName(String n) {name = n;
}public void play() {System.out.println(name + " likes to play with string.");
}
f. Implement the eat method.
public void eat() { System.out.println("Cats like to eat spiders and mice.");}
4. Create the Fish class.
a. Create the Fish class with the following characteristics:
Class Name: Fish
Project: InterfaceProject
Extends: Animal
Implements: Pet
Location: Source Packages
Package: default package
5-24Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 3)
b. This class must include a String instance variable to store the nameof the pet.
private String name;
c. Define a no-arg constructor that calls the superclass constructor tospecify that fish do not have legs.
public Fish() { super(0); // this line must be here }
d. Implement the Pet interface methods.
public void setName(String name) { this.name = name; } public String getName() { return name; } public void play() { System.out.println("Fish swim in their tanks all day."); }
e. Override the walk method. This method should call the super methodand they print a message that fish do not walk
public void walk() { super.walk(); System.out.println("Fish, of course, can’t walk; they swim."); }
f. Implement the eat method.
public void eat() { System.out.println("Fish eat pond scum."); }
Task 3 – Creating the TestAnimalsClass
Complete the following steps:
1. In this task, you create the TestAnimals class with the followingcharacteristics:
Class Name: TestAnimals
Project: InterfaceProject
Location: Source Packages
Package: default package
Advanced Class Features Lab 5-25Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise 2: Working With Interfaces and Abstract Classes (Level 3)
Lab
2. Add the main method to create and manipulate instances of the classes youcreated previously.
Here is a sample test program:
public class TestAnimals { public static void main(String[] args) { Fish f = new Fish(); Cat c = new Cat("Fluffy"); Animal a = new Fish(); Animal e = new Spider(); Pet p = new Cat();
// Demonstrate different implementations of an interface f.play(); c.play();
// Demonstract virtual method invocation e.eat(); e.walk();
// Demonstrate calling super methods a.walk(); }}
Task 4 – Compiling the TestAnimalsClass
In this task, you compile the TestAnimals class.
Task 5 – Running the TestAnimalsProgram
In this task, you run the TestAnimals program.
5-26Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise Summary
Exercise Summary
?!
Discussion – Take a few minutes to identify what experiences, issues, ordiscoveries you had during the lab exercise.
l Experiences
l Interpretations
l Conclusions
l Applications
_
Advanced Class Features Lab 5-27Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A
Exercise Summary
Lab
5-28Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision A