Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 27 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
27
Dung lượng
1,14 MB
Nội dung
Lab Guide Lab version:1.0.0 Last updated:1/4/2014 Page Overview In OOP, we often organize classes in hierarchy to avoid duplication and reduce redundancy The classes in the lower hierarchy inherit all the variables (static attributes) and methods (dynamic behaviors) from the higher hierarchies A class in the lower hierarchy is called a subclass (or derived, child, extended class) A class in the upper hierarchy is called a superclass (or base, parent class) In this lab, you will write simple applications to practice some basic feature of OOP such as: Inherit, Polymorphism, etc Objectives Once you have completed this lab, you will understand: How to create a class inherit from other class How to create a class implement from a Interface Exercises This Hands-On Lab is comprised by the following exercises: Subclassing and Inheritance Polymorphism Abstract class Inteface Estimated time to complete this lab: hours Page Index EXERCISE 1: SUBCLASSING AND INHERITANCE Task - Create Circle class Task - Write code inside Circle.java file Task - Created Cylinder class extended Circle class Task - Write code inside Cylinder.java file Task - Create a class to test above classes Task - Write code inside TestCylinder.java file Task - Execute your program EXERCISE 2: SUBCLASSING AND INHERITANCE Task - Create Subclass Project Task - Create Employee Class Task 10 - Write code in Employee.java 10 Task 11 - Create SalaryEmployee Class 10 Task 12 - Write code in SalaryEmployee.java 10 Task 13 - Create WageEmployee Class 11 Task 14 - Write code in WageEmployee.java 11 Task 15 - Write a Program class to Show Employee’s salary 12 Task 16 - Execute your program 13 EXERCISE 3: POLYMORPHISM 13 Task - Create Shape Class 14 Task - Write code in Shape.java 14 Task - Create Rectangle Class 14 Task - Write code in Rectangle.java 15 Task - Create Triangle Class 15 Page Task - Write code in Triangle.java 16 Task - Write a TestShape class 16 Task - Execute your program and see the result 17 EXERCISE 4: ABSTRACT 18 Task - Create Shape class (abstract class) 18 Task - Write code in Shape.java 18 Task - Create Circle class 19 Task - Write code in Circle.java 19 Task - Create Rectangle class 20 Task - Write code in Rectangle.java 20 Task - Create Square class 21 Task - Write code in Square.java 21 Task - Test program 22 EXERCISE 5: INTERFACE 23 Task - Create Movable Interface 23 Task - Create MovablePoint Class implements Movable Interface 24 Task - Write code in MoveablePoint.java 25 Task - Create TestMovable Class to test 26 Task - Execute your program 26 Page Next Step Exercise 1: Subclassing and Inheritance In this exercise you will write a program to test how to use class and subclass In this exercise you will create a base named Circle and another class named Cylinder Inherited Circle class, after that, write a small program to test these class The diagram of your proram will be: Task - Create Circle class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Circle " Click "Finish" Task - Write code inside Circle.java file public class Circle { private double radius; private String color; // 1st constructor, which sets both radius and color to default public Circle() { Page radius = 1.0; color = "red"; } // 2nd constructor with given radius, but color default public Circle(double r) { radius = r; color = "red"; } // A public method for retrieving the radius public double getRadius() { return radius; } // A public method for computing the area of circle public double getArea() { return radius*radius*Math.PI; } } Task - Created Cylinder class extended Circle class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter "Cylinder " Click "Finish" Task - Write code inside Cylinder.java file public class Cylinder extends Circle { private double height; // Constructor with default color, radius and height public Cylinder() { super(); // call superclass no-arg constructor Circle() height = 1.0; } // Constructor with default radius, color but given height public Cylinder(double height) { super(); // call superclass no-arg constructor Circle() this.height = height; } // Constructor with default color, but given radius, height Page public Cylinder(double radius, double height) { super(radius); // call superclass constructor Circle(r) this.height = height; } // A public method for retrieving the height public double getHeight() { return height; } // A public method for computing the volume of cylinder // use superclass method getArea() to get the base area public double getVolume() { return getArea()*height; } } Task - Create a class to test above classes In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " TestCylinder " Click "Finish" Task - Write code inside TestCylinder.java file public class TestCylinder { // save as "TestCylinder.java" public static void main (String[] args) { // Declare and allocate a new instance of cylinder // with default color, radius, and height Cylinder c1 = new Cylinder(); System.out.println("Cylinder:" + " radius=" + c1.getRadius() + " height=" + c1.getHeight() + " base area=" + c1.getArea() + " volume=" + c1.getVolume()); // Declare and allocate a new instance of cylinder // specifying height, with default color and radius Cylinder c2 = new Cylinder(10.0); System.out.println("Cylinder:" + " radius=" + c2.getRadius() + " height=" + c2.getHeight() + " base area=" + c2.getArea() Page + " volume=" + c2.getVolume()); // Declare and allocate a new instance of cylinder // specifying radius and height, with default color Cylinder c3 = new Cylinder(2.0, 10.0); System.out.println("Cylinder:" + " radius=" + c3.getRadius() + " height=" + c3.getHeight() + " base area=" + c3.getArea() + " volume=" + c3.getVolume()); } } Task - Execute your program To run the program, right-click anywhere on the source file " TestCylinder.java" (or from the "Run" menu) ⇒ Choose "Run As" ⇒ "Java Application" The output appears on the "Console" panel Exercise 2: Subclassing and Inheritance Class diagram of the program: Page Task - Create Subclass Project Choose "File" menu ⇒ "New" ⇒ "Java project" The "New Java Project" dialog pops up In the "Project name" field, enter "Subclass" Check "Use default location" In the "JRE" box, select your JDK version installed on your system Click "Finish" Task - Create Employee Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Employee " Click "Finish" Page Task - Write code in Employee.java Create Constructors public Employee() { } public Employee(String name) { empName = name; } Instance variable to store the employee's name String empName; Method to display the name void displayDetails() { System.out.printf("Employee Name: %s", empName); } Task - Create SalaryEmployee Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter "SalaryEmployee " In SupperClass field, enter “Employee” Click "Finish" See the code what IDE generate: public class SalaryEmployee extends Employee { } Task - Write code in SalaryEmployee.java Create Constructors public SalaryEmployee() { } Page 10 Task - Execute your program To run the program, right-click anywhere on the source file "Program.java" (or from the "Run" menu) ⇒ Choose "Run As" ⇒ "Java Application" The output appears on the "Console" panel Exercise 3: Polymorphism In this exercise, you will define many kinds of shapes, such as triangle, rectangle and so on You will design a superclass called Shape, which defines the public interface (or behaviours) of all the shapes The diagram class are shown below: Page 13 Task - Create Shape Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Shape " Click "Finish" Task - Write code in Shape.java // Instance variable private String color; // Constructor public Shape (String color) { this.color = color; } public String toString() { return "Shape of color=\"" + color + "\""; } // All shapes must has a method called getArea() public double getArea() { System.err.println("Shape unknown! Cannot compute area!"); return 0; // Need a return to compile the program } Task - Create Rectangle Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Rectangle" In SupperClass field, enter “Shape” Click "Finish" See the code what IDE generate: public class Rectangle extends Shape { Page 14 } Task - Write code in Rectangle.java Instance variables private int length; private int width; Constructor public Rectangle(String color, int length, int width) { super(color); this.length = length; this.width = width; } Two override methods @Override public String toString() { return "Rectangle of length=" + length + " and width=" + width + ", subclass of " + super.toString(); } @Override public double getArea() { return length*width; } Task - Create Triangle Class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Triangle " In SupperClass field, enter “Shape” Click "Finish" See the code what IDE generate: public class Triangle extends Shape { Page 15 } Task - Write code in Triangle.java Instance variables private int base; private int height; Constructor public Triangle(String color, int base, int height) { super(color); this.base = base; this.height = height; } Two override methods @Override public String toString() { return "Triangle of base=" + base + " and height=" + height + ", subclass of " + super.toString(); } @Override public double getArea() { return 0.5*base*height; } Task - Write a TestShape class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " TestShape" Check "public static void main(String[] args)" box Click "Finish" Write code for main() method: public static void main(String[] args) { Shape s1 = new Rectangle("red", 4, 5); System.out.println(s1); Page 16 System.out.println("Area is " + s1.getArea()); Shape s2 = new Triangle("blue", 4, 5); System.out.println(s2); System.out.println("Area is " + s2.getArea()); Shape s3 = new Shape("green"); System.out.println(s3); System.out.println("Area is " + s3.getArea()); } Task - Execute your program and see the result To run the program, right-click anywhere on the source file "TestShape.java" (or from the "Run" menu) ⇒ Choose "Run As" ⇒ "Java Application" The output appears on the "Console" panel Page 17 Exercise 4: Abstract Task - Create Shape class (abstract class) In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Shape " Click "Finish" Task - Write code in Shape.java public abstract class Shape { private String color; private boolean filled; Shape() { Page 18 } Shape(String color,boolean filled) { … } String getColor() { … } void setColor(String color) { … } boolean isFilled() { … } void setFilled(boolean filled) { … } abstract double getArea(); abstract double getPerimeter(); public abstract String toString(); } Task - Create Circle class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Circle" Click "Finish" Task - Write code in Circle.java public class Circle extends Shape { private double radius; Circle() { } Circle(double radius) { … } Circle(double radius,String color,boolean filled) { … } Page 19 public double getRadius() { … } void setRadius(double radius) { … } double getArea() { … } double getPerimeter() { … } public String toString() { … } } Task - Create Rectangle class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Rectangle" Click "Finish" Task - Write code in Rectangle.java public class Rectangle extends Shape { private double width; private double length; Rectangle() { } Rectangle(double width,double length) { … } Rectangle(double width,double length,String color,boolean filled) { … } double getWidth() Page 20 { … } void setWidth(double width) { … } double getLength() { … } void setLength(double length) { … } double getArea() { … } double getPerimeter() { … } public String toString() { … } } Task - Create Square class In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter " Square" Click "Finish" Task - Write code in Square.java public class Square extends Rectangle { … } Page 21 Task - Test program Using below code to test all class created in above code: In these code there are some error, explain, fix it and run the program public class TestAllShape { public static void main(String[] args) { Shape s1 = new Circle(5.5, "RED", false); System.out.println(s1); System.out.println(s1.getArea()); System.out.println(s1.getPerimeter()); System.out.println(s1.getColor()); System.out.println(s1.isFilled()); System.out.println(s1.getRadius()); Circle c1 = (Circle)s1; System.out.println(c1); System.out.println(c1.getArea()); System.out.println(c1.getPerimeter()); System.out.println(c1.getColor()); System.out.println(c1.isFilled()); System.out.println(c1.getRadius()); Shape s2 = new Shape(); Shape s3 = new Rectangle(1.0, 2.0, "RED", false); System.out.println(s3); System.out.println(s3.getArea()); System.out.println(s3.getPerimeter()); System.out.println(s3.getColor()); System.out.println(s3.getLength()); Rectangle r1 = (Rectangle)s3; // downcast System.out.println(r1); System.out.println(r1.getArea()); System.out.println(r1.getColor()); System.out.println(r1.getLength()); Shape s4 = new Square(6.6); // Upcast System.out.println(s4); System.out.println(s4.getArea()); System.out.println(s4.getColor()); System.out.println(s4.getSide()); Rectangle r2 = (Rectangle)s4; System.out.println(r2); Page 22 // Upcast System.out.println(r2.getArea()); System.out.println(r2.getColor()); System.out.println(r2.getSide()); System.out.println(r2.getLength()); Square sq1 = (Square)sq1; System.out.println(sq1); System.out.println(sq1.getArea()); System.out.println(sq1.getColor()); System.out.println(sq1.getSide()); System.out.println(sq1.getLength()); } } Exercise 5: Interface In Exercise, suppose that the application involves many objects that can move You could define an interface called movable, containing the signatures of the various movement methods Class diagram of the program are shown below: Task - Create Movable Interface In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Interface The "New Interface Class" dialog pops up In "Name" field, enter " Moveable" Page 23 Click "Finish" See the code what IDE generate: public interface Moveable { } Write code in Moveable.java public interface Movable { public void moveUp(); public void moveDown(); public void moveLeft(); public void moveRight(); } Task - Create MovablePoint Class implements Movable Interface In the "Package Explorer" (left panel) ⇒ Right-click on your Project (or use the "File" menu) ⇒ New ⇒ Class The "New Java Class" dialog pops up In "Name" field, enter "MoveablePoint " In Interface field, press “Add” button, Enter Movable and press Add Click "Finish" See the code what IDE generate: public class MoveablePoint implements Movable { @Override public void moveUp() { // TODO Auto-generated method stub } @Override public void moveDown() { // TODO Auto-generated method stub } @Override Page 24 public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } } Task - Write code in MoveablePoint.java Instance variables - (x, y) coordinates of the point private int x; private int y; Constructor public MovablePoint(int x, int y) { this.x = x; this.y = y; } public String toString() { return "Point at (" + x + "," + y + ")"; } Implement abstract methods defined in the interface Movable @Override public void moveUp() { y ; } @Override public void moveDown() { y++; } @Override public void moveLeft() { x ; Page 25 ... parent class) In this lab, you will write simple applications to practice some basic feature of OOP such as: Inherit, Polymorphism, etc Objectives Once you have completed this lab, you will understand:... Exercises This Hands-On Lab is comprised by the following exercises: Subclassing and Inheritance Polymorphism Abstract class Inteface Estimated time to complete this lab: hours Page Index