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


posted Oct 15, 2018, 6:19 AM by Konstantinovich Samuel   [ updated Oct 15, 2018, 6:26 AM ]
Create a repo  MKS21X-Numbers
Create RealNumber and RationalNumber classes.
List the methods you think would be useful in the RealNumber and RationalNumber classes.
Place your empty methods inside of the classes (and return dummy values so it compiles)

The pclassic competition is upon us. info here:
This programming competition is on Sat Nov 17th. It is a great way to see if you like coding competitions.
Suggestions if you want to go:
0-You probably want to be a beginner (problems 1 - 8, not 5 - 12)
1-Write in python not java. You don't know java well yet. You will need to practice your python a bit.
2-On your own: DO A FULL PROBLEM SET (see how many you can do)
3-With a team of 3 others and 1 computer: DO A DIFFERENT FULL PROBLEM SET (see how many you can do)
    The practice is important because you don't want to be "that group" that does just 2 problems... you can do better!
4-There will be a sign up sheet posted later. You need to create a team of 4 to sign up.
5- The competitive coding club meets Tues/Fri in this room! You can check out other enthusiastic coders and get tips to .

Do now: Discuss and write down the answers in your notes:

-Default constructor:
What is it?
When does java give it to you? 
What does java give you?

-Subclass constructor:
    How does it initialize the private variables of the super class?
   What does implicit mean?
    When does it have an implicit call to super?


public class A{
    private int x;
    public A(){
             x = 10;

    public A(int value){
             x = value;

    public int getX(){
             return x;

      public String toString(){
        return "" + getX();

 public class B extends A{

    public B(){
            //you can call a different constructor if you like

    public B(int value){

    //Override getX to be a negation
    public int getX(){
            return -1 * super.getX();//call to parent class method


 public class DriverAB{
    public static void main(String[]args){

       System.out.println("ObB B():");
        B obB1 = new B();
        System.out.println("value: "+obB1.getX());

      System.out.println("ObA1: A()");
        A obA1 = new A();
        System.out.println("value: "+obA1.getX());

      System.out.println("ObA2: A(99)");
        A obA2 = new A(99);
        System.out.println("value: "+obA2.getX());


Consider a Number class hierarchy:

RealNumber    -> we will use a double
RationalNumber extends RealNumber -> we will use the ratio of two integers

Discuss with your neighbors and WRITE DOWN the answers to all of these prompts (or series of prompts):
1 What methods would these classes have in common?

2 How would you compare a RealNumber to a RealNumber? Is a method required? Is there any reason to have one?

3 Should we be able to compare a Real to a Rational?

4 Can we avoid rounding errors when comparing Rational numbers to other Rational numbers?

5 Should the class be mutable?

6 When do you NEED to reduce?

7 What happens with a zero denominator?

8 What happens when the ratio is negative?

9 What happens when you display a number with a numerator of 0, or a denominator of 1?