1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
CMT Programming Software Applications
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
How Create a C++ Program. #include using namespace std; void main() { cout
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Chapter 2: Introduction to C++.
Chapter 2 types, variables, methods Pages Horstmann.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Classes CS 21a: Introduction to Computing I First Semester,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 2: Java Fundamentals
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 3 Implementing Classes
A Sample Program #include using namespace std; int main(void) { cout
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Lecture 3 John Woodward.
Chapter 3: Using Methods, Classes, and Objects
Data types and variables
Chapter 4 Procedural Methods.
Java Programming with BlueJ
Chapter 2 Using Objects.
2.1 Parts of a C++ Program.
elementary programming
Chapter 2: Introduction to C++.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
Use a variable to store a value that you want to use at a later time
Each object belongs to a class
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects

2 Lecture Outline Working with types and variables Classes and objects Methods Parameters and return values Constructing and using objects API documentation CSC120 — Berry College — Fall 2005

3 Types Every value (piece of data) has a type "Hello World" : String System.out : PrintStream 13 : int Type determines what can be done with the values Can call println on any PrintStream object Can compute sum/product of any int (eger)s

4 Variables To store values for use at a later time A variable is a storage location in memory with Type (what type of data can be stored) Name (how you refer to the data) Contents (the actual data) Variables must be declared before use: String greeting = "Hello, World!"; PrintStream printer = System.out;... printer.println( greeting );

5 Syntax: Variable Declaration typeName variableName = value; or typeName variableName; Example: String greeting = "Hello, Dave!"; int x;Purpose: To define a new variable of a particular type and optionally supply an initial value

6 Identifiers (Names) Identifier: name of a variable, method, or class Case sensitive: greeting and Greeting different Rules Made up of letters, digits, underscore _ No other symbols allowed, including spaces May not start with a digit May not be a reserved word, like ‘public’ Conventions Variable and method names start with lowercase Class names start with uppercase letter Use ‘camelCase’ names Conventions are useful for other people to be able to easily read and understand your code

7 Assignment Operator Use = (assignment operator) to change value of an existing variable Note: = symbol does not refer to equality in Java 12 = 12; Error to use variable that does not have value assigned

8 Syntax: Assignment variableName = value; Example: luckyNumber = 12;Purpose: To assign a new value to a previously defined variable

9 Objects ‘Things’ that you can manipulate in your Java programs Represent entities in real world: bank accounts, employee records, graphical shapes, computer game player Often don’t know detailed internal structure (data) of objects Can still manipulate objects by calling methods

10 Classes Every object belongs to a class System.out object (representing terminal output window) belongs to PrintStream class Classes are blueprints for creating and using objects Define internal data (fields) Define operations (methods)

11 Methods Sequence of instructions to carry out some operation Usually accesses internal data of an object Every method has a name May take some input(s) and return some output Objects belonging to the same class all support the same methods (operations) To get a method to carry out its operation, you call or invoke the method

12 Object/Method Examples System.out print() println() "Hello World" length() toUpperCase()

13 Class/Method/Object Summary Every object belongs to a class Class defines methods for its objects These form the public interface of the class Class also defines data stored inside objects These form the private implementation Details (most often) hidden from other programmers using your objects and methods

14 Method Parameters Input provided to a method to give details about operation to be performed println method takes a string parameter (input) to tell what to print out on the screen System.out.println( "Hello, World!" ); "Hello World" is an explicit parameter Object on which method is called is also an implicit parameter length method of String class needs no explicit parameters

15 Return Values Result of a method’s computation length method returns a value: the number of characters in the string Return values can be Stored in a variable Used as parameter of another method String river2 = river.replace("issipp", "our") greeting.replace("World", "Dave").length()

16 Method Definition Headers String class: public int length() public String replace(String target, String replacement) PrintStream class: public void println(String output) public void println(int output) Overloaded methods: two methods with same name but different parameters Return value types Parameter types “Void” method returns no value

17 Aside: Number Types Integers: whole numbers Java type: int (or short, or long ) Floating-point: numbers with fractional parts Java type: double (or float ) Numbers are of primitive types, not objects Number types have no methods Numbers can be combined using arithmetic operators +*-/

18 Rectangle Objects Objects of type Rectangle describe rectangular shapes Rectangle class is predefined in Java library Understand the distinction: Rectangle object is block of memory storing some data In programmer’s mind, object describes a geometric figure

19 Constructing Objects To ‘make’ a new rectangle: new Rectangle(5, 10, 20, 30) The new operator takes name of a class (Rectangle) additional parameters required to construct a new object of that class (x, y, width, height) new operator returns the newly constructed object Usually one stores the result in a variable: Rectangle box = new Rectangle(5, 10, 20, 30);

20 Constructing Objects (cont.) Many classes allow construction of objects in multiple ways new Rectangle() All parameters are taken as being 0 (zero)

21 Syntax: Object Construction new ClassName( parameters ); Examples: new Rectangle( 5, 10, 20, 30 ) new Rectangle()Purpose: To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object

22 Accessor/Mutator Methods Accessor - method that accesses object and returns some information about it double width = box.getWidth(); Mutator - method that modifies the state of the object box.translate(15, 25); Given box object of unknown dimensions, how do you translate it so the x-coordinate becomes 0?

23 Writing a Test Program Provide a new class Define a main method Inside the main method, construct object(s) Rectangle x=5, y=10, width=20, height=30 Apply object methods Move rectangle 15 pixels horizontally, 25 vertically Display results

24 Importing Packages Java classes are grouped into packages Packages are grouped into a library To use class(es) defined in another package, you must import them at the beginning of your program file: import java.awt.Rectangle; ‘awt’ = Abstract Windowing Toolkit System and String classes are in java.lang package - automatically imported

25 Syntax: Importing Classes import packageName.ClassName; Examples: import java.awt.Rectangle;Purpose: To import a class from a package for use in a program

26 Writing and Testing Code Using DrJava… Write program that constructs two rectangle objects with arbitrary position/size constructs a third rectangle with top-left corner halfway between top-left corners of original two Width and height the average of the original two

27 Object References (Section 2.10) Primitive type variables store actual values Object variables store references to objects Multiple object variables can refer to same object Rectangle box = new Rectangle(5, 10, 20, 30); Rectangle box2 = box; int luckyNum = 13; int luckyNum2 = luckyNum; // translate, toUpperCase…

28 API Documentation API = Application Programming Interface Documentation lists classes and methods in Java library Not possible to memorize entire API Use online documentation, or download it to your computer Self-Check 22, 23 (pg. 52)

29 Programming Exercise P2.10 Project 2.1

30 Random Fact 2.1: Mainframes