1 / 26

Loops, etc.

Loops, etc. Some Java details …. Static variables and methods Standard input and output Command-line arguments toString() data types: primitive and class Iteration: for loops, while loops Selection: if-then-else statements. Python class members. Recall class Tracker:. class Tracker:

rowena
Download Presentation

Loops, etc.

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. Loops, etc.

  2. Some Java details … • Static variables and methods • Standard input and output • Command-line arguments • toString() • data types: primitive and class • Iteration: for loops, while loops • Selection: if-then-else statements

  3. Python class members Recall class Tracker: class Tracker: numCreated = 0 def __init__(self): Tracker.numCreated += 1 t1 = Tracker() t2 = Tracker() print Tracker.numCreated # class variable Output is: 2 # access with class name as prefix

  4. Tracker in Java // class variable declared public static class Tracker { public static int numCreated = 0; public Tracker () { numCreated += 1; } } public class TestTracker { public static void main (String[ ] args) { Tracker t1 = new Tracker(); Tracker t2 = new Tracker(); System.out.println(Tracker.numCreated); } } Output is: 2 // access with class name as prefix

  5. Static methods class Tracker { private static int value = 0; public static void increment () { value += 1; } public static int getValue () { return value; } } No object instantiated! public class TestTracker { public static void main (String[ ] args) { Tracker.increment(); Tracker.increment(); System.out.println(Tracker.getValue()); } }

  6. Why is main() static? public static void main (String[ ] args) { … } At the start of program execution, there are no objects. What exists before the dawn of time? main() must be static in order to be invoked.

  7. Standard output What, exactly, does this mean? System.out.println(“Hello World!”); Easy. println() is a static method of class System.out.

  8. Standard input What, exactly, does all of this mean? import java.io.*; public class StandardInput { public static void main (String[ ] args) throws IOException { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String line = input.readLine(); System.out.println(line); } } Alas, this is not so easy to explain .

  9. Command-line args How is comand-line input parsed? public class CommandLine { public static void main (String[ ] args) { for (int i = 0; i < args.length; i++) System.out.println(args[i]); } } % javac CommandLine.java % java CommandLine hello there 4 5 6 hello there 4 5 6

  10. toString() Much the same as in Python public class ToStringExample { public static void main (String[ ] args) { Blah p = new Blah(456); System.out.println(p); } } class Blah { private int value; public Blah (int value) { this.value = value; } public String toString () { return "This object has value: " + this.value; } } % java ToStringExample This object has value: 456

  11. Data Types • There are two kinds of variable in Java • primitive: e.g. int, double, char, etc. • class: e.g. String, user-defined, etc. • The data model is different for each …

  12. 67 k Primitive variables int k = 67; k is the “name”, or address, of a memory location which can hold only ints.

  13. p 67 value Class variables Blah p = new Blah (67); class Blah { private int value; public Blah (int value) { this.value = value; } }

  14. 67 67 k j 2 Be careful using “=” • Assignment behaves differently with primitive and class variables • With primitives: int k = 67; int j = k; j = 2;

  15. p q 67 value “=” with class variables Blah p = new Blah (67); Blah q = p; class Blah { public int value; public Blah (int value) { this.value = value; } }

  16. p q 67 value 2 Alias unexpected! q.value = 2; Blah p = new Blah (67); // changes p.value! Blah q = p; class Blah { public int value; public Blah (int value) { this.value = value; } }

  17. And parameters? public class TestParameter { private static void bloop (int k, Blah p) { k += 1; p.value += 1; } public static void main (String[ ] args) { int k = 67; Blah p = new Blah(67); bloop(k, p); System.out.println("k: " + k + ", p.value: " + p.value); } } k: 68, p.value: 68 k: 67, p.value: 67 k: 67, p.value: 68 class Blah { public int value; public Blah (int value) { this.value = value; } }

  18. s t hello hello Strings: class variables String s = new String (“hello”); String t = new String (“hello”);

  19. s t E7947A9D AYC796D4 Strings: class variables Is “s == t” from previous slide “true”? NO! s and t are unequal references; e.g., Memory addresses E7947A9D and AYC796D4differ.

  20. hello hello Strings: class variables Is “s.equals(t)” from previous slide “true”? YES! s and t objects hold the same data.

  21. Selection in Python a = 7 if a < 0: print "less that zero" elif a == 0: print "equal to zero" else: print "greater than zero" # don’t need “else” … b = 9 if b < 0: print "less that zero" elif b == 0: print "equal to zero" # don’t need “elif” … c = 12 if c < 0: print "less that zero" else: print "greater than zero"

  22. Selection in Java int a = 7; if (a < 0) SOP("less that zero"); else if (a == 0) SOP("equal to zero"); else SOP("greater than zero"); // don’t need “else” … int b = 9; if (b < 0) SOP("less that zero"); else if (b == 0) SOP("equal to zero"); // don’t need “else if” … int c = 12; if (c < 0) SOP("less that zero"); else SOP("greater than zero");

  23. Statement blocks // be sure to enclose blocks with { } int d = 27; if (d < 0) SOP("less that zero"); else { d = -d - 1; SOP("now it is less than zero"); }

  24. Iteration in Python # while loops # low level for-loop control a = 7 while a > 0: print a, a -= 1 b = ['hi', 'there', 'how', 'are', 'you'] for i in range(len(b)): print b[i], # high level for-loop control b = ['hi', 'there', 'how', 'are', 'you'] for str in b: print str,

  25. Iteration in Java // while loops // low level for-loop control int a = 7; while (a > 0) { SOP(a); a -= 1; } String[ ] b = {"hi", "there", "how", "are", "you"}; for (int i = 0; i < b.length; i++) SOP(b[i]); // high level for-loop control String[ ] b = {"hi", "there", "how", "are", "you"}; for (String str : b) SOP(str);

  26. HLC: Worth the risk? # echo from keyboard input import sys input = sys.stdin for line in input: print >> sys.stdout, line, This works. Here is some text input appearing across two lines. # now hard-code the input as a string and print input = 'Here is some text input\nappearing across two lines.' print input # now try this high level for-loop for line in input: print line This works. This does not work! Why not?

More Related