• Tidak ada hasil yang ditemukan

Computer Programming

N/A
N/A
Protected

Academic year: 2025

Membagikan "Computer Programming"

Copied!
4
0
0

Teks penuh

(1)

King Saud University

College of Computer and Information Sciences Department of Computer Science

CSC113 – Computer Programming II – Tutorial No 5 – Spring 2017

Counter class:

Attributes:

 start: starting value of the counter

 count: the current value of the counter Methods:

 Counter(value: int): constructor

 increment(): this method increments the count by one

 reset(): this method resets the count to its starting value

 toString(): this method returns the object info in the following format ‘( count )’

AdvancedCounter class:

Attributes:

 amount: the amount to increment the count with Methods:

 AdvancedCounter(value: int, amount: int): constructor

 increment():this method increments the count by amount

(2)

King Saud University

College of Computer and Information Sciences Department of Computer Science

CSC113 – Computer Programming II – Tutorial No 5 – Spring 2017

LimitedCounter class:

Attributes:

 limit: the upper limit that the counter stops at Methods:

 LimitedCounter(value: int, limit: int): constructor

 increment(): this method increments the count by one only if the limit hasn’t been reached. It prints a message saying the limit has been reached otherwise.

Exercise 1: Translate into Java-code the class Counter, AdvancedCounter, and LimitedCounter.

Exercise 2: Write a test class with a main method and do the following:

- create three objects of type Counter, AdvancedCounter and LimitedCounter

- increment the three of them once - print the value of the counter for each

- increment the LimitedCounter object to its limit - reset it and print its value

(3)

King Saud University

College of Computer and Information Sciences Department of Computer Science

CSC113 – Computer Programming II – Tutorial No 5 – Spring 2017

public class Counter { protected int start;

protected int count;

public Counter(int value) {

this.start = this.count = value;

}

public void increment() { count++;

}

public void reset() { count = start;

}

public String toString() { return "(" + count + ")";

} }

public class AdvancedCounter extends Counter { private int amount;

public AdvancedCounter(int value, int amount) { super(value);

this.amount = amount;

}

public void increment() { count += amount;

} }

public class LimitedCounter extends Counter { private int limit;

public LimitedCounter(int value, int limit) { super(value);

this.limit = limit;

}

public void increment() { if (count >= limit) {

System.out.println("Reached limit, cannot increment anymore.");

} else {

count++;

} }

}

(4)

King Saud University

College of Computer and Information Sciences Department of Computer Science

CSC113 – Computer Programming II – Tutorial No 5 – Spring 2017

public class TestCounter {

public static void main(String[] args) { Counter basic = new Counter(0);

Counter limited = new LimitedCounter(0, 3);

Counter advanced = new AdvancedCounter(0, 2);

basic.increment();

limited.increment();

limited.increment();

limited.increment();

limited.increment();

advanced.increment();

System.out.println(basic.toString());

System.out.println(limited.toString());

System.out.println(advanced.toString());

limited.reset();

System.out.println(limited.toString());

} }

Referensi

Dokumen terkait

Mirza *, Mohammed Al-Abdulkareem College of Computer and Information Sciences, King Saud University, Riyadh, Saudi Arabia Received 12 February 2010; accepted 9 February 2011 Available

This con- firms the commitment of our journal, Journal of King Saud University – Computer and Information Sciences, to abiding by the high international standards of ethics and

Program title and code: Computer Sciences and Information/ CSC Dean of the College Computer Sciences department Structure of Computer Sciences and Information department

ةيساردلا جهانملاو ططخلا ةنجل 221 Course Specifications Institution: KING KHALID UNIVERSITY , Abha Date: 24/02/2019 College/Department : COLLEGE OF COMPUTER SCIENCE / DEPARTMENT OF

H Computer Science, Semester II Lab Exercises BHCS03 – Programming in JAVA Attempt the following questions as part of JAVA Lab class today: 1.. Write a program that will count

H Computer Science, Semester II Lab Exercises BHCS03 – Programming in JAVA Attempt the following questions as part of JAVA Lab class today: 1.. Write a program that prompts the

Member, Committee of recruitments and promotions Department of Biochemistry, College of Science, Sciences & Medical Studies Department for Women Students, King Saud University,

Research & Teaching Experiences: King Saud University - College of Computer & Information Sciences, Computer Engineering Department August 2013 Assistant Professor Courses Name: