logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    sfolax6776
    Feb 07, 2021
      ·  Edited: Jan 28

    6-6 Parent-child relationship layer by layer: late binding

    in AP Computer Science A

    A. Introduction

    Consider the Shape class and its three subclasses below. There are three methods in the superclass: area(), perimeter(), and toStr(). Rectangle class overrides only toStr(); Triangle class overrides all three methods, and Circle class overrides only area() and perimeter() besides providing new methods of toString(), roll() and equals(). Notice toString() does not override toStr() since they have different names.



    B. Late Binding (a.k.a Dynamic Binding):

    Now review the Main class which creates an array of three different shapes:

    import java.util.Scanner;
    class Main {
      public static void main(String[] args) {
        Shape[] three = new Shape[3];
        Scanner in = new Scanner(System.in);
        for(int i=0;i<three.length;i++){
        System.out.println("What shape do you have?(1-circle, 3-Triangle, 4-rectangle)");
        int choice = in.nextInt();
        in.nextLine();
        System.out.println(" choice: " + choice);
        if(choice==1){
          System.out.println("radius and center?");
          double r = in.nextDouble();
          double x = in.nextDouble();
          double y = in.nextDouble();
          in.nextLine();
          Point p = new Point(x, y) ;
          three[i] = new Circle(p, r);
          ((Circle)three[i]).roll();  // type casting
        }
        else if (choice ==3){
          System.out.println("three sides of the triangel?");
          double a = in.nextDouble();
          double b = in.nextDouble();
          double c = in.nextDouble();
          in.nextLine();
          three[i]=new Triangle(a, b, c);
          // Is it possible to convert a triangle into a circle? - A runtime type casting exception!
          ((Circle)three[i]).roll();
        } 
        else if (choice==4){
          System.out.println("length and width?");
          double l = in.nextDouble(); 
          double w = in.nextDouble();
          in.nextLine();
          three[i] = new Rectangle(l, w);  
        }
        System.out.println(three[i].toStr()+", area = "+three[i].area()+", perimeter = "+three[i].perimeter());
        }
      }
    }

    Notice three[i].toStr(), three[i].area(), and three[i].perimeter() will invoke different versions of toStr(), area(), and perimeter() depends on the ACTUAL object type of three[i].

    Parent-child relationship rules #5: the following type rules must be followed for calling polymorphic methods


    C. Try this:

    1. Question 1-8 and 10 at end of Chapter 3 (page 150), Barron's AP CS A


    2. Implement the public double weeklyPay() method in the class hierarchy below so you can solve the payroll problem as described here.


    3. Create an array of animals that has 1 cat, 1 dog and 1 cow, then call public String say() to let each of them speak in their own language.

    1 comment
    0
    William Li
    Jan 30

    https://replit.com/@offexe/6-6

    0
    1 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners