Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9.

Similar presentations


Presentation on theme: "CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9."— Presentation transcript:

1 CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9

2 References GNU make http://www.gnu.org/software/make/manual/http://www.gnu.org/software/make/manual/ http://java.sun.com/javase/downloads/index.jsp docs. http://java.sun.com/javase/downloads/index.jsp http://java.sun.com/javase/6/docs/ has on-line docs. http://java.sun.com/javase/6/docs/ http://java.sun.com/javase/6/docs/api/index.html class lib. http://java.sun.com/javase/6/docs/api/index.html /export/home/faculty/parson/JavaLang on bill.kutztown.edu Make sure that /usr/jdk/jdk1.6.0_02/bin is near the front of UNIX PATH. Follow instructions in the JavaShellSetup.pdf document (accessible via my home page at http://faculty.kutztown.edu/parson) to set up and verify your Java compilation environment.JavaShellSetup.pdfhttp://faculty.kutztown.edu/parson

3 Boolean example code /export/home/faculty/parson/JavaLang/week1/bool Make sure you have a copy of this. cp –pr ~parson/JavaLang/week1/bool ~/JavaLang/week1/bool Program verifies valid command line args. 1. Program reads command line into an array of floats, raising an Exception if any of the command line arguments is not a float. 2. Verify that array is in ascending order using &&. 3. Verify that array is in ascending order using ||.

4 Booleans Boolean variables and constants boolean isSortedAnd = true, isOutOfOrderOr = false ; Boolean expressions isOutOfOrderOr || ! isLessThanOrEqual(numbers[i], numbers[i+1]) Condition control constructs use booleans if (isSortedAnd != ! isOutOfOrderOr) { Conditional expressions use booleans System.exit(isSortedAnd ? 0 : 1);

5 Class java.lang.System System.in is equivalent to C++ cin. import java.util.Scanner ; Scanner inputScanner = new Scanner(System.in); System.out and System.err are equivalent to C++ cout and cerr. System.out.println("sort result = " + isSortedAnd); System.exit(0) causes the process to exit with status of 0, meaning there is no error. A non-0 exits status signifies an error.

6 Creating Java Arrays Arrays are constructed using new (like C++). “float [] numbers ;” creates a null array reference. float [] numbers = new float [ INTEGER_SIZE ] ; Java implicitly initializes an array of numbers with 0. Booleans gets false, chars get ‘\0000’. float [] numbers = {1.9F, 2.9F, 3.4F, 3.5F} ; Myclass [] myobject = new Myclass[ INTEGER_SIZE ] ; Java initializes an array of object references with null. Java objects and arrays created using “new” need not be “deleted.” JAVA HAS GARBAGE COLLECTION!

7 Using Java Arrays “String [] args” is the parameter for Java main. public static void main(String args[]) { main is needed for running a class from the UNIX command line. Expression “numbers.length” gives the number of elements in the array. if (args.length == 0) { float [] numbers = new float [ args.length ] A subscript access and element of an array numbers[i] = Float.parseFloat(args[i]);

8 Helper class java.util.Arrays Utility methods for manipulating arrays. public static int[] copyOf(int[] original, int newLength); public static void fill(int[] a, int val); public static void sort(int[] a); public static int binarySearch(int[] a, int key); public static T[] copyOf(T[] original, int newLength); public static void sort(T[] a, Comparator c) ;Comparator Comparator int compare(T o1, T o2) returns -1 or 0 or 1 for o1 o2 respectively.T

9 Multidimensional Arrays int [][] matrix ; // an array of arrays, null int [][] matrix = new int[4][3]; // 4 rows of 3 int [][] matrix = { // See Figure 6.11, p. 204 {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; matrix[0][0] is the initial element of the initial row matrix[3][2] is the last element of the last sub-array (last column of the last row)

10 “Ragged Arrays” int [][] triangle = { // p. 205 {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; int [][] triangle = new int [5][]; for (int i = 0 ; i < triangle.length ; i++) { triangle[i] = new int [triangle.length – i]; } triangle[1].length gives the length of the second sub-array triangle[1], which == 4.

11 Handling an exception from a library method Read on-line doc on java.lang.Float.parseFloat try { numbers[i] = Float.parseFloat(args[i]); } catch (NumberFormatException nfexp) { System.err.println("format error on " + args[i] + ": " + nfexp.getMessage()); isSortedAnd = false ; isOutOfOrderOr = true ; // Initialize array element to a default value. numbers[i] = 0.0F ; }

12 Notes on the makefile If you run a test that you intend to fail – System.exit(NON-0) – then put a “-” in front of that command invocation in the makefile. -java $(PACKAGE).$(BASENAME) -1.0 0 fred 1.0 32.0 Also, since your error messages are sent to System.err, you need to redirect System.err to a file to capture its output to use with diff. >> $(BASENAME).out 2>&1 >> $(BASENAME).out 2>$(BASENAME).err

13 Programming practices Always handle exceptions! We may handle some by explicitly ignoring them. Always use { curly braces } for control blocks. Use coding standards that we discuss in class. Write Javadoc documentation. Use both healthy and degenerate tests. » Ignoring these rules will cost you points.


Download ppt "CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9."

Similar presentations


Ads by Google