1 CS 335 Lecture 02 Java Programming
Programming in Java Define data Calculate using data Output result Java is object-oriented: Java program must: Merge data and functions into object Invoke functions to operate on data 2 Define data and functions (in a class) Invoke functions to compute things
Object-Oriented Programming: Classes A class is an object definition, and includes data and functions on that data: public class MyCourseGrade { int pset1; int pset2; : int final_exam; object data 3 } computeaverage() { : } object method
Classes Class: code which defines an object Object: a variable (data + methods) which is an instance of a class Java program: a bunch of class definitions, variables, etc. 4
Classes One special class (the "mother of all classes") contains main(), and this is where flow of control begins: class Test main Kernel of execution is here! class AnotherClass (like MyCourseGrade) memberfunc1() 5 memberfunc2()
Notes Java flow of control starts in main(), in whichever class main() is defined There can only be one class per file (unless you are defining subclasses) The filename must match the class name in a Java source file! 6
Example 1 A Java program with one class and one member called main(): import java.io.*; 7 public class Test { public static void main( String[] ) throws IOException { System.out.println("That it, folks!"); } }
Example 1: Scoping class Test member functions data (none defined) main() 8
Example 2 Add another member function: 9 import java.io.*; public class Test { public static void main( String[] ) throws IOException { System.out.println( That it, folks!"); } public void dostuff() { System.out.println( doing stuff." ); } }
Example 2: Scoping class Test member functions dostuff() data (none defined) main() 10
Example 3 Define an object of class Test and make a function call import java.io.*; public class Test { public static void main( String[] ) throws IOException { Test t; // t is of type "Test" t = new Test(); // allocate object t.dostuff(); // call member function System.out.println( "That it, folks!"); } 11
Example 3 (continued) public void dostuff() { System.out.println( "I'm doing stuff." ); } } 12 Notes: Static methods cannot access nonstatic class members directly main() must always be static
Example 4 13 Test.java: import java.io.*; public class Test { public static void main( String[] ) throws IOException { Stuff t; t = new Stuff(); t.dostuff(); System.out.println( "That it, folks!" ); } } Put main class and a different class in separate files:
Example 4 Stuff.java: public class Stuff { public void dostuff() { System.out.println( "I'm doing stuff." ); } } Notes One class per file To compile: javac Test.java 14
Java I/O The System object provides a way to manage I/O from a more traditional "stream" (terminal window). GUI-based I/O requires the action() method to deal with GUI mouse events. The System object requires no action() method 15 But terminal I/O is inadequate in a browser-based (GUI) environment.
Summary of Some Basic Java Constructs Everything is related to objects: Data declaration: 16 int i; // declare i to be an int Test t; // declare t to be // an object of type Test i = 0; // set the int i equal to 0 t = new Test(); // initialize t and allocate space // using a constructor
Java Constructs Flow of Control: Traditional, but with object-oriented syntax for function calls and member functions Where control starts in the Applet class is important Executable statements Similar to C/C++: while, for, if/else, switch, etc. 17
Java Control Structures Selection: If, If/Else, Switch Repetition (looping): While, For, Do/While Assignment: Expressions, increment/decrement 18
19 Java Reserved Words
20 Example: Average
21
22 Assignments and Expressions
23 Increment/Decrement
24 Java Operators
Java Operators Order is important in post/pre increment operators Increment operators (and combined operators like *=) implicitly are assignments; they must always have an LVALUE as a target One can still confuse = with == 25
26 Primitive Data Types
Java Data Types Java is a strongly-typed language Primitive data types are meant to be portable across all platforms Note that char is 16 bits in Java, not 8! 27
CS 335 Lecture 03 Java Programming Methods Fall 2003 28
Java Methods Methods are functions/procedures attached to data via classes Methods are always invoked on an object, either implicitly or explicitly As with procedures, methods do not need to return a value (void is an option) As with functions, methods can return a value 29
The Math Class The Math class is an object library, and one must use a Math object to invoke methods: System.out.println (Math.sqrt (16.0)); 30
31
Example class class SquareRoot { public static void void main(string[] args) args) { String outputstring; double result; result = Math.sqrt(64.0); // // result = mysquare(result); outputstring = Double.toString(result); // // outputstring = "The "The result is is " + outputstring; System.out.println(outputString); 32 }
Example (Continued) public static double mysquare (double s) s) { return s*s; } } 33
Method Definition Note user-defined method square() Parameter passing: reference and value Type definitions and coercion of arguments Explicit cast necessary when there are no promotion rules 34
Defining Methods Note scope and purpose of method: constructor accessor mutator Parameter passing vs. object-based data access Public/private method access and data hiding 35
36 Java s Allowed Promotions
The Java API Packages Java.awt: abstract windowing toolkit javax.swing: Swing GUI components java.util: date, time, random numbers, etc. java.sql: database connectivity java.io: streams, etc java.rmi: remote method invocation 37
Random Numbers Random number generation is important in most gaming/simulation situations java.util contains the random number package Typical call (returns value between 0 and 1) Math.random(); 38 value = 1+(int)(Math.random()*12)