Lab 02

You might also like

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

Lab 2

Code re-use
• Achieved in java using
– Composition-
• define a new class, which is composed of existing
classes
– Inheritance
• derive a new class based on an existing class, with
modifications or extensions.
Create the following class
/*/The Author class model a book's author.
public class Author {
// The private instance variables
private String name;
private String email;
private char gender; // 'm' or 'f'

// The constructor
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}
// The public getters and setters for the private instance variables.
// No setter for name and gender as they are not designed to be changed.
public String getName() {
return name;
}
public char getGender() {
return gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// The toString() describes itself
public String toString() {
return name + " (" + gender + ") at " + email;
}
}
/*
* A test driver for the Author class.
*/
public class TestAuthor {
public static void main(String[] args) {
// Test constructor and toString()
Author author = new Author("Abebe Kebede", "akeb@nowhere.com", 'm');
System.out.println(aKebede); // toString()

// Test Setters and Getters


author.setEmail("akeb@kmu.edu");
System.out.println(author); // toString()
System.out.println("name is: " + author.getName());
System.out.println("gender is: " + author.getGender());
System.out.println("email is: " + author.getEmail());
}
}
Composition- Using an "Object" Member Variable

Assume that a book is written by one (and exactly one) author


/* * The Book class models a book with public Author getAuthor() {
one (and only one) author. */ return author; // return member author, which is
public class Book { an instance of the class Author
}
// The private instance variables
public double getPrice() {
private String name;
return price;
private Author author; }
private double price; public void setPrice(double price) {
private int qty; this.price = price;
// Constructor }
public Book(String name, Author public int getQty() {
author, double price, int qty) { return qty;
this.name = name; }
public void setQty(int qty) {
this.author = author;
this.qty = qty;
this.price = price;
}
this.qty = qty; // The toString() describes itself
} public String toString() {
// Getters and Setters return "'" + name + "' by " + author;
public String getName() { }
return name; }
}
/* System.out.println("price is: " + dummyBook.getPrice());
* A test driver program for the Book class. System.out.println("qty is: " + dummyBook.getQty());
*/ System.out.println("author is: " +
dummyBook.getAuthor()); // invokeAuthor's toString()
public class TestBook {
System.out.println("author's name is: " +
public static void main(String[] args) {
dummyBook.getAuthor().getName());
// We need an Author instance to create a Book
System.out.println("author's email is: " +
instance
dummyBook.getAuthor().getEmail());
Author aKebede= new Author("Abebe Kebede",
"akeb@nowhere.com", 'm'); System.out.println("author's gender is: " +
System.out.println(aKebede); // Author's toString() dummyBook.getAuthor().getGender());

// Test Book's constructor and toString() // Using an anonymous Author instance to create a
Book instance
Book dummyBook = new Book("Java for dummies",
aKebede, 9.99, 99); Book moreDummyBook = new Book("Java for more
dummies",
System.out.println(dummyBook); // Book's
toString() new Author("Peter Lee", "peter@nowhere.com",
'm'), // an anonymous Author's instance
19.99, 8);
// Test Setters and Getters
System.out.println(moreDummyBook); // Book's
dummyBook.setPrice(8.88);
toString()
dummyBook.setQty(88);
}
System.out.println(dummyBook); // Book's
toString() }
System.out.println("name is: " +
dummyBook.getName());
Inheritance
• Extends the existing class called super class
• Use the extends keyword to indicate that
one class inherits from another
– The subclass inherits all the fields and methods of
the superclass
• Use the super keyword in the subclass
constructor to call the superclass constructor
– This ensures that the superclass part of the object
is constructed before the subclass part
Dog Cat
String name String name
int fleas int hairballs
String getName() String getName()
int getFleas() int getHairballs()
void speak() void speak()

using
inheritance

superclass
Animal
subclass
String name
subclass String getName()

Dog Cat
int fleas int hairballs
int getFleas() int getHairballs()
void speak() void speak() 10
The superclass Animal.java
public class Animal {
private String name;

public Animal(String n) {
name = n;
}

public String getName() {


return name;
}
public void speak(){
System.out.println(“Everything");
}
}
The subclass Cat.java
public class Cat extends Animal {
private int hairballs;
public Cat(String n, int h) {
super(n); // calls Animal constructor
hairballs = h;
}
public int getHairballs() {
return hairballs;
}
//This class override the parentclass’s method
@Override
public void speak() {
System.out.println("Meow");
}
}
Subclass Dog.java
public class Dog extends Animal {

private int fleas;


public Dog(String n, int f) {
super(n); // calls Animal constructor
fleas = f;
}
public int getFleas() {
return fleas;
}
@override
public void speakk() {
System.out.println("Woof");
}
}
TestAnimal.java
public class TestAnimal{
public static void main(String [] args){

Dog d = new Dog("Rover", 3);


Cat c = new Cat("Kitty", 2);
System.out.println(d.getName() + " has " + d.getFleas() + " fleas");
d.speak();
System.out.println(c.getName() + " has " + c.getHairballs() + "
hairballs");
c.speak();

}
}

You might also like