1 / 20

CS110- Lecture 9 Mar 7, 2005

CS110- Lecture 9 Mar 7, 2005. Agenda Conditional Operator Encapsulation Constructors Practice Session. 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.

howton
Download Presentation

CS110- Lecture 9 Mar 7, 2005

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS110- Lecture 9Mar 7, 2005 • Agenda • Conditional Operator • Encapsulation • Constructors • Practice Session CS110-Spring 2005, Lecture 9

  2. 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 (:). CS110-Spring 2005, Lecture 9

  3. 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; Conditional operator CS110-Spring 2005, Lecture 9

  4. 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; CS110-Spring 2005, Lecture 9

  5. 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; } CS110-Spring 2005, Lecture 9

  6. 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!"; CS110-Spring 2005, Lecture 9

  7. A Class as an Outline Class name: Account Fields (Instance data): owner account number balance Methods (actions): initialize data: (Constructor) deposit money: How: balance = balance + amount withdraw money: How: balance = balance – amount get the balance: How: return the 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 Same name as of class and is called when the new operator is used to create an instance of class Methods determine the behavior CS110-Spring 2005, Lecture 9

  8. 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; } } CS110-Spring 2005, Lecture 9

  9. Encapsulation Object Methods Client or programmer who uses the class Instance data of an object should be modified only by that object Data CS110-Spring 2005, Lecture 9

  10. Encapsulation A well encapsulated class definition Implementation Interface private instance data private constants public constants private methods public methods Comments Headings of public methods public defined constants Programmer who uses the class CS110-Spring 2005, Lecture 9

  11. 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. CS110-Spring 2005, Lecture 9

  12. Visibility modifiers public private Violate Encapsulation Enforce Encapsulation data Provide services to clients Support other methods in the class methods CS110-Spring 2005, Lecture 9

  13. 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. CS110-Spring 2005, Lecture 9

  14. 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. CS110-Spring 2005, Lecture 9

  15. 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; } CS110-Spring 2005, Lecture 9

  16. 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. CS110-Spring 2005, Lecture 9

  17. 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 Personobjects. CS110-Spring 2005, Lecture 9

  18. Practice Session (Extra Credit) • Design and implement a class called Bookthat 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 whosemain method instantiates and update several Bookobjects. CS110-Spring 2005, Lecture 9

  19. 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. CS110-Spring 2005, Lecture 9

  20. 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. CS110-Spring 2005, Lecture 9

More Related