// APS101, Winter 2009: Lecture 5 (Jan. 19) // // Review: we began with primitive types. // ex. int, double, boolean. // then, last time, we looked at non-primitive types. // i.e. objects. ex. String, Date. // we also looked at method calls. String myString; // declaring an object variable. myString myString = "APS 101"; // initialization myString.length() // an ARGUMENT is the actual value that // is being put into the method. myString.charAt(5) myString.charAt(4) "123".length() int n = 5; n.length() // the Integer class is called a "wrapper" class // for the primitive type int. Integer n = new Integer(5); n n.intValue() Double x = new Double(123.5) x x.intValue() Double x = new Double(123) x Integer n = new Integer(123.5) x x.toString() // combining Strings: concatenation // concatenation operator: + "hello" + " world" String s = 5 // simple way to convert int -> String String s = "" + 5 s String s = "" + 123.5 s // converting String -> int... not so simple! String s = "5" s.intValue() // doesn't work! s int n = s.charAt(0) // also doesn't work! n s.charAt(0) (int) '5' n = s.charAt(0) n // need to create a new Integer object, // and use the methods of the Integer class. Integer n = new Integer(5) Integer.parseInt("5") Integer.parseInt("123.5") Double.parseDouble("123.5") Double.parseDouble("123") n Integer m = new Integer(4) n.compareTo(m) m.compareTo(n) // we also looked at the Date class last time Date d; // error! import java.util.Date; Date d; d d = new Date() d.getMonth() // this is informally called a "getter" method // ex. getVariableName() Date d = new Date(109, 9, 31) d Date e = new Date(108, 9, 31) e // comparison operator: == 5 == 5 // how do you compare objects? // use .equals() method d.equals(e) d == e d // create new Date object, set it equal to d. Date f = d; f f == d d.setYear(108); // just changing d, not f d f // f also changes! // why? f references ("points to") the same object // as d. f.setYear(109) f d // now, let's take a look at incrementing an int value. int n = 5; n = n + 1 n n++; n // auto-increment: ++ // auto-decrement: -- n--; n ++n; n int m = 10; m = n++; m n int x = n++ * 4 x n int x = (n++) * 4 x // a more challenging one x = n++ * ++x // let's make the values smaller n = 2 m = 3 int x = n++ * ++m x // now, let's take a look at another type of object: JFrame // JFrame: allows you to create a visual interface. // ex. buttons, text boxes, etc. // you will see this in Lab 1 today! JFrame j; import javax.swing.*; JFrame j; j j = new JFrame(); j.setVisible(true) j.setVisible(false) j.setVisible(true) j = new JFrame(); j.setVisible(true) j.setSize(100, 200) j.setVisible(true) j.setSize(500, 100) j.setLocation(0, 0) j.setLocation(750, 200) j.setLocation(320, 150) j.setLocation(400, 200)