Sunteți pe pagina 1din 13

LAB 9: INHERITANCE, ABSTRACT AND PACKAGE

Learning Outcomes This Lab sheet encompasses of activities which 9A, 9B, 9C, 9D and 9E By the end of this laboratory session, you should be able to: 1. Create packages in Java program. 2. Apply keyword super in Java programs. 3. Implement overriding method in Java programs 4. Implement abstract classes in Java programs. Hardware/Software: Computer with JDK latest version Activity 9A
Activity Outcomes: Create packages in Java programs Procedures: Step1: Create a subdirectory test in the current directory. Step 2: Type below program using text editor (Note Pad). Step 3: Save your program as Class1.java in the test subdirectory. For example if d:\ is the current directory, then create d:\test and save ClassOne.java in it.

Step 4: Save and compile the program. Step 5: Open a new text editor (Note Pad). Step 6: Type the below code and save it as PackageOne.java in the current directory

Step 7: If d: is the directory in which the Packtest1.java is present and the user-defined package is in d:\pack1, then set the classpath as set CLASSPATH=%CLASSPATH%; D:\PACK1; in the current directory. Type the above command in the command prompt and execute it to set the path. Step 8: Compile and run the program. Step 9: Observe the output from the program.

Output

Activity 9B
Activity Outcomes: Apply keyword super in Java programs Procedures: Step1: Type below program using text editor (note Pad) Step 2: Save your program as SuperClass.java in the bin directory.

Step 3: Open a new text editor (Note Pad) Step 4: Type the code and save it as SubClass.java bin directory.

Output

Activity 9C
Activity Outcomes: Implement overriding method in Java programs. Procedures: Step1: Type below program using text editor (note Pad) Step 2: Save your program as SuperA.java in the bin directory.

Step 3: Open a new text editor (Note Pad) Step 4: Type the code and save it as SubClassB.java bin directory.

Step 5: Compile and run the program. Output

Activity 9D
Activity Outcomes: Implement abstract classes in Java programs. Procedures: Step1: Type below program using text editor (note Pad) that come with Windows Platform. Step 2: Type the below program using text editor.

Step 3: Save the above program as Figure.java in bin directory Step 4: Open a new text editor, type the below program and save it as Rectangle.java

Step 5: Open a notepad, type the below program and save it as Triangle.java in the same directory

Step 6: Open text editor and type the below program as a main program Step 7: Save the program as AbstractAreas.java Step 8: Compile and run the program and then see the result.

Output:

Activity 9E
Activity Outcomes: Implement polymorphism in Java programs. Procedures: Step1: Copy all the code in activity 9D in different file

Step 2: Adding another constructor that retrieve both parameter as integer and a method to calculate volume in Rectangle.class

Step 3: Do the same thing for the Triangle.class. Copy the triangle class and paste in different file. Create another constructor with retrieve int as for both parameters. B

Next, create a method to calculate Pythagoras in Figure class. Use the below formula: C2 = a2 + b2 After that create a method to calculate Volume of the rectangle in figure class.

Step 4: Type below code and determine the output

Step 5: Create a main class to test your program. Rewrite the below code and run it.

Output:

LAB EXERCISE 9: Inheritance, Abstract And Package


Instructions: Answer all questions. You are required to print the program codes and its output. In order to make sense of program listing you have written in the past it is a good idea to being each program with the following sections. /* Author: Program Title: Description: Date: Version Number: */ Comment your code! Trust me its worth the extra effort.

Indent your code to increase clarity

Questions:
1. Create package store. Write a class name store in that package. Think of the

attributes and methods should be for store as super class. Create a subclasses and store in packages for store such as music store, bike store and grocery store. Design any output that your think suitable for the classes.

2. Create an abstract Auto class with fields for the car make and price. Include get and set methods for these fields: the setPrice() method is abstract. Create two subclasses for individual automobile makers (for example, Ford and Chevy) and include appropriate setPrice() methods in each subclass. Finally, write a program that uses the Auto class and subclass to display information about different cars.

OUTPUT

CONCLUSION End of this lab, student should be able to know how to create packages in Java program and apply keyword super in Java programs. Besides that, student should know how to implement overriding method in Java programs and implement abstract classes in Java programs.

S-ar putea să vă placă și