Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defining Classes and Methods CS140: Introduction to Computing 1 Savitch Chapter 5, 6 9/30/13.

Similar presentations


Presentation on theme: "Defining Classes and Methods CS140: Introduction to Computing 1 Savitch Chapter 5, 6 9/30/13."— Presentation transcript:

1 Defining Classes and Methods CS140: Introduction to Computing 1 Savitch Chapter 5, 6 9/30/13

2 Abstraction and Generalization 2 CLASS NAME METHODS ATTRIBUTES UML Class Diagram

3 Abstraction and Generalization 3 CLASS NAME METHODS ATTRIBUTES Classes have no data, they’re just a structure, specifying what object of that class (type) will look like, what variables and methods they will have available. The individual objects have the data. Objects of a class are variables of that type.

4 Objects are instances of classes 4

5 Tic Tac Toe – structure code design 5

6 Class Files Each class should be in a.java file by itself – the name of the class is the name of the file – e.g. Person.java Classes can compile without a program – compiling creates a file of type.class – e.g. Person.class – no need to recompile The file that holds the main method (your actual executable program) is also in a class – the name is the name of the Project 6

7 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 7 MEMBERS

8 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 8 INASTANCE VARIABLES PUBLIC – no restrictions on how they’re used

9 new Person p_1 = new Person(); new: – reserved word in Java – creates an object of a class – returns the memory address of the object, which is assigned to the variable – places instance variables (when applicable) inside the object 9

10 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 10 INASTANCE VARIABLES PUBLIC – no restrictions on how they’re used Person p_1 = new Person(); p_1.name = “Ellie”; p_1.role = “teacher”; p_1.major = “”; p_1.gender = ‘F’; p_1.age = 100; System.out.print( “My name is “ + p_1.name ); each object (instance) has its own set of instance variables and methods

11 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 11 INASTANCE VARIABLES Person p_1 = new Person(); p_1.name = “Ellie”; p_1.role = “teacher”; p_1.major = “”; p_1.gender = ‘F’; p_1.age = 100; each object (instance) has its own set of instance variables and methods Person p_3 = new Person(); p_3.name = “Olaf”; p_3.role = “Austauschstudent”; p_3.major = “CS”; p_3.gender = ‘M’; p_3.age = 21;

12 Methods Methods are called Methods can be passed input values (parameters) Methods can return a value or an object (but don’t have to) Naming convention: start with lower case Try to use just one return statement per method 12

13 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 13 local variable

14 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 14 METHODS Person p_1 = new Person(); p_1.speak( “Lalala” ); p_1.age = 100; int godAge = p_1.ageInDogYears();

15 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 15 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.speak( “Lalala” ); p_1.age = 100; int dogAge = p_1.ageInDogYears(); System,out.println(p_1.ageInDogYears()); } -dot notation -don’t forget the brackets! -no assignment if the method is void -assignment if there is a return value or use directly

16 this this: – reserved word in Java – inside the definition of a method, points to the object receiving the method call (usually optional, Java lets you omit it) 16 public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } public int ageInDogYears() { int ageAlternative = this.age/7; return ageAlternative; }

17 So far… Read chapter 5.1 17

18 Java method syntax Required: – Return type – data type of the value returned or void if the method returns nothing – Name – convention: verb in lowercase (e.g. speak()) or multi-word name beginning with verb in lower case (e.g. sayThis()) – use camelCase – Parentheses () after the name – Body between braces {} Optional: – Modifiers – public, private, abstract, static, final, native, strictfp, synchronized – Parameter list in the parentheses 18

19 Java method syntax modifier parentheses return type name public double calculateAnswer(double wingSpan, int numberOfEngines, double length, double grossTons) { //do the calculation here } parameter list body in braces 19

20 Types of Java methods public void speak() { System.out.print(“Hi, I’m here”); } public void speak(String sayThis) { System.out.print(sayThis); } public String speak() { return “Hi, I’m here”; } 20 public String speak(String sayThis) { String words = "I said: " + sayThis; return words; } public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; }

21 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak() { System.out.print( “Hi, I’m here” ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 21 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.speak(); } Hi, I’m here.

22 public class Person { public String name; public String role; public String major; public char gender; public int age; public void speak( String sayThis ) { System.out.print( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 22 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.speak( “Hi, I’m here” ); } Hi, I’m here.

23 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak() { return “Hi, I’m here”; } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 23 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String words = p_1.speak(); System.out.print( words ); } Hi, I’m here.

24 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak(String sayThis) { String words = "I said: " + sayThis; return words; } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 24 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String words = p_1.speak( “Hi, I’m here” ); System.out.print( words ); } I said: Hi, I’m here.

25 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; } } 25 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String words = p_1.speak(“Hi, I’m here”, 2); System.out.print( words ); } Hi, I’m here. Hi, I’m here.

26 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; } } 26 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String say = “Hi, I’m here”; int howMany = 2; String words = p_1.speak(say, howMany); System.out.print( words ); } Hi, I’m here. Hi, I’m here.

27 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; } } 27 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String words = p_1.speak(“Hi, I’m here”, 2); System.out.print( words ); } arguments (in method call) parameters (in method definition) called formal parameters

28 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; } } 28 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String words = p_1.speak(“Hi, I’m here”, 2); System.out.print( words ); } When you call (invoke) a method, the arguments MUST match the parameters in the method heading in their number, order and data types arguments (in method call) parameters (in method definition)

29 Variable scope 29 public class Person { public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; } } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String say = “Hi, I’m here”; int howMany = 2; String words = p_1.speak(say, howMany); System.out.print( words ); }

30 Variable scope 30 public class Person { public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + “ “ + sayThis; return words; } } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String say = “Hi, I’m here”; int howMany = 2; String words = p_1.speak(say, howMany); System.out.print( words ); } Variables are local to the block they are declared in and can be used only in that block = scope of the variable

31 Variable scope 31 public class Person { public String speak(String sayThis, int times) { String words = ""; for (int i = 0; i < times; i++) words = words + “ “ + sayThis; return words; } } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String say = “Hi, I’m here”; int howMany = 2; String words = p_1.speak(say, howMany); System.out.print( words ); }

32 Variable scope 32 public class Person { public String speak(String sayThis, int times) { String words = ""; for (int i = 0; i < times; i++) words = words + “ “ + sayThis; return words; } } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); String say = “Hi, I’m here”; int howMany = 2; String words = p_1.speak(say, howMany); System.out.print( words ); } Parameter names are local to the method

33 public class Person { public String name; public String role; public String major; public char gender; public int age; public String speak(String sayThis) { String words = ""; for ( int I = 0; I < age; i++ ) words = words + " " + sayThis; return words; } } 33 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.age = 3; String words = p_1.speak("Hi, I’m here"); System.out.print( words ); } Hi, I’m here. Hi, I’m here. Hi, I’m here.

34 Methods Use meaningful descriptive names for your methods Comment 34 /* * descr: takes a phrase and a number of times to repeat it and creates a new phrase by concatenation * input: String sayThis – the phrase to be repeated * int times – the number of times to repeat the phrase * returns: String – the concatenated phrase */ public String speak(String sayThis, int times) { String words = ""; for (int i=0; i<times; i++) words = words + " " + sayThis; return words; }

35 The public and private modifiers public and private are access modifiers public – applied to a class, method or instance variable means that any other class can directly use or access the class, method, or instance variable by name 35

36 The public and private modifiers public – applied to a class, method or instance variable means that any other class can directly use or access the class, method, or instance variable by name  Having public classes and methods is bad programming practice  All instance variables should be private 36

37 public class Person { public String name; public String role; public String major; public char gender; private int age; public void speak( String sayThis ) { System.out.println( sayThis ); } public int ageInDogYears() { int ageAlternative = age/7; return ageAlternative; } 37 Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - age has private access… public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.name = “Ellie”; p_1.age = 100; } ok When a variable is private, it is NOT accessible outside of the class definition (i.e. CANNOT make any direct reference to its name) but it’s freely accessible inside the entire class definition

38 public class Person { public String name; public String role; public String major; public char gender; private int age; public void setAge( int inputAge ) { if ( inputAge <=0 ) { System.out.print(“neg age!”); System.exit( 0 ); } age = inputAge; } public int getAge() { return age; } 38 public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(10); System.out.print( p_1.getAge() ); } 10

39 public class Person { public String name; public String role; public String major; public char gender; private int age; public void setAge( int inputAge ) { if ( inputAge <=0 ) { System.out.print(“neg age!”); System.exit( 0 ); } age = inputAge; } public int getAge() { return age; } 39 public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(10); System.out.print( p_1.getAge() ); } get method (access method) – lets you access private instance variables set method (mutator method) – lets you change the value of private instance variables

40 The public and private modifiers private – applied to a method or instance variable means that NO OTHER class can directly use or access the method or instance variable by name, but they can be accessed within the parent class  It’s a good idea to make helper methods that don’t need to be accessed outside the class private  Do NOT declare outer classes private, they will be unusable, as no one would be able to access them 40

41 public class Person { public String name; public String role; public String major; public char gender; private int age; public void setAge( int inputAge ) { age = inputAge; } private double ageInDogYears() { double ageAlternative = age/7.0; return ageAlternative; } 41 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); double ageInDog = p_1.ageInDogYears(); } ok Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - ageInDogYears() has private access… When a method is private, it is NOT accessible outside of the class definition (i.e. CANNOT make any direct reference to its name) but it’s freely accessible inside the entire class definition

42 public class Person { private int age; public void setAge( int inputAge ) { age = inputAge; } public double getAgeInDogYears() { double ageInDogYears = ageInDogYears(); return ageInDogYears; } private double ageInDogYears() { double ageAlternative = age/7.0; return ageAlternative; } 42 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); double ageInDog; ageInDog = p_1.getAgeInDogYears(); System.out.print( ageInDog ); } If a method calls a method in the same class, there is no need for a receiving object (no dot notation)

43 public class Person { private int age; public void setAge( int inputAge ) { age = inputAge; } public double getAgeInDogYears() { double ageInDogYears = this.ageInDogYears(); return ageInDogYears; } private double ageInDogYears() { double ageAlternative = age/7.0; return ageAlternative; } 43 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); double ageInDog; ageInDog = p_1.getAgeInDogYears(); System.out.print( ageInDog ); }

44 public class Person { private int age; public void setAge( int inputAge ) { age = inputAge; } public double getAgeInDogYears() { double ageInDogYears = ageInDogYears(); return ageInDogYears; } private double ageInDogYears() { double ageAlternative = age/7.0; return ageAlternative; } 44 METHODS public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); double ageInDog; ageInDog = p_1.getAgeInDogYears(); System.out.print( ageInDog ); } 14.285714285714286 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12 13

45 So far… Chapter 5.1 Chapter 5.2 45

46 Primitive Types in Java Type NameValuesMemoryRange of ValuesOperations* byte integer1 byte-128 to 127= + - / * short integer2 bytes-32,768 to 32,767 int integer4 bytes-2,147,483,648 to 2,147,483,647= + - / * long integer8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 = + - / * float real4 bytes±3.40282347 × 10 38 to ±1.40239846 × 10 -45 = + - / * double real8 bytes±1.79769313486231570 × 10 308 to ±4.94065645841246544 × 10 -324 = + - / * char Single character 2 bytesA B C a b c 1 2 3 ! @ # $. © Δ Ω Ψ Å= boolean Boolean1 bitTrue or False= 46 Based on Table 2.1 on page 52 * An incomplete set of operations

47 Class (Reference) Types in Java Every variable regardless of type is saved in a location in memory Primitive types – data value is stored in the memory location assigned to the variable Class types – contains memory address of the object named by the variable – object is not stored in the variable, it’s stored some place else in memory – the address of this other location is called a REFERENCE to the object 47

48 Class (Reference) Types in Java What this looks like in memory….. 48

49 Class (Reference) Types in Java String s = "ola"; String t = "OLE"; System.out.println(s); System.out.println(t); s = t; System.out.println(s); System.out.println(t); 49 ola OLE

50 Class (Reference) Types in Java String s = "ola"; String t = "OLE"; System.out.println(s); System.out.println(t); s = t; System.out.println(s); System.out.println(t); 50 ola OLE

51 Class (Reference) Types in Java 51 public class Person { private int age; public void setAge( int inputAge ) { age = inputAge; } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); Person p_2 = new Person(); p_2.setAge(100); System.out.print( p_1 == p_2 ); } false

52 Defining a method equals() 52 public class Person { private int age; public void setAge( int inputAge ) { age = inputAge; } public boolean equals( Person anotherObject ) { if ( this.age == anotherObject.age ) return true; else return false; } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); Person p_2 = new Person(); p_2.setAge(100); System.out.print( p_1.equals(p_2) ); } true always use the name equals for this type of method, so Java doesn’t auto-create it for you because it might not do what you need it to do

53 this 53 public class Person { private int age; public void setAge( int inputAge ) { this.age = inputAge; } public boolean equals( Person anotherObject ) { if ( this.age == anotherObject.age ) return true; else return false; } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); Person p_2 = new Person(); p_2.setAge(100); System.out.print( p_1.equals(p_2) ); }

54 Primitive types as arguments 54 public class Person { private int age; public void setAge( int inputAge ) { this.age = inputAge; inputAge = 500; } public boolean equals( Person anotherObject ) { if ( this.age == anotherObject.age ) return true; else return false; } public class PersonDemo { public static void main(String[] args) { int age = 100; Person p_1 = new Person(); p_1.setAge( age ); System.out.print( age ); } 100

55 Primitive types as arguments Primitive type arguments cannot be changed by the method they are passed to 55

56 Objects as arguments 56 public class Person { private int age; public void setAge( int inputAge ) { this.age = inputAge; } public int getAge() { return age; } public boolean equals( Person anotherObject ) { if ( this.age == anotherObject.age ) return true; else return false; } public void makeEqual( Person anotherObject ) { anotherObject.age = this.age; } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); Person p_2 = new Person(); p_2.setAge(50); p_1.makeEqual(p_2); System.out.print( p_2.getAge() ); } 100

57 Objects as arguments Unlike primitive type arguments, the state of objects passed as arguments can be changed by the method they are passed to 57

58 Objects as arguments 58 public class Person { private int age; public void setAge( int inputAge ) { this.age = inputAge; } public int getAge() { return age; } public boolean equals( Person anotherObject ) { if ( this.age == anotherObject.age ) return true; else return false; } public void makeSame( Person anotherObject ) { anotherObject = this; } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); Person p_2 = new Person(); p_2.setAge(50); p_1.makeSame(p_2); System.out.print( p_2.getAge() ); } 50

59 Objects as arguments Objects passed as arguments cannot be replaced by the method they are passed to Change state (values of instance variables) – yes Replace the object - no 59

60 Defining Boolean methods 60 public class Person { private int age; public void setAge( int inputAge ) { age = inputAge; } public boolean isOld() { if ( this.age >=65 ) return true; else return false; } public class PersonDemo { public static void main(String[] args) { Person p_1 = new Person(); p_1.setAge(100); boolean old = p_1.isOld(); System.out.println( old ); System.out.println( p_1.isOld() ); if ( p_1.isOld() ) System.out.println( “you are old” ); } true you are old true you are old

61 class MyTime 61 class MyTime { private int hour; private int min; MyTime() { hour = 0; min = 0; } } CONSTRUCTOR

62 What’s wrong? 1.wrong type 2.can’t access private member 3.Illegal value 4.logic 5.nothing 62 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.hour = 25; }

63 class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void setHour(int h){ hour = h; } } What’s wrong? 1.wrong type 2.can’t access private member 3.Illegal value 4.logic 5.nothing 63 class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setHour(25); }

64 What’s the output? 1.25:0 2.25:10 3.0:0 4.0:10 5.nothing 64 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setHour(25); myTime.setMin(10); }

65 What’s the output? 1.25:0 2.25:10 3.0:0 4.0:10 5.nothing 65 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public String toString(){ String tStr; tStr = hour + ":" + min; return tStr; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setHour(25); myTime.setMin(10); System.out.println(myTime.toString()); }

66 What’s the output? 1.8:0 2.8:60 3.0:-2 4.0:0 5.nothing 66 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public String toString() { String tStr; tStr = hour + ":" + min; return tStr; } public void setHour(int h) { if(h = 0) hour = h; else hour = 0; } public void setMin(int m) { if(m = 0) min = m; else System.exit(-2); } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setHour(8); myTime.setMin(60); System.out.println(myTime.toString()); }

67 What’s the output? 1.10:08 2.10:8 3.0:0 4.nothing 67 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public String toString(){ String timeString; if (min < 10) timeString = hour + ":0" + minute; else timeString = hour + ":" + minute; return timeString; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setHour(10); myTime.setMin(8); System.out.println(myTime.toString()); }

68 What’s the output? 1.10:08 2.10:8 3.0:00 4.nothing 68 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public String toString(){ String timeString; if (min < 10) timeString = hour + ":0" + min; else timeString = hour + ":" + min; return timeString; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } class TimeDemo { public static void main(String args) { MyTime time1 = new MyTime(); MyTime time2 = new MyTime(); time2.setHour(10); time2.setMin(8); System.out.println(time1.toString()); }

69 What’s the output? 1.25:0 2.25:10 3.0:0 4.0:10 5.nothing 69 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } public String toString() { return hour + ":" + min; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setHour(25); myTime.setMin(10); System.out.print(myTime.toString()); }

70 class MyTime { private int hour; private int min; public void setHour(int h) { if(h = 0) hour = h; else hour = 0; } public void setMin(int m) { if(m = 0) min = m; else System.exit(-2); } public String toString() { String tStr; tStr = hour + ":" + min; } 70 MyTime - hour : int - minute : int + toString() : String + add(Time) : void + addMinutes(int) : void + setMin(int) : void + setHour(int) : void

71 What’s the output? 1.0:0 2.0:10 3.0:20 4.nothing 71 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } // omit: setHour() // omit: setMin() public String toString(){ String tStr; tStr = hour + ":" + min; } public void addMin(int m){ min += m; } } // end of MyTime class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setMin(10); myTime.addMin(10); System.out.print(myTime.toString()); }

72 What’s the output? 1.0:00 2.0:60 3.0:20 4.nothing 72 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } // omit: setHour() // omit: setMin() public String toString(){ String tStr; tStr = hour + ":" + min; } public void addMin(int m){ min += m; } } // end of MyTime class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setMin(10); myTime.addMin(50); System.out.print(myTime.toString()); }

73 What’s the output? 1.0:10 2.0:-10 3.0:20 4.nothing 73 public class MyTime { private int hour; private int min; MyTime(){ hour = 0; min = 0; } // omit: setHour() // omit: setMin() public String toString(){ String tStr; tStr = hour + ":" + min; } public void addMin(int m){ min += m; } } // end of MyTime class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setMin(10); myTime.addMin(-20); System.out.print(myTime.toString()); }

74 74 public class MyTime { private int hour; private int min; MyTime() { hour = 0; min = 0; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } public void addMin(int m){ min += m; checkMin(); } public String toString(){ String timeString; if (min < 10) timeString = hour + ":0" + min; else timeString = hour + ":" + min; return timeString; } private void checkMin(){ if(min < 0) System.exit(-2); if(min >= 60){ hour += min / 60; min %= 60; } private void checkHour(){ if(hour < 0) System.exit(-1); if(hour > 23) hour += hour%24; }

75 What’s the output? 1.0:60 2.1:0 3.1:00 4.nothing 75 public void addMin(int m){ min += m; checkMin(); } public String toString(){ String timeString; if (min < 10) timeString = hour + ":0" + min; else timeString = hour + ":" + min; return timeString; } private void checkMin(){ if(min < 0) System.exit(-2); if(min >= 60){ hour += min / 60; min %= 60; } private void checkHour(){ if(hour < 0) System.exit(-1); if(hour > 23) hour += hour%24; } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setMin(10); myTime.addMin(50); System.out.print(myTime.toString()); }

76 What’s the output? 1.0:0 2.2:50 3.4:10 4.nothing 76 public void addMin(int m){ min += m; checkMin(); } public String toString(){ String timeString; if (min < 10) timeString = hour + ":0" + min; else timeString = hour + ":" + min; return timeString; } private void checkMin(){ if(min < 0) System.exit(-2); if(min >= 60){ hour += min / 60; min %= 60; } private void checkHour(){ if(hour < 0) System.exit(-1); if(hour > 23) hour += hour%24; } class TimeDemo { public static void main(String args) { MyTime myTime = new MyTime(); myTime.setMin(10); myTime.addMin(240); System.out.print(myTime.toString()); }

77 77 public class MyTime { private int hour; private int min; MyTime() { hour = 0; min = 0; } public void setHour(int h){ if(h = 0) hour = h; else hour = 0; } public void setMin(int m){ if(m = 0) min = m; else System.exit(-2); } public void addMin(int m){ min += m; checkMin(); } public String toString(){ String timeString; if (min < 10) timeString = hour + ":0" + min; else timeString = hour + ":" + min; return timeString; } private void checkMin(){ if(min < 0) System.exit(-2); if(min >= 60){ hour += min / 60; min %= 60; } private void checkHour(){ if(hour < 0) System.exit(-1); if(hour > 23) hour += hour%24; }

78 UML Class Diagrams 78 MyTime - hour : int - minute : int + MyTime() + setHour(int) : void + setMin(int) : void + addMinutes(int) : void + toString() : String - checkMin() : void - checkHour() : void private public

79 Debugging Breakpoints Debug mode 79

80 So far… This has been Chapter 5 + some extras Next: Read chapter 6 80


Download ppt "Defining Classes and Methods CS140: Introduction to Computing 1 Savitch Chapter 5, 6 9/30/13."

Similar presentations


Ads by Google