ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.

Slides:



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

© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Fields, Constructors, Methods
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
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.
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
Chapter 4 Writing Classes. We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on:  class.
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.
ELC 312 Day 6. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Agenda Questions? Problem set two Posted in WebCT  Due Sept 30  On Page 153&154.
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.
© 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)
Objects and Classes. Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate.
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.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
CSE 1201 Object Oriented Programming Writing Classes.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
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 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
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.
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.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
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.
Anatomy of a Class & Method
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Chapter 4 Writing Classes.
Chapter 4 Writing Classes.
Chapter 5 – Writing Classes
Encapsulation & Visibility Modifiers
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.
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 4 Writing Classes
Anatomy of a Method.
Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation

ECE122 L6: Problem Definition and Implementation February 15, 2007 Outline °Problem: Create, read in and print out four sets of student grades °Setting up the problem °Breaking the problem into pieces °Writing needed classes and methods °Additional Java stuff Encapsulation, private/public, conditionals

ECE122 L6: Problem Definition and Implementation February 15, 2007 Encapsulation °Two views of an object: internal - the details of the variables and methods of the class that defines it external - the services that an object provides and how the object interacts with the rest of the system °From the external view, an object is an encapsulated entity Provides a set of specific services °These services define the interface to the object

ECE122 L6: Problem Definition and Implementation February 15, 2007 Encapsulation °One object (called the client) may use another object for the services it provides °The client of an object may request its services (call its methods) Should not have to be aware of how those services are accomplished °Any changes to the object's state (its variables) should be made by that object's methods

ECE122 L6: Problem Definition and Implementation February 15, 2007 Encapsulation °An encapsulated object can be thought of as a black box Inner workings are hidden from the client °The client invokes the interface methods of the object, which manages the instance data Methods Data Client

ECE122 L6: Problem Definition and Implementation February 15, 2007 Visibility Modifiers °Accomplish encapsulation through the appropriate use of visibility modifiers °A modifier is a Java reserved word Specifies particular characteristics of a method or data °The final modifier to define constants °Java has three visibility modifiers: public, protected, and private °The protected modifier will be discussed later

ECE122 L6: Problem Definition and Implementation February 15, 2007 Visibility Modifiers °Members of a class that are declared with public visibility can be referenced anywhere °Members of a class that are declared with private visibility can be referenced only within that class °Members declared without a visibility modifier have default visibility Can be referenced by any class in the same package

ECE122 L6: Problem Definition and Implementation February 15, 2007 Visibility Modifiers °Methods that provide the object's services are declared with public visibility They can be invoked by clients °Public methods are also called service methods °A method created simply to assist a service method is called a support method °Support method should be declared with private visibility

ECE122 L6: Problem Definition and Implementation February 15, 2007 Visibility Modifiers publicprivate Variables Methods Provide services to clients Support other methods in the class Enforce encapsulation Violate encapsulation

ECE122 L6: Problem Definition and Implementation February 15, 2007 Accessors and Mutators °Instance data is usually private A class usually provides services to access and modify data values °An accessor method returns the current value of a variable °A mutator method changes the value of a variable °The names of accessor and mutator methods often take the form getX and setX X is the name of the value °They are sometimes called “getters” and “setters”

ECE122 L6: Problem Definition and Implementation February 15, 2007 Mutator Restrictions °The use of mutators restricts modifications to an object’s state °A mutator is often designed so that the values of variables can be set only within particular limits °Example: setFaceValue mutator of the Die class should have restricted the value to the valid range (1 to MAX )

ECE122 L6: Problem Definition and Implementation February 15, 2007 Bank Account Example °Example that demonstrates the implementation details of classes and methods °We’ll represent a bank account by a class named Account °It’s state can include the account number, the current balance, and the name of the owner °An account’s behaviors (or services) include deposits and withdrawals, and adding interest

ECE122 L6: Problem Definition and Implementation February 15, 2007 Bank Account Example acct acctNumber balance name “Ted Murphy” acct acctNumber balance name “Jane Smith”

ECE122 L6: Problem Definition and Implementation February 15, 2007 Constructors °Note that a constructor has no return type specified in the method header, not even void °A common error is to put a return type on a constructor Makes it a “regular” method that happens to have the same name as the class °The programmer does not have to define a constructor for a class

© 2004 Pearson Addison-Wesley. All rights reserved // Account.java Author: Lewis/Loftus // Represents a bank account with basic services such as deposit and withdraw. //******************************************************************** import java.text.NumberFormat; public class Account. { private final double RATE = 0.035; // interest rate of 3.5% private long acctNumber; private double balance; private String name; // Sets up the account by defining its owner, account number and initial balance. public Account (String owner, long account, double initial) { name = owner; acctNumber = account; balance = initial; } // Deposits the specified amount into the account. Returns the new balance. public double deposit (double amount) { balance = balance + amount; return balance; } // Withdraws the specified amount from the account and applies the fee. Returns the new balance. public double withdraw (double amount, double fee) { balance = balance - amount - fee; return balance; } Instance data. EACH object of type Account will get its own copy of these variables. Note: all ‘private.’ (means only methods of these objects can access these variables. Note: the Constructor. Same name as class. Initializes attributes, and sets up initial ‘state’ of an object. Note: all methods public (here). Note: method names, formal params, and return types, if any.

ECE122 L6: Problem Definition and Implementation February 15, 2007 Summary °Ideas on setting up problems from scratch Identify the classes Identify the data for each class Identify the methods for each class Write the code needed for each class Test and debug °Encapsulation Most class data is private Data is accessed and modified via accessors and mutators Allows for limits to be set on data range