Java

ADVANCED OOP GUIDES / WORK

 

Object Arrays – Interfaces – Inheritance –

Polymorphism – Abstract Classes

 

SHORT ANSWER QUESTIONS

 

Interfaces & Polymorphism (Solutions)

 

Inheritance & Polymorphism I (Solutions)

 

Inheritance & Polymorphism II (Solutions)

 

 

GUIDES / TASKS

 

Extremable interface

-Creating an Interface (Intro)

-Note: Used in lesson 12.13.

Solutions

Collidable interface

-Interface Design (Basic)

-Note: Used in lesson 12.13

Solutions

Duration class

-Basics: instance variables, constructors, instance methods, encapsulation)

Solutions

Duration class 2

-Implementing advanced methods

-Private instance method

Solutions

String class

Integer class

Double class

Demo idea:

-Use compareTo to determine alphanumeric order

-Integer and Double both implement Comparable

-Sort String array

-Would be for 12.14

 

Card class

-Comparable interface

-Implementing compareTo (step by step)

-Object arrays

-Note: Used in lesson 12.14.

Solutions

Student class

-Comparable interface

-Implementing compareTo

-Object arrays

-Note: Used in lesson 12.14.

Solutions

Line class

-Comparable interface

-Implementing compareTo

-Sorting ArrayLists

-Note: Used in lesson 12.14.

Solutions

Card class 2

-Comparable interface

-Implementing compareTo

-Object arrays

-Note: Used in lesson 12.14.

Solutions

Student class 2

-Comparable interface

-Implementing compareTo

-Implementing multiple sorting criteria

-Object arrays

-Note: Used in lesson 12.14.

Solutions

BadGuy Superclass
Goomba subclass

-Inheritance

-Extending a class

-Superconstructor

-Note: Used in lesson 12.15.

Solutions

Vehicle Superclass

Truck subclass

Car subclass

-Inheritance

-Extending a class

-Superconstructor

-Note: Used in lesson 12.15.

Solutions

Person superclass

Student subclass
Teacher subclass

-Inheritance

-Extending a class

-Superconstructor

-Note: Used in lesson 12.15.

Solutions

 Food superclass

-Inheritance

-Object superclass

-Overriding methods

-Note: Used in lesson 12.16.

Solutions

Food superclass 2

Sandwich subclass

-Inheritance

-Protected

-Note: Used in lesson 12.16.

Solutions

RangedWeapon class

-Inherits from Weapon class

-Inheritance

-Protected instance variables

-Aggregation

-Note: To be used in lesson 12.16

Solutions

ShoeStore class

Shoe class

-Interface

-Implementation of Comparable

-Note: Used in lesson 12.17T (Review)

Solutions

RangedWeapon class 2

-Inheritance

-Polymorphism

-Note: Used in lesson 12.17T (Review)

Solutions

 

 

 

POSSIBLE ADDITIONS

Delimiters class     -Using ArrayLists in classes

-Note: Used for 12.17

-Source: 2019 AP Exam Q3       Solutions

 

+++++

 

PART 1

Create a class called Building that will have the following:

·                         4 datafields holding the x,y,width and height

·                         A constructor

·                         A draw method to display a rectangle where the building is located (using NOOPDraw)

 

PART 2


In a class called Tester, create a Building object and draw it to screen.

 

PART 3

 

Create a SimpleHouse object that extends Building and will have the following:

·                         1 datafield for the roof height (plus the inherited datafields)

·                         A constructor

·                         A draw method that overrides the draw method in Building

 

PART 4

 

In the same Tester class as above, create a SimpleHouse object and draw it to screen.

 

PART 5

 

Create a NiceHouse class that extends SimpleHouse and has the following:

·                         2 datafields for doorWidth and doorHeight (plus the inherited datafields)

·                         A constructor

·                         A draw method

 

PART 6

 

In the same Tester class as above, create a NiceHouse object and draw it to screen.

 

PART 7 (POLYMORPHISM TIME)

 

In a new class called Tester2, inside the main, create an array of Building objects.  The array’s size should be 6.  You should add 2 Building objects, 2 SimpleHouse objects and 2 NiceHouse objects to the array.

 

Then, using a for loop, draw each house in the array onto screen. 

 

+++++

 

FORMERLY 12.13’s QUESTION 3 (GROUP WORK) (NEED TO ADD LINKS)

 

Create the interface called Summable that has the following methods:

·                         int total() – returns the sum of all values in the object

·                         int count() – return the amount of values in the object

 

Consider the Sequence class here.  Make it implement the Summable interface.

 

Consider the Budget class here.  Make it implement the Summable interface.

 

Inside a Tester class, create a Sequence object and a Budget object.  Then create a static function called average that gets a Summable object and outputs the average value in that object.

 

++++

 

Create a Point class.  Make use of the Comparable interface in order to sort Point objects as they appear from left to right on a graph.  Create an array of Point objects and sort it to see if your code works.

 

Consider looking up Comparator as a different interface option for sorting.

 

Get students to create a map like below using https://fsymbols.com/draw/.  Then, create a Map class that gets the strings from the map.  It then draws the map on the screen.  Add the ability to make an X move around the map.

 

███████████████████████

██░░░░░░░░███░░░░░░░░██

██░██████░███░████░████

██░██░░░░░░░░░████░░░██

█████░███████░████░████

██░░░░░░░█████████░░███

██░░░░░░░██░░░░████░███

███████████░█████░░░███

░░░░░░░░░░░░░░░░░░█████

███████████████████████

 

 

Inheritance & Polymorphism

 

Example idea: A MonsterCircle that consumes shapes such as Circle, Rectangle and Triangle objects.  All of these inherit from the Shape superclass with an area() method (or abstract class maybe).  MonsterCircle’s area grows based on the consumed shapes’ area.  Need to calculate the new radius.  Could even add a NOOPDraw part to it.  Note that MonsterCircle could maybe be called BlackHole instead.

 

Interfaces & Polymorphism

 

Interface ideas: Incrementable (inc(), dec()), Extremable (getLeft, getRight, getTop, getBottom), Summable,

 

Java Interfaces: CharSequence (used by String),

 

Appendable (used by StringBuilder), Comparable, Iterable

Idea: Maybe make our own Appendable interface with append(Object obj).  It can work on my own String class adding characters, on a Polygon class adding a point, on …

 

BLANKS

 

What is wrong with the following interface?

public interface SomeInterface

{

    void someMethod(int n)

    {

        System.out.println("Hello world");

    }

}

>You can never implement a method inside an interface.

 

++++++

OLD INHERITANCE QUESTIONS

 

In this question, you will create four different classes that make use of inheritance.

 

PART A – THE VEHICLE CLASS

 

Create a superclass called Vehicle that contains the following 2 datafields, 1 constructor and 2 instance methods:

 

            protected String owner;

protected int registrationNumber;

 

public Vehicle(String o, int r)

 

public String getOwner()

public int getRegistrationNumber()

 

You need to implement all parts of the class.

 

PART B – THE CAR CLASS

 

Create a Car class that extends the Vehicle class and contains 1 new datafield, 1 constructor and 1 instance method.

 

protected int numberOfDoors;

 

public Car(String o, int r, int n)

 

public int getNumberOfDoors()

 
PART C THE TRUCK CLASS

 

Create a Truck class that extends the Vehicle class and contains the following:

 

protected int numberOfAxels;

 

public Truck(String o, int r, int n)

 

public int getNumberOfAxels()

 

PART D – THE MOTORCYCLE CLASS

 

Create a Motorcycle class that extends the Vehicle class and contains the following:

 

protected boolean hasSideCar;

 

public Motorcycle(String o, int r, boolean c)

 

public boolean getHasSideCar()

 


PART E – COSTS METHODS

 

i)       Add a public double costs() method to Vehicle.  It always returns 20000.0.

ii)     Add a public double costs() method to Car.  If it has 2 or less doors, the cost is 20000.0.  If it has 3 or 4 doors, the cost is 25000.  If it has 5 or 6 doors, the cost is 35000.  If it has more than that, the cost is 50000.

iii)    Add a public double costs() method to Truck.  It costs 40000 if it has two axels.  It then costs an extra 8000 for each extra axel.

iv)   Add a public double costs() method to Motorcycle.  It costs 18000 if it has no side car.  The side car costs an extra 6000.

v)     Make sure you add @Override on the line of the costs() methods that are in the Car, Truck and Motorcycle class (but not in the Vehicle class).

 


PART F – TESTING TIME (WITH POLYMORPHISM)

 

In the main method of the class called Tester, do the following:

 

a)    Create 2 instances of each of the four classes.

b)    Still in the Tester class, add the outputVehicle static method (function) below.  It will output the owner of the vehicle along with the vehicle’s cost.

public static void outputVehicle(Vehicle v)
{
   //you need to implement this
}


c)     Pass each of the 8 instances of our classes to the outputVehicle function.  This is an example of polymorphism.  Check that the output is as you would expect.  Notably, check that the cost is correct.

 

Taken from the ICS3U test review:

QUESTION 6 – PROGRAMMING INHERITANCE

Consider the class below called BasicBullet that will work for a game similar to Space Invaders. 

 

public class BasicBullet
{

    public double x;

    public double y;

    public String filename;

    public BasicBullet(double tmpX, double tmpY, String tmpFilename)
    {

        x = tmpX;

        y = tmpY;

        filename = tmpFilename;

    }

}

 

Now write the class called HomingBullet that extends (inherits from) BasicBullet.  It has the extra data fields:

  • public double targetX;
  • public double targetY;

 

You will also need to add a constructor to the class.


POSSIBLE ADDITIONS

Adding classes and having students create and manipulate objects of that class.

 

 

++++++

Taken from old ICS4U test review

 

QUESTION 6 – INHERITANCE

 

6.1 – Create the class called Student that will inherit from the Person class above.  Here are the specifications:

 

a)    It has two new datafields representing:

               -student number (integer)

               -current school (String)

 

b)    It has a constructor that gets all five datafields as parameters.

c)     It has a changeSchool(String newSchool) method that allows for access to the corresponding datafield.

QUESTION 7 – INTERFACE

 

Consider the following interface:

 

     public interface Executable
     {

        public void execute();

     }

7.1 – The line below starts a new class (in its own file).  What text is needed to make that class use the interface above?

 

     public class NewClass

     {

     }

 

7.2 – Now that NewClass is using the interface, what has to be done for it to be error-free?

QUESTION 8 – POLYMORPHISM

 

8.1 – Similar to the Student class from above, create a Teacher class that inherits from Person.  (Yes, teachers are people too!)  You decide on the datafields, constructor(s) and methods.

8.2 – Inside the main of a Tester class, create an array of Person objects.  Assign both Student and Teacher objects to the elements of the array.  Loop through the array outputting the names of the People in the array.