public class Circle {
/* class body */
}
class Circle {
/* Instance variables are defined here*/
/* Point x coordinate */
private int x;
/* Point y coordinate */
private int y;
/* Circle's radius */
private int radius;
}
class Circle {
/* Point x coordinate */
private int x;
/* Point y coordinate */
private int y;
/* Circle's radius */
private int radius;
/* Methods are defined here */
/* Returns the x coordinate */
public int getX() { return x; }
/* Returns the y coordinate */
public int getY() { return y; }
/* Returns the radius */
public int getRadius() { return radius; }
}
Fields should be accessed and modified only through dedicated methods
class Circle {
/* x and y coordinates and radius */
private int x;
private int y;
private int radius;
/* getters and setters methods */
public void setX(int x) { this.x = x; }
public int getX() { return this.x; }
public void setY(int y) { this.y = y; }
public int getY() { return this.y; }
public void setRadius(int r) { this.radius = r; }
public int getRadius() { return radius; }
}
Recommended way to access or modify fields
Circle c = new Circle();
c.setX(5);
c.setY(3);
System.out.println("Hello, I'm circle (" + p.getX() + "," + p.getY() + ")");
I'm circle (5,3)
And never directly (this would be possible only if x and y instance variables had public access
Circle c = new Circle();
c.x = 5;
c.y = 3;
System.out.println("Hello, I'm circle (" + p.x + "," + p.y + ")");
class Circle {
/* x and y coordinates and radius */
private int x;
private int y;
private int radius;
public void setCoordinates(int x, int y) {
this.x = x;
this.y = y;
}
}
public double getPerimeter(double pi) {
double perimeter = 2*pi*this.radius;
return perimeter;
}
pi : argument/parameter of type double named as pi
A method's signature is the method name + the parameters. Signature defines how a method can be Overloaded
public double getPerimeter(double pi)
void methods perform an operation without returning a value
public void printPerimeter(double pi) {
double perimeter = 2*pi*this.radius;
System.out.println("Perimeter is " + perimeter);
}
Is the ability to have more than one methods with the same name but with different parameters. Different in sense of, type, number and order
/** valid overloaded methods **/
public void dummy() {}
public void dummy(String message) {} // different number of arguments
public void dummy(String message, int number) {} // different number of arguments
public void dummy(int number) {} // different number of arguments
public void dummy(int number, String message) {} // different order of arguments
public void dummy(float number) {} // different type of arguments
public int dummy(String message, double d) { return 0; } // different return type. The return type does not affect the overloading process
private void dummy(int[] array) {} // different type of arguments. Private does not affect the overloading process
public static void dummy(double real) {} // static modifier does no affect the overlaoding process
public final void dummy(float f1, float f2) {} // final modifier does no affect the overlaoding process
/** invalid overloaded methods **/
public void dummy(String text) {} // different argument name but same type
public void dummy(String message, int x) {} // the same as above
In other words, only the signature of the method affects its overloading ability
/* default constructor */
Circle() {
this.x = this.y = 0; // you can omit this line. x and will be initialized to zero either way.
this.radius = 3;
}
Creating an object of type Circle:
Circle c = new Circle();
Constructors inherit the class's access modifier
/* Overloaded constructor */
Circle(int x, int y) {
this.x = x;
this.y = y;
this.radius = 3; //hard coded
}
/* Overloaded constructor */
Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.radius = r;
}
Creating instances of type Circle:
Circle c1 = new Circle();
Circle c2 = new Circle(4,4);
Circle c3 = new Circle(3,4,5);
Similarly, a constructor with the this call can trigger any other constructor in the class!
/* default constructor */
Circle() {
this.x = 0;
this.y = 0;
this.radius = 3;
}
/* Overloaded constructor calling the default
constructor with the use of this */
Circle(int x, int y) {
this();
this.x = x;
this.y = y;
}
Creating instances of type Circle:
Circle c2 = new Circle(2,3);
Circle c1 = new Circle(5,2,5);
Circle c2 = new Circle(10,4,10);
c2 = c1;
Circle c1 = new Circle(5,2,5);
Circle c2 = new Circle(10,4,10);
c2 = c1;
c1.calculatePerimeter();
c2.calculatePerimeter();
Results to:
Perimeter is 18.84
Perimeter is 18.84
Circle c1 = new Circle(5,5);
Circle c2 = c1;
c2.setRadius(10);
c1.calculatePerimeter();
c2.calculatePerimeter();
Circle c1 = new Circle(5,5);
Circle c2 = c1;
c2.setRadius(10);
c1.calculatePerimeter();
c2.calculatePerimeter();
Results to:
Perimeter is 18.84
Perimeter is 18.84
/* Regular Overloaded Constructor */
Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.radius = r;
}
/* Copy Constructor */
public Circle(Circle original) {
this(original.x, original.y, original.radius);
}
Return the new object
Circle original = new Circle(5,2);
Circle copyOfOriginal = new Circle(original);
/** Circle destructor */
public void finalize() {
System.out.println("Circle (" +
this.x +"," + this.y + "," + this.radius + ") deleted");
}
Release the memory and run the garbage collector
c = null;
System.gc(); // Garbage Collector call
Results to
Circle (5,5,10) deleted
The garbage collection is a process that identifies unused object that are no longer referenced by any part of your program and reclaims the allocated memory
class Circle {
/* counts the number of the created circles */
public static int count;
/* Constructor */
Circle() {
count += 1; // increase count by one after creating a new circle
}
}
Static fields are common for all instances of the class
Circle c1 = new Circle();
Circle c2 = new Circle();
System.out.println("c1 count is " + c1.count); //c1.count gives a warning. Why?
System.out.println("c2 count is " + Circle.count);
c1 count is 2
c2 count is 2
Static methods can access only static fields (Doesn't apply on Constructors) If count was private we would need a get method to access it as follows
public static int getCount() { return count; }
Static methods can be called by other classes only by the name of the class
System.out.println("Number of existing circles : " + Circle.getCount());
Number of existing circles : 2
Circle c1 = new Circle();
Circle c2 = new Circle();
In our previous Circle class example we add the following field
/* The unique id of a circle */
private final int id;
We are obliged to initialize the field id
/* Modified constructor */
Circle() {
this.x = 0;
this.y = 0;
this.radius = 3;
count += 1;
/* we assign the current count as the circle's id */
id = count;
}
final fields cannot change their value/state after their initialization
Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle();
System.out.println("c1 count is " + Circle.getCount() + ", id is " + c1.getId());
System.out.println("c2 count is " + Circle.getCount() + ", id is " + c2.getId());
System.out.println("c3 count is " + Circle.getCount() + ", id is " + c3.getId());
Which result the following output
c1 count is 3, id is 1
c2 count is 3, id is 2
c3 count is 3, id is 3
class DBConnector
{
/* unique instance of the class */
private static DBConnector unique_db_connection_instnce;
/* Private constructor of the class */
private DBConnector()
{
...
}
/* method that creates (if not initialized)
and returns the unique_db_connection_instnce of the class */
public static DBConnector getInstance()
{
if (unique_db_connection_instnce == null)
unique_db_connection_instnce = new DBConnector();
return unique_db_connection_instnce;
}
/* sample method */
public void connect()
{
...
}
}
class Laptop {
/* The serial number of the product */
private String serialNumber;
/* Custom class representing the central processor unit */
private Cpu cpu;
/* Custom class representing the random access memory */
private Memory memory;
/* Custom class representing the Operating system license
The license is bound to this Laptop object */
private OSLicense osLicense;
}
class Laptop {
private String serialNumber;
private Cpu cpu;
private Memory memory;
private OSLicense osLicense;
/* Constructor */
Laptop () {
this.serial_number = "XXXX-XXXX-XXXX-XXXX";
this.cpu = new Cpu();
this.memory = new Memory();
//osLicense is not initialized
}
}
You have the following relations between entities:
Create a TestLibrary class with a main method in which you will execute the following code block:
/** Create Random authors */
Author ruth = new Author("Ruth");
Author diane = new Author("Diane");
Author jacqueline = new Author("Jacqueline");
Author rachel = new Author("Rachel");
Author joan = new Author("Joan");
Author theresa = new Author("Theresa");
Author angela = new Author("Angela");
Author helen = new Author("Helen");
Author lisa = new Author("Lisa");
/** Create Random books from the above authors */
Book book1 = new Book("Book1",ruth,"368777540-2",10,2,20);
Book book2 = new Book("Book2",diane,"963099898-2",10,1,22);
Book book3 = new Book("Book3",jacqueline,"005382097-2",10,0,23);
Book book4 = new Book("Book4",rachel,"538310208-2",10,3,24);
Book book5 = new Book("Book5",joan,"562448132-2",10,4,26);
Book book6 = new Book("Book6",theresa,"670364563-2",10,2,21);
Book book7 = new Book("Book7",angela,"466916869-2",10,5,17);
Book book8 = new Book("Book8",helen,"764674973-2",10,0,15);
Book book9 = new Book("Book9",lisa,"052469721-2",10,6,17);
Book book10 = new Book("Book10",ruth,"609291817-2",10,3,13);
Book book11 = new Book("Book11",diane,"451378028-2",10,8,12);
Book book12 = new Book("Book12",jacqueline,"142352773-2",10,6,20);
Book book13 = new Book("Book13",rachel,"115135166-2",10,0,20);
Book book14 = new Book("Book14",joan,"631942468-2",10,3,20);
Book book15 = new Book("Book15",theresa,"323662444-2",10,0,23);
Book book16 = new Book("Book16",angela,"121360492-2",10,0,12);
Book book17 = new Book("Book17",helen,"391199302-2",10,0,20);
Book book18 = new Book("Book18",ruth,"549307784-2",10,1,20);
Book book19 = new Book("Book19",ruth,"368777230-2",10,23,20);
Book book20 = new Book("Book20",ruth,"793027213-2",10,0,20);
/** Add the books to a Book array*/
Book[] books = {book1,book2,book3,book4,book5,book6,book7,
book8,book9,book10,book11,book12,book13,book14,book15,
book16,book17,book18,book19,book20};/** Assign the book collection to the library */
Library library = new Library(books);
/** Librarian, theGuyWhoKnowsAlot, undertakes the operation of the library */
Librarian theGuyWhoKnowsAlot = new Librarian(library);
theGuyWhoKnowsAlot.findMeAvailableBooks();
theGuyWhoKnowsAlot.findMeBook("Book3");
theGuyWhoKnowsAlot.findMeBooksFromAuthor("Ruth");
theGuyWhoKnowsAlot.findMostPopularBook();
Compare your output to the one in the next slide
The following books are available at the library for renting
Books available for renting:
1. Book [title=Book1, author=Ruth, isbn=368777540-2, physicalCopies=10, availableCopies=2, timesRented=20]
2. Book [title=Book2, author=Diane, isbn=963099898-2, physicalCopies=10, availableCopies=1, timesRented=22]
3. Book [title=Book4, author=Rachel, isbn=538310208-2, physicalCopies=10, availableCopies=3, timesRented=24]
4. Book [title=Book5, author=Joan, isbn=562448132-2, physicalCopies=10, availableCopies=4, timesRented=26]
5. Book [title=Book6, author=Theresa, isbn=670364563-2, physicalCopies=10, availableCopies=2, timesRented=21]
6. Book [title=Book7, author=Angela, isbn=466916869-2, physicalCopies=10, availableCopies=5, timesRented=17]
7. Book [title=Book9, author=Lisa, isbn=052469721-2, physicalCopies=10, availableCopies=6, timesRented=17]
8. Book [title=Book10, author=Ruth, isbn=609291817-2, physicalCopies=10, availableCopies=3, timesRented=13]
9. Book [title=Book11, author=Diane, isbn=451378028-2, physicalCopies=10, availableCopies=8, timesRented=12]
10. Book [title=Book12, author=Jacqueline, isbn=142352773-2, physicalCopies=10, availableCopies=6, timesRented=20]
11. Book [title=Book14, author=Joan, isbn=631942468-2, physicalCopies=10, availableCopies=3, timesRented=20]
12. Book [title=Book18, author=Ruth, isbn=549307784-2, physicalCopies=10, availableCopies=1, timesRented=20]
13. Book [title=Book19, author=Ruth, isbn=368777230-2, physicalCopies=10, availableCopies=23, timesRented=20]
Book with title= 'Book3' found! Details:
Book [title=Book3, author=Jacqueline, isbn=005382097-2, physicalCopies=10, availableCopies=0, timesRented=23]
Book with author= 'Ruth' found! Details:
Book [title=Book1, author=Ruth, isbn=368777540-2, physicalCopies=10, availableCopies=2, timesRented=20]
Book with author= 'Ruth' found! Details:
Book [title=Book10, author=Ruth, isbn=609291817-2, physicalCopies=10, availableCopies=3, timesRented=13]
Book with author= 'Ruth' found! Details:
Book [title=Book18, author=Ruth, isbn=549307784-2, physicalCopies=10, availableCopies=1, timesRented=20]
Book with author= 'Ruth' found! Details:
Book [title=Book19, author=Ruth, isbn=368777230-2, physicalCopies=10, availableCopies=23, timesRented=20]
Book with author= 'Ruth' found! Details:
Book [title=Book20, author=Ruth, isbn=793027213-2, physicalCopies=10, availableCopies=0, timesRented=20]
Most popular book:
Book [title=Book5, author=Joan, isbn=562448132-2, physicalCopies=10, availableCopies=4, timesRented=26]
Book with title:'Book0' not found
Book with author:'angor' not found
Create a command-line user interface for Exercise 3, providing the user, at least, the following options:
Welcome to the Bootcamp library.
How do you want to proceed?
1. Show all available books
2. Search for a book (by book title)
3. Display books from a specific author
4. Show me the most popular book
q. Quit
>
Create (a fully detailed) class diagram for the entities described in exercise 3.