1 / 10

CPSC 233 Tutorial

CPSC 233 Tutorial. Xin Mar 2, 2011. toString () method. Add a toString () to a class to enable the print function public String toStinrg ( ). Example. public class Foo { private int num; public Foo () { num = 0; } public Foo ( int n ) { setNum(n ); }

yahto
Download Presentation

CPSC 233 Tutorial

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. CPSC 233 Tutorial Xin Mar 2, 2011

  2. toString() method • Add a toString() to a class to enable the print function • public String toStinrg ( )

  3. Example public class Foo { private int num; public Foo () { num = 0; } public Foo (intn) { setNum(n); } public intgetNum () { return num; } public void setNum (intn) { num = n; } public String toString () { String temp = "Num="; temp = temp + num; return temp; } public boolean equals (Foof) { return(this.num == f.num); } } public class ReferenceDriver { public static void main (String [] args) { Foof1; f1 = new Foo(10); System.out.println(f1); } }

  4. Deep copy vs. Shallow copy • Deep copy • Create a new object, and make it equal to the old one • Shadow copy • Just return the reference

  5. Example public class MyCopy { public FooshallowCopy (FooaFoo) { Foo temp = aFoo; return temp; } public FoodeepCopy (FooaFoo) { Foo temp = new Foo (); temp.setNum(aFoo.getNum()); return temp; } } public class ReferenceDriver { public static void main (String [] args) { Foo f1; Foo f2; MyCopyaCopier = new MyCopy(); f1 = new Foo(10); f2 = aCopier.deepCopy(f1); f2.setNum(20); System.out.println(f1); System.out.println(f2); } }

  6. Static variables • All objects share the same copy • Can be used as “global” variables • Use directly with class name • eg. Mode.debug • Even no objects are created

  7. Static method • Use with the class name • eg. foo.max() • don’t need to create an object • although you can still use them with object name • Static method • access static attributes ✓ • access non-static attributes ✗ • attributes belong to objects • call another static method ✓ • call non-static class method ✗ • using this reference ✗

  8. Example class staticExampleDriver { public static void main (String [] args) { int result = staticExample.max(1, 2); System.out.println("the result is " + result); result = staticExample.number; System.out.println("the result is " + result); staticExampleeg = new staticExample(); result = eg.max(2, 3); System.out.println("the result is " + result); } } class staticExample { public static int number = 1; public static int max (int n1, int n2) { if (n1 > n2) return n1; else return n2; } }

  9. Read from a file • import libraries • import java.util.Scanner; • import java.io.*; • Open a file • Scanner inputStream = new Scanner(newFileInputStream(“filename.txt”)); • Read the file • String line = inputStream.nextLine (); • Close the file • inputStream.close ();

  10. Improve the grid program • Initialize array from a txt file • add tokens to random vacant space

More Related