CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.

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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
CSC Programming for Science Lecture 11: Switch Statements & ?: Operator.
Java Syntax Primitive data types Operators Control statements.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 212 – Data Structures Lecture 12: Java Review.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 212 Object-Oriented Programming and Java Part 1.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage.
English Conundrum s  In English, add “s” to end of word to make plural s  But for 1 special word, adding an “s” at the end:  Makes word go from plural.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Problem of the Day  Why are manhole covers round?
I Power Int 2 Computing Software Development High Level Language Constructs.
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CSC 107 – Programming For Science. The Week’s Goal.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Objects and Classes Mostafa Abdallah
In Java.... Variables contain the values of primitive data types Value Types.
CSC 212 Object-Oriented Programming and Java Part 2.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
CSC Programming for Science Lecture 5: Actual Programming.
Lecture 3: Fields, Methods, & Constructors
Lecture 2: Primitives & References
CSE 116/504 – Intro. To Computer Science for Majors II
Yanal Alahmad Java Workshop Yanal Alahmad
String Objects & its Methods
Variables ICS2O.
Value Types and Reference Types
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Language
Introducing JavaScript
Chapter 3 Introduction to Classes, Objects Methods and Strings
Presentation transcript:

CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes

Announcements Still need a note-taker; please see me if you are interested in the $50 If you need more review of Java, talk to me and/or seek out additional resources

Tracing Code Vital skill for writing and debugging Still my favorite way to debug  Quickest way to find common, simple bugs Good way to understand how code works Execute line-by-line just like the computer  Update variables’ value  Record any output  Need to work slowly and methodically

Data Types Java has 7+1 primitive data types  boolean, char, int, long, float, double, String * Only types that work with Java operators  E.g., +, -, %, &&, ||, |, >=, <, … Computers also use these types natively  Simplifies how they work and are used

Primitive Types Primitive variables are simple to use Each variable is “box” holding its value  Assignments copy values  Updates only to the local variable But primitives of limited usefulness  Finite range of possible values  Each variable has 1 value at a time

Primitives Example int x = 5; int sum = 0; for (int i=0; i < x; i++) { sum = sum + x; }

Classes In real world, must describe more than just primitives Java classes define these additional types Classes begin with: public class ClassNameGoesHere {  Usually start with capital letter  Usually use interior capitals to highlight words ClassNameGoesHere is used as the type

Using Classes Normally use instances of a class Class instantiated using new command public class Kitty {... } Kitty kat = new Kitty(...); Each instantiated object is unique: Kitty cat = new Kitty(...); Kitty tiger = new Kitty(...); cat = new Kitty(...);

Reference Variables Variables of class type called references Must be assigned to instance before using  Special value, null, marks that reference is not currently assigned to any instance References similar to a remote control  Do not equal object, but refer to object TV kat = new TV(...); kat

Using References Assignments alias references  Makes variables refer to same instance  new is only way to create an instance Kitty cat, tiger; cat = new Kitty(...); tiger = new Kitty(...); tiger = cat; cat tiger

Aliasing Aliased variables refer to same instance  Can make change using either variable  Both variables will see changes that are made  Assignments only to one reference, however Kitty cat, tiger, kat; cat = new Kitty(...); tiger = new Kitty(...); kat = tiger tiger = cat; cat = kat; cat tiger kat

Arrays Arrays are special object type  Array variables are references in own right  Arrays must refer to instance before use int[] bob = new int[30]; Each entry in array is like its own variable  bob’s entries are primitive variables  Following array’s entries are references Car[] parkingLot = new Car[300];  Can’t use entry in Car until it refers to instance

Tracing Example int max = 0; Car[] parkingLot = new Car[4]; for (int i = 0; i < 2; i++) { parkingLot[i] = new Car(...); } Could we use:  parkingLot[0]?  parkingLot[3]?  parkingLot[4]?

Before Next Lecture… Review basic Java syntax and loops  Will not go over this in class (sorry!)  Book contains a good review of this Look over week #1 homework and see if you have questions Friday’s lecture will cover constructors, fields, & methods  May want to review any old notes & handouts