• Tidak ada hasil yang ditemukan

Inheritance & Abstract Classes

N/A
N/A
Protected

Academic year: 2025

Membagikan "Inheritance & Abstract Classes"

Copied!
20
0
0

Teks penuh

(1)

CSC 113   Tutorial 6

Inheritance & Abstract Classes

(2)

# mobileNb : String

# startingDate : String + Employee()

+ calculateSalary(): double

+ subInfo(): String

+ display(): void

Faculty - nbTeachingHours: int

- hourlyRate: double

- coursesList: Course[]

- nbCourses: int + Faculty()


+ Faculty(hRate: double, size: int) 
+ addCourse(c: Course): void

+ deleteCourse(title: String): void

+ calculateSalary(): double

+ subInfo(): String

Staff - nbWorkingDays: int

- dailyRate: double

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String Course

- title : String

- nbHours : int + Course()

+ Course(title: String, h: int)

+ display()


+ read()
(3)

Abstract class and abstract methods

Employee {abstract}

# id : String

# name : String

# mobileNb : String

# startingDate : String + Employee()

+ calculateSalary(): double

+ subInfo(): String

+ display(): void public abstract class Employee {

protected String name;

protected String id;

protected String mobileNb;

protected String startingDate;

public Employee()

{

}

public abstract double calculateSalary();

public abstract String subInfo();

(4)

Final display calls subInfo

# mobileNb : String

# startingDate : String + Employee()

+ calculateSalary(): double

+ subInfo(): String

+ display(): void

public final void display() {

System.out.println("Name: "+name);

System.out.println("ID: "+id);

System.out.println("Mobile: "+mobileNb);

System.out.println("Starting date: "+startingDate);

System.out.println(subInfo());

}

(5)

Staff - nbWorkingDays: int - dailyRate: double - tasks: String[]

- nbTasks: int

+ Staff()


 + Staff(nbDays: int, dRate: double, size: int) + calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

(6)

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

public class Staff extends Employee{

private int nbWorkingDays;

private double dailyRate;

private String[] tasks;

private int nbTasks;

public Staff() {

}

Declare variables and default

constructor

(7)

Staff - nbWorkingDays: int

- dailyRate: double

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

Copy Constructor

public Staff(int nbDays, double dRate, int size) {

this.dailyRate = dRate;

this.nbWorkingDays = nbDays;

this.tasks = new String[size];

}

(8)

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

Add task

public void addTask(String t) {

if(nbTasks < tasks.length) {

this.tasks[nbTasks++] = t;

System.out.println("Add operation successful.");

return;

}

System.out.println("Error: Courses list is full.");

}

(9)

Staff - nbWorkingDays: int

- dailyRate: double

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

Delete

public void deleteTask(String task) {

int i = search(task);

if(i==-1) {

System.out.println("Error: Course does not exist.");

return;

}

tasks[i] = tasks[--nbTasks];

tasks[nbTasks] = null;

System.out.println("Delete operation successful.");

return;

}

(10)

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

private int search(String title) {

int i;

for(i=0; i< tasks.length; i++) {

if(tasks[i] != null)

if(tasks[i].equals(title))

return i;

}

return -1;

}

internal use only.

(11)

Staff - nbWorkingDays: int

- dailyRate: double

- tasks: String[]

- nbTasks: int + Staff()


+ Staff(nbDays: int, dRate:

double, size: int)

+ calculateSalary(): double

+ addTask(task: String): void

+ deleteTask(task: String): void

+ subInfo(): String

public double calculateSalary() {

return nbWorkingDays * dailyRate;

}

public String subInfo() {

return "Working days : " + nbWorkingDays + "\nDaily rate: "+ dailyRate+

"\n# of tasks: " + nbTasks;

}

CalculateSalary and subInfo

implementation

(12)

- title : String

- nbHours : int + Course()

+ Course(title: String, h: int)

+ display()


+ read()

(13)

Declare variables and default

constructor and copy constructor

public class Course { private String title;

private int nbHours;

public Course() {

}

public Course(String title,int h) {

this.title = title;

nbHours = h;

}

Course - title : String

- nbHours : int + Course()

+ Course(title: String, h: int)

+ display()


+ read()
(14)

Display and read

+ Course()

+ Course(title: String, h: int)

+ display()


+ read()

public void display() {

System.out.print("title: " + title + " Hours: "+nbHours);

}

public void read() {

Scanner s = new Scanner(System.in);

System.out.println("Enter couse name: ");

title = s.next();

System.out.println("Enter number of hours: ");

nbHours = s.nextInt();

}

(15)

Faculty - nbTeachingHours: int - hourlyRate: double - coursesList: Course[]

- nbCourses: int

+ Faculty()


 + Faculty(hRate: double, size: int) 
 + addCourse(c: Course): void

+ deleteCourse(title: String): void + calculateSalary(): double

+ subInfo(): String

(16)

constructor and copy constructor

public class Faculty extends Employee{

private int nbTeachingHours;

private double hourlyRate;

private Course[] coursesList;

private int nbCourses;

public Faculty() {

}

public Faculty(double hRate, int size) {

this.hourlyRate = hRate;

this.coursesList = new Course[size];

nbTeachingHours = 0;

}

- coursesList: Course[]

- nbCourses: int + Faculty()


+ Faculty(hRate: double, size: int) 
+ addCourse(c: Course): void

+ deleteCourse(title: String): void

+ calculateSalary(): double

+ subInfo(): String
(17)

addCourse should add the course and also add the hours.

Faculty - nbTeachingHours: int

- hourlyRate: double

- coursesList: Course[]

- nbCourses: int + Faculty()


+ Faculty(hRate: double, size: int) 
+ addCourse(c: Course): void

+ deleteCourse(title: String): void

+ calculateSalary(): double

+ subInfo(): String

public void addCourse(Course c) {

if(nbCourses < coursesList.length) {

coursesList[nbCourses++] = c;

nbTeachingHours += c.getNbHours();

System.out.println("Add operation successful.");

return;

}

System.out.println("Error: Courses list is full.");

}

(18)

given course title and subtract the hours.

- coursesList: Course[]

- nbCourses: int + Faculty()


+ Faculty(hRate: double, size: int) 
+ addCourse(c: Course): void

+ deleteCourse(title: String): void

+ calculateSalary(): double

+ subInfo(): String

public void deleteCourse(String title) {

int i = search(title);

if(i==-1) {

System.out.println("Error: Course does not exist.");

return;

}

nbTeachingHours -= coursesList[i].getNbHours();

coursesList[i] = coursesList[--nbCourses];

coursesList[nbCourses] = null;

System.out.println("Delete operation successful.");

return;

}

(19)

Search is a private method for internal use only.

Faculty - nbTeachingHours: int

- hourlyRate: double

- coursesList: Course[]

- nbCourses: int + Faculty()


+ Faculty(hRate: double, size: int) 
+ addCourse(c: Course): void

+ deleteCourse(title: String): void

+ calculateSalary(): double

+ subInfo(): String

private int search(String title) {

int i;

for(i=0; i< coursesList.length; i++) {

if(coursesList[i] != null)

if(coursesList[i].getTitle().equals(title))

return i;

}

return -1;

}

(20)

- coursesList: Course[]

- nbCourses: int + Faculty()


+ Faculty(hRate: double, size: int) 
+ addCourse(c: Course): void

+ deleteCourse(title: String): void

+ calculateSalary(): double

+ subInfo(): String

public double calculateSalary() {

return nbTeachingHours * hourlyRate;

}

public String subInfo() {

return "Teaching Hours: " + nbTeachingHours + "\nHourly rate: "+ hourlyRate+

"\n# of courses: " + nbCourses;

}

CalculateSalary and subInfo

implementation

Referensi

Dokumen terkait

 arr.sisip(“Rico” string belakang , ”Chandra” string depan , 43 int usia ) =&gt; variabel arr memanggil fungsi void sisip dengan menempatkan nilai 77

int ex_idx_max; double[,] ex_jlh_detail; string[] ex_sma; double[,,] ex_dta_lls; DataGridView ex_dgv_training; int in_atribute, node_flag = 0; public DataTestingint

int size(); boolean isEmpty(); // Bulk Operations void putAll(Map t); void clear(); // Collection Views public Set keySet();. public Collection

An abstract method is a method that is declared without an implementation without braces, and followed by a semicolon, like this: abstract void moveTodouble deltaX, double deltaY;

class Bag { public: Bag int bagCapacity = 10; // constructor ~Bag; // destructor int Size const; bool IsEmpty const; int Element const; void Pushconst int; void Pop; private:

The switch Statement EXAMPLE 2 public static void main String[] args { //Declaration section Scanner read = new Scanner System.in; int choice, num1, num2, result = -1; String

 perTablet_mg: Number of milligrams of the medicine per tablet o Methods:  Tabletname: String, expiry: String, perKGDosage: double, perPacketTables: int, perTablet_mg: double:

Jawaban: a Atribut/Field private int mProductID; private String mProductName; private double mPrice; private int mQuantity; b Konstruktor public Productint productID, String