Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looking inside classes Conditional Statements Week 4.

Similar presentations


Presentation on theme: "Looking inside classes Conditional Statements Week 4."— Presentation transcript:

1 Looking inside classes Conditional Statements 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 change given. –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 Improved printTicket() Uses if else to check if there are sufficient funds to buy a ticket

9 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

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

11 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.

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

13 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.

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

15 Required Reading Objects First With Java – A Practical Introduction using BlueJ Reading for this week’s lecture Chapter 2


Download ppt "Looking inside classes Conditional Statements Week 4."

Similar presentations


Ads by Google