Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Assignment 1

Unit 2
CPRG304 – Object-Oriented
Programming III

Kitty Wong © 2023


Shape Comparisons in Assignment
• Your project should be able to compare using one of 3
criteria:
1. By height – this is just an attribute of the object (using
comparable)
2. By base area – calculated from one or more attributes of the
object (using comparator)
3. By volume – calculated from one or more attributes of the
object (using comparator)

• Formulas for these calculations are given on the last page of


the Assignment specifications pdf
Shapes
• Array[Shape] to store all the objects
• 4 types of shapes:

• 4 different prisms:
Shape Classes
Cylinder Cone Pyramid
double height; double height; double height;
double radius; double radius; double side;
double calcVolume(); double calcVolume(); double calcVolume();
double calcBaseArea(); double calcBaseArea(); double calcBaseArea();

SquarePrism TriangularPrism PentagonalPrism OctagonalPrism


double height; double height; double height; double height;
double side; double side; double side; double side;
double calcVolume(); double calcVolume(); double calcVolume(); double calcVolume();
double calcBaseArea(); double calcBaseArea(); double calcBaseArea(); double calcBaseArea();
Data Modelling in OOP
• Make our code reusable, maintainable and scalable!
Prism Shape
double height; double height;
double side; double calcVolume();
double calcVolume(); double calcBaseArea();
double calcBaseArea();

• Now we have a base class to compare the height!


• Shape implements the Comparable interface
public int compareTo(Shape s){
if ( this.getHeight() > s.getHeight() ) return 1;
else if ( this.getHeight() < s.getHeight() ) return -1;
else return 0;
}
Converting Text to Objects
• How would you read a String from a text file and
instantiate the correct object?
• Hard-code:
if (str.equals(“Cylinder”) {
Cylinder c = new Cylinder();
}
else if (str.equals(“Cone”) {
Cone c = new Cone();
}
else if (str.equals(“Pyramid”) {
Pyramid p = new Pyramid();
}
// etc..
Reflection
• Runtime examination and modification of classes and
their methods
• Doesn’t require prior knowledge of the class and its method
signatures!
• JUnit annotations uses reflection heavily to look at the test
class to find methods, then call them while running tests
• To establish a DB connection
Class.forName(“com.mysql.jdbc.Driver”);

• Note: reflection != introspection


Hello Kitty Example
• Just a regular class and method definition:

public class MyClass


{
public void HelloKitty()
{
System.out.println(“Hello Kitty!”);
}
}
Making more Kitties
• Normal instantiation and invocation:
MyClass mc = new MyClass();
mc.HelloKitty();

• With reflection:
Class reflected = Class.forName(“MyClass”);
Object o = reflected.newInstance();
Method m = o.getMethod(“HelloKitty”);
m.invoke();

• Why? Looks like a lot of work for the same thing!


The Power of Reflection
• What if you didn’t know the class/method names?

String s1;
String s2;
// now grab the values of s1 and s2 from another
// part of the system at runtime! E.g. user input,
// file, database, network, etc.
Class reflected = Class.forName(s1);
Object o = reflected.newInstance();
Method m = o.getMethod(s2);
m.invoke();

You might also like