Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.

Similar presentations


Presentation on theme: "Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson."— Presentation transcript:

1 Introduction to Java

2 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson Education, 2009 ISBN-10: 0137005628 (http://www.bluej.org)

3 3 Lecture overview Objects and classes Understanding class definitions Object interaction Grouping objects

4 4 Real WorldComputer Program

5 5 No need for car parks

6 6 Class - Object analogy Definition (Class): a written or printed work of fiction or nonfiction, usually on sheets of paper fastened or bound together within covers. Concrete instance (Object):

7 7 Demo

8 8 Demo steps Create a circle object from the circle class Invoke the makeVisible, moveRight methods Invoke the moveHorizontal method with 50 as parameter Explain the signature of the method Invoke the changeColor method that uses a different data type parameter Create another instance of circle, change its size Inspect the objects to see the difference in state Usually manipulating objects is done by a program, not by hand Close shapes and open picture Picture creates four objects and changes their state Show the source code and add another sun

9 9 So far object class fields, constructor, method parameter data type state source code

10 10 The ticket machine program source code of a class fields constructor method assignment statements conditional statements

11 Basic class structure public class TicketMachine { Inner part of the class omitted. } public class ClassName { Fields Constructors Methods } The outer wrapper of TicketMachine The contents of a class

12 Fields Fields store values for an object. Fields define the state of an object. public class TicketMachine { private int price; private int balance; private int total; Constructor and methods omitted. } private int price; visibility modifiertypevariable name

13 Constructors Constructors initialize an object. They have the same name as their class. They store initial values into the fields. They often receive external parameter values for this. public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; }

14 Passing data via parameters

15 Assignment Statements Values are stored into fields (and other variables) via assignment statements: variable = expression; price = ticketCost; A variable stores a single value, so any previous value is lost.

16 Accessor methods public int getPrice() { return price; } start and end of method body (block) return type method name parameter list (empty) return statement visibility modifier

17 Mutator methods public void insertMoney(int amount) { balance = balance + amount; } return type ( void ) method name parameter visibility modifier assignment statement field being changed

18 Printing from methods public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total = total + balance; // Clear the balance. balance = 0; }

19 Exercise Implement a method, setPrice, that is able to set the price of tickets to a new value. The new price is passed in as a parameter value to the method. Test your method by creating a machine, showing the price of tickets, changing the price, and then showing the new price. Is this method a mutator or an accessor?

20 Reflecting on the 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.

21 Making choices public void insertMoney(int amount) { if(amount > 0) { balance = balance + amount; } else { System.out.println("Use a positive amount: " + amount); }

22 Making choices 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 } ‘if’ keyword ‘else’ keyword boolean condition to be tested - gives a true or false result actions if condition is true actions if condition is false

23 Exercise Include a check in the constructor to ensure that the price passed is greater than zero. If this is not the case the price of the ticket should be set to the default value and the constructor should send a message to the user saying something like: "Ticket cost cannot be. It has been set to 1000" (or whatever the default value is for you).

24 Local variables public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } A local variable No visibility modifier

25 Review Class bodies contain fields, constructors and methods. Fields, parameters and local variables are all variables. Objects can make decisions via conditional (if) statements. A true or false test allows one of two alternative courses of actions to be taken.

26 26 public class Picture { private Square wall; private Square window; private Triangle roof; private Circle sun; … public void draw() { … sun = new Circle(); sun.changeColor("yellow"); sun.moveHorizontal(180); sun.moveVertical(-10); sun.changeSize(60); sun.makeVisible(); } Object types!

27 Primitive types vs. object types object type SomeObject obj; 32 primitive type int i;

28 Primitive types vs. object types 32 int a;int b; b = a; SomeObject a; SomeObject b;


Download ppt "Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson."

Similar presentations


Ads by Google