CS110- Lecture 9 Mar 7, 2005 Agenda Conditional Operator Encapsulation Constructors Practice Session 6/30/2016 CS110-Spring 2005, Lecture 9 1 Conditional Operator Java conditional operator is similar to an if-else statement in some ways. It is a ternary operator because it requires three operands. A conditional operator expression starts with a boolean expression followed by a ? and then two expressions separated by a colon (:). 6/30/2016 CS110-Spring 2005, Lecture 9 2 Conditional Operator If the boolean expression is true then the first of the two expressions is returned; otherwise the second of the two expressions is returned. Example 1 1.if( n1 > n2) max = n1; else max = n2; 2. max = (n1 > n2)? n1 : n2; 6/30/2016 CS110-Spring 2005, Lecture 9 Conditional operator 3 Conditional Operator Example 2 if( hoursWorked <= 40) pay = hoursWorked * payRate; else pay = 40 * payRate + 1.5 * (hoursWorked – 40) * payRate; OR pay = ( hoursWorked <= 40)? hoursWorked * payRate: 40 * payRate + 1.5 * (hoursWorked – 40) * payRate; 6/30/2016 CS110-Spring 2005, Lecture 9 4 Conditional Operator Example 3 public boolean isEven(int num) { if(num % 2 == 0) return true; else return false; } OR public boolean isEven(int num) { return (num % 2 == 0)? true :false; } 6/30/2016 CS110-Spring 2005, Lecture 9 5 Conditional Operator Example 4 String msg; if(count1 == count2) msg = “A Tie!”; else if ( count1 > count2) msg = name1 + “ wins!”; else msg = name2 + “ wins!”; OR String msg = (count1 == count2) ? "A Tie!" :(count1 > count2) ? name1 + " wins!" : name2 + " wins!"; 6/30/2016 CS110-Spring 2005, Lecture 9 6 A Class as an Outline Class name: Account Fields (Instance data): owner account number balance Data determine the state. It is called instance data because memory is determined for each instance of the class that is created. Scope of instance data is the whole class Methods (actions): initialize data: (Constructor) deposit money: How: balance = balance + amount withdraw money: How: balance = balance – amount get the balance: How: return the balance 6/30/2016 Same name as of class and is called when the new operator is used to create an instance of class CS110-Spring 2005, Lecture 9 Methods determine the behavior 7 Account Class public class Account { private String owner; private long acctNumber; private double balance; public Account(String o, long a, double b) { owner = o; acctNumber = a; balance = b; } public double deposit(double amount) { balance = balance + amount; return balance; } public double withdraw(double amount) { balance = balance – amount; return balance; } public double getBalance() { return balance; } } 6/30/2016 CS110-Spring 2005, Lecture 9 8 Encapsulation Object Client or programmer who uses the class Methods Data 6/30/2016 CS110-Spring 2005, Lecture 9 Instance data of an object should be modified only by that object 9 Encapsulation A well encapsulated class definition Implementation private instance data Interface private constants Comments public constants Headings of public methods private methods public defined constants Programmer who uses the class public methods 6/30/2016 CS110-Spring 2005, Lecture 9 10 Visibility Modifiers We accomplish object encapsulation using modifiers. Some modifiers are called visibility modifiers because they control access to the members of a class. public , private and protected public - could be directly referenced from outside of the object. private - can be used anywhere in the class definition but cannot be referenced outside the object. 6/30/2016 CS110-Spring 2005, Lecture 9 11 Visibility modifiers public data methods 6/30/2016 private Violate Encapsulation Enforce Encapsulation Provide services to clients Support other methods in the class CS110-Spring 2005, Lecture 9 12 Accessors and Mutators Because instance data is generally declared private, a class usually provides services to access and modify data. Accessors (getters) – read only access to data. Usually named as getX where X is the variable name to which it provides access. Mutators (setters) - changes the value of data. Usually named setX. 6/30/2016 CS110-Spring 2005, Lecture 9 13 Constructors We often use a constructor to initialize variables associated with each object. A constructor differs from a regular method in two ways: Name of constructor is the same name as that of class. Constructor cannot return a value and does not have a return type specified on the method header. 6/30/2016 CS110-Spring 2005, Lecture 9 14 Constructors Example 1: Constructor of Die class public Die() { faceValue = 1; } Example 2: Constructor of Account class public Account(String o, long a, double b) { owner = o; acctNumber = a; balance = b; } 6/30/2016 CS110-Spring 2005, Lecture 9 15 Practice Session (Extra Credit) Design and implement a class called Circle that contains instance data that represents the circle’s radius. Define the Circle constructor to accept and initialize the radius, and include getter and setter methods for the radius. Include method area that calculates and return the area and another method perimeter that calculates and return the perimeter. 6/30/2016 CS110-Spring 2005, Lecture 9 16 Practice Session (Extra Credit) Design and implement a class called Person that contains instance data that represents the person’s name and age. Define the Person constructor to accept and initialize the name and age, and include getter and setter methods for the name and age. Create a driver class called PersonClient whose main method instantiates and update several Person objects. 6/30/2016 CS110-Spring 2005, Lecture 9 17 Practice Session (Extra Credit) Design and implement a class called Book that contains instance data that represents the book’s title, author, publisher, edition and isbn number. Define the Book constructor to accept and initialize the instance data, and include getter and setter methods for all instance data. Create a driver class called BookShelf whose main method instantiates and update several Book objects. 6/30/2016 CS110-Spring 2005, Lecture 9 18 Practice Session (Page 277) 5.17: Write a for loop to print the odd numbers from 1 to 99. 5.18: Write a for loop to print the multiples of 3 from 300 down to 3. 5.19: Write a code fragment that reads 10 integer values from the user and prints the highest value entered. 5.20: Write a code fragment that determines and prints the number of times the character ‘a’ appears in a String object called name. 5.21: Write a code fragment that prints the characters stored in a String object called str backward. 6/30/2016 CS110-Spring 2005, Lecture 9 19 Practice Session (Page 277) 5.22: Write a code fragment that prints every other character in a String object called word starting from the first character. 5.23: Write a method called powersOfTwo that prints the first 10 powers of 2 (starting with 2). The method takes no parameter and doesn’t return anything. 6/30/2016 CS110-Spring 2005, Lecture 9 20