Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)

Similar presentations


Presentation on theme: "Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)"— Presentation transcript:

1 Agenda Warmup Lesson 2.6 (Constructors, Encapsulation) Independent Practice (2.6 Assignments) (Time Permitting) Lesson 2.7 Independent Practice (2.7 Assignments) Closure Activity Students will be able to: Understand what a constructor is Create a constructor in a class, as well as call that constructor from a client Understand the concept of encapsulation, and how it affects code See how today's lesson fits into the unit and the course as a whole

2 Warmup What is an immutable class?
2) Should instance variables ever be public? 3) Can a method be private? 4) Is it possible for a method to receive no parameters, display nothing, and return nothing? String x = “abcdef”; System.out.print(x.substring(4)); System.out.print(x.substring(1, 3)); // result? 6) Which word changes in pronunciation and meaning when you capitalize the first letter?

3 Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is declared (in other words, when you “connect to” (instantiate) a class) example: ClassName Q = new ClassName( ); It is used to “set up” the class – usually this means initializing its instance variables, sometimes this means calling other methods Constructors cannot return a value (but they don’t use void either!) They cannot be called like other methods: they are only called when an class is instantiated Example: ConstructorDemoClass & ConstructorDemoClient

4 Encapsulation Encapsulation is an important concept in object-oriented programming. Essentially, it means that the internal workings of an object should be hidden. Or, in other words, when you (as a programmer) use a class (by creating an object), you should know what the class does, but not necessarily how it does it. When data is restricted by using private variables and methods, this is a concept called information hiding.

5 Practically speaking, this means simply that almost always, instance variables in a class should be private. Then, if you want to access those variables from a client, the only way to do this should be by calling public accessor methods which return those variables. So, directly accessing a class’s variables violates the concept of encapsulation. (It does happen anyway, sometimes.)

6 We have already learned that a mutator method sets the value of a private instance variable.
One reason for encapsulation is that you can use error checking in a mutator method to validate data. For example, in a class called Student, there is an instance variable, studentIDNumber. If that number must be 6 digits long, this can be enforced in a mutator method. If, instead, studentIDnumber was public, it could potentially be set by the client to an invalid number. See next slide.

7 public class Student { private String studentIDnumber = 0; public void setIDnumber(String x) while(x.length() != 6) x = SavitchIn.readLine(); studentIDnumber = x; } public String getIDnumber() return studentIDnumber;

8 Encapsulation also means that even some methods should be private.
This is because certain “helper” methods perform tasks that support other methods, and are not meant to be called by a client. Using helper methods is a concept known as procedural abstraction. Demo: EncapsulationDemoClass & EncapsulationDemoClient

9 IMPORTANT: technically speaking, a helper method is private – meaning that the client does not have access to it. It can ONLY be called by another method in the same class. However, sometimes a public method, meant to be called from a client, can ALSO be called by other methods. It’s not technically a helper method, but it can be used by other methods as if it was. HINT – use this technique for today’s assignment. One of your methods should be called from another method, even though it is public!

10 Example: System.out.println(objectName);
toString( ) method Almost every class should have a toString( ) method. toString() is used to quickly and easily display the most important info from a class toString( ) is called automatically when you put the name of an object inside a System.out.println( ). Example: System.out.println(objectName); You can also call toString( ) like you would call any other method that returns a String, but displaying the object name is the preferred technique. Demo: add on to EncapsulationDemoClass

11 Assignment Create a class called Mar16Class with the following methods. (Then create Mar16Client to test it.) Create a constructor. It receives the person’s age and favorite donut as parameters, and it assigns these values to instance variables in the class. ageDiv() – accepts no parameters, returns true if the person’s age is divisible by 5, false if not abs() - accepts one number as a parameter, returns the absolute value of that number. Although there is a Math.abs() method, do this instead with an if. nextToLast() - accepts one word as a parameter, returns the next-to-last letter in the word getName() – accepts no parameters; asks for the user’s name, then returns one String, consisting of the name with an insult attached to it ***More on next slide…

12 displayStuff() – accepts a num and a word, displays that word num+1 times, returns nothing
findIndex() – accepts a word and a letter as parameters, returns which index that letter was found at in the word (ignore multiple occurrences of the letter), returns 999 if the letter is not found primeNum() - receives a number, returns true if it is prime, false if not. (make sure to test this!) getPrimes( ) -- Accepts two #s, returns how many primes exist between (and including) the two numbers. sumOfDigits( ) – accepts a 4-digit number, returns the sum of its digits. No Strings allowed. toString( ) – returns the name, age, the favorite donut, and the sum of the 4 digits.


Download ppt "Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)"

Similar presentations


Ads by Google