Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A...

27
Introduction to Java

Transcript of Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A...

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

Introduction to Java

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

2

Textbook

David J. Barnes & Michael Kölling

Objects First with JavaA Practical Introduction using BlueJ

Fourth edition, Pearson Education, 2009ISBN-10: 0137005628(http://www.bluej.org)

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

3

Lecture overview

Objects and classes Understanding class definitions Object interaction Grouping objects

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

4

Real World Computer Program

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

5

No need for car parks

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

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):

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

7

Demo

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

9

So far

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

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

10

The ticket machine program

source code of a class fields constructor method assignment statements conditional statements

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

Basic class structure

public class TicketMachine{ Inner part of the class omitted.}

public class ClassName{ Fields Constructors Methods}

The outer wrapperof TicketMachine

The contents of aclass

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

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 modifier type variable name

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

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;}

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

Passing data via parameters

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

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.

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

Accessor methods

public int getPrice(){ return price;}

start and end of method body (block)

return typemethod name

parameter list (empty)

return statement

visibility modifier

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

Mutator methods

public void insertMoney(int amount){ balance = balance + amount;}

return type (void)

method name parameter

visibility modifier

assignment statementfield being changed

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

Printing from methodspublic 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;}

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

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?

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

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.

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

Making choices

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

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

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

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

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 <specified amount>. It has been set to 1000" (or whatever the default value is for you).

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

Local variables

public int refundBalance(){ int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund;}

A local variable

No visibilitymodifier

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

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.

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

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!

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

Primitive types vs. object types

object typeSomeObject obj;

32 primitive type

int i;

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

Primitive types vs. object types

32 32

int a; int b;

b = a;SomeObject a; SomeObject b;