Download Lecture notes for week 3, 11 September

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC 243 - Java Programming,
Fall, 2008
Week 3: Objects, Classes, Strings,
Text I/O, September 11
References
•
•
•
•
•
•
GNU make http://www.gnu.org/software/make/manual/
http://java.sun.com/javase/downloads/index.jsp docs.
http://java.sun.com/javase/6/docs/ has on-line docs.
http://java.sun.com/javase/6/docs/api/index.html class lib.
/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.
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)
“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.
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 ;
}
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
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
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.
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!
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 ;
a
String object
b
• Assignment copies the reference.
• == compares references (exactly the same object when
true)
• object.equals() compares two objects using an objectspecific equals operation.
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
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!
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
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.
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.