As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

1 Working with References. 2 References Every object variable is a reference to an object Also true when an object is passed as an argument When the object.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Intro to Generic Programming Templates and Vectors.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Intro to Programming Lecture 13
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
1 Exercise /* A lockbox can be open or closed. If closed, only a valid password will open the box. Once the box is open, the contents can be retrieved.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
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!
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
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.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Inheritance Only not the good kind of inheritance where you discover your Great Aunt left you a yacht. The bad kind that involves obscure Java syntax.
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Introduction to Generics
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
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,
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Today’s lecture Review chapter 5 go over exercises.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Introduction to Exceptions in Java CS201, SW Development Methods.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Memory Management in Java Mr. Gerb Computer Science 4.
Classes and Objects.
You should understand everything on this slide
Some Eclipse shortcuts
Intro To Classes Review
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Class Structure 16-Nov-18.
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Java Programming Language
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Building Java Programs
Class Structure 2-Jan-19.
CS2011 Introduction to Programming I Objects and Classes
Class Structure 25-Feb-19.
Java Programming Language
Visibilities and Static-ness
Presentation transcript:

As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code you snarfed, if you’d like to play with it in Eclipse): DnaStrand a = new DnaStrand(); a.setStrandData("aaaa"); DnaStrand b = a; b.addBasePair("t"); //prints aaaat System.out.println(a.getStrandData()); FORTH: determine what would happen if the final line were changed to: System.out.println(b.getStrandData());

What does this code print? DnaStrand a = new DnaStrand(); a.setStrandData("aaaa"); DnaStrand b = a; b.addBasePair("t"); System.out.println(b.getStrandData()); 1.t 2.aaaa 3.aaaat 4.Nothing, this could would cause an error

The Mysteries of Pointers By the end of the class, you should be able to 1.Correctly anticipate tricky pointer manipulations and understand the difference between changing with a myVariable.method() and changing with ‘=‘ 2.Write constructors for your objects

In the Optional Textbook Your book calls pointers “references” The stuff today will be mostly out of Chapter 3 (especially starting on pg. 54) Coming up – read Ch 4 and Ch 6 (though you can skip the big extended example in Ch 5 and Ch 6 unless you’re really feeling motivated)

Why does this code work differently? Old Code from the Beginning of Class: DnaStrand a = new DnaStrand(); a.setStrandData("aaaa"); DnaStrand b = a; b.addBasePair("t"); //prints aaaat System.out.println(a.getStrandData()); New similar but differently functioning: String a = new String("aaaa"); String b = a; b = a.concat("t"); //prints aaaa System.out.println(a); //prints aaaat System.out.println(b);

The Rules of Pointers New objects are only created with “new” Variables are not objects they are pointers to objects (except for primitives) By using ‘=‘ we change the object a variable points to. Two variables can point to the same object. By using myVariable.someFunction() we (may) change the object being pointed at, which affects everything that’s pointing to it When pointers are newly created, they point to “null” If you attempt to call a function on something that points to null (e.g. MyClass foo = null; foo.doSomething();) you’ll generate a NullPointerException and your program will end

public static void randomFunction() { ArrayList list = new ArrayList (); ArrayList otherList = list; list.add(44); otherList.add(55); } What does otherList contain at the end of randomFunction? A.[44] B.[55] C.[44,55] D.None of these; the function won’t compile because it is static

public static void randomFunction2() { String a = "Hello"; String b = "Goodbye"; b = a; String c = a.concat(" CS100"); } What is true at the end of randomFunction2? A. b contains “Hello CS100” B. b contains “Goodbye” because strings are immutable C. b contains“Hello” and a will contain “Hello CS100” because string are immutable D.Both b and a contain “Hello” because strings are immutable

Check out the Example 3 code Fix it Remember pointer rule 1!

Constructors DnaStrand a = new DnaStrand(); Looks a lot like a function call, doesn’t it?

Constructors A constructor is a special function. Declare a constructor like this: public ClassName(Type parameter, Type otherParameter) { //Your code here } Example: public DnaStrand(String data) { myData = data; } It’s called when an object is created with new: DnaStrand myVariable = new DnaStrand(“ttttta”);

The Rules of Constructors Constructors always are named the same as their class They never have a return type If your class does not have a constructor, one without parameters will be created for you

Constructor Exercise Make a new class NamedDna. Its constructor should should take two parameters: a name and a DnaStrand. Store both of those values in instance variables. Example code: DnaStrand d = new DnaStrand(“tta”); NamedDna myVar = new NamedDna(“Mikes Strand”, d); When this works, go ahead and submit the code via Ambient.

Summing up! 1.Don’t be fooled by tricky code. All (non- primitive) variables are pointers. Avoid null! Remember the rules of pointers! 2.Write constructors for your objects, and know where to look when you see them called