Using Objects CS 101-E Aaron Bloomfield. Announcements Midterm 1 is a week from this Wednesday Midterm 1 is a week from this Wednesday TESTS ARE IN CHM.

Slides:



Advertisements
Similar presentations
4-1 Chapter 4 (a) Defining Classes. 4-2 The Contents of a Class Definition A class definition specifies the data items and methods that all of its objects.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
Kernighan/Ritchie: Kelley/Pohl:
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 Review for Midterm 3 CS 101 Spring Topic Coverage  Loops Chapter 4  Methods Chapter 5  Classes Chapter 6, Designing and creating classes.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Using Objects Chapter 3 Fall 2005 CS 101 Aaron Bloomfield.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 Using Objects Chapter 2 (part 2 of 2) Spring 2007 CS 101 Aaron Bloomfield.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
© A+ Computer Science - Chicken yeller = new Chicken();
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
1 Using Objects Chapter 3 Fall 2006 CS 101 Aaron Bloomfield.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Session 7 Methods Strings Constructors this Inheritance.
Objects. Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Review for exam 1 CS 101-E Aaron Bloomfield. 2 Announcements Exam this Wed Exam this Wed In CHM 402 (NOT in Clark G004) In CHM 402 (NOT in Clark G004)
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1- 3 and 5 Stop me if you want me to go.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 3 Spring 2005 CS 101 Aaron Bloomfield
Objects.
Elementary Programming
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Data types, Expressions and assignment, Input from User
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
Review for Midterm 3.
Review for Midterm 3.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Packages & Random and Math Classes
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
Announcements & Review
Objects.
Visibilities and Static-ness
Review for Midterm 3.
Week 7 - Monday CS 121.
Presentation transcript:

Using Objects CS 101-E Aaron Bloomfield

Announcements Midterm 1 is a week from this Wednesday Midterm 1 is a week from this Wednesday TESTS ARE IN CHM 402!!!!!!!! TESTS ARE IN CHM 402!!!!!!!! Schedule: Schedule: This week: chapter 3 This week: chapter 3 Next Monday: review for midterm Next Monday: review for midterm Bring in questions!!!! Bring in questions!!!! Homework done via CodeLab Homework done via CodeLab Use code VIRGIN Use code VIRGIN There is a lab due this week There is a lab due this week

Warn your grandparents! Historically, this class has been lethal to grandparents of students in the class Historically, this class has been lethal to grandparents of students in the class More often grandmothers More often grandmothers This happens most around test time This happens most around test time Although occasionally around the times a big assignment is due Although occasionally around the times a big assignment is due

What is a reference References are like pointers in C/C++ References are like pointers in C/C++ But they are not the exact same thing! But they are not the exact same thing! C++ has references also (in addition to pointers) C++ has references also (in addition to pointers) A reference is a memory address A reference is a memory address

References 1 Consider: Consider: int j = 5; String s = “Hello world”; Java translates that last line into: Java translates that last line into: String s = new String (“Hello world”); Note that there is no “new” here

References 2 What’s happening in memory What’s happening in memory Primitive types are never references; only objects Primitive types are never references; only objects 5 int j Hello world String s Takes up 32 bits (4 bytes) of memory Takes up 32 bits (4 bytes) of memory Takes up  12 bytes of memory

References 3 Consider our Circle class Consider our Circle class Circle c = new Circle(); radius = 1.0 Pi = Circle c 0x0d4fe1a8 At memory location 0x0d4fe1a8 Declares a reference to a Circle object Creates a new Circle object in memory; Returns a reference to it

Circle c1 = new Circle(); Circle c2 = new Circle(); c1.radius = 5; c2 = c1; c2.radius = 7; System.out.println (c1.radius); Consider: Consider: Circle c1 = new Circle(); Circle c2 = new Circle(); c1.radius = 5; c2 = c1; c2.radius = 7; System.out.println (c1.radius); Pi = radius = 7.0radius = 1.0radius = 5.0 References 4 Circle c1 Circle c2 radius = 1.0 Pi = What happens to this?

Java’s garbage collection If an object in memory does not have a reference pointing to it, Java will automagically delete the object If an object in memory does not have a reference pointing to it, Java will automagically delete the object This is really cool! This is really cool! In C/C++, you had to do this by yourself In C/C++, you had to do this by yourself

References and memory Most modern computers are 32-bit computers Most modern computers are 32-bit computers This means that a reference takes up 32 bits This means that a reference takes up 32 bits 2 32 = 4 Gb 2 32 = 4 Gb This means that a 32-bit machine cannot access more than 4 Gb of memory! This means that a 32-bit machine cannot access more than 4 Gb of memory! Well, without doing some “tricks”, at least Well, without doing some “tricks”, at least Most machines come with 1 Gb memory these days Most machines come with 1 Gb memory these days Will come with 4 Gb in a year or so Will come with 4 Gb in a year or so 64-bit machines will have 16 exabytes of memory 64-bit machines will have 16 exabytes of memory Giga, Tera, Peta, Exa Giga, Tera, Peta, Exa That’s 16 billion Gb! That’s 16 billion Gb!

The null reference Sometimes you want a reference to point to nothing Sometimes you want a reference to point to nothing Use the null reference: Use the null reference: Circle c = null; The null reference is equivalent to a memory address of zero (0x ) The null reference is equivalent to a memory address of zero (0x ) No user program can exist there No user program can exist there

The null reference Consider: Consider: Circle c = null; c.radius = 0.0; What happens? What happens?

What happens in Windows…

The null reference Consider: Consider: Circle c = null; c.radius = 0.0; This is called accessing (or following) a null pointer/reference This is called accessing (or following) a null pointer/reference What happens? What happens? Java: java.lang.NullPointerException Java: java.lang.NullPointerException C/C++: Segmentation fault (core dumped) C/C++: Segmentation fault (core dumped)

So what is a null reference good for? Let’s say you had a method that returned a Circle when passed some parameters Let’s say you had a method that returned a Circle when passed some parameters Normally it returns a valid Circle Normally it returns a valid Circle But what if it can’t? How to deal with that? But what if it can’t? How to deal with that? Return a null reference Return a null reference

A bit of humor…

Variable initialization Recall that Java will NOT initialize a variable in a method Recall that Java will NOT initialize a variable in a method But it does initialize a field of a class But it does initialize a field of a class Consider: Consider: String s; If s is a variable in a method, it is not initialized If s is a variable in a method, it is not initialized If s is a field in a class, it is initialized to null If s is a field in a class, it is initialized to null Save yourself the hassle, and always initialize your variables and fields Save yourself the hassle, and always initialize your variables and fields

Getting classy Your current job Your current job Gain experience creating and manipulating objects from the standard Java types Gain experience creating and manipulating objects from the standard Java types Why Why Prepares you for defining your own classes and creating and manipulating the objects of those classes Prepares you for defining your own classes and creating and manipulating the objects of those classes

Values versus objects Numbers Numbers Have values but they do not have behaviors Have values but they do not have behaviors Objects Objects Have attributes (fields) and behaviors (methods) Have attributes (fields) and behaviors (methods) System.in System.in References an InputStream References an InputStream Attribute: keyboard Attribute: keyboard Behaviors: reading Behaviors: reading System.out System.out References an OutputStream References an OutputStream Attribute: monitor Attribute: monitor Behaviors: printing Behaviors: printing

Other Java object types String String Rectangle Rectangle Color Color JFrame JFrame

Consider Statements Statements int peasPerPod = 8; String message = "Don't look behind the door!“ How do we represent these definitions according to the notions of Java? How do we represent these definitions according to the notions of Java?

A bit of humor…

Representation

Shorthand representation 8 message peasPerPod "Don't look behind the door!"

Examples Consider Consider String a = "excellence“; String b = a; What is the representation? What is the representation?

Uninitialized versus null Consider (in a method): Consider (in a method): String dayOfWeek; Scanner inStream; What is the representation? What is the representation?

Uninitialized versus null Consider (in a class): Consider (in a class): String dayOfWeek; Scanner inStream; What is the representation? What is the representation? null dayOfWeek null inStream

Uninitialized versus null Consider Consider String fontName = null; Scanner fileStream = null; What is the representation? What is the representation?

Assignment Consider Consider String word1 = "luminous"; String word2 = "graceful"; word1 = word2; Initial representation Initial representation

Using objects Consider Consider Scanner stdin = new Scanner(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine(); Suppose the user interaction is Suppose the user interaction is Enter your account name: artiste Scannerstdin

A bit of humor…

String representation Consider Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; String alphabet = "abcdefghijklmnopqrstuvwxyz"; Standard shorthand representation Standard shorthand representation Truer representation Truer representation

String representation Consider Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); char c1 = alphabet.charAt(9); char c2 = alphabet.charAt(15); char c2 = alphabet.charAt(15); char c3 = alphabet.charAt(2); char c3 = alphabet.charAt(2); What are the values of c1, c2, and c3? Why? What are the values of c1, c2, and c3? Why?

Program WordLength.java public class WordLength { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.nextLine(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + "."); }}

More String methods Consider Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output? What is the output? Month is August.

More String methods Consider Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output? What is the output? First search: 1 Second search: 3 Third search: -1

More String methods Consider Consider int v1 = -12; double v2 = 3.14; char v3 = 'a'; String s1 = String.valueOf(v1); String s2 = String.valueOf(v2); String s3 = String.valueOf(v3);

Final variables Consider Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; What is the representation? What is the representation? "Appearance of Brown"POEM_TITLE "Weather ball is black"WARNING The locks indicate the memory location holds constants

Final variables object type constant Value In general, these attributes can be modified through member methods The reference cannot be modified once it is established

Rectangle The third and fourth parameters of the Rectangle constructor specify the dimensions of the new Rectangle The first two parameters of the Rectangle constructor specify the position of the upper-left-hand corner of the new Rectangle int x = 3; int y = 4; int width = 5; int height = 2; Rectangle r = new Rectangle(x, y, width, height);

Consider Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); Consider Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); Rectangle

Final variables Consider Consider final String LANGUAGE = "Java"; "Java" The contents are immutable because there are no String methods that allow the contents to be changed The reference cannot be modified once it is established LANGUAGE

A bit of humor…