GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved2/48 Writing Classes We've been using predefined classes. Now we will learn.
Writing Classes (Chapter 4)
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
GP4, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 4 We start at 13:00 Slides are available from the course home page Feel free to.
SEEM Java – Basic Introduction, Classes and Objects.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4: Writing Classes. 2 b We've been using predefined classes. Now we will learn to write our own classes to define new objects b Chapter 4 focuses.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. Chapter 4 focuses on: class definitions.
Chapter 4: Writing Classes
Lecture 14 Writing Classes part 2 Richard Gesick.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to print them now Martin Lillholm

GP3, Martin Lillholm 2 Mandatory Assignment Assignment sheets: –We’ll strive to make them public Thursday How did it go with last week’s mandatory assignment ? –Easy? –Hard? This week … a bit harder … more free

GP3, Martin Lillholm 3 Last week A bit more on compilation, runtime environment, classes, objects, methods Identifiers Variables Primitive types Operators Expressions Packages Graphics and applets

GP3, Martin Lillholm 4 Today … we have to go through a lot Classes, objects (instances) Attributes (instance variables) Methods and constructors A first look a static variables and variables (class variables) Encapsulation Object/reference variables – variable of non-primitive type The String, Random, Math og Scanner classes Formatted output

GP3, Martin Lillholm 5 Classes and Objects Classes – blue prints –Definitions of objects to come Attributes or instance variables Methods –Define a new non-primitive type Objects – instances –Realisations/instances of classes Instance variables Access to methods declared in “their” class Static variables and methods … later

GP3, Martin Lillholm 6 Attributes / Instance Variables Principally as (local) variables in methods but –Defines an object’s state – main OO point! –Declared outside methods –Visible throughout the class – all methods –“Lives” as long as the instance/object –Receives a standard value at object instantiation 0 (byte, char, short, int, long) 0.0 (float, double) false (boolean)

GP3, Martin Lillholm 7 Methods Methods are used to combine and name a collection of statements Parameters and return value Recycling Abstraction

GP3, Martin Lillholm 8 Constructors Special methods with the same name as the class they’re defined in Called/invoked at object creation/instantiation No return value Seldom called explicitly A class can have one or more constructors but doesn’t need any Always has an implicitly defined constructor that takes no arguments

GP3, Martin Lillholm 9 Classes and objects again Attrib. (def.) Constructors Methods Class 1 Attrib. (def.) Constructors Methods Class n Attrib. Class ref. Object m Attrib. Class ref Object 1 Attrib Class ref. Object 1 Attrib. Class ref. Object k ”Abstract” model/conceptRealisation – instance

GP3, Martin Lillholm 10 Dice Attributes Instance variables Constructor Methods New – non-primitive type

GP3, Martin Lillholm 11 How do We Instantiate/Create an Object? Using the new statement Typically by assignment and initialisation of object variables. operator

GP3, Martin Lillholm 12 What Actually Happens During Instantiation? A new object is created Memory is allocated for instance variables Possible initialisations of instance variables The statements of the “appropriate” constructor is executed Which constructor?

GP3, Martin Lillholm 13 Methods Again int methodName (int a, int b,...) { // Method body variabelErklæring1; kommando1; kommando2; variabelErklæring2;... kommando3; return... } int, double, char, boolean, String,... Return type Parameter names and types Note that parameters just are “exotic” local variables

GP3, Martin Lillholm 14 What Happens When We Call a Method? char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (25, count, "Hello"); Formal and actual parameters

GP3, Martin Lillholm 15 What Happens When We Call a Method? myMethod(); myMethodcompute

GP3, Martin Lillholm 16 What Happens When We Call a Method? doIt helpMe helpMe(); obj.doIt(); main

GP3, Martin Lillholm 17 The Method Body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } Following slides … otherwise as the main method sum and result are local – as are the parameters

GP3, Martin Lillholm 18 The return statement The return type indicates which type is returned A method that doesn’t return anything must have return type void A return statement return the value of an expression and terminates the method return Expression The expression must evaluate to a value that has the same type as the method’s return type

GP3, Martin Lillholm 19 Constructors Again Special methods with the same name as the class they’re defined in Called/invoked at object creations/instantiation No return value Seldom called explicitly A class can have one or more constructors but doesn’t need any Always has an implicitly defined constructor that takes no arguments

GP3, Martin Lillholm 20 More Dice... and BlueJ –RollingDice.java and Die.java i BlueJ (L&L page )

GP3, Martin Lillholm 21 Encapsulation (1) An object can be viewed from two angles: –Internally – all details: instance variables and the class’ methods –Externally – the services (the object’s interface) an object offers. All others details are encapsulated Another object (a client) can use the object’s services (methods) The client doesn’t know how the services work or any other private details. Only an object itself should be able to chance its state (instance variables) directly. Although other objects may cause it indirectly through services. Objects should be self governing.

GP3, Martin Lillholm 22 Encapsulation (2) Think of objects a black box Only possible access should be through services Why all this ? Methods Attributes/data Client

GP3, Martin Lillholm 23 Visibility Modifiers (1) Used for enforcing/implementing encapsulation Three types: private, public, protected We’ll wait with protected until we discuss inheritance Class members (methods and attributes) declared public can accessed and in the latter case changed by all clients Class members declared private are only available to the class itself Class members without a visibility modifier has default or package access

GP3, Martin Lillholm 24 Visibility Modifiers (2) publicprivate Variables Methods Provide services to clients Support other methods in the class Enforce encapsulation Violate encapsulation

GP3, Martin Lillholm 25 Accessors and Mutators To facilitate access to private attributes one can provide service methods – only if needed! An accessor method (e.g. getX ) makes it possible to read private attributes indirectly. A mutator method (e.g. setX ) makes it possible to change private attributes indirectly. Often with one or more limitations. Isn’t this exactly what we were striving to avoid?

GP3, Martin Lillholm 26 More Dice …

GP3, Martin Lillholm 27 Our First DIY Class … the Car Class A Car is defined by its: –Make –Top Speed A Car in BlueJ …

GP3, Martin Lillholm 28 Static variables and Methods Lives at class level and across instances One per class – despite the number if instances Can be accessed/used without instantiation Declared using the keyword static public static double sqrt (double num) {... } public static double PI; Using these examples from the Math class: –Math.PI*5 –int a = Math.sqrt(9); Why static methods and variables ?

GP3, Martin Lillholm 29 Static Methods and Variables Attrib. (def.) Constructors Methods Class 1 Attrib. (def.) Constructors Methods Class n Attrib. Class ref. Object m Attrib. Class ref Object 1 Attrib Class ref. Object 1 Attrib. Class ref. Object k ”Abstract” model/conceptRealisation – instance public static double sqrt (double num) {... } public static double PI;

GP3, Martin Lillholm 30 Hello.java again …

GP3, Martin Lillholm 31 Contains a reference to the object – not the object itself as is the case for variables of primitive type! Die number1 = new Die (); Die number2; number2 = new Die (); number2 = number1; // A reference is copied! number1 Die objekt1 number2 Die objekt2 Garbage collection. Object variables (Pointer fun with Binky)

GP3, Martin Lillholm 32 The String Class The type String is a class-type! Strings are objects/instances. String variables contains a reference. String title; title = ”Java Software Solutions”; (title = new String(”Java Software Solutions”)) This “trick” only works for Strings – syntactic sucker.

GP3, Martin Lillholm 33 The String Class Once a String object has been instantiated it can’t be changed – it’s immutable. But a variable that references it can be changed... unless of course it’s declared final Many of the String class’ methods return new instances and it looks (and works) as if one can change a String object. L&L page 119 (in BlueJ if time allows)

GP3, Martin Lillholm 34 The toString method Any class should have a toString() method! Should return a string representation of an object’s state Is called automatically using e.g. System.out.println and string concatenation. Die number1 = new Die (); System.out.println(number1); String message = ”Resultatet blev: ” + number1; System.out.println(message); toString can be as complex as desired – keep it simple...

GP3, Martin Lillholm 35 The Bank Account... –Transactions.java and Account.java in BlueJ. (L&L page )

GP3, Martin Lillholm 36 The Scanner Class (1)

GP3, Martin Lillholm 37 The Scanner Class (2)

GP3, Martin Lillholm 38 The Random Class

GP3, Martin Lillholm 39 The Math Class

GP3, Martin Lillholm 40 The DecimalFormat Class

GP3, Martin Lillholm 41 Questions... We’ve been through a lot … Ask away … This weeks mandatory assignment