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

Similar documents
Chapter 4 Defining Classes I

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

Notes on Chapter Three

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

PROGRAMMING FUNDAMENTALS

Assignment 1 due Monday at 11:59pm

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

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

TaxiBot New attributes Variables Math! TaxiBot

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

Chapter 4. Defining Classes I

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

8/27/13. An intro to programming* *Java. Announcements. Professor Rodger. The Link. UTAs start TONIGHT!

Ch 7 Designing Java Classes & Class structure

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

Chapter 4: Writing Classes

Lecture Set 4: More About Methods and More About Operators

Java and OOP. Part 2 Classes and objects

CSC Java Programming, Fall Java Data Types and Control Constructs

CS 251 Intermediate Programming Methods and Classes

EECS168 Exam 3 Review

CS 251 Intermediate Programming Methods and More

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

Android/Java Lightning Tutorial JULY 30, 2018

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Defining Classes and Methods

Programming II (CS300)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

Lecture Set 4: More About Methods and More About Operators

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Object oriented programming Concepts

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Java Classes & Primitive Types

Crash Course Review Only. Please use online Jasmit Singh 2

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Java Classes & Primitive Types

Objects as a programming concept

What will this print?

Comments are almost like C++

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Chapter 4. Defining Classes I

COMPUTER APPLICATIONS

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

C++ Important Questions with Answers

Computational Expression

CS18000: Problem Solving And Object-Oriented Programming

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

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

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Methods and Data (Savitch, Chapter 5)

Chapter 3 Objects and Classes

Java Identifiers, Data Types & Variables

Defining Your Own Classes

BM214E Object Oriented Programming Lecture 8

Getting started with Java

Recitation 3 Class and Objects

ECE 122. Engineering Problem Solving with Java

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Introduction to Programming Using Java (98-388)

CS1004: Intro to CS in Java, Spring 2005

CS112 Lecture: Defining Instantiable Classes

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

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Defining Classes and Methods

Anatomy of a Class Encapsulation Anatomy of a Method

Classes Classes 2 / 35

COSC 1010 Introduction to Computer Programming

Selected Questions from by Nageshwara Rao

THE CONCEPT OF OBJECT

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS112 Lecture: Working with Numbers

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

9 Working with the Java Class Library

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

CHAPTER 7 OBJECTS AND CLASSES

CPSC 233: Assignment 4 (Due March 26 at 4 PM)

Lec 3. Compilers, Debugging, Hello World, and Variables

Program Fundamentals

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

CS 302 Week 9. Jim Williams

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

BM214E Object Oriented Programming Lecture 7

Lecture 18 Tao Wang 1

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Object Oriented Programming in C#

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CS121/IS223. Object Reference Variables. Dr Olly Gotel

JAVA GUI PROGRAMMING REVISION TOUR III

Object-Oriented Programming Concepts

Principles of Object Oriented Programming. Lecture 4

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Computer Science is...

Transcription:

Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn how to design and make use of data structures that are fundamental to all areas of computing. We will focus on abstract data types (ADTs) using the object-oriented paradigm. An ADT is abstractions of data and basic operations on the abstractions. We will use a Java class to implement an ADT. 3 1

Object-oriented paradigm 4 Object-oriented paradigm In general, programs are instructions to do something on given data. Programs two main components are: Data (or variables to store data). Statements that define computation (or something to do). 5 Object-oriented paradigm In conventional programming, data and computation are defined separately. In object-oriented programming, data and computation must be defined together. 6 2

What is an object? An object is data equipped with relevant computation. Formally, an object is defined by: A set of attributes that define data. A set of operations (or behaviors) that define actions on data. 7 What is an object? Example. Lucy, a dog, is an object defined by the following: Attributes: female, Golden Retriever, 22 in., 60 lbs., 5 years old. Operations (or behaviors): bark, walk, run, sit, jump, swim,... 8 Objects Many objects share common attribute types and operations. For example, all dogs share: Attributes: gender, breed, height, weight, age. Operations (or behaviors): bark, walk, run, sit, jump, swim,... In fact, all dog objects can be defined by these attributes and operations. 9 3

What is a class? A class is an abstract definition of objects that share common attribute types and operations. Formally, a class is defined by: A set of attributes without specific data. A set of operations (or behaviors) that define actions on data. 10 Classes and objects A class is an abstract description of objects that share common attribute types and operations. A class may be regarded as simply collection of objects that share common attribute types and operations. An object is an instance of a class. For example, the object Lucy is an instance of the class Dog. 11 Classes and objects In Java, an object must be defined as an instance of a class. Every Java class has a constructor that instantiates an object. In Java, attributes are called fields, and operations (or behaviors) are called methods. 12 4

Java class public class Dog { Fields (attributes) Constructor Methods (operations or behaviors) 13 Accessibility Classes, fields and methods must be declared with access modifiers. Classes and methods are usually declared as public accessible by anyone from anywhere. Fields are usually declared as private accessible only within the object. 14 Primitive data types Fields are often defined using variables. In Java, variables must be declared with types. There are four primitive data types: int integers:..., -1, 0, 1, 2,... double reals:..., 3.14159,... char letters: a, b, c,... boolean boolean values: true, false 15 5

public class Dog { Defining fields private String breed; private char gender; private double height; private double weight; private int age; Constructor Methods (operations or behaviors) 16 Methods Methods define objects operations, similar to the way functions define basic computational tasks. Many (but not all) methods return values, just like many functions return values. All methods must be declared with return types (which specify the types of values being returned). 17 public class Dog { Defining methods private String breed; private char gender; private double height; private double weight; private int age; Constructor public int getage() { return age; 18 6

public class Dog { Defining methods private String breed; private char gender; private double height; private double weight; private int age; Constructor public void getolder() { age = age + 1; 19 Constructor A constructor is a special method that instantiates an object. Objects are created from a class by invoking a constructor. Constructors usually have parameters, and constructors create objects according to the values of parameters. 20 Parameters Many (but not all) constructors and methods have parameters, just like many functions have parameters. In Java, parameters must be declared with types, just like variables must be declared with types. 21 7

public class Dog { Defining a constructor private String breed; private char gender; private double height; private double weight; private int age; public Dog(String b, char g, double h, double w, int a) { breed = b; gender = g; height = h; weight = w; age = a; Methods (operations or behaviors) 22 Instantiating an object Dog lucy = new Dog( Golden Retriever, f, 22, 60, 5); lucy.getolder(); System.out.print( Lucy is now + lucy.getage() + years old. ); 23 Accessors A method that simply returns the value of a field is called an accessor (or get method). An accessor for a field f is usually named getf. An accessor has no parameter. The return type of an accessor for a field f is the type of f. 24 8

Accessors public class Coo { private type f;... public type getf() { return f; 25 Mutators A method that simply changes the value of a field by a parameter value is called a mutator (or modifier or set method). A mutator for a field f is usually named setf. A mutator returns nothing. A mutator for a field f has a parameter whose type is the same as that of f. 26 public class Coo { private type f;... public type getf() { return f; Mutators public void setf(type f0) { f = f0; 27 9

Method calls Every method belongs to some object. In general, when we call a method, we must specify both the object and method: object.method(<parameters>) 28 Internal method calls To invoke a method within the same object, we can just call by the method name. public class Coo { public void moo() { noo(5); public void noo(int x) {... 29 Internal method calls Or, to be more explicit, you may also put this with the method name. public class Coo { public void moo() { this.noo(5); public void noo(int x) {... 30 10

External method calls To invoke a method of another object, you must always explicitly specify the object. In general, from a class Doo, to call a Coo object c s method noo(), put: public class Doo { public void poo() { Coo c = new Coo(); c.noo(7); 31 The main method Some class has a special method, called main, that defines a stand-alone program. A class that has the main method is called a Java application. public class Coo { public static void main(string[] args) {... 32 The main method To execute such a stand-alone program defined in the main method, just invoke java with the name of its class: $ java Coo 33 11

static method A method that does not belong to any object is called a static method. In general, when we call such a method, we must specify the class and method: Class.method(<parameters>) 34 static method public class Coo { public static void print() {... To call Coo s static method print() from outside, put: Coo.print(); 35 12