3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Written by: Dr. JJ Shepherd
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Java Syntax Primitive data types Operators Control statements.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
BPJ444: Business Programming using Java Java basics Tim McKenna
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.
CSE 131 Computer Science 1 Module 1: (basics of Java)
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
I Power Int 2 Computing Software Development High Level Language Constructs.
3.1 Data Types: Using Objects. Data type. Set of values and operations on those values. Primitive types. n “Built-in” for a language. Basic “building.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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,
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Written by: Dr. JJ Shepherd
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat.
Topic: Classes and Objects
Objects as a programming concept
Introduction to Java part 2
Andy Wang Object Oriented Programming in C++ COP 3330
Java Primer 1: Types, Classes and Operators
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Selenium WebDriver Web Test Tool Training
Student Book An Introduction
Programming Language Concepts (CIS 635)
I/O Basics.
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Java Programming: From Problem Analysis to Program Design, 4e
Primitive Types Vs. Reference Types, Strings, Enumerations
Java Programming with BlueJ
An Introduction to Java – Part I, language basics
Introduction to Java part 2
Chapter 2: Basic Elements of Java
More About Data Types & Functions
String Methods: length substring
The Building Blocks Classes: Java class library, over 1,800 classes:
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Java Inheritance.
slides created by Ethan Apter
Manipulating Pictures, Arrays, and Loops
Python Primer 1: Types and Operators
Introduction to Java part 2
Java Programming Language
Defining Classes and Methods
Chapter 9: Pointers and String
Chapter 11 Inheritance and Polymorphism Part 1
slides created by Ethan Apter
Presentation transcript:

3.1 Objects

Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can still store only the same type of data You don’t have to know HOW the data types and their operations are implemented -> Abstraction You may want to define your own data types. 2 Data Types OperationsSet of ValuesData Type not, and, or, xor true, false boolean double int add, subtract, multiplyany of 2 64 possible reals add, subtract, multiply to

3 Objects Object. An entity that can take on a data type value. An object’s “value” can be returned to a client or be changed by one of the data type’s operations. Impact. Enables us to create our own data types; define operations on them; and integrate into our programs. length, substring, comparesequence of characters String OperationsSet of ValuesData Type get red component, brighten24 bits Color Picture get/set color of pixel (i, j)2D array of colors

What distinguishes creating arrays from creating variables of basic data types? n Explicit memory management Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n The memory needs to be initialized n Assigning one object/array to another object/array results in an alias Key difference n Arrays are part of the core Java language/standard library. Objects are not. “Class” vs. “Object” n Similar to difference between “type” vs. “variable” n An object is an instance of a class 4 Objects and Arrays are Similar

A Simple First Class Height n Data: – Feet (as an integer value) – Inches (as a double value) n Methods: – Set height – Display height API: n Height(int feet, double inches) //constructor n void displayHeight() //instance method 5

Objects are reference types => Need to initialize their memory Can use the new operator, just like arrays One special instance method in the API: n Height(int feet, double inches) //constructor n This method is called when you invoke “new” to create a new object n You can pass parameters to control initialization. n The class’ author defines the logic for this method to carry out proper initialization. 6 Constructors

Constructors vs. Methods Height(int feet, double inches) n How is this signature different from a method signature? No return type n The constructor manipulates the data in the object n No “return” statement 7

Instance methods are called “on” an object (a variable) n System.out.println(); // call println on System.out n String s = aCharge.toString(); // call toString on aCharge n In fileIn = new In(“file.txt”); fileIn.readInt(); Ways to think about this: n Send a message to an object. n Tell an object to do something. 8 Instance Methods vs. Static Methods

9 Constructors and Methods To construct a new object: Use keyword new and name of data type. To apply an operation: Use name of object, the dot operator, and the name of the method.

Height.java public class Height { // data int feet; double inches; // constructor Height(int setFeet, double setInches) { feet = setFeet; inches = setInches; } //method void displayHeight() { System.out.println("Height: "+feet+" feet and "+inches+ " inches"); } } 10

Using Height.java Compile.java file into a.class file Create objects of that class in other programs Dr. Java Coding Demo 11

Extending the Height Class double getHeightInches() n Returns the height in inches Dr. Java Coding Demo 12

Adding More Constructors May want to initialize an object in more than one way E.g., allow initialization when only the feet value is provided n Height(int setFeet) n Set “feet” to “setFeet”; set “inches” to zero Use the “this” keyword to invoke the original constructor Dr. Java Coding Demo 13

14 String Data Type String data type. Basis for text processing. Set of values. Sequence of Unicode characters. API. …

15 Typical String Processing Code How do we implement a recursive version of this function?

16 String Data Type …

Recursive Palindrome Check static boolean isPalindrome(String s) { if(s.length() < 1) //base return true; if(s.charAt(0) == s.charAt(s.length()-1)) //reduction return isPalindrome(s.substring(1,s.length()-1)); return false; } 17