CS 340 DATA STRUCTURES Lecture: Everything is an Object.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming C
Advertisements

Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
CS 355 – Programming Languages
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java versus C# An introduction to C# and Visual Studio.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
DAT602 Database Application Development Lecture 5 JAVA Review.
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.
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 1: Introduction to OOP.
Chapter 2: Everything is an Object ● C++ has many non object oriented features inherited from C. It is a hybrid language meaning that it support different.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Introduction to java Prepared By:-Pragnesh Patel Lect. In Computer Dept. NSIT,Jetalpur 1.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java for C++ Programmers Clint Jeffery University of Idaho
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Data Types and Operations On Data Objective To understand what data types are The need to study data types To differentiate between primitive types and.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Copyright Curt Hill Variables What are they? Why do we need them?
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
CompSci Today’s topics Parsing Java Programming Notes from Tammy Bailey Reading Great Ideas, Chapter 3 & 4.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Software Development 4. C# data types, objects and references.
By Mr. Muhammad Pervez Akhtar
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
OOP Basics Classes & Methods (c) IDMS/SQL News
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Object Oriented Programming Lecture 2: BallWorld.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Basic Introduction to C#
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
Chapter No. : 1 Introduction to Java.
Java Primer 1: Types, Classes and Operators
Lecture 2: Data Types, Variables, Operators, and Expressions
Programming Language Concepts (CIS 635)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
(Computer fundamental Lab)
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

CS 340 DATA STRUCTURES Lecture: Everything is an Object

OOP or… Object Oriented Programming CS 340 2

Object-Oriented Languages Smalltalk, C++, Java, etc… You can make any kind of objects you want How different from procedural languages? No different at all: Every (reasonable) language is “Turing complete” Very different: Make expression easier, less error-prone CS 340 3

O-O Languages (Alan Kay) Everything is an object. A program is a bunch of objects telling each other what to do, by sending messages. Each object has its own memory, and is made up of other objects. Every object has a type (class). All objects of the same type can receive the same messages. CS 340 4

Objects An object has an interface, determined by its class. A class is an abstract data type, or user-defined type. Designing a class means defining its interface. CS 340 5

Built-In Types Think of an int … What is its interface? How do you “send it messages”? How do you make one? Where does it go when you’re done with it? In solving a computational problem, the goal is to Invent useful classes, and Give them appropriate characteristics. CS 340 6

Example Suppose I’ve defined this class in Java: To make one, I type BottleOfBeer myBeer = new BottleOfBeer(); If I want myBeer opened, I say myBeer.open(); CS 340 7

But Why Not Just… This is legal, but just makes a “reference variable” named myBeer This variable can refer to any BottleOfBeer object, but currently refers to nothing The operator new actually causes an object to be created, so we tell it what kind we want CS BottleOfBeer myBeer;

Designers Design, Users Use The interface is the critical part, but the details (implementation) are important too. Users use the interface (the “public part”); the implementation is hidden by “access control”. CS 340 9

Two Ways of Reusing Classes Composition: One class has another as a part (indicated by the diamond “aggregation” symbol). CS

Two Ways of Reusing Classes Inheritance: One class is a specialized version of another (indicated by the triangle “inheritance” symbol). CS

Polymorphism Different subclasses respond to the same message, possibly with different actions. CS

Some Java Code Patron p1 = new Patron(); Patron p2 = new YankPatron(); Patron p3 = new BritPatron(); Patron p4 = new GermanPatron(); p1.BeerPlease() // polite request p2. BeerPlease() // rude request p3.BeerPlease() // polite request p4.BeerPlease() // request in German (but polite) This is a bit of a trick: it requires late binding of the function call. CS

QUESTIONS? CS

Creating Objects We usually assume this is free; with built-in types like int or char, we just say int i; char c; With user-defined types (the ones we make), we need to be explicit about what we want: constructor function This is a very important issue! CS

Destroying Objects If an object goes “out of scope,” it can no longer be used (its name is no longer known). Java uses references and “garbage collection”. CS

Example of Object Scope What happens to lect ? The LectureNotes object still exists, but the reference lect disappears (it’s out of scope after return). Eventually, the garbage collector removes the actual LectureNotes object. CS public String getTitle(int lectureNumber) { LectureNotes lect; lect = syllabus.getLecture(lectureNumber); String s = lect.getLine(1); return s; }

Java’s Use of Memory Stack Heap Static variables Constants Non-RAM storage CS

Java’s Primitive Types CS TypeSizeWrapper type boolean-Boolean char16-bitCharacter byte8-bitByte short16-bitShort int32-bitInteger long64-bitLong float32-bitFloat double64-bitDouble void-Void

Wrapper Types Variables of primitive types are “automatic”, i.e., they are stored on the stack. They are automatically deleted when they go out of scope. What if you want an object holding a primitive type? Example: char c = ‘x’; Character C = new Character(‘x’); CS

Really Big Numbers BigInteger, BigDecimal These are arbitrary precision, as big as they need to be. You can’t use the usual operators (+-*/) since they are objects. But there are methods (functions) to do these things. CS

Creating New Types class MyNewType { // definition here } Now it’s legal to say CS MyNewType m = new MyNewType();

Class Members Fields (a.k.a. member variables, data members) Methods (a.k.a. member functions): they determine the messages objects can receive The method argument list specifies what information you pass into the method CS class MyClass { int a; YourClass b; float memberFunction(int x, float f) { return 0; }

Let’s Write Something // Our first program. File: HelloDate.java // Note that the file name is exactly the same // as the class name, including capitalization. import java.util.*; public class HelloDate { public static void main(String[] args) { System.out.println(“Hello, it is ”); System.out.println(new Date()); } CS

QUESTIONS? CS

Java design CS

Java Design Goals Simple, object oriented, and familiar Robust and secure Architecture neutral and portable High performance Interpreted, threaded, and dynamic CS

Java abbreviations JDK: Java Development Kit JSDK: Java Servlet Development Kit JVM: Java Virtual Machine J2EE: Java Platform, Enterprise Edition. A widely used platform for server programming. CS

Java vs C# Program Structure Operators Choices Loops CS

Java vs C#: Program Structure 30 JavaC# package hello; public class HelloWorld { public static void main(String[] args) { String name = "Java"; // See if an argument was passed from the command line if (args.length == 1) name = args[0]; System.out.println("Hello, " + name + "!"); } using System; namespace Hello { public class HelloWorld { public static void Main(string[] args) { string name = "C#"; // See if an argument was passed from the command line if (args.Length == 1) name = args[0]; Console.WriteLine("Hello, " + name + "!"); }

Java vs C#: Comments 31 JavaC# // Single line /* Multiple line */ /** Javadoc documentation comments */ // Single line /* Multiple line */ /// XML comments on a single line /** XML comments on multiple lines */

Java vs C#: Data Types 32 JavaC# Primitive Types boolean byte char short, int, long float, double Reference Types Object (superclass of all other classes) String arrays, classes, interfaces Value Types bool byte, sbyte char short, ushort, int, uint, long, ulong float, double, decimal structures, enumerations Reference Types object (superclass of all other classes) string arrays, classes, interfaces, delegates

Java vs C#: Data Types 33 JavaC# Conversions // int to String int x = 123; String y = Integer.toString(x); // y is "123" // String to int y = "456"; x = Integer.parseInt(y); // x is 456 // double to int double z = 3.5; x = (int) z; // x is 3 (truncates decimal) Conversions // int to string int x = 123; String y = x.ToString(); // y is "123" // string to int y = "456"; x = int.Parse(y); // or x = Convert.ToInt32(y); // double to int double z = 3.5; x = (int) z; // x is 3 (truncates decimal)

Java vs C#: Constants 34 JavaC# // May be initialized in a constructor final double PI = 3.14; const double PI = 3.14; // Can be set to a const or a variable. //May be initialized in a constructor. readonly int MAX_HEIGHT = 9;

Java vs C#: Operators 35 JavaC# Comparison == = != Arithmetic + - * / % (mod) / (integer division if both operands are ints) Math.Pow(x, y) Assignment = += -= *= /= %= &= |= ^= >= >>>= Bitwise & | ^ ~ > >>> Comparison == = != Arithmetic + - * / % (mod) / (integer division if both operands are ints) Math.Pow(x, y) Assignment = += -= *= /= %= &= |= ^= >= Bitwise & | ^ ~ >

Java vs C#: Operators 36 JavaC# Logical && || & | ^ ! Note: && and || perform short-circuit logical evaluations String Concatenation + Logical && || & | ^ ! Note: && and || perform short-circuit logical evaluations String Concatenation +

Java vs C#: Choices 37 JavaC# greeting = age < 20 ? "What's up?" : "Hello"; if (x < y) System.out.println("greater"); if (x != 100) { x *= 5; y *= 2; } else z *= 6; greeting = age < 20 ? "What's up?" : "Hello"; if (x < y) Console.WriteLine("greater"); if (x != 100) { x *= 5; y *= 2; } else z *= 6;

Java vs C#: Choices 38 JavaC# int selection = 2; switch (selection) { // Must be byte, short, int, char, or enum case 1: x++; // Falls through to next case if no break case 2: y++; break; case 3: z++; break; default: other++; } string color = "red"; switch (color) { // Can be any predefined type case "red": r++; break; // break is mandatory; no fall- through case "blue": b++; break; case "green": g++; break; default: other++; break; // break necessary on default }

Java vs C#: Loops 39 JavaC# while (i < 10) i++; for (i = 2; i <= 10; i += 2) System.out.println(i); do i++; while (i < 10); for (int i : numArray) // foreach construct sum += i; while (i < 10) i++; for (i = 2; i <= 10; i += 2) Console.WriteLine(i); do i++; while (i < 10); foreach (int i in numArray) sum += i;

Java vs C#: Loops 40 JavaC# // for loop can be used to iterate through any Collection import java.util.ArrayList; ArrayList list = new ArrayList (); list.add(10); // boxing converts to instance of Integer list.add("Bisons"); list.add(2.3); // boxing converts to instance of Double for (Object o : list) System.out.println(o); // foreach can be used to iterate through any collection using System.Collections; ArrayList list = new ArrayList(); list.Add(10); list.Add("Bisons"); list.Add(2.3); foreach (Object o in list) Console.WriteLine(o);

Java vs C#: Arrays 41 JavaC# int nums[] = {1, 2, 3}; or int[] nums = {1, 2, 3}; for (int i = 0; i < nums.length; i++) System.out.println(nums[i]); String names[] = new String[5]; names[0] = "David"; float twoD[][] = new float[rows][cols]; twoD[2][0] = 4.5; int[][] jagged = new int[3][]; jagged[0] = new int[5]; jagged[1] = new int[2]; jagged[2] = new int[3]; jagged[0][4] = 5; int[] nums = {1, 2, 3}; for (int i = 0; i < nums.Length; i++) Console.WriteLine(nums[i]); string[] names = new string[5]; names[0] = "David"; float[,] twoD = new float[rows, cols]; twoD[2,0] = 4.5f; int[][] jagged = new int[3][] { new int[5], new int[2], new int[3] }; jagged[0][4] = 5;

QUESTIONS? CS