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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
1 Review for Midterm 3 CS 101 Spring Topic Coverage  Loops Chapter 4  Methods Chapter 5  Classes Chapter 6, Designing and creating classes.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
CS/ENGRD 2110 SPRING 2013 Lecture 2: Introduction to Java 1.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC204 – Programming I Lecture 4 August 28, 2002.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
Chapter 2: Java Fundamentals
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9.
CSC Java Programming, Fall, 2008 August 28, 2008, class 2 Tuesday, September 2 uses Monday’s schedule!
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Object Oriented Programming Lecture 2: BallWorld.
CMSC 202 Text File I/O.
3 Introduction to Classes and Objects.
Objects, Classes, Program Constructs
File Handling in Java January 19
CSC Java Programming, Spring, 2012
Review for Midterm 3.
A type is a collection of values
Presentation transcript:

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

References GNU make docs. has on-line docs. 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 to set up and verify your Java compilation environment.JavaShellSetup.pdfhttp://faculty.kutztown.edu/parson

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) fred 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. 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 ; 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.

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.