Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.

Similar presentations


Presentation on theme: "Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors."— Presentation transcript:

1 Looking inside classes Choices Week 4

2 Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors initialize objects. Methods implement the behavior of objects. Fields persist (remain) for the lifetime of an object. Parameters are used to receive values into a constructor or method. Java Class Definitions WHAT WE LOOKED AT LAST WEEK

3 Conditional Statements Local Variables Java Class Definitions CONCEPTS COVERED THIS WEEK

4 Java Class Definitions REFLECTING ON OUR TICKET MACHINES Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible initialization. How can we do better? –We need more sophisticated behavior.

5 Java Class Definitions MAKING CHOICES public void insertMoney(int amount) { if(amount > 0) { balance = balance + amount; } else { System.out.println("Use a positive amount: " + amount); }

6 if(perform some test) { Do the statements here if the test gave a true result } else { Do the statements here if the test gave a false result } Java Class Definitions MAKING CHOICES ‘if’ keyword boolean condition to be tested - gives a true or false result actions if condition is true actions if condition is false ‘else’ keyword

7 Making decisions in Java This is the code to test whether a student has passed or failed a module, assuming 40% is the pass mark: if ( studentMark >= 40 ) { result = "pass"; } else { result = "fail"; } // end if

8 public class Person { private String name; private int age; private String job; private float salary; public Person(String newName, int newAge) { name = newName; age = newAge; salary = 0.0f; } Java Class Definitions EXAMPLE CLASS CONSTRUCTOR

9 if (mark.getAge() == 65) { mark.changeJob(“RETIRED”); } //flow-of-control carries on from here … Graphically - this is an example of a flow chart Program - Selection SELECTION CONSTRUCT: if

10 public void payRise (float percentage) { if ( getAge() >= 65 ) { System.out.println("No pay rise for you!"); } else { salary = salary + (salary * percentage) } //flow-of-control carries on from here … } Program - Selection SELECTION CONSTRUCT: if

11 Program - Selection SELECTION CONSTRUCT: if … else

12 Improved printTicket() Uses if else to check if there are sufficient funds to buy a ticket

13 EQUALITY AND RELATIONAL OPERATORS ExampleMeaning balance == price balance equal to price balance != price balance not equal to price balance > price balance greater than price balance < price balance less than price balance >= price balance greater or equal to price balance <= price balance less or equal to price

14 Examples int janet = 44, john = 12; janet == john john != 12 john <= 12 7 < john janet > john janet >= 44

15 The == operator is not a valid way make a comparison of two String objects. It does not compare Strings the way we would expect. The String class contains a method that is appropriate. The method returns a boolean, which takes the value true when the string executing the method has exactly that same characters in the same order as the string that is passed as a parameter, and false otherwise. For example "hello".equals("hello") returns true "hello".equals("goodbye") returns false

16 Logical Operators Logical operator Java Logical operator cond-1 AND cond-2 && cond-1 OR cond-2 || cond-1 EXCLUSIVE OR cond-2 ^ NOT cond-1 ! e.g. if (gender == 1 && age >= 65) Note the use of the round brackets which MUST surround the whole condition. Extra sets of brackets CAN be used for clarity if desired.

17 Java Class Definitions NESTED SELECTION: if … else Note that else links with the preceding nearest if statement, from the deepest level of nesting outwards, e.g. if ( condition1 ) if ( condition2 ) statement1; else statement2; else statement3; Q1. If condition1 is true, and condition2 is false, which statement will be executed? Q2. If condition1 is false, and condition2 is true, which statement will be executed?

18 Java Class Definitions LOCAL VARIABLES public int refundBalance() { return balance; balance = 0; } Why won’t this method compile? unreachable statement!

19 Java Class Definitions LOCAL VARIABLES Fields are only one sort of variable. –They store values through the life of an object. –They are accessible by all methods throughout the class. Methods can include other (shorter-lived) variables. –They exist only as long as the method is being executed. –They are only accessible from within the specific method.

20 Java Class Definitions LOCAL VARIABLES public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } A local variable No visibility modifier

21 Required Reading Objects First With Java – A Practical Introduction using BlueJ Reading for this week’s lecture Chapter 2 Reading for next week’s lecture Chapter 3 (pages 56 – 75)


Download ppt "Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors."

Similar presentations


Ads by Google