1 / 27

Introduction to Java

This textbook provides a practical introduction to Java programming using BlueJ, covering topics such as objects, classes, object interaction, grouping objects, and more.

nwagner
Download Presentation

Introduction to Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java

  2. Textbook David J. Barnes & Michael KöllingObjects First with JavaA Practical Introduction using BlueJFourth 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

  8. So far • object • class • fields, constructor, method • parameter • data type • state • source code

  9. The ticket machine program • source code of a class • fields • constructor • method • assignment statements • conditional statements

  10. 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 }

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

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

  13. Passing data via parameters

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

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

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

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

  18. 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?

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

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

  21. 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 }

  22. 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).

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

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

  25. 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(); }

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

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

More Related