Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 335 Lecture 02 Java Programming 1 Programming in Java Define data z Calculate using data z Output result Java is object-oriented: z • Merge data and functions into object • Invoke functions to operate on data Java program must: 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; computeAverage() { : } 3 } object data 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 z z z 6 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! Example 1 A Java program with one class and one member called main(): import java.io.*; public class Test { public static void main( String[] ) throws IOException { System.out.println("That it, folks!"); } } 7 Example 1: Scoping class Test member functions main() data (none defined) 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) 10 main() 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 Test.java: 13 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 But terminal I/O is inadequate in a browser-based (GUI) environment. 15 Summary of Some Basic Java Constructs Everything is related to objects: Data declaration: int i; Test t; // declare i to be an int // 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 16 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 z Selection: – z Repetition (looping): – z While, For, Do/While Assignment: – 18 If, If/Else, Switch Expressions, increment/decrement Java Reserved Words 19 Example: Average 20 21 Assignments and Expressions 22 Increment/Decrement 23 Java Operators 24 Java Operators z z z 25 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 “==“ Primitive Data Types 26 Java Data Types z z z 27 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! CS 335 Lecture 03 Java Programming – Methods Fall 2003 28 Java Methods z z z z 29 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 The Math Class z 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 SquareRoot {{ public public static static void void main(String[] main(String[] args) args) {{ String String outputString; outputString; double double result; result; result result == Math.sqrt(64.0); Math.sqrt(64.0); // // result result == mysquare(result); mysquare(result); outputString outputString == Double.toString(result); Double.toString(result); // // outputString outputString == "The "The result result is is "" ++ outputString; outputString; System.out.println(outputString); System.out.println(outputString); 32 }} Example (Continued) public public static static double double mysquare mysquare (double (double s) s) {{ return return s*s; s*s; }} }} 33 Method Definition z z z z 34 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 Defining Methods z Note scope and purpose of method: – – – – z z 35 constructor accessor mutator … Parameter passing vs. object-based data access Public/private method access and data hiding Java’s Allowed Promotions 36 The Java API Packages z z z z z z 37 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 Random Numbers z z z 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(); value = 1+(int)(Math.random()*12) 38