1 / 21

COMP 121

COMP 121. Week 04. Solutions. Review Homework 1 Solutions. Outcomes. Distinguish between abstract and concrete classes and methods. Describe the uses and effects of the keywords final, public, protected, and private as they apply to classes, methods, and instance fields.

Télécharger la présentation

COMP 121

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 121 Week 04

  2. Solutions • Review Homework 1 Solutions

  3. Outcomes • Distinguish between abstract and concrete classes and methods. • Describe the uses and effects of the keywords final, public, protected, and private as they apply to classes, methods, and instance fields. • Supply appropriate equals, toString, and clone methods for base, derived, and composed classes. • Apply appropriate access modifiers within class design. • Recognize and apply the Template Method design pattern to solve a given problem.

  4. Abstract Classes • Can’t be instantiated • May contain abstract methods that derived classes will be required to implement • May contain concrete methods with implementations as well as member variables • An interface can be thought of as a pure abstract class

  5. Abstract Classes abstract class Dog { private boolean hungry = true; public abstract void bark(); public void eat() { hungry = false; } } class Husky extends Dog { public void bark() { System.out.println(“Woof!”); } } class Chihuahua extends Dog { public void bark() { System.out.println(“Yip!”); } }

  6. Guided Learning Activity Solutions for Week 04

  7. Learning Activities Activity 4-1 Outcome: Distinguish between abstract and concrete classes and methods.

  8. Access Modifiers • public – can be accessed from any class • private – can only be accessed from inside the declaring class • default – can only be accessed from inside the same package • protected – can be accessed from the same package, or a derived class

  9. Access Modifiers class A { int a; public int b; protected int c; private int d; } // In same package class B extends A { void foo() { a = 1; b = 1; c = 1; d = 1; // Error } }

  10. Access Modifiers class A { int a; public int b; protected int c; private int d; } // In different package class C extends A { void foo() { a = 1; // Error b = 1; c = 1; d = 1; // Error } }

  11. Learning Activities Activity 4-2 Outcome: Describe the uses and effects of the keywords final, public, protected, and private as they apply to classes, methods, and instance fields.

  12. The Object Class • equals() – compares objects using == • clone() – makes a shallow copy • toString()– prints class name and hash code (Point@65fab3e)

  13. Implementing equals() public class Student { private String firstName; private String lastName; public boolean equals(Object o) { Student s2 = (Student)o; return firstName.equals(s2.firstName) && lastName.equals(s2.lastName); } }

  14. Implementing toString() public class Student { private String firstName; private String lastName; public String toString() { return firstName + “ “ + lastName; } public static void main(String [] args) { Student s = new Student(“John”, “Doe”); System.out.println(s); } }

  15. Implementing clone() public class Circle implements Cloneable { private int r; private Point center; public Object clone() { Circle cloned = super.clone(); cloned.center = new Point(center.getX(), center.getY()); return cloned; } public static void main(String [] args) { Circle c = new Circle(5, new Point(3, 3)); Circle c2 = c.clone(); c.getCenter().setX(2); System.out.println(c + “ “ + c2); } }

  16. Learning Activities Activity 4-3 Outcome: Supply appropriate equals, toString, and clone methods for base, derived, and composed classes. Activity 4-4 Outcome: Apply appropriate access modifiers within class design.

  17. Template Method Pattern • Creates a fixed structure for an algorithm • Lets the details of each step change

  18. Template Method Pattern public class PizzaShop { public final void makePizza() { makeDough(); tossDough(); addToppings(); cook(); slice(); } public void makeDough() {…} public void tossDough() {…} public void addToppings() {…} public void cook() {…} public void slice() {…} }

  19. Learning Activities Activity 4-5 Outcome: Recognize and apply the Template Method design pattern to solve a given problem.

  20. Homework Assignments • Due this week • Lab 1 • Homework 2 • Reflection Paper Draft • Next week • Exam 1

  21. Question and Answer Session

More Related