The Clock Example. 1. The Clock Example 2 3 public class Clock { // instance variables private int hr; private int min; private int sec; // constructors:

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 15: Generic Methods, Classes, and Array-Based Lists
Chapter 12: Classes and Data Abstraction
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CLASSES AND DATA ABSTRACTION
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Java – Methods Part II Lecture Notes 6. Objects, Classes and Computer Memory When a Java program is executing, the memory must hold: 1. Templates for.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Midterm Review 22C:21 Computer Science II. Problem 1 A Set ADT represents some subset of {1,2,..., n} for some natural number n. It supports the operations.
Chapter 8: User-Defined Classes and ADTs
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 12: Classes and Data Abstraction.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
CompSci 230 S Programming Techniques
CSC111 Quick Revision.
Software Development Java Classes and Methods
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
SELECTION STATEMENTS (1)
CS 302 Week 11 Jim Williams, PhD.
public class BankAccount{
User-Defined Classes and ADTs
Control Statement Examples
בניית מחלקות.
CSC 113 Tutorial QUIZ I.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Classes & Objects: Examples
د.سناء الصايغ الفصل الأول البرمجة الشيئية
SELECTION STATEMENTS (2)
בניית מחלקות.
Outline Boolean Expressions The if Statement Comparing Data
Chapter 8: User-Defined Classes and ADTs
#include <iostream.h>
Chapter 12: Classes and Data Abstraction
Presentation transcript:

The Clock Example

1. The Clock Example 2

3 public class Clock { // instance variables private int hr; private int min; private int sec; // constructors: same name as class name. No type. public Clock () { //default constructor setTime (0, 0, 0); } public Clock (int hours, int minutes, int seconds) { setTime (hours, minutes, seconds); } Variables and Constructors Calls a setter method

4 // instance methods public void setTime (int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) //check if hours exceeds 24 hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) //check if minutes exceeds 60 min = minutes; else min = 0; if (0 <= seconds && seconds < 60) //check if seconds exceeds 60 sec = seconds; else sec = 0; } //end of setTime Setters (1) Called method

5 // mutator methods public void incrementSeconds () { sec++; if (sec > 59) { sec = 0; incrementMinutes(); } // end if } // end incrementSeconds public void incrementMinutes () { min++; if (min > 59) { min = 0; incrementHours(); } // end if } // end incrementMinutes Setters (2) Calls another setter method

6 Setters (3) public void incrementHours () { hr++; if (hr > 23) hr = 0; } // end incrementHours } // end of Clock

7 // accessor methods public int getHours () { return hr; } //end of getHours public int getMinutes () { return min; } //end of getMinutes public int getSeconds () { return sec; } //end of getSeconds Accessors

8 public void printTime() { //prints time in the form hh:mm:ss if (hr < 10) System.out.print (“0”); System.out.print (hr + “:”); if (min < 10) System.out.print (“0”); System.out.print (min + “:”); if (sec < 10) System.out.print (“0”); System.out.print (sec); } // end printTime Display

9 Other Instance Methods public boolean equals (Clock otherClock) { //compare two times return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec) } //end equals public void makeCopy (Clock otherClock) { //object1 = object2 hr = otherClock.hr; min = otherClock.min; sec = otherClock.sec; } public Clock getCopy() { //get a copy of a given object Clock temp = new Clock(); temp.hr = hr; temp.min = min; temp.sec = sec; return temp; } } //end class

10 Clock -hr: int -min: int -sec: int + Clock() + Clock(int, int, int) + setTime (int, int, int):void + incrementSeconds(): void + incrementMinutes(): void + incrementHours(): void + getHours(): int + getMinutes(): int + getSeconds(): int + printTime(): void + equals(Clock):boolean + makeCopy(Clock):void + getCopy():Clock UML Diagram

11 Using in the Application Class import java.util.*; public class clockApplication { static Scanner read = new Scanner (System.in); static final int SIZE = 100; public static void main (String[] args) { int ndx; //instantiate arrays of objects Clock[] arrivalTime = new Clock(SIZE); Clock[] departTime = new Clock(SIZE); //instantiate the object elements of arrays for (ndx = 0; ndx < arrivalTime.length; ndx++) { //invoke constructor with hr = 8, min = 30 and sec = 0 arrivalTime[ndx] = new Clock(8, 30, 0); // invoke constructor with hr = 15, min 30 and sec = 0 departTime[ndx] = new Clock(15, 30, 0); } //end for // update the arrival times of late employees lateEmployees(arrivalTime);//process the whole array

12 Using in the Application Class // check if two employees leave at the same time to take the same bus for example int ID1= -1, ID2 = -1; boolean sameTime = false; System.out.println (“Enter ID of employee1”); ID1 = read.nextInt(); System.out.println (“Enter ID of employee2”); ID2 = read.nextInt(); sameTime = departTime[ID1].equals(departTime[ID2]); //Copy the arrival information of empLoyee ID1 to employee ID2 arrivalTime[ID2].makeCopy(arrivalTime[ID1]); //Get a copy of the departure information of employee ID2 Clock tempInfo = new Clock(); tempInfo = getCopy(departTime[ID2]); // specify the employees that deserve an overtime //create a parallel array boolean[] overTime = new boolean[SIZE]; overTimedEmployees(departTime, overTime); } // end main

13 Using in the Application Class public static void lateEmployees(Clock[] arrival) { int employeeID = -1, arrivalHour = 0, arrivalMinute = 0, arrivalSecond = 0; do { // Assume ndx is used as employee ID System.out.println (“Enter employee ID or -1 to exit”); employeeID = read.nextInt(); if (employeeID == -1) break; System.out.println (“Enter employee arrival hour”); arrivalHour = read.nextInt(); System.out.println (“Enter employee arrival minute”); arrivalMinute = read.nextInt(); System.out.println (“Enter employee arrival seconds”); arrivalSecond = read.nextInt(); // Update employee’s data in the array. Assume ID = ndx arrival[employeeID].setTime(arrivalHour, arrivalMinute, arrivalSecond); } while (employeeID != -1); }//end lateEmployees

14 Using in the Application Class public static void overTimedEmployees(Clock[] depart, boolean[] bonus) { int ndx; Clock template = new Clock(15, 30, 0); for (ndx = 0; ndx < depart.length; ndx++) if (depart.hr > template.hr) { bonus[ndx] = true; System.out.printf (“%d deserves a bonus %n”, ndx); } //end if }//end overTimedEmployees }//end class It is recommended that you try the previous example on the computer