1 / 14

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;

kert
Download Presentation

As you arrive…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 = newDnaStrand(); 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()); As you arrive…

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

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

  4. 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)

  5. Why does this code work differently? Old Code from the Beginning of Class: DnaStrand a = newDnaStrand(); 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);

  6. 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. MyClassfoo = null; foo.doSomething();) you’ll generate a NullPointerException and your program will end

  7. publicstaticvoidrandomFunction() { ArrayList<Integer> list = newArrayList<Integer>(); ArrayList<Integer> otherList = list; list.add(44); otherList.add(55); } What does otherList contain at the end of randomFunction? [44] [55] [44,55] None of these; the function won’t compile because it is static

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

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

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

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

  12. 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

  13. 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”); NamedDnamyVar = new NamedDna(“Mikes Strand”, d); When this works, go ahead and submit the code via Ambient.

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

More Related