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

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Advertisements

Chapter 4: Writing Classes
Chapter 4: Writing Classes
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes The programs we’ve written in previous examples have.
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.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 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.
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)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
SEEM Java – Basic Introduction, Classes and Objects.
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.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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
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
Chapter 4 Writing Classes.
Chapter 4 Writing Classes.
Chapter 5 – Writing Classes
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
Chapter 4 Writing Classes.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes
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)
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
What to expect this week
Presentation transcript:

Based on Java Software Development, 5th Ed. By Lewis &Loftus 4. Writing Classes Based on Java Software Development, 5th Ed. By Lewis &Loftus

Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

User-defined Classes Java program consists of a set of classes. One class must contain a method namd main()—which becomes the starting point of the program. We have been using classes from the Java Standard Library (API). You will now write your own classes.

Classes & Objects Class has E.g., to represent dice in a game program: Name State (attributes) Behavior (methods) E.g., to represent dice in a game program: Name: Die (singular of dice) Attributes: MAXVALUE, faceValue Methods: constructor, roll(), toString(), getFacevalueValue()

Classes A class can contain data declarations and method declarations MAXVALUE, faceValue Data declarations Method declarations

Classes & Objects Necessary methods RollingDice.java Die.java Constructor – to create an object of Die class toString – returns the String value that represents the object in some way getFaceValue – to return the current face value of a die RollingDice.java Die.java

Constructor Method Constructor method(s) is used to instantiate objects. It can set initial values for objects. It has the same name as the class name. It has no return type. E.g., Die(){…} Student (String name, int age){…}

toString() Method Every class should include a toString() method, which returns some String value representing an object of the class. It is called automatically when an object is passed to the System.out.println() mthod. E.g., String toString() { return “Die object”; }

Scope of Data Scope of data Area in the program where data can be referenced (can be used) Data declared at the class level can be reference in all methods of the class (global visibility). Data declared in a method can be referenced only in that method (local visibility).

Instance Data In class Die, faceValue is called instance data, because each instance of Die maintains its own memory for faceValue with a value. Each instance (object) of class Die shares its methods, but maintains its own data space. Every time class Die is instantiated, a new memory for faceValue is allocated.

UML Diagram UML (Unified Modeling Language) diagrams show relationships among classes and objects. RollingDice main (args : String[]) : void Die faceValue : int Die() roll() : int setFaceValue (int value) : void getFaceValue() : int toString() : String

Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

Encapsulation Two views of a class Interface Internal (all variables and code visible) External (only public elements are visible) Interface Method names with parameters—no internal details of method body Methods and instance variables are encapsulated – combined into a single entity, for the purpose of Information hiding Data and behavior abstraction

Visibility Modifiers Modifier specifies some characteristic of method or data—e.g., final. Visibility Modifiers public Can be referenced from anywhere protected Can be referenced from subclasses (derived from a class) private Visible only from within the class default Visible to all classes within the same package

Visibility Modifiers Instance variables Methods Constants Should not be declared public Should not be accessible directly Should be accessed or modified via public methods Methods that provide services should be public that provide support for methods should not be public Constants Can be public, because they cannot be modified

Accessors and Mutators Given: class Fraction Accessors Methods which return the value of an instance variable E.g., getNumerator(), getDenominator() Mutators Mehtods which change the value of an instance variable E.g., setNumerator(), setDenominator() Required for each instance variable

Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

Method Header A method declaration begins with a method header boolean isEven (int num) return type method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter

Method Body The method header is followed by the method body Boolean isEven (int num) result is a local variable. It is created each time the method is called, and is destroyed when it finishes executing { boolean result; if (num % 2 == 0) result = true; else result = false; return result; } The return expression must be consistent with the return type

Invoking a Method If (isEven(a * b - c)){ … } else{ … } Actual parameter boolean isEven (int num) Formal parameter { boolean result; if (num % 2 == 0) result = true; else result = false; return result; } The value of actual parameter is copied to the formal parameter

Example Programs Transactions.java Account.java

Topics Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields