CS 318 - Week 6 Lab - 2-25-13
...edited version of the projected DrJava Interactions window
from lab
Welcome to DrJava. Working directory is /Users/smtuttle/humboldt/s13cs318/318labs/318lab06
> int num;
> num = 42;
> num
42
> num == 42
true
> num < 1
false
> while (num > 10)
{
System.out.println("num is: " + num);
num -= 10;
}
num is: 42
num is: 32
num is: 22
num is: 12
> if (num < 10)
{
System.out.println("num is now: " + num);
}
num is now: 2
> for (int i=3; i<10; i++)
{
System.out.println("i is: " + i);
}
i is: 3
i is: 4
i is: 5
i is: 6
i is: 7
i is: 8
i is: 9
> boolean flag;
> flag = true;
> flag
true
> flag = 1;
Static Error: Bad types in assignment: from int to boolean
> flag == false
false
> flag == 0
Static Error: Illegal comparison: cannot compare a boolean to a int
> flag == ""
Static Error: Illegal comparison: cannot compare a boolean to a String
> if (1)
{
System.out.println("won't get here");
}
Static Error: Condition must be boolean
> // line comment
> /* multi
> line
Static Error: Undefined name 'line'
> */
Invalid top level statement
// OK, you can't do a multi-line comment in the Interactions
// window. But /* and */ do start and end a multi-line
// comment in Java!
> // relational and boolean operators: > < >= <= == != && ||
> true && false
false
> true && 3
Static Error: Bad type in boolean expression
>
> // ALL Java code is contained in classes
> // a Java class that contains a method
> // whose signature is:
> // public static void main(String args[])
> // is called a Java application,
> // and when such an application it executed,
> // its action starts with the main method
>
> // in Java, there is a HIERARCHTY of classes --
> // every class that is not a subclass of another
> // class is a subclass of Object
> // (note that Java class names are EXPECTED to start
> // with an uppercase letter, and be written in
> // camel-case (ResultSet))
> // Instances are expected to start with a lowercase
> // letter, and be written in camel-case as well
> String firstName;
> String lastName;
> // Only named constants are written with underscores
> // to separate "words" -- because they, too, should
> // be written in all-uppercase.
> // BUT they are TYPICALLY declared using static and final:
>
> static final int MAX_WIDGETS = 13;
Modifier static is not allowed here
// OK, maybe not in the DrJava Interactions window --
// but within a class, for a data field, DO usually
// make them static (just one copy of that element
// no matter how many instances)
> final int MAX_WIDGETS = 13;
> MAX_WIDGETS
13
> MAX_WIDGETS = 14;
Static Error: Variable 'MAX_WIDGETS' cannot be modified
> // in Java, the source code for a public class
> // MUST be in a file named DesiredClassName.java
> // to compile Java at the command-line,
> // javac NameOfSourceCodeFile.java
> // and the result is a .class bytecode file for
> // each class contained within that Java source code
> // file (one public class, possibly numerous non-public
> // classes
// NOTE: a variable whose type is one of the Java primitive
// types is a name for a location in memory, like
// you are probably used to;
// BUT! a variable whose type is a class is really
// a REFERENCE -- a pointer! -- to an object of
// that class;
// AND when first declared, it does NOT yet reference
// any instance -- its value, thus, is null!
> String name;
> name
null
> import java.sql.*;
> // the above says, let me use classes from the package
> // java.sql without having to precede their names with
> // java.sql.
> ResultSet rS;
> rS
null
> name
null
// and you might remember from C++: can't call a method
// of an instance that doesn't exist --
// in Java, you'll get a NullPointerException if you
// try that;
> name.length()
java.lang.NullPointerException
> // you'll get this NullPointerException every time you
> // try to call a method for a reference not pointing
> // at anything -- every time you try to all a method
> // for a null reference
> if (name != null)
{
System.out.println("Looky: " + name.length());
}
> name = "George";
> name.length()
6
> // but typically, you call a class's constructor to
> // create a new instance
> name = new String("Sally");
> name
"Sally"
// by the way, a String literal is indeed an instance
// of the String class -- you CAN class String methods
// for it;
> "Mooooooooo".length()
10
> // Java has a small number of non-object primitive types
> // int float double long char boolean
> // ...each of these also has a corresponding "wrapper"
> // class (Integer Float Double Long Character Boolean)
> // for those times when you NEEEED objects!
> // ...thanks to autoboxing, often the Java compiler
> // helpfully wraps primitives in an wrapper class object
> // as needed
>
> // a Java class has a name, methods, data fields...
> //
> // if something in a class -- whether a method or data field --
> // is declared to be static, there is ONE copy of
> // that method of data field, no matter how many instances
> // of the class there are
> // The most convenient way to call a static method is to
> // use the name of the class (instead of an instance)
> // in calling that method
> Math.abs(-3)
3
> Math.ceil(2.7)
3.0
> Integer.parseInt("13")
13
> Double.parseDouble("13.77")
13.77
> Integer.parseInt("moo")
java.lang.NumberFormatException: For input string: "moo"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
> Integer.parseInt("13.7")
java.lang.NumberFormatException: For input string: "13.7"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:458)
at java.lang.Integer.parseInt(Integer.java:499)
> Integer.parseInt("13.0")
java.lang.NumberFormatException: For input string: "13.0"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:458)
at java.lang.Integer.parseInt(Integer.java:499)
> Double.parseDouble("45")
45.0
> (double) "45"
Static Error: Bad types in cast: from String to double
// that Java class Object includes a toString method,
// that produces a String depiction of that object --
// all classes inherit that method, although some
// override it to create a "nicer" output String
// than others...
> String s = "123";
> int num
> s + num
"1230"
// what just happened? if one of +'s operand expressions
// is a String, Java assumes you want String concatenation,
// and tries to call the toString method for the
// other operand (with a primitive type instance
// being auto-boxed in a wrapper class instance if
// necessary)...!
> num=456;
> num.toString()
Static Error: No method in int has name 'toString'
> Integer number = new Integer(num);
> number
456
> number.toString()
"456"
> // method names (except for constructors) also start with
> // a lowercase letter (constructor names are the same
> // as the class, so they are an exception!!)
> // data fields are declared INSIDE the class body but
> // OUTSIDE of any method --
> // They have scope of the entire class (unless a
> // local name overshadows them within some method);
> // local variables are those declared inside of a method,
> // and their scope is just that method
> // SO -- if a name needs to be used in more than 1 method,
> // it either needs to be a data field, or you need to pass
> // that value as a parameter
// WARNING -- note the actual MEANING/SEMANTICS of ==!
// it is what you probably expects for primitive
// type instances:
> int val1;
> int val2;
> val1=3;
> val2=3;
> val1==val2
true
// BUT -- for references?
> String name1;
> String name2;
> name1="Charlie";
> name2="Charlie";
> name1==name2
false
> // with OBJECTS, == is REALLY asking, do these
> // two objects reference the SAME object in
> // memory? (are they the same address?)
> // classes that are comparable for content TEND to
> // include a .equals method
> name1.equals(name2)
true
> name2.equals(name1)
true
> // Note: Java will NOT let you override operators...
>
> // Java arrays:
> // type myArrayName[] = new type[size];
> String names[] = new String[13];
> names
{ null, null, null, null, null, null, null, null, null, null, null, null, null }
> names[0] = "Charlie";
> names
{ Charlie, null, null, null, null, null, null, null, null, null, null, null, null }
> names[1] = "Brown";
> names
{ Charlie, Brown, null, null, null, null, null, null, null, null, null, null, null }
> names[13] = "Sally";
java.lang.ArrayIndexOutOfBoundsException
> // Java arrays have a data field length that can be
> // used to get their number of elements
> names.length
13
// and if an element of an array is an object --
// you can indeed call methods, of course, on that
// object:
> names[1].length()
5
> names[1]
"Brown"
> names
{ Charlie, Brown, null, null, null, null, null, null, null, null, null, null, null }
> for (int i=0; i<names.length; i++)
{
if (names[i] == null)
names[i] = "Joe";
}
> names
{ Charlie, Brown, Joe, Joe, Joe, Joe, Joe, Joe, Joe, Joe, Joe, Joe, Joe }
> // enhanced for loop as of Java 6:
> for (String nextName: names)
{
System.out.println(nextName);
}
Charlie
Brown
Joe
Joe
Joe
Joe
Joe
Joe
Joe
Joe
Joe
Joe
Joe
> // to CATCH exceptions in Java, use a try-catch
> // try
> // {
> // ...code that MIGHT throw an exception...
> // }
> // catch (exceptnType exceptnParam)
> // {
> // ...exception handler for exceptnType...
> // }
> // catch (exceptnType excpetnParam)
> // {
> // ...
> // }
> // note that Exception is a superclass of all exception
> // classes, catch that as an "everything else" type thing;
> // LIKE PL/SQL, these are ONE WAY --
> // if an exception is thrown in a try-block,
> // you LEAVE the try block RIGHT THEN and control
> // passes to the FIRST catch with a matching exception-type
> // then you do the actions in that catch, and LEAVE the
> // try-catch(es) (on to the next statement after the
> // last catch)
> // for a Java application -- a class with a main method --
> // (that is public, static, void, and expects an array of
> // String objects) --
> // you can RUN it using
> // java ClassName arg arg arg
>
> // tip: if you neeeeed a certain number of command line
> // arguments, check args.length, that'll give you that
> // number
then walked through and ran HelloWorld318.java