150 likes | 261 Views
Chapter 4. Topics: class declarations encapsulation instance variables method declarations return types parameters method overloading. The Person class, version 1. public class Person { // state // behavior public void talk() { System.out.println("blah, blah, blah, ...<br>"); }
E N D
Chapter 4 • Topics: • class declarations • encapsulation • instance variables • method declarations • return types • parameters • method overloading
The Person class, version 1 public class Person { // state // behavior public void talk() { System.out.println("blah, blah, blah, ...\n"); } public void study (String course) { System.out.println("study, study, study, ..."); System.out.println("I just love studing " + course + "!\n"); } public boolean likes (String firstName) { if ( firstName.charAt( firstName.length()-1 ) == 'y' ) return true; else return false; } }
The Driver class, version 1 public class Driver { public static void main(String [] args) { Person student1 = new Person(); student1.talk(); student1.study("CSCI 201"); if ( student1.likes("Tommy") ) System.out.println("Student #1 likes Tommy"); else System.out.println("Student #1 does not like Tommy"); } }
Person class, version 2---adding state public class Person { private int age; private String name; … public String getName() { return name; } public void setName( String newName ) { name = newName; } public int getAge() { return age; } public void setAge( int newAge ) { age = newAge; } }
Driver class, version 2 public class Driver { public static void main(String [] args) { Person student1 = new Person(); student1.setName("Sally"); student1.setAge(21); System.out.println("Student #1's name is " + student1.getName() + "\n"); System.out.println("Student #1's age is " + student1.getAge() + "\n"); student1.talk(); student1.study("CSCI 201"); if ( student1.likes("Tommy") ) System.out.println("Student #1 likes Tommy"); else System.out.println("Student #1 does not like Tommy"); } }
Person class, version 3 public class Person { private int age; private String name; Person (String startingName, int startingAge) { age = startingAge; name = startingName; } Person () { age = 18; name = "Bob"; } … public int getAge() { return age; } public void setAge( int newAge ) { age = newAge; } }
Driver class, version 3 public class Driver { public static void main(String [] args) { Person student1 = new Person(); Person student2 = new Person("Tommy", 23); System.out.println("Student #1's name is " + student1.getName() ); System.out.println("Student #1's age is " + student1.getAge() ); System.out.println("Student #2's name is " + student2.getName() ); System.out.println("Student #2's age is " + student2.getAge() ); if ( student1.likes( student2.getName() ) ) System.out.println("Student #1 likes " + student2.getName() ); else System.out.println("Student #1 does not like " + student2.getName() ); } }
Formal Parameters • A method has a header and a body. This is the simple version of the setAge method of the Person class: • public void setAge(int newAge) • { • age = newAge; • } • This method has a parameter called newAge which is an int. When this method is called, it expects to be passed an integer value. • newAge is called a formal parameter. It is a placeholder for a value that will be supplied when the method is invoked. } Method header Method body
Actual Arguments • The method is called by addressing a Person object and asking it to invoke this method. Person friend = new Person(); friend.setAge(20); • The integer value 20 is the actual argument that is given to the method when it is invoked. int myAge = 25; friend.setAge(myAge); • The setAge method sees only the value 25. It does not see the name of the variable that happens to contain the value of the actual argument.
Memory Allocation for Parameters • Memory is allocated to the formal parameter at the time the method is called. • The memory location is initialised to the actual argument. • The memory for the formal parameter is deallocated when the method finishes executing. We call this "going out of scope".
Methods with Multiple Parameters • Suppose we had a method that could change all the attributes of a Person object. public void setAllAttributes(String aName, int anAge ) { name = aName; age = anAge; } • The call to this method would have to pass two actual arguments, e.g. friend.setAllAttributes("Jack", myAge + 1);
Matching Formal Parameters with Actual Arguments • The actual arguments passed to a method must match the formal parameters in all of the following: number type order • The compiler assumes that the first actual argument matches the first formal parameter, the second actual argument matches the second formal parameter, etc. • The names of formal parameters do not have to match the names of the actual arguments. The called method does not see the names, it sees only values.
Passing Multiple Values of the Same Type public double divideNumbers(int dividend, int divisor) { if (divisor != 0) return (double)dividend / divisor; return 0; } • When the method is called, e.g. System.out.println(divideNumbers(12, 3)); • the first parameter is taken as the dividend, and the second as the divisor.
Returning Values from Methods • A value returned from a method replaces the call to the method. Person aPerson = new Person(); System.out.println(aPerson.getAge()); System.out.println(0);
Returning Values from Methods • When a method returns a value you need to use right away, or “hold on” to it to use it later. • Holding on to it: Person aperson = new Person(); String goodName = aperson.getName(); if( aperson.likes(goodName) ) … else … • Using it right away: Person aperson = new Person(); if( aperson.likes( aperson.getName() ) ) … else …