ECE 122. Engineering Problem Solving with Java

Similar documents
Chapter 4: Writing Classes

Anatomy of a Class Encapsulation Anatomy of a Method

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

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

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

Formatting Output & Enumerated Types & Wrapper Classes

Using Classes and Objects. Chapter

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

CS1004: Intro to CS in Java, Spring 2005

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

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

Designing Classes part 2

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!

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

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

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

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

We now start exploring some key elements of the Java programming language and ways of performing I/O

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

CSC Java Programming, Fall Java Data Types and Control Constructs

Chapter 5: Writing Classes and Enums

Assignment 1 due Monday at 11:59pm

Lab5. Wooseok Kim

A variable is a name for a location in memory A variable must be declared

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Java Basic Introduction, Classes and Objects SEEM

Java Basic Introduction, Classes and Objects SEEM

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS162: Introduction to Computer Science II

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Lab5. Wooseok Kim

Introduction to Programming Using Java (98-388)

CS1004: Intro to CS in Java, Spring 2005

writing classes objectives chapter

ECE 122. Engineering Problem Solving with Java

CS110D: PROGRAMMING LANGUAGE I

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Identifiers, Data Types & Variables

JAVA WRAPPER CLASSES

Data Types Reference Types

ECE 122. Engineering Problem Solving with Java

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

Using Classes and Objects

Lecture 5: Methods CS2301

Packages & Random and Math Classes

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

CS111: PROGRAMMING LANGUAGE II

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS 251 Intermediate Programming Methods and More

Objects and Classes -- Introduction

CS 251 Intermediate Programming Methods and Classes

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CompSci 125 Lecture 11

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

More About Objects and Methods

Methods (Deitel chapter 6)

Computer Science II (20082) Week 1: Review and Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

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

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

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Chapter 6 Introduction to Defining Classes

CT 229 Java Syntax Continued

CHAPTER 7 OBJECTS AND CLASSES

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Classes and Objects Part 1

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Lecture 04 FUNCTIONS AND ARRAYS

Methods (Deitel chapter 6)

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

More About Objects and Methods

Motivation was to facilitate development of systems software, especially OS development.

CMSC131. Library Classes

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Using Classes and Objects

CS-201 Introduction to Programming with Java

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

CS1150 Principles of Computer Science Methods

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

ECE 122. Engineering Problem Solving with Java

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Objects and Classes Lecture 2

From C++ to Java. Duke CPS

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

CS110: PROGRAMMING LANGUAGE I

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 5 Anatomy of a Class

Outline Problem: How do I build and use a class? Need to understand constructors A few more tools to add to our toolbox Formatting data Wrapper classes Putting it all together Understanding all the details of building and using classes Private and public data and methods

The Math Class The Math class is part of the java.lang package The Math class contains methods that perform various mathematical functions These include: absolute value square root exponentiation trigonometric functions

The Math Class The methods of the Math class are static methods (also called class methods) Static methods can be invoked through the class name no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); No need to instantiate the object here (built-in) Useful for various math functions

Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type byte short int long float double char boolean void Wrapper Class Byte Short Integer Long Float Double Character Boolean Void

Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object Integer age = new Integer(40); Wrapper classes used references Primitive classes do not use references Mem Loc 1089 688 40 Mem Loc 1089 Contents of memory

Wrapper Classes Wrapper classes contain methods that help manage the associated type intvalue longvalue Prevents the need to manage primitive types The Integer class contains a method to convert an integer stored in a String to an int value: num = Integer.parseInt(str);

Classes and Objects An object has state (declarations) and behavior (methods) Consider a coin State can be defined as heads or tails Behavior is that it can be flipped We can represent a coin in software by designing a class called coin that models this state and behavior The class serves as the blueprint for a coin object We can then instantiate any number of coins that we need for a program

Classes A class can contain data declarations and method declarations Already seen this many times int size, weight; char category; Data declarations Method declarations

Constructors A constructor is a special method that is used to set up an object when it is initially created A constructor has the same name as the class coin constructor used create an object which will generate a random variable public coin() { /* Create an instance of a random object */ rnd = new Random(); } Any unusual features?

Instance Data The facevalue variable in the Die class is called instance data Each instance (object) that is created has its own version of it A class declares the type of the data, but it does not reserve any memory space for it Every time a Die object is created, a new facevalue variable is created as well The objects of a class share the method definitions Each object has its own memory space

Instance Data We can depict the two Die objects from the RollingDice program as follows: die1 facevalue 5 die2 facevalue 2 Each object maintains its own facevalue variable, and thus its own state

Method Declarations A method declaration specifies the code that will be executed when the method is invoked (called) When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined Remember that software is sequential. Only one thing is happening at a time

Method Control Flow If the called method is in the same class, only the method name is needed compute mymethod mymethod();

Method Control Flow The called method is often part of another class or object main doit helpme obj.doit(); helpme();

Method Header A method declaration begins with a method header char calc (int num1, int num2, String message) method name parameter list return type 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 char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charat (sum); } return result; The return expression must be consistent with the return type sum and result are local data They are created each time the method is called, and are destroyed when it finishes executing

The return Statement The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type A return statement specifies the value that will be returned return expression; Its expression must conform to the return type

Parameters When a method is called, the actual parameters in the invocation are copied into the formal parameters Formal parameters located in method header ch = obj.calc (2, count, "Hello"); char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charat (sum); } return result;

Data Scope The scope of data is the area in a program in which that data can be referenced (used) Data declared at the class level can be referenced by all methods in that class Data declared within a method can be used only in that method Data declared within a method is called local data In the Die class, the variable result is declared inside the tostring method It is local to that method and cannot be referenced anywhere else

Local Data Local variables can be declared inside a method Method creates automatic local variables when the method is invoked When the method finishes, all local variables are destroyed (including the formal parameters) Instance variables, declared at the class level, exists as long as the object exists new indicates data is allocated and stays allocated until garbage collected. Local data in methods disappear when the method disappears

Formatting Output It is often necessary to format values in certain ways so that they can be presented properly The Java standard class library contains classes that provide formatting capabilities The NumberFormat class allows you to format values as currency or percentages The DecimalFormat class allows you to format values based on a pattern Both are part of the java.text package

Enumerated Types Java allows you to define an enumerated type, which can then be used to declare variables An enumerated type establishes all possible values for a variable of that type The values are identifiers of your own choosing The following declaration creates an enumerated type called Season enum Season {winter, spring, summer, fall}; Any number of values can be listed

Enumerated Types Once a type is defined, a variable of that type can be declared Season time; and it can be assigned a value time = Season.fall; The values are specified through the name of the type Enumerated types are type-safe you cannot assign any value other than those listed

Ordinal Values Internally, each value of an enumerated type is stored as an integer, called its ordinal value The first value in an enumerated type has an ordinal value of zero, the second one, and so on However, you cannot assign a numeric value to an enumerated type, even if it corresponds to a valid ordinal value

Enumerated Types The declaration of an enumerated type is a special type of class, and each variable of that type is an object The ordinal method returns the ordinal value of the object The name method returns the name of the identifier corresponding to the object's value

Summary We ve now learned most of what we need to know for classes Important concepts: constructors, parameters, return values, how computer memory is used Many objects can be created, each with their own memory Next time: Problem solving Solving a problem from definition to implementation