Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 243 - Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.

Similar presentations


Presentation on theme: "CSC 243 - Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11."— Presentation transcript:

1 CSC 243 - Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11

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 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)

4 “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.

5 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 ; }

6 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

7 Review of qualifiers on methods and data in a class or object public, protected, private or implicit package visibility of each field and method static for data shared by all objects in a class (for example, initialization data), non-static for data that is specific to each object\ final is like const in C++ -- the value may not change

8 Scrabble! What classes and objects do we need to represent a Scrabble game? What fields would be in these classes? What methods would be in these classes? Here is my first guess. Here is my first guess

9 Methods within Classes Constructor has same name as class. It may be overloaded, like any method. Each variant has a different set of parameter types. Constructor initializes a new object of the class when new is invoked on the Constructor. An Access Method retrieves data. A Mutator Method modifies an object. There are no Destructors in Java!

10 Objects and References An object is accessed via a reference, which acts like a pointer in C++. String a = new String(“A”); String b = a ; aString objectb Assignment copies the reference. == compares references (exactly the same object when true) object.equals() compares two objects using an object- specific equals operation.

11 Reference equality != Object equality public class main { public static void main(String [] args) { String a = new String("A") ; String b = a ; System.out.println("after pointer copy a == b -> “ + String.valueOf(a == b) + ", a.equals(b) -> " + String.valueOf(a.equals(b))); a = new String("A") ; System.out.println("after reconstruction a == b -> “ + String.valueOf(a == b) + ", a.equals(b) -> " + String.valueOf(a.equals(b))); } } $ java strings.PtrCompare after pointer copy a == b -> true, a.equals(b) -> true after reconstruction a == b -> false, a.equals(b) -> true

12 chars, Characters and Strings A char is a primitive type. A Character is a wrapper class for char. A String is a class known to the compiler. String objects are immutable. “abc” compiles as an interned (unique) String object. StringBuffer is a mutable string class. StringBuilder is a non-multithread-safe, efficient variant of StringBuffer. Conversion methods abound!

13 A StringBuilder object can serve as a text template String editing operations indexOf() to search delete from start to end-1 replace from start to end – 1 insert before start index replaceCharAt for individual characters /export/home/faculty/parson/JavaLang/strings

14 Text File I/O import java.io.PrintWriter ; PrintWriter output = new PrintWriter(filepath); print, println and printf (formatted output) methods Just like System.out and System.err. import java.util.Scanner ; java.io.File(filepath) gets at directory information. Scanner input = new Scanner(new File(filepath)); Scanner input = new Scanner(System.in); hasNextTYPE() controls looping over input items. nextTYPE() scans and returns these items.

15 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 3: Objects, Classes, Strings, Text I/O, September 11."

Similar presentations


Ads by Google