ECE 122. Engineering Problem Solving with Java

Similar documents
Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Anatomy of a Class Encapsulation Anatomy of a Method

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Designing Classes part 2

Chapter 4: Writing Classes

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

Where do objects come from? Good question!

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

CS1004: Intro to CS in Java, Spring 2005

Encapsulation. Mason Vail Boise State University Computer Science

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS112 Lecture: Defining Instantiable Classes

Objects and Classes -- Introduction

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Assignment 1 due Monday at 11:59pm

Chapter 5: Writing Classes and Enums

Chapter 4 Defining Classes I

CPS122 Lecture: Defining a Class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Chapter 4 Writing Classes

Packages & Random and Math Classes

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

writing classes objectives chapter

Programming II (CS300)

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

Container Vs. Definition Classes. Container Class

Classes and Methods: Classes

ECE 122. Engineering Problem Solving with Java

Programming II (CS300)

Programming II (CS300)

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

The Object Oriented Paradigm

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited

Java Basic Introduction, Classes and Objects SEEM

Object-Oriented Programming (Java)

Java Basic Introduction, Classes and Objects SEEM

EECS168 Exam 3 Review

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

$ % $ % BankAccount. public class BankAccount {... private??? balance; } "commands, transformers, mutators! "abstract state! "queries, accessors!

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

Defensive Programming

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

CS 302: Introduction to Programming in Java. Lecture 15

Programming II (CS300)

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

STUDENT LESSON A5 Designing and Using Classes

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Object-Oriented Programming in Java

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

CSE 143. "Class Relationships" Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization

CSCI 253. Outline. Background. George Blankenship 1

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

6.096 Introduction to C++

CSCE 156 Computer Science II

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

LECTURE 03 LINKED LIST

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

UNDERSTANDING CLASS DEFINITIONS CITS1001

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Overview of Eclipse Lectures. Module Road Map

C08: Inheritance and Polymorphism

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

What will this print?

Chapter 4. Defining Classes I

Methods and Data (Savitch, Chapter 5)

Recommended Group Brainstorm (NO computers during this time)

Principles of Object Oriented Programming. Lecture 4

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Software Design and Analysis for Engineers

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; }

5. Defining Classes and Methods

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing

Chapter 6 Introduction to Defining Classes

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Encapsulation in Java

Classes Classes 2 / 36

Classes and Objects. CGS 3416 Spring 2018

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation

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

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

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

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 Client Methods Data

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

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

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

Visibility Modifiers public private Variables Violate encapsulation Enforce encapsulation Methods Provide services to clients Support other methods in the class

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

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)

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

Bank Account Example acct1 72354 acctnumber balance 102.56 name Ted Murphy acct2 69713 acctnumber balance 40.00 name Jane Smith

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

// 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; 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. // Sets up the account by defining its owner, account number and initial balance. public Account (String owner, long account, double initial) { Note: the Constructor. name = owner; Same name as class. acctnumber = account; Initializes attributes, and sets balance = initial; up initial state of an object. } // Deposits the specified amount into the account. Returns the new balance. public double deposit (double amount) Note: all methods public (here). { Note: method names, formal params, balance = balance + amount; return balance; and return types, if any. } // 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 2004 Pearson balance; Addison-Wesley. All rights reserved }

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