L15 - Computer Science 1 1 SS C Lecture 15 Contents 2...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CompSci 101 - Lecture 15 1 Computer Science 1 1 SS C Lecture 15 Contents Practice defning a class - Time24Hr class toString() method public/private modifer – not examinable accessor methods mutator methods Coursebook: §13 1 2 3 4 2 Time24Hr - instance variables Consider defning a class to represent hours and minutes on a 24 hour clock. public class Time24Hr { } private int hour; private int minutes; The instance variables represent the inFormation which all instances oF the Time24Hr class will contain. 3 Time24Hr - constructor The constructor is called whenever a new instance oF the Time24Hr class is required. 1 2 3 4 5 6 7 8 public class Time24Hr { } public Time24Hr(int h, int m) { } private int hour; private int minutes; hour = h; minutes = m; 4 Time24Hr - instance methods Decide which operations are possible on objects oF this class? addToMinutes() : add some minutes to the time stored in the object. isAM() : return true iF the time is an AM time, return False otherwise. setMinutes() : change the value stored in the minutes variable oF the Time24Hr object. setHour() : change the value stored in the hour variable oF the Time24Hr object. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 Time24Hr - instance methods public class Time24Hr { public void setHour( int h ) { hour = h; } public void setMinutes( int m ) { minutes = m; } public Time24Hr( int h, int m ) { hour = h; minutes = m; } private int hour; private int minutes; 6 Time24Hr - instance methods 14 15 16 17 18 19 20 21 22 23 24 25 26 } public void addToMinutes( int moreM ) { minutes = minutes + moreM; hour = hour + minutes/60; hour = hour%24; minutes = minutes%60; } public boolean isAM() { if (hour < 12) { return true; } return false; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CompSci 101 - Lecture 15 2 1 2 3 4 5 6 7 8 7 Ex01 - Draw the two objects public class L15Program { public void start() { } } time1 time2 Time24Hr time1 = new Time24Hr(hrs, mins); Time24Hr time2 = new Time24Hr(9, 20); int hrs = 10; int mins = 15; hour minutes hour minutes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 8 Ex02 - What is the output? public class L15Program {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 5

L15 - Computer Science 1 1 SS C Lecture 15 Contents 2...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online