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

Slides:



Advertisements
Similar presentations
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Advertisements

Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
Object Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Looking inside classes Choices Week 4. Class bodies contain fields, constructors and methods. Fields store values that determine an object’s state. Constructors.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions Looking inside classes 3.0.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 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.
Object interaction Creating cooperating objects 5.0.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Understanding class definitions Looking inside classes.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Object Oriented Design: Identifying Objects
CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Classes CS 21a: Introduction to Computing I First Semester,
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11& ) Lab: B43, MB321, MB536 6:00-7:30 (from ) [each student must have obtained.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 CSC 221: Computer Programming I Fall 2004 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
Looking inside classes Conditional Statements Week 4.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
Understanding class definitions Exploring source code 6.0.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
Class definitions CITS1001 week 2.
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java Creating cooperating objects
Objects First with Java
Understanding class definitions
Objects First with Java A Practical Introduction using BlueJ
public class BankAccount{
Java Programming with BlueJ
Understanding class definitions
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 3 Tony Gauvin.
2 The anatomy of class definitions
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
F II 3. Classes and Objects Objectives
Objects First with Java Creating cooperating objects
COS 260 DAY 4 Tony Gauvin.
Objects First with Java Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Classes CS 21a: Introduction to Computing I
COS 260 DAY 6 Tony Gauvin.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

Introduction to Java

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

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

4 Real WorldComputer 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 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 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 wrapper of TicketMachine The contents of a class

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

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 type method 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 statement field being changed

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

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. 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 visibility modifier

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 type SomeObject obj; 32 primitive type int i;

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