Object oriented programming (OOP): Just an extension to modularity. Why objects? 1. What is an object? Prof. Dionne Aleman.

Size: px
Start display at page:

Download "Object oriented programming (OOP): Just an extension to modularity. Why objects? 1. What is an object? Prof. Dionne Aleman."

Transcription

1 Object oriented programming (OOP): Just an extension to modularity Prof. Dionne Aleman MIE250: Fundamentals of object-oriented programming University of Toronto MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 1 / 1 Why objects? 1 Reduce complexity Facilitate understanding and modification Help testing and debugging Promote reusability 1 Sound familiar? MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 2 / 1 What is an object? Objects sound mysterious and complex, but are in fact very simple. Objects are essentially glorified variables. They are just wrappers around other bits of information (variables called member fields). They can also have internal functions, called methods, to interact with member fields and do whatever we want. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 3 / 1

2 Can you think of any examples we have seen so far? String object Fields Probably an array of chars, maybe an int for length Methods.length(),.contains(),.equals(String s2), etc. BufferedReader object Fields No idea Methods.readLine(), probably others that we haven t learned Math object Fields E, PI, maybe more Methods.sqrt(double x),.sin(double x),.random(), etc. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 4 / 1 What s the common theme? We don t really know what is happening inside those objects. Do we care? Nope. Is it easier for us that way? You betcha. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 5 / 1 Anatomy of an object: Encapsulation MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 6 / 1

3 Java program structure The.java file Header files The class Global variables (NEW) Function Function MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 7 / 1 Java class structure The.java file Header files The class Member variables (fields) Function (method) Function (method) MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 8 / 1 Class v. Object Class The blueprint from which individual objects are made Object An instance of a class For example: 1 String s; Variable s is an instance (object) of type (class) String. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 9 / 1

4 The Distance Calculator program Get a time and speed from the user and print out the distance traveled. Get speed input from user Get time input from user Calculate distance Print distance to screen Quit Get speed input from user Valid number? yes Get time input from user Valid number? yes Calculate distance no no Quit Print distance to screen MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 10 / 1 Remember this code? Get user input Do things with it Quit 1 public class DistanceV5 { 2 3 public static void main ( String [] args ) { 4 5 DistanceCalculator dc = new DistanceCalculator (); 6 dc. getuserinput (); 7 dc. printdistance (); } // end of main function 11 } // end of class src/distancecalculatoroop/distancev5.java MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 11 / 1 See it in action Let s re-build the Distance Calculator game as an OOP. We will go step-by-step: 1. Start with a simple object. 2. Slowly add in useful functionality. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 12 / 1

5 The steps to build an object oriented program 1. How do we want to the non-object part of the program (e.g., the main function) to look? 2. What classes do we need? 3. What data should go in them? 4. What methods to access and manipulate the data do we need? 5. Write the code. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 13 / 1 Step 1: We already know what the main function should look like 1 public class DistanceV5 { 2 3 public static void main ( String [] args ) { 4 5 DistanceCalculator dc = new DistanceCalculator (); 6 dc. getuserinput (); 7 dc. printdistance (); } // end of main function 11 } // end of class src/distancecalculatoroop/distancev5.java MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 14 / 1 Step 2: What classes do we need? A class to hold all speed and time data and do the calculations. Let s call the class DistanceCalculator. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 15 / 1

6 Step 3: What data should go in it? Speed Time Anything else? MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 16 / 1 Step 3: The class with data Note the file name must be the class name, just like in a regular program. All the data are basically like global variables to the class. What is private? 1 public class DistanceCalculator { 2 3 // member fields ( variables ): everything we need to know 4 private double speed ; // speed from user in mph 5 private double time ; // time from user in min 6 7 } MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 17 / 1 Step 4: How do we want to access/manipulate the data? Action Get user input Print distance Method name getuserinput() printdistance() MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 18 / 1

7 this is the best practice Use this to preface member fields and other member methods in methods for clarity and specificity: 1 public class DistanceCalculator { 2 3 private double speed ; // speed from user in mph 4 private double time ; // time from user in min 5 6 public void setvalues ( double speed, double time ) { 7 this. speed = speed ; // No conflict thanks to "this "! 8 this. time = time ; 9 System.out. println ("New distance calculation values :"); 10 this. printvalues (); 11 } } MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 19 / 1 Step 5: Write the code To do together: 1. Write the DistanceCalculator class. 2. Write the main function. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 20 / 1 One thing missing... The code works, but there is one thing missing that is strongly recommended for reliable performance of object oriented programs. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 21 / 1

8 Constructors A constructor is a method with no return type that is called when the object is declared. It usually just initializes variable values. 1 public class DistanceCalculator { 2 private double speed ; 3 private double time ; 4 5 // a " void " constructor : 6 // DistanceCalculator dc = new DistanceCalculator (); 7 public DistanceCalculator () { 8 this. speed = 0; 9 this. time = 0.; 10 } // an alternate constructor : 13 // DistanceCalculator dc = new DistanceCalculator (5.2, 20.75) ; 14 public DistanceCalculator ( double speed, double time ) { 15 this. speed = speed ; 16 this. time = time ; 17 } 18 } MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 22 / 1 Controlling data An important aspect of good OOP is controlling how other programs/functions can interact with our object s data. We don t want another program(mer) messing up the raw data inside a class we ve created. If someone needs access to an object s data, we should anticipate that need and write a function that 1. Makes that access easy. 2. Prevents someone else screwing up the data in our object. If data has to be accessed, it will be done on our terms. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 23 / 1 Access modifiers Access modifiers control the access we have to an object s data. Modifier Within class Outside class, in same package Outside class, outside package public protected private Unless you have a good reason to do otherwise, member variables should be private. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 24 / 1

9 Putting it together... Where have we seen public before? What does that mean? MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 25 / 1 public static void main We understand public now, but what is static? static means that particular member function (or variable) is the same across all objects of that class. In other words, we do not need to declare an object of that class in order to use a static function or variable. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 26 / 1 static example Say we want to use DistanceCalculator like this: 1 double dist = DistanceCalculator. calcdist (speed, time ); Do we need to declare an actual DistanceCalculator object in order to run that line of code? MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 27 / 1

10 When to use static So, we use static when an actual object of a class isn t needed: 1 public class DistanceCalculator { 2 // all the other class members and methods // a static method 6 public static double calcdist ( int speed, int time ) { 7 return speed * time ; 8 } 9 } Also works for members, not just methods MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 28 / 1 public or not? static or not? public static Math.sqrt() Math.PI str.equals() X MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 29 / 1 The Lucky Sevens program Let s re-build the Lucky Sevens game as an OOP. We will go step-by-step: 1. Start with a simple object. 2. Slowly add in useful functionality. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 30 / 1

11 The steps to build an object oriented program 1. How do we want to the non-object part of the program (e.g., the main function) to look? 2. What classes do we need? 3. What data should go in them? 4. What methods to access and manipulate the data do we need? 5. Write the code. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 31 / 1 Step 1: The program should look something like this 1 public class LuckySevens_OOP { 2 3 public static void main ( String [] args ) { 4 5 int startingcash = getuserstartingcash (); 6 LuckySevens game = new LuckySevens ( startingcash ); 7 8 game. play (); 9 game. summarize (); 10 game. checkfairness (); } // end main } MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 32 / 1 Step 2: What classes do we need? A class to hold all of the Lucky Sevens game data and run the game. Let s call the class LuckySevens. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 33 / 1

12 Step 3: What data should go in it? Starting cash Number of rolls until bankruptcy Peak cash Roll distribution (to check for fairness) Amount won on 7 Amount lost on non-7 MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 34 / 1 Step 3: The class with data Note the file name must be the class name, just like in a regular program. All the data are basically like global variables to the class. What is private? 1 public class LuckySevens { 2 3 // member fields ( variables ): everything we want to know about a game 4 private int winvalue ; // amount won on roll of 7 5 private int losevalue ; // amount lost on non -7 roll 6 private int startingcash ; // starting cash value 7 private int nrolls ; // number of rolls until bankruptcy 8 private int maxcash ; // peak winnings in the game 9 private int [] rollfreq = new int [13]; // frequency of roll values } MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 35 / 1 Step 5: Write the code To do together: 1. Write the LuckySevens class. 2. Write the main function. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 36 / 1

13 Point class example Say we want a program that will store many points in a Cartesian coordinate system. We will want to do some calculations on these points, e.g., determine which is closest to the origin, determine which points are on a particular line, etc. Each point consists of an (x, y) coordinate. We could store an array of x-coordinates and an array of y-coordinates, but that would be cumbersome. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 37 / 1 Start simple We know our object will need two member variables to store the x-coordinate and y-coordinate. Let s start with the following capabilities: Initialize the point Modify the coordinates Print out the coordinates MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 38 / 1 What do we want our main program to look like? 1 public class GridSystem { 2 3 public static void main ( String [] args ) { 4 Point p1 = new Point (); 5 Point p2 = new Point (4,5); 6 7 System.out. print (" Point p1: "); 8 p1. print (); 9 10 System.out. print (" Point p2: "); 11 p2. print (); p1 = p2; 14 System.out. print ("New point p1: "); 15 p1. print (); 16 } } src/gridsystemoop/gridsystem.java MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 39 / 1

14 Let s build the Point class Again, building it TOGETHER. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 40 / 1 Good job! Now let s make it more interesting Any ideas? Translate point a certain amount Calculate distance to origin Function to determine which point is closest to the origin Calculate distance between points Function to determine which points are closest together Function to determine which points are furthest apart Determine if a point is on a line Function to determine how many and which points are on a line Function to determine how many and which points are not on a line MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 41 / 1 The new Point class Build it... MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 42 / 1

15 How about a Line class? Member variables: Point Slope Methods: Constructor by Point object and slope value, or maybe by two Points Find y-intercept Find root Check if a point is on the line MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 43 / 1 Points and Lines Let s build a program that uses both Point and Line objects. We may want to revise some of our Point code to use the Line class. MIE250: Fundamentals of object-oriented programming (Aleman) Object oriented programming (OOP) 44 / 1

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

More information

Class 9: Static Methods and Data Members

Class 9: Static Methods and Data Members Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward Goals This the session in which we explain what static means. You

More information

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

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

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

Chapter 15: Object Oriented Programming

Chapter 15: Object Oriented Programming Chapter 15: Object Oriented Programming Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey How do Software Developers use OOP? Defining classes to create objects UML diagrams to

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

For example, when we first started Java programming we described the HelloWorld class:

For example, when we first started Java programming we described the HelloWorld class: Classes and Methods We have already been using classes in Java as we have seen, a class corresponds to an object. Classes encourage good programming style by allowing the user to encapsulate both data

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

Java and OOP. Part 2 Classes and objects

Java and OOP. Part 2 Classes and objects Java and OOP Part 2 Classes and objects 1 Objects OOP programs make and use objects An object has data members (fields) An object has methods The program can tell an object to execute some of its methods

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

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

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

Class and Functions. Reusable codes

Class and Functions. Reusable codes Class and Functions Reusable codes Object Oriented Design First concept of object oriented programming is emerged at 60 s. Smalltalk language which is introduced at 1972 was first object oriented programming

More information

XNA Tutorials Utah State University Association for Computing Machinery XNA Special Interest Group RB Whitaker 21 December 2007

XNA Tutorials Utah State University Association for Computing Machinery XNA Special Interest Group RB Whitaker 21 December 2007 XNA Tutorials Utah State University Association for Computing Machinery XNA Special Interest Group RB Whitaker 21 December 2007 Console Windows Supplementary Tutorial 8 Overview The majority of beginning

More information

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class For this assignment we will be developing a text-based Tic Tac Toe game 1. The key to this assignment is that we re going

More information

Programming Exercise 7: Static Methods

Programming Exercise 7: Static Methods Programming Exercise 7: Static Methods Due date for section 001: Monday, February 29 by 10 am Due date for section 002: Wednesday, March 2 by 10 am Purpose: Introduction to writing methods and code re-use.

More information

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output Last revised January 12, 2006 Objectives: 1. To introduce arithmetic operators and expressions 2. To introduce variables

More information

static String usersname; public static int numberofplayers; private static double velocity, time;

static String usersname; public static int numberofplayers; private static double velocity, time; A class can include other things besides subroutines. In particular, it can also include variable declarations. Of course, you can declare variables inside subroutines. Those are called local variables.

More information

Week 3 Classes and Objects

Week 3 Classes and Objects Week 3 Classes and Objects written by Alexandros Evangelidis, adapted from J. Gardiner et al. 13 October 2015 1 Last Week Last week, we looked at some of the different types available in Java, and the

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14 COSC 2P91 Introduction Part Deux Week 1b Brock University Brock University (Week 1b) Introduction Part Deux 1 / 14 Source Files Like most other compiled languages, we ll be dealing with a few different

More information

CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 7&8: Methods Lecture Contents What is a method? Static methods Declaring and using methods Parameters Scope of declaration Overloading

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

Classes Classes 2 / 35

Classes Classes 2 / 35 Classes 1 / 35 Classes Classes 2 / 35 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

Write for your audience

Write for your audience Comments Write for your audience Program documentation is for programmers, not end users There are two groups of programmers, and they need different kinds of documentation Some programmers need to use

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

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

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

The return Statement

The return Statement The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

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

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

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

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady. Introduction After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady. Your theory that the sun is the centre of the solar system, and the earth

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

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

Keep Track of Your Passwords Easily

Keep Track of Your Passwords Easily Keep Track of Your Passwords Easily K 100 / 1 The Useful Free Program that Means You ll Never Forget a Password Again These days, everything you do seems to involve a username, a password or a reference

More information

JAVA GUI PROGRAMMING REVISION TOUR III

JAVA GUI PROGRAMMING REVISION TOUR III 1. In java, methods reside in. (a) Function (b) Library (c) Classes (d) Object JAVA GUI PROGRAMMING REVISION TOUR III 2. The number and type of arguments of a method are known as. (a) Parameter list (b)

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java 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

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really Week 9: Methods 1 We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and hard to read Sometimes you

More information

Programming assignment A

Programming assignment A Programming assignment A ASCII Minesweeper Official release on Feb 14 th at 1pm (Document may change before then without notice) Due 5pm Feb 25 th Minesweeper is computer game that was first written in

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

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 Classwork 7: Craps N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 For this classwork, you will be writing code for the game Craps. For those of you who do not know, Craps is a dice-rolling

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018 Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018 Due: Tuesday, September 18, 11:59 pm Collaboration Policy: Level 1 (review full policy for details) Group Policy: Individual This lab will give you experience

More information

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

More information

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226 Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters

More information

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness. Spring 2014 Student Responsibilities Reading: Textbook, Sections 6.1 6.3 Attendance Recall: Writing Methods Decomposition: break a problem down into smaller subproblems Use methods whenever you can in

More information

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit ICOM 4015 Advanced Programming Laboratory Chapter 1 Introduction to Eclipse, Java and JUnit University of Puerto Rico Electrical and Computer Engineering Department by Juan E. Surís 1 Introduction This

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Ray John Pamillo 1/27/2016 1 Nokia Solutions and Networks 2014 Outline: Brief History of OOP Why use OOP? OOP vs Procedural Programming What is OOP? Objects and Classes 4 Pillars

More information

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 15 Object-Oriented Development from Structs to Classes The difference between structs and classes A class in C++ is basically the same thing as a struct The following are exactly equivalent struct

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm) CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, 2015 11:30pm) This program focuses on binary trees and recursion. Turn in the following files using the link on the

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module Basic Class Design Goal of OOP: Reduce complexity of software development by keeping details, and especially changes to details, from spreading throughout the entire program. Actually, the same goal as

More information

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER. Are we there yet?

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER. Are we there yet? PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER Are we there yet? Developing software, OOA&D style You ve got a lot of new tools, techniques, and ideas about how to develop

More information

Sedgewick Specialties

Sedgewick Specialties Sedgewick Specialties Our textbook is apparently a follow-up on another CS1083-ish book that uses a lot of simplified libraries, to avoid overwhelming newbies with the standard Java libraries. This book

More information

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types. Class #07: Java Primitives Software Design I (CS 120): M. Allen, 13 Sep. 2018 Two Types of Types So far, we have mainly been dealing with objects, like DrawingGizmo, Window, Triangle, that are: 1. Specified

More information

Objects as a programming concept

Objects as a programming concept Objects as a programming concept IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4:

More information

Introduction to C: Pointers

Introduction to C: Pointers Introduction to C: Pointers Nils Moschüring PhD Student (LMU) Nils Moschüring PhD Student (LMU), Introduction to C: Pointers 1 1 Introduction 2 Pointers Basics Useful: Function

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

Guessing Game with Objects

Guessing Game with Objects Objectives Lab1: Guessing Game with Objects Guessing Game with Objects 1. Practice designing and implementing an object-oriented program. 2. Use Console I/O in Java. Tasks 1. Design the program (problem

More information

Chapter 11: Create Your Own Objects

Chapter 11: Create Your Own Objects Chapter 11: Create Your Own Objects Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Our usual text takes a fairly non-standard departure in this chapter. Instead, please refer

More information

QUICK EXCEL TUTORIAL. The Very Basics

QUICK EXCEL TUTORIAL. The Very Basics QUICK EXCEL TUTORIAL The Very Basics You Are Here. Titles & Column Headers Merging Cells Text Alignment When we work on spread sheets we often need to have a title and/or header clearly visible. Merge

More information

Classes, interfaces, & documentation. Review of basic building blocks

Classes, interfaces, & documentation. Review of basic building blocks Classes, interfaces, & documentation Review of basic building blocks Objects Data structures literally, storage containers for data constitute object knowledge or state Operations an object can perform

More information

Week 3. This is CS50. Harvard University. Fall Cheng Gong

Week 3. This is CS50. Harvard University. Fall Cheng Gong This is CS50. Harvard University. Fall 2014. Cheng Gong Table of Contents Command-Line Arguments... 1 Memory Access... 5 Return Values... 6 More on argv... 8 Sorting... 10 Bubble Sort... 11 Selection Sort...

More information

Declaring and ini,alizing 2D arrays

Declaring and ini,alizing 2D arrays Declaring and ini,alizing 2D arrays 4 2D Arrays (Savitch, Chapter 7.5) TOPICS Multidimensional Arrays 2D Array Allocation 2D Array Initialization TicTacToe Game // se2ng up a 2D array final int M=3, N=4;

More information

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes Student Responsibilities Mat 2170 Week 9 Objects and Classes Spring 2014 Reading: Textbook, Sections 6.1 6.3 Lab 9 Attendance 1 2 Recall: Writing Methods 3 Decomposition: break a problem down into smaller

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

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

Objectives of CS 230. Java portability. Why ADTs?  8/18/14 http://cs.wellesley.edu/~cs230 Objectives of CS 230 Teach main ideas of programming Data abstraction Modularity Performance analysis Basic abstract data types (ADTs) Make you a more competent programmer

More information

How to make a "hello world" program in Java with Eclipse *

How to make a hello world program in Java with Eclipse * OpenStax-CNX module: m43473 1 How to make a "hello world" program in Java with Eclipse * Hannes Hirzel Based on How to make a "hello world" program in Java. by Rodrigo Rodriguez This work is produced by

More information

Java Review. Fundamentals of Computer Science

Java Review. Fundamentals of Computer Science Java Review Fundamentals of Computer Science Link to Head First pdf File https://zimslifeintcs.files.wordpress.com/2011/12/h ead-first-java-2nd-edition.pdf Outline Data Types Arrays Boolean Expressions

More information

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created. + Inheritance + Inheritance Classes that we design in Java can be used to model some concept in our program. For example: Pokemon a = new Pokemon(); Pokemon b = new Pokemon() Sometimes we need to create

More information

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen Computer Programming Computers can t do anything without being told what to do. To make the computer do something useful, you must give it instructions. You can give a computer instructions in two ways:

More information

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 I. Logic 101 In logic, a statement or proposition is a sentence that can either be true or false. A predicate is a sentence in

More information

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)... Remembering numbers (and other stuff)... Let s talk about one of the most important things in any programming language. It s called a variable. Don t let the name scare you. What it does is really simple.

More information

The Java Main Method

The Java Main Method The Java Main Method Introduction In this article from my free Java 8 Course, I will be discussing the public static void main(string[] args) method. Up until this point in the series, we have run our

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

HOW TO WRITE RECURSIVE FUNCTIONS ON YOUR OWN

HOW TO WRITE RECURSIVE FUNCTIONS ON YOUR OWN HOW TO WRITE RECURSIVE FUNCTIONS ON YOUR OWN In this tutorial, you ll learn - well, you ve already read the title - how to write recursive functions on your own. If you re the person to whom recursive

More information

Object-Oriented Programming

Object-Oriented Programming Object-Oriented Programming In C++ classes provide the functionality necessary to use object-oriented programming OOP is a particular way of organizing computer programs It doesn t allow you to do anything

More information

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS113: Lecture 4. Topics: Functions. Function Activation Records CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make

More information

Files and Streams

Files and Streams Files and Streams 4-18-2006 1 Opening Discussion Do you have any questions about the quiz? What did we talk about last class? Do you have any questions about the assignment? What are files and why are

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 #23: OO Design, cont d. Janak J Parekh janak@cs.columbia.edu Administrivia HW#5 due Tuesday And if you re cheating on (or letting others see your) HW#5

More information

CSSE 220 Day 13. Encapsulation Coupling and Cohesion Scoping. Please download EncapsulationExamples from your SVN

CSSE 220 Day 13. Encapsulation Coupling and Cohesion Scoping. Please download EncapsulationExamples from your SVN CSSE 220 Day 13 Encapsulation Coupling and Cohesion Scoping Please download EncapsulationExamples from your SVN The plan Learn 3 essential object oriented design terms: Encapsulation Coupling Cohesion

More information

Lesson 12: OOP #2, Accessor Methods (W03D4)

Lesson 12: OOP #2, Accessor Methods (W03D4) Lesson 12: OOP #2, Accessor Methods (W03D4) Balboa High School Michael Ferraro September 3, 2015 1 / 29 Do Now In your driver class from last class, create another new Person object with these characteristics:

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: 1 Date:

More information

Programming Assignment 2

Programming Assignment 2 CS 122 Fall, 2004 Programming Assignment 2 New Mexico Tech Department of Computer Science Programming Assignment 2 CS122 Algorithms and Data Structures Due 11:00AM, Wednesday, October 13th, 2004 Objectives:

More information

Part 2: The Material PART 2

Part 2: The Material PART 2 PART 2 With the introduction of what an object is, now we are ready to learn the CONSTRUCTOR concept. Just to refresh our memory, let s take a look at what we have learned in part 1. A sample class declaration,

More information

Lecture 1 - Introduction (Class Notes)

Lecture 1 - Introduction (Class Notes) Lecture 1 - Introduction (Class Notes) Outline: How does a computer work? Very brief! What is programming? The evolution of programming languages Generations of programming languages Compiled vs. Interpreted

More information