Vous êtes sur la page 1sur 3

CMSC 123 LAB EXERCISE 2 Total: 30 points

Object-Oriented Programming

What you need to know:


● Creating classes
public class ​Person{
String ​name; // attributes / properties
int ​age;
boolean ​isCool;

public void ​talk( ){


System.out.println(“Hello, my name is “ + name); // method
}
}
● Creating objects
Person john = ​new ​Person( );
● Accessing properties
john.name = “John”;
john.age = 20;
john.isCool = false;
● Calling object methods
john.talk( );
● Static methods​ → no need to create an object of that class to use the method
public ​static ​void main(​String​[ ] args) { …. }
● Constructors
public ​Person(​String​ name, ​int ​age){
this​.name = name; // this → refers to object being created
this​.age = age;
}
● Private properties
private ​String name; // inaccessible outside the class
// Accessing john.name results in an error
● Getters ​and ​Setters
// Allows you to encapsulate your data
// Gives you better control of how properties are accessed
public String ​getName( ){
return ​name;
}
public void ​setName(​String​ newName){
if​(newName != “”){
name = newName;
}
}
● Inheritance
// Superclasses allow its subclasses to inherit their properties and methods
// Good for code reuse
class ​Animal​{ // Superclass
String ​name;
public ​Animal(​String​ name){
this.name = name;
}
void ​walk( ){
System.out.println(name + “ is walking..”);
}
}
class Dog ​extends ​Animal{ // Subclass of Animal
public ​Dog(​String​ name){
super​(name); ​ // calls the superclass’ constructor
}
void ​bark( ){
System.out.println(name + “ says arf! arf! “);
}
}
class Cat ​extends ​Animal{ // Subclass of Animal
public ​Cat(​String​ name){
super​(name);
}
void ​meow( ){
System.out.println(name + “ says meow! meow! “);
}
}

Dog bantay = ​new ​Dog(“Bantay”);


Cat miming = ​new ​Cat(“Miming”);
bantay.bark( );
miming.meow( );
bantay.walk( );
miming.walk( );
● Polymorphism
// Arrays in Java are homogeneous → array elements must be of one type
// You can put a Dog and Cat object together in one array, since both are Animal objects
Animal ​pets[ ] = ​new ​Animal[3]; // creating an empty Animal array of size 3
pets[0] = ​new ​Animal(“Bugsy”);
pets[1] = ​new ​Dog(“Bantay”);
pets[2] = ​new ​Cat(“Miming”);
● Abstract classes
abstract class ​Animal { … } // Animal class can only function as a superclass
// Cannot create Animal objects
// Can only create Animal subclass objects (e.g. Dog, Cat)

Animal bugsy = ​new ​Animal(“Bugsy”); ​// results in an error → Animal is an abstract class
Animal bantay = ​new ​Dog(“Bantay”); ​// this is OK
● Interfaces
interface ​Printable {
void ​print( );
}
interface ​Displayable {
void ​display( );
}
class ​WebDocument ​implements ​Printable, Displayable{
void ​print( ){
System.out.println(“Printing web document..”);
}
void ​display( ){
System.out.println(“Displaying web document..”);
}
}
class ​MobileDocument ​implements ​Displayable{
void ​display( ){
System.out.println(“Displaying mobile document..”);
}
}

What you need to do:


1. (​10 pts​) Create 5 classes - A, B, C, D, E (create your own scenario / topic), where:
a. Class A is ​abstract
b. Class B and C ​extends ​class A
c. Class D and E ​extends ​class B
d. Each class has their ​constructor ​defined
2. (​10 pts​) Each class should have at least ​2 properties​, and at least ​1 method​, where:
a. Some properties are ​private
b. Some methods are ​getters ​/ ​setters
c. Some methods are ​static
3. (​10 pts​) Create a program that will do the ff:
a. Create new class B, C, D, and E ​objects
b. Access / modify some object’s ​properties
c. Call an object’s ​own​ ​method
d. Call an object’s ​method ​inherited from its ​superclass
e. Create a class A ​array​, and put different types of objects (class B, C, D, E) in the array
f. Loop through the array elements and access a property / call a method that is common to
all (inherited from class A)

Vous aimerez peut-être aussi