introduction to java. 2 textbook david j. barnes & michael kölling objects first with java a...
TRANSCRIPT
Introduction to Java
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)
3
Lecture overview
Objects and classes Understanding class definitions Object interaction Grouping objects
4
Real World Computer Program
5
No need for car parks
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
Demo
9
So far
object class fields, constructor, method parameter data type state source code
10
The ticket machine program
source code of a class fields constructor method assignment statements conditional statements
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
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
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;}
Passing data via parameters
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.
Accessor methods
public int getPrice(){ return price;}
start and end of method body (block)
return typemethod name
parameter list (empty)
return statement
visibility modifier
Mutator methods
public void insertMoney(int amount){ balance = balance + amount;}
return type (void)
method name parameter
visibility modifier
assignment statementfield being changed
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;}
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?
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.
Making choices
public void insertMoney(int amount){ if(amount > 0) { balance = balance + amount; } else { System.out.println("Use a positive amount: " + amount); }}
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
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).
Local variables
public int refundBalance(){ int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund;}
A local variable
No visibilitymodifier
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
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!
Primitive types vs. object types
object typeSomeObject obj;
32 primitive type
int i;
Primitive types vs. object types
32 32
int a; int b;
b = a;SomeObject a; SomeObject b;