A Computer Science LIST OF REFerences List References

  • Slides: 29
Download presentation
A+ Computer Science LIST OF REFerences

A+ Computer Science LIST OF REFerences

List References © A+ Computer Science - www. apluscompsci. com

List References © A+ Computer Science - www. apluscompsci. com

List References List<Some. Class> list; list null nothing list is a reference to some

List References List<Some. Class> list; list null nothing list is a reference to some list © A+ Computer Science - www. apluscompsci. com

List References new Array. List<Some. Class>(); 0 x 213 [] Array. Lists are Objects.

List References new Array. List<Some. Class>(); 0 x 213 [] Array. Lists are Objects. © A+ Computer Science - www. apluscompsci. com

List References List<Some. Class> list; list = new Array. List<Some. Class>(); list 0 x

List References List<Some. Class> list; list = new Array. List<Some. Class>(); list 0 x 213 [] list is a reference to an Array. List. © A+ Computer Science - www. apluscompsci. com

List Of References © A+ Computer Science - www. apluscompsci. com

List Of References © A+ Computer Science - www. apluscompsci. com

public class Dog { private int age; private String name; public Dog( String n,

public class Dog { private int age; private String name; public Dog( String n, int a ) { age = a; name = n; } public int get. Age() return age; } List Of References { public String get. Name() return name; } { public String to. String() { return "Dog - " + name + " " + age; } } © A+ Computer Science - www. apluscompsci. com

dog. java dogrunner. java

dog. java dogrunner. java

List Of References List<Dog> ray; ray = new Array. List<Dog>(); ray. add( new Dog(

List Of References List<Dog> ray; ray = new Array. List<Dog>(); ray. add( new Dog( "fred", 11) ); ray. add( new Dog( "ann", 21) ); System. out. println( ray ); OUTPUT [Dog - fred 11, Dog - ann 21] © A+ Computer Science - www. apluscompsci. com

List Of References ray. add( new Dog( "fred", 11) ); ray. add( new Dog(

List Of References ray. add( new Dog( "fred", 11) ); ray. add( new Dog( "ann", 21) ); ray. add( new Dog( "bob", 4) ); 0 ray 1 2 0 x 12 0 x 32 0 x. D 2 Dog 11 Dog 21 Dog 4

doggiesrunner. java

doggiesrunner. java

List Of References public class Creature implements Comparable { private int size; //checks to

List Of References public class Creature implements Comparable { private int size; //checks to see if this Creature is big – size > x public boolean is. Big() //implementation details not show public boolean equals(Object obj) //implementation details not show public int compare. To(Object obj) //implementation details not show } //other methods and constructors not shown © A+ Computer Science - www. apluscompsci. com

List Of References Array. List<Creature> creatures; creatures = new Array. List<Creature>(); creatures. add(new Creature(4));

List Of References Array. List<Creature> creatures; creatures = new Array. List<Creature>(); creatures. add(new Creature(4)); creatures. add(new Creature(9)); creatures. add(new Creature(1)); 0 creatures 1 2 0 x 12 0 x 32 0 x. D 2 Creature 4 Creature 9 Creature 1 © A+ Computer Science - www. apluscompsci. com

List Of References Array. List<Creature> creatures; creatures = new Array. List<Creature>(); creatures. add( new

List Of References Array. List<Creature> creatures; creatures = new Array. List<Creature>(); creatures. add( new Creature(41) ); creatures. add( new Creature(91) ); creatures. add( new Creature(11) ); out. println( creatures. get(0) ); creatures. get(0). set. Size(79); out. println( creatures. get(0) ); OUTPUT 41 79 11 true out. println( creatures. get(2) ); out. println( creatures. get(1). is. Big() ); © A+ Computer Science - www. apluscompsci. com

creatures. get(0). set. Size(7); 0 x 242 What does this return? What does the.

creatures. get(0). set. Size(7); 0 x 242 What does this return? What does the. dot do? Creature The. dot grants access to the Object at the stored address. © A+ Computer Science - www. apluscompsci. com

List Of References /* method count. Big. Ones should return the count of big

List Of References /* method count. Big. Ones should return the count of big creatures - use the is. Big() Creature method */ public int count. Big. Ones() { int cnt = 0; //for each loop //if statement //increase cnt by 1 } return cnt; © A+ Computer Science - www. apluscompsci. com

creature. java herdrunner. java

creature. java herdrunner. java

Boxing Un. Boxing © A+ Computer Science - www. apluscompsci. com

Boxing Un. Boxing © A+ Computer Science - www. apluscompsci. com

Box / Unbox primitive object byte Byte short int Short Integer long float double

Box / Unbox primitive object byte Byte short int Short Integer long float double char boolean Long Float Double Character Boolean == . equals() © A+ Computer Science - www. apluscompsci. com

Box / Unbox Java now wraps automatically. Integer num. One = 99; Integer num.

Box / Unbox Java now wraps automatically. Integer num. One = 99; Integer num. Two = new Integer(99); =99; =new Integer(99); These two lines are equivalent. © A+ Computer Science - www. apluscompsci. com

Box / Unbox Java now wraps automatically. Double num. One = 99. 1; Double

Box / Unbox Java now wraps automatically. Double num. One = 99. 1; Double num. Two = new Double(99. 1); =99. 1; =new Double(99. 1); These two lines are equivalent. © A+ Computer Science - www. apluscompsci. com

Box / Unbox Before Java 5 added in autoboxing and autounboxing, you had to

Box / Unbox Before Java 5 added in autoboxing and autounboxing, you had to manually unwrap references. Integer ref = new Integer(98); int y = ref. int. Value(); © A+ Computer Science - www. apluscompsci. com

Box / Unbox Java now unwraps automatically. Integer num = new Integer(3); int prim

Box / Unbox Java now unwraps automatically. Integer num = new Integer(3); int prim = num. int. Value(); out. println(prim); OUTPUT prim = num; out. println(prim); 3 prim=num. int. Value(); prim=num; These two lines are equivalent. © A+ Computer Science - www. apluscompsci. com 3

Box / Unbox Double dub = 9. 3; double prim = dub; out. println(prim);

Box / Unbox Double dub = 9. 3; double prim = dub; out. println(prim); OUTPUT 9. 3 0 -1 1 int num = 12; Integer big = num; out. println(big. compare. To(12)); out. println(big. compare. To(17)); out. println(big. compare. To(10)); © A+ Computer Science - www. apluscompsci. com

autoboxunbox. java

autoboxunbox. java

For Each Loop Array. List<Integer> ray; ray = new Array. List<Integer>(); //add some values

For Each Loop Array. List<Integer> ray; ray = new Array. List<Integer>(); //add some values to ray OUTPUT 153 int total = 0; for(Integer num : ray) { //this line shows the AP preferred way //it shows the manual retrieval of the primitive total = total + num. int. Value(); //the line below accomplishes the same as the line above //but, it uses autounboxing to get the primtive value //total = total + num; } out. println(total); © A+ Computer Science - www. apluscompsci. com

foreachloopone. java foreachlooptwo. java

foreachloopone. java foreachlooptwo. java

Work on Programs! Crank Some Code! © A+ Computer Science - www. apluscompsci. com

Work on Programs! Crank Some Code! © A+ Computer Science - www. apluscompsci. com

A+ Computer Science LIST OF REFerences

A+ Computer Science LIST OF REFerences