ECE 122. Engineering Problem Solving with Java

Size: px
Start display at page:

Download "ECE 122. Engineering Problem Solving with Java"

Transcription

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

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

10 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

11 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)

12 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

13 Bank Account Example acct acctnumber balance name Ted Murphy acct acctnumber balance name Jane Smith

14 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

15 // 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 }

16 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

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

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation Anatomy of a Method September 15, 2006 HW3 is due Today ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Midterm 1 Next Tuesday Sep 19 @ 6:30 7:45pm. Location:

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes 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 definitions instance data encapsulation and

More information

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

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes: Designing Classes part 2 CSC 1051 Data Structures and Algorithms I Getting to know classes so far Predefined classes from the Java API. Defining classes of our own: Driver classes: Account Transactions

More information

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

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

Designing Classes part 2

Designing Classes part 2 Designing Classes part 2 CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Getting

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

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 Designing Classes CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects

More information

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 Designing Classes CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects

More information

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

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects Writing Classes 4 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Writing Classes We've been using predefined

More information

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

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1 Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

Where do objects come from? Good question!

Where do objects come from? Good question! Designing Classes CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Where do objects

More information

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

Encapsulation. Mason Vail Boise State University Computer Science

Encapsulation. Mason Vail Boise State University Computer Science Encapsulation Mason Vail Boise State University Computer Science Pillars of Object-Oriented Programming Encapsulation Inheritance Polymorphism Abstraction (sometimes) Object Identity Data (variables) make

More information

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

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 4: Writing Classes Objects An object has: Presentation slides for state - descriptive characteristics Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus,

More information

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

Designing Classes. Where do objects come from? Where do objects come from? Example: Account datatype. Dr. Papalaskari 1 Designing Classes Where do objects come from? CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

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 Last Class CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Objects and Classes -- Introduction

Objects and Classes -- Introduction Objects and Classes -- Introduction Now that some low-level programming concepts have been established, we can examine objects in more detail Chapter 4 focuses on: the concept of objects the use of classes

More information

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

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited Table of Contents Date(s) Title/Topic Page #s 11/6 Chapter 3 Reflection/Corrections 56 Chapter 4: Writing Classes 4.1 Objects Revisited 57 58-59 look over your Ch 3 Tests and write down comments/ reflections/corrections

More information

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

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017 This week the main activity was a quiz activity, with a structure similar to our Friday lecture activities. The retrospective for the quiz is in Quiz-07- retrospective.pdf This retrospective explores the

More information

Assignment 1 due Monday at 11:59pm

Assignment 1 due Monday at 11:59pm Assignment 1 due Monday at 11:59pm The heart of Object-Oriented Programming (Now it gets interesting!) Reading for next lecture is Ch. 7 Focus on 7.1, 7.2, and 7.6 Read the rest of Ch. 7 for class after

More information

Chapter 5: Writing Classes and Enums

Chapter 5: Writing Classes and Enums Chapter 5: Writing Classes and Enums CS 121 Department of Computer Science College of Engineering Boise State University August 22, 2016 Chapter 5: Writing Classes and Enums CS 121 1 / 48 Chapter 5 Topics

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

CPS122 Lecture: Defining a Class

CPS122 Lecture: Defining a Class Objectives: CPS122 Lecture: Defining a Class last revised January 14, 2016 1. To introduce structure of a Java class 2. To introduce the different kinds of Java variables (instance, class, parameter, local)

More information

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

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

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

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

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

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables

More information

Chapter 4 Writing Classes

Chapter 4 Writing Classes Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Writing Classes We've been using predefined classes from the Java API. Now we will

More information

Packages & Random and Math Classes

Packages & Random and Math Classes Packages & Random and Math Classes Quick review of last lecture September 6, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Objects Classes An object

More information

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Classes Chapter 4 Classes and Objects Data Hiding and Encapsulation Function in a Class Using Objects Static Class members Classes Class represents a group of Similar objects A class is a way to bind the

More information

writing classes objectives chapter

writing classes objectives chapter 4 chapter objectives Define classes that serve as blueprints for new objects, composed of variables and methods. Explain the advantages of encapsulation and the use of Java modifiers to accomplish it.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

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

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation COMP-202 Unit 8: Defining Your Own Classes CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation Defining Our Own Classes (1) So far, we have been creating

More information

Container Vs. Definition Classes. Container Class

Container Vs. Definition Classes. Container Class Overview Abstraction Defining new classes Instance variables Constructors Defining methods and passing parameters Method/constructor overloading Encapsulation Visibility modifiers Static members 14 November

More information

Classes and Methods: Classes

Classes and Methods: Classes Class declaration Syntax: [] Classes and Methods: Classes [] class [] [] [] When

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 15 Class Relationships Outline Problem: How can I create and store complex objects? Review of static methods Consider static variables What about objects

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

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

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10B Class Design By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public

More information

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

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program

More information

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

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access. ENCAPSULATION private, public, scope and visibility rules. packages and package level access. Q. Explain the term Encapsulation with an example? Ans: The wrapping up to data and methods into a single units

More information

The Object Oriented Paradigm

The Object Oriented Paradigm The Object Oriented Paradigm Joseph Spring 7COM1023 Programming Paradigms 1 Discussion The OO Paradigm Procedural Abstraction Abstract Data Types Constructors, Methods, Accessors and Mutators Coupling

More information

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

Inheritance. Quick Review of Last Lecture. November 12, Passing Arguments. Passing Arguments. Variable Assignment Revisited Inheritance November 12, 200 Quick Review of Last Lecture ComS 20: Programming I (in Java) Iowa State University, FALL 200 Instructor: Alexander Stoytchev Passing Arguments Another important issue related

More information

Java Basic Introduction, Classes and Objects SEEM

Java Basic Introduction, Classes and Objects SEEM Java Basic Introduction, Classes and Objects SEEM 3460 1 Java A programming language specifies the words and symbols that we can use to write a program A programming language employs a set of rules that

More information

Object-Oriented Programming (Java)

Object-Oriented Programming (Java) Object-Oriented Programming (Java) Topics Covered Today 2.1 Implementing Classes 2.1.1 Defining Classes 2.1.2 Inheritance 2.1.3 Method equals and Method tostring 2 Define Classes class classname extends

More information

Java Basic Introduction, Classes and Objects SEEM

Java Basic Introduction, Classes and Objects SEEM Java Basic Introduction, Classes and Objects SEEM 3460 1 Java A programming language specifies the words and symbols that we can use to write a program A programming language employs a set of rules that

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2015) ramana@cs.columbia.edu Lecture-3 Constructor and destructor review Data and Member functions review Data encapsulation public, private and protected members

More information

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

$ % $ % BankAccount. public class BankAccount {... private??? balance; } commands, transformers, mutators! abstract state! queries, accessors! "! public class { private??? balance; "! # "! "commands, transformers, mutators! "abstract state! "queries, accessors! "constructors! & "! public class { private double balance; "! void "this! x.command1();

More information

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

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others COMP-202 Objects, Part III Lecture Outline Static Member Variables Parameter Passing Scopes Encapsulation Overloaded Methods Foundations of Object-Orientation 2 Static Member Variables So far, member variables

More information

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

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance

More information

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

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos. Accelerating Information Technology Innovation http://aiti.mit.edu Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos. Data Field Encapsulation Sometimes we want

More information

Defensive Programming

Defensive Programming Defensive Programming Software Engineering CITS1220 Based on the Java1200 Lecture notes by Gordon Royle Lecture Outline Why program defensively? Encapsulation Access Restrictions Documentation Unchecked

More information

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

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

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 - Week 7 1 Static member variables So far: Member variables

More information

CS 302: Introduction to Programming in Java. Lecture 15

CS 302: Introduction to Programming in Java. Lecture 15 CS 302: Introduction to Programming in Java Lecture 15 Class Instances of the class (objects) only valid at runtime Private Instance Methods Instance methods usually public why? If we have an internal

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects Outline Problem: How do I create multiple objects from a class Java provides a number of built-in classes for us Understanding

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

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

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs. Lab 8 Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs. Preparation A: Exercise using the Die class Create a subfolder Dice for your files in this exercise.

More information

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

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by: Recap of OO concepts Objects, classes, methods and more. Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Classes and Objects A class defines a

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java Software and Programming I Object-Oriented Programming in Java Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Object-Oriented Programming Public Interface of a Class Instance Variables

More information

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

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy

More information

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

CSE 143. Class Relationships Class Relationships and Inheritance. A Different Relationship. Has-a vs. Is-a. Hierarchies of Organization Class Relationships and Inheritance [Chapter 8, pp.343-354] "Class Relationships" is the title of Chapter 8 One class may include another as a member variable Date" used inside "Performance" Called a "has-a"

More information

CSCI 253. Outline. Background. George Blankenship 1

CSCI 253. Outline. Background. George Blankenship 1 CSCI 253 Object Oriented Design: Object Oriented Design and Programming in Java George Blankenship George Blankenship 1 Outline Background Rationale for the course Why object oriented programming? OOP

More information

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

6.096 Introduction to C++

6.096 Introduction to C++ MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. MASSACHUSETTS INSTITUTE

More information

CSCE 156 Computer Science II

CSCE 156 Computer Science II CSCE 156 Computer Science II Lab 04 - Classes & Constructors Dr. Chris Bourke Prior to Lab 1. Review this laboratory handout prior to lab. 2. Read Object Creation tutorial: http://download.oracle.com/javase/tutorial/java/javaoo/objectcreation.

More information

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

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

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

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

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

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes 1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist

More information

LECTURE 03 LINKED LIST

LECTURE 03 LINKED LIST DATA STRUCTURES AND ALGORITHMS LECTURE 03 LINKED LIST IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD LINKED LISTS DEFINITION A linked list is a data structure where each object is stored in

More information

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

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics in C++ Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved. 12.1 Procedural and Object-Oriented

More information

UNDERSTANDING CLASS DEFINITIONS CITS1001

UNDERSTANDING CLASS DEFINITIONS CITS1001 UNDERSTANDING CLASS DEFINITIONS CITS1001 Main concepts to be covered Fields / Attributes Constructors Methods Parameters Source ppts: Objects First with Java - A Practical Introduction using BlueJ, David

More information

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

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

Overview of Eclipse Lectures. Module Road Map

Overview of Eclipse Lectures. Module Road Map Overview of Eclipse Lectures 1. Overview 2. Installing and Running 3. Building and Running Java Classes 4. Refactoring Lecture 2 5. Debugging 6. Testing with JUnit 7. Version Control with CVS 1 Module

More information

C08: Inheritance and Polymorphism

C08: Inheritance and Polymorphism CISC 3120 C08: Inheritance and Polymorphism Hui Chen Department of Computer & Information Science CUNY Brooklyn College 2/26/2018 CUNY Brooklyn College 1 Outline Recap and issues Project progress? Practice

More information

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

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

What will this print?

What will this print? class UselessObject{ What will this print? int evennumber; int oddnumber; public int getsum(){ int evennumber = 5; return evennumber + oddnumber; public static void main(string[] args){ UselessObject a

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Methods and Data (Savitch, Chapter 5)

Methods and Data (Savitch, Chapter 5) Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private 2 public class Temperature { public static void main(string[] args) {

More information

Recommended Group Brainstorm (NO computers during this time)

Recommended Group Brainstorm (NO computers during this time) Recommended Group Brainstorm (NO computers during this time) Good programmers think before they begin coding. Part I of this assignment involves brainstorming with a group of peers with no computers to

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

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

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150, COMP 401: THE DUAL ROLE OF A CLASS Instructor: Prasun Dewan (FB 150, dewan@unc.edu) SCRIPTS ANALOGY Script Program Follows Follows Theater Performer 2 STRUCTURING IN SCRIPTS Script (Folder) Act (File)

More information

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

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

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

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance CS257 Computer Science I Kevin Sahr, PhD Lecture 10: Inheritance 1 Object Oriented Features For a programming language to be called object oriented it should support the following features: 1. objects:

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

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

Exercises 1. class member { int membernum = 25; float memberpay; public void Input(cin >> membernum >> memberpay); void Output; } Exercises 1 Part 1: Explain the output of the following code without running it: struct data { float z; char type; }; int main() { data D1 = {20, 'P'}; cout

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! Describe and define concepts of class and object! Describe use

More information

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

Chapter. Focus of the Course. Object-Oriented Software Development. program design, implementation, and testing Introduction 1 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Focus of the Course Object-Oriented Software Development

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

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

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static,

More information

Encapsulation in Java

Encapsulation in Java Encapsulation in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Encapsulation (1.1) Consider the following problem: A person has a name, a weight, and a height. A person s

More information

Classes Classes 2 / 36

Classes Classes 2 / 36 Classes 1 / 36 Classes Classes 2 / 36 Anatomy of a Class By the end of next lecture, you ll understand everything in this class definition. package edu. gatech. cs1331. card ; import java. util. Arrays

More information

Classes and Objects. CGS 3416 Spring 2018

Classes and Objects. CGS 3416 Spring 2018 Classes and Objects CGS 3416 Spring 2018 Classes and Objects An object is an encapsulation of data along with functions that act upon that data. It attempts to mirror the real world, where objects have

More information

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

Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class CS112 Lecture: Course Introduction Last revised 1/3/06 Objectives: 1. Introduce the course. 2. Define programming 3. Introduce fundamental concepts of OO: object, class Materials: 1. Questionnaire 2. Syllabi

More information

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

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling) Understanding class definitions Looking inside classes (based on lecture slides by Barnes and Kölling) Main Concepts fields constructors methods parameters assignment statements Ticket Machines (an external

More information