come 339 week 1. example: the course class 2 testcourceruncourse

14
COME 339 WEEK 1

Upload: paulina-price

Post on 19-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Designing the Loan Class 3 TestLoanClassRunLoan

TRANSCRIPT

Page 1: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

COME 339

WEEK 1

Page 2: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

2

Example: The Course Class

TestCource RunCourse

Course

-name: String -students: String[] -numberOfStudents: int

+Course(name: String) +getName(): String +addStudent(student: String): void +getStudents(): String[] +getNumberOfStudents(): int

The name of the course. The students who take the course. The number of students (default: 0).

Creates a Course with the specified name. Returns the course name. Adds a new student to the course list. Returns the students for the course. Returns the number of students for the course.

Page 3: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

3

Designing the Loan Class

TestLoanClass RunLoan

Loan

-annualInterestRate: double -numberOfYears: int -loanAmount: double -loanDate: Date +Loan() +Loan(annualInterestRate: double,

numberOfYears: int, loanAmount: double)

+getAnnualInterestRate(): double +getNumberOfYears(): int +getLoanAmount(): double +getLoanDate(): Date +setAnnualInterestRate( annualInterestRate: double): void +setNumberOfYears( numberOfYears: int): void +setLoanAmount( loanAmount: double): void +getMonthlyPayment(): double +getTotalPayment(): double

The annual interest rate of the loan (default: 2.5). The number of years for the loan (default: 1) The loan amount (default: 1000). The date this loan was created. Constructs a default Loan object. Constructs a loan with specified interest rate, years, and

loan amount. Returns the annual interest rate of this loan. Returns the number of the years of this loan. Returns the amount of this loan. Returns the date of the creation of this loan. Sets a new annual interest rate to this loan.

Sets a new number of years to this loan. Sets a new amount to this loan. Returns the monthly payment of this loan. Returns the total payment of this loan.

Page 4: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

Concepts of Object Oriented Programming

• Three main concepts of object-oriented praogramming

– Encapsulation

– Inheritance

– Polymorphism

Page 5: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

5

Superclasses and Subclasses

GeometricObject1

Circle4

Rectangle1

TestCircleRectangle

Run

GeometricObject -color: String -filled: boolean -dateCreated: java.util.Date

+GeometricObject() +GeometricObject(color: String,

filled: boolean) +getColor(): String +setColor(color: String): void +isFilled(): boolean +setFilled(filled: boolean): void +getDateCreated(): java.util.Date +toString(): String

The color of the object (default: white). Indicates whether the object is filled with a color (default: false). The date when the object was created.

Creates a GeometricObject. Creates a GeometricObject with the specified color and filled

values. Returns the color. Sets a new color. Returns the filled property. Sets a new filled property. Returns the dateCreated. Returns a string representation of this object.

Circle -radius: double

+Circle() +Circle(radius: double) +Circle(radius: double, color: String,

filled: boolean) +getRadius(): double +setRadius(radius: double): void +getArea(): double +getPerimeter(): double +getDiameter(): double +printCircle(): void

Rectangle -width: double -height: double

+Rectangle() +Rectangle(width: double, height: double) +Rectangle(width: double, height: double

color: String, filled: boolean) +getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(height: double): void +getArea(): double +getPerimeter(): double

Page 6: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

6

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

1. Start from the main method

animation

Page 7: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

7

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

2. Invoke Faculty constructor

animation

Page 8: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

8

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

3. Invoke Employee’s no-arg constructor

animation

Page 9: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

9

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends aPerson { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

4. Invoke Employee(String) constructor

animation

Page 10: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

10

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

5. Invoke Person() constructor

animation

Page 11: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

11

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

6. Execute println

animation

Page 12: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

12

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }}a class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

7. Execute println

animation

Page 13: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

13

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person { public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

8. Execute println

animation

Page 14: COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse

14

Trace Executionpublic class Faculty extends Employee { public static void main(String[] args) { new Faculty(); } public Faculty() { System.out.println("(4) Faculty's no-arg constructor is invoked"); }} class Employee extends Person {a public Employee() { this("(2) Invoke Employee’s overloaded constructor"); System.out.println("(3) Employee's no-arg constructor is invoked"); }  public Employee(String s) { System.out.println(s); }} class Person { public Person() { System.out.println("(1) Person's no-arg constructor is invoked"); }}

9. Execute println

animation