Phil Tayco Slide version 1.0 Created Sep 10, 2017

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Copyright Curt Hill Variables What are they? Why do we need them?
Working With Objects Tonga Institute of Higher Education.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
The Object-Oriented Thought Process Chapter 03
Sections 10.1 – 10.4 Introduction to Arrays
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Programming Logic and Design Seventh Edition
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Array, Strings and Vectors
Yanal Alahmad Java Workshop Yanal Alahmad
Road Map Introduction to object oriented programming. Classes
Computer science C programming language Lesson 5
Java Review: Reference Types
Comparing Objects Phil Tayco San Jose City College Slide version 1.1
Composition Phil Tayco San Jose City College Slide version 1.2
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Abstract Data Types
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Object-Oriented Programming Using C++ Second Edition
Java Programming Arrays
Lesson Objectives Aims Key Words:
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Dr. Bhargavi Dept of CS CHRIST
Object-Oriented Programming
Classes and Objects.
Classes and Objects Phil Tayco San Jose City College Slide version 1.1
OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
CPS120: Introduction to Computer Science
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Object Comparisons and Arrays
Object Oriented Programming in java
Phil Tayco Slide version 1.3 Updated Feb 12, 2018
Java Programming Language
C Programming Pointers
Data Structures & Algorithms
Review of Previous Lesson
Chapter 3 Introduction to Classes, Objects Methods and Strings
Variables and Constants
Creating and Using Classes
SPL – PS2 C++ Memory Handling.
Week 9 - Monday CS222.
Phil Tayco San Jose City College Slide version 1.2 Updated Sep 9, 2019
Presentation transcript:

Phil Tayco Slide version 1.0 Created Sep 10, 2017 Simple Objects Phil Tayco Slide version 1.0 Created Sep 10, 2017

Primitive Data Types All variables are defined with a data type and variable name int age = 21; double gpa = 3.9; Memory for age and gpa is instantly allocated and contents are initialized Process is automatically done because the language already knows these simple data types All variables in memory have a location, but knowing exactly where they are is not important for primitives Advantages arise when knowing how these memory locations can be used age 21 gpa 3.9

Memory References Memory locations can be referred to by other variables (in C/C++, this is called a “pointer”) A memory reference is essentially a variable that contains a memory address as its value The value refers to a memory location where information is stored When memory references are defined, the location it is pointing to must be reserved (a.k.a. memory must be “allocated”) Modern programming languages have “no pointers”, but in fact they are handling a lot of the pointer process automatically for you C and C++ gives you more memory management control, which can be both a blessing and a curse!

Memory References Memory references are useful when handling complex data In Java, memory reference management is automatically done by default when you use data types that are not primitive Exception: String data types are technically treated as complex data types, but can be used the same way as primitives in many ways Java is often referred to as a language with “no pointers”, but in fact it does use memory reference management…it just does a lot of the hard work for you Nevertheless, it is important to know the process that occurs behind the scenes as it will become useful to understand later with advanced concepts

Memory References When we declare a variable with a complex data type, the memory location it is referring to is yet to be defined Student me; me ?

Memory References To allocate the memory, we request it with the “new” keyword Student me = new Student(); When the compiler sees “new Student()” it uses what it knows a Student looks like and looks in memory for a location large enough to store that data 0x1234abcd is an example of a numeric location where the Student data could reside “me” is the variable that now refers to the location of the Student data. In Java, this is transparent to us, but we should know that this structure exists me 0x1234abcd (Student data)

Complex Data Types But how does Java know what “a Student looks like”? Integers and doubles are simple data types because they store one value. (age stores 21 and gpa stores 3.9) Conceptually, a Student can store much more types of information. Think of every Student that you may want to model in a program. Every Student has: First name Last name Age Gpa ID Each one of these elements of a Student is called a “property” (also called an “attribute”)

Complex Data Types These properties make up the template for defining a Student In the example, we declared a variable “me” whose data type is “Student” (this is the same as when we declared a variable “age” whose data type is an integer) We assigned age a value of 21 after we declared it. Similarly, we will want to assign values to me’s properties This means Student’s properties are like variables grouped together within (or “encapsulated”) the overall Student variable (“me”) me has its own first name, last name, ID, etc. As such, each property is defined like a variable

Complex Data Types public class Student { public String firstName; public String lastName; public int age; public int id; public double gpa; } These properties are defined in a separate Java file called a “class” The file is separate because a class is designed to be a “stand-alone” data type. Put another way, the class is intended to be used by any number of other programs

Complex Data Types In another program (such as in the “main” function), we can assign values to these properties for the “me” variable In memory, technically the location where the Student data was allocated is found through the memory address – a process called “dereferencing” Student me = new Student(); me.id = 1234; me.age = 21; me.firstName = “Phil”; me 0x1234abcd firstName “Phil” lastName id 1234 age 21 gpa

Complex Data Types You can define as many different variables defined with primitive data types as memory allows (int x, int y, int z, etc.) Similarly, you can define as many different variables with classes (Student me, Student you, Student[] roster, etc.) Variables with primitive types are commonly referred to as just variables. A variable whose data type is a class is called an “object” Objects are seen as “instances” of a class. Creating objects is also called “instantiation” Object-oriented programming is the study of designing solutions primarily through the manipulation of such objects (variables with complex data types)

Exercise 1 Design a simple class with 4 properties. Remember to think of a property as an attribute of your class. Ask yourself “what properties does every instance of this class have?” Create a program with 2 files One file must implement your class design The other file is the main program that creates 2 objects of the class In your main program, demonstrate using the objects by assigning and displaying different values of each object’s properties You can be as simple or as clever as you like with your program as long as you show using class design and object creation and management