Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎


posted Oct 16, 2018, 6:07 AM by Konstantinovich Samuel   [ updated Oct 17, 2018, 8:34 AM ]
Goal: Lets get real and rational...

IS-A Relationship:  
Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS-A relationship.
X is-a Y  just means that "X is a Y type of thing", or more precisely, that X inherited the properties of Y.

Apple is a Fruit,
Car is a Vehicle etc.

Since all classes are sub-classes of Object
EVERY CLASS is-a Object.

This is uni-directional For example, House is a Building. But Building is not a House.

HAS-A Relationship:

X has-a Y suggests that Y is instance variables inside of X.   Triangle has-a Point,  House has-a Bathroom.

public class A{}
public class B extends A{}
B is-a A means that B is a subclass of A, and can be treated just like an A

B is-a A
B is-a Object

This works for multiple layers of inheritance too!
public class Person{}
public class GovernmentWorker extends Person{}
public class FireFighter extends GovernmentWorker{}

A FireFighter is-a GovernmentWorker
A FireFighter is-a Person
A FireFighter is-a Object

A GovernmentWorker is-a Person
A GovernmentWorker is-a Object

A Person is-a Object

Consider the RealNumber and RationalNumber classes:

Both classes need:
 -getValue()  - think: what is the getValue of RationalNumber(1,2) ?

Lab + Homework (Due Thurs)
Complete the RealNumber and RationalNumber classes.

public class RealNumber{
  private double value;

  public RealNumber(double v){
    value = v;

  *Return the sum of this and the other
  public double add(RealNumber other){
     //other can be ANY RealNumber, including a RationalNumber
     //or other subclasses of RealNumber (that aren't written yet)
     return 0;

  /* *Return the product of this and the other */ public double multiply(RealNumber other){ return 0; }

 /* *Return the this divided by the other */ public double divide(RealNumber other){ return 0; }
/* *Return the this minus the other */ public double subtract(RealNumber other){ return 0; } public double getValue(){ return value; } public String toString(){ return ""+value; } }

public class RationalNumber extends RealNumber { private int numerator, denominator; /**Initialize the RationalNumber with the provided values
* if the denominator is 0, make the fraction 0/1 instead
*@param nume the numerator *@param deno the denominator */ public RationalNumber(int nume, int deno){ super(0.0);//this value is ignored! }

public double getValue(){ return 0.0; }
 /** *@return the numerator */ public int getNumerator(){ return 0; } /** *@return the denominator */ public int getDenominator(){ return 0; } /** *@return a new RationalNumber that has the same numerator *and denominator as this RationalNumber but reversed. */ public RationalNumber reciprocal(){ return null; } /** *@return true when the RationalNumbers have the same numerators and denominators, false otherwise. */ public boolean equals(RationalNumber other){ return false; } /** *@return the value expressed as "3/4" or "8/3" */ public String toString(){ return "0"; } /**Calculate the GCD of two integers. *@param a the first integers *@param b the second integer *@return the value of the GCD */ private static int gcd(int a, int b){ /*use euclids method or a better one*/
  return 0; } /** *Divide the numerator and denominator by the GCD *This must be used to maintain that all RationalNumbers are *reduced after construction. */ private void reduce(){ } /******************Operations Return a new RationalNumber!!!!****************/ /** *Return a new RationalNumber that is the product of this and the other */ public RationalNumber multiply(RationalNumber other){ return null; } /** *Return a new RationalNumber that is the this divided by the other */ public RationalNumber divide(RationalNumber other){ return null; } /** *Return a new RationalNumber that is the sum of this and the other */ public RationalNumber add(RationalNumber other){ return null; } /** *Return a new RationalNumber that this minus the other */ public RationalNumber subtract(RationalNumber other){ return null; } }