Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.

Slides:



Advertisements
Similar presentations
Web Application Development Slides Credit Umair Javed LUMS.
Advertisements

Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Road Map Introduction to object oriented programming. Classes
Chapter 41 Defining Classes and Methods Chapter 4.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Introduction to Java Programming, 4E Y. Daniel Liang.
Introduction to Methods
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
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.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
ECE122 Feb. 22, Any question on Vehicle sample code?
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Working With Objects Tonga Institute of Higher Education.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Classes - Intermediate
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Chapter 5: Enhancing Classes
3 Introduction to Classes and Objects.
Chapter 3: Using Methods, Classes, and Objects
Creating Objects & String Class
Road Map Introduction to object oriented programming. Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Classes & Objects: Examples
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Packages & Random and Math Classes
Object Oriented Programming in java
Outline Creating Objects The String Class The Random and Math Classes
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Chapter 3 Classes and Methods

2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know what attributes are Know what responsibilities are Know how assignment of an object reference differs from assignment of a standard type

3 Knowledge Goals Appreciate how aliases can lead to errors Know what garbage collection is Understand the distinction between instance methods and class methods Know what a constructor does Appreciate the difference between void and value-returning methods Understand what immutability is

4 Skill Goals Determine the attributes and responsibilities of a class Write the heading for a new class Write the class declaration for a new class Write an instance method Write a class method Write a constructor

5 Skill Goals Write a helper method Write a value-returning method Assemble class declarations into a working class

6 New Classes of Objects A new class is just like an application except that it doesn't have a main method

7 New Classes of Objects Can you name some attributes for the cat class ?

8 New Classes of Objects Instance values A value that is associated with a specific object Class value A value that is associated with a class and is the same for every object of the class Attributes The values defined by a class that are used to represent its objects; the combination of instance and class values Were any cat attributes class values?

9 New Classes of Objects See the difference between class and instance values?

10 New Classes of Objects A class and three instantiated objects

11 New Classes of Objects With each new class comes responsibilities… Responsibilities The operations that are provided by a class of objects Class responsibility An operation performed with respect to a class of objects Instance responsibility An operation performed with respect to a specific object

12 New Classes of Objects Method A Java subprogram Class method A method that implements a class responsibility Instance method A method that implements an instance responsibility

13 New Classes of Objects Constructor An operation (responsibility) that creates a new instance of a class Constructor (in Java) A method with the same name as the class, which is neither a void or a value-returning method How many kinds of attributes and responsibilities are there?

14 New Classes and Objects Every class must have a constructor; if you don't define one, Java supplies one

15 Overview of Java Data Types

16 Overview of Java Data Types Primitive values require a fixed number of bytes; object values can be any size, so…

17 Overview of Java Data Types A variable of a primitive type contains the value of the variable A variable of an object type contains the address of where the value (object) begins

18 Overview of Java Data Types

19 Overview of Java Data Types letter char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; Example Walkthrough

20 Overview of Java Data Types letter title char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; Example Walkthrough

21 Overview of Java Data Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; Example Walkthrough

22 Overview of Java Data Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; ‘J’ Example Walkthrough

23 Overview of Java Data Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; 2003 “Problem Solving” Memory Location 2003 ‘J’ Example Walkthrough

24 Overview of Java Data Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; 2003 “Problem Solving” Memory Location ‘J’ Example Walkthrough

25 Overview of Java Data Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; 2003 “Problem Solving” Memory Location ‘J’ Walkthrough Complete

26 Overview of Java Data Types Alias When multiple variables refer to the same object, acting as synonyms

27 Overview of Java Data Types Address is unreachable

28 Recycling Discarded Objects Garbage Objects that are unreachable Garbage collection Recycling of memory when objects are no longer needed Explicit memory management Recycling of object memory that must be manually coded Memory leak Failure to recycle memory when explicit memory management is being used Java has automatic garbage collection

29 Responsibilities as Methods Heading Body Parameter list contains information the method must have in order to execute; this information is given at the call

30 Responsibilities as Methods Arguments substituted for parameters at time of call

31 Responsibilities as Methods

32 Responsibilities as Methods Class variable Instance variables How does Java distinguish them?

33 Responsibilities as methods Review void methods Called as a statement value-returning methods Called in an expression; return a value

34 Responsibilities as methods Constructor An operation (method) that creates a new instance of a class  Same name as class  Default constructor (no parameters)  Parameterized constructor (with parameters)  A class can have more than one constructor  Is almost always public (Why?)

35 Responsibilities as methods Observer An operation that returns information from an object without changing the content of the object Immutability The property of a class that its instances cannot be modified once they are created Constructor-Observer classes Classes that have only constructors and observers and are therefore immutable (all we've seen so far)

36 Cat Class public class Cat { String name; String color; float weight; // constructors public Cat() { name = " "; color = " "; weight = 0.0; }

37 Cat Class public Cat(String newName, String newColor, float newWeight) { name = newName; weight = newWeight; colr = newColor; } public String getName() { return name; } Methods that return the value of a variable are often "get" + identifier. (Why?)

38 Cat Class public float getWeight() { return weight; } public String getColor() { return color; } Is class Cat immutable?

39 Driver for Cat Class public class CatDriver { public static void main (String[] args) { Cat myCat = new Cat("Meow", "ginger", 10.5); Cat cat = new Cat(); System.out.print("myCat "); System.out.println(myCat.getName() + " " + myCat.getColor() + " " + myCat.getWeight()); System.out.print("cat "); System.out.println(cat.getName() + " " + cat.getColor() + " " + cat.getWeight()); }

40 Extras I am called the first programmer Can you remember my name? My father's name?