270 likes | 284 Views
This textbook provides a practical introduction to Java programming using BlueJ, covering topics such as objects, classes, object interaction, grouping objects, and more.
E N D
Textbook David J. Barnes & Michael KöllingObjects First with JavaA Practical Introduction using BlueJFourth edition, Pearson Education, 2009ISBN-10: 0137005628 (http://www.bluej.org)
Lecture overview • Objects and classes • Understanding class definitions • Object interaction • Grouping objects
Real World Computer Program
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):
So far • object • class • fields, constructor, method • parameter • data type • state • source code
The ticket machine program • source code of a class • fields • constructor • method • assignment statements • conditional statements
The outer wrapper of TicketMachine The contents of a class Basic class structure public class TicketMachine { Inner part of the class omitted. } public class ClassName { Fields Constructors Methods }
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. } visibility modifier type variable name private int price;
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; }
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.
return type visibility modifier method name parameter list (empty) return statement start and end of method body (block) Accessor methods public int getPrice() { return price; }
return type (void) visibility modifier method name parameter field being changed assignment statement Mutator methods public void insertMoney(int amount) { balance = balance + amount; }
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; }
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); } }
boolean condition to be tested - gives a true or false result ‘if’ keyword actions if condition is true actions if condition is false ‘else’ keyword 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 }
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).
A local variable No visibility modifier Local variables public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; }
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.
Object types! 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(); }
SomeObject obj; object type int i; primitive type 32 Primitive types vs. object types
SomeObject a; SomeObject b; int a; int b; 32 32 Primitive types vs. object types b = a;