Title: Understanding Java
1Understanding Java
- Discussion Section 08/28/2008
- 22C21 Computer Science II Data Structures
2Sample Programs
- In this class we will be discussing programs
- ReadNumbers.java. This shows a simple example of
Java input statements. - TwoDPoint.java. This shows how to define a class
(called TwoDPoint) and TwoDPointTest.java shows
how to define and use TwoDPoint objects. - (Distribute printed copies of the programs to the
students)
3ReadNumbers.java illustrates
- (a) the overall structure of a java program
- (b) how to import packages/classes
- (c) while-statements
- (d) the try-catch stuff
- (e) Defining a BufferedReader
- (f) the readLine method
- (g) the Integer class and the parseInt method
4Structure
Class
Import packages
Variable declaration
Main method declaration
5import
- only tells the compiler where to look for other
classes. - has purpose to allow a class to use the
facilities of another class
import java.util. import java.io.
6Java.util.
- The java.util package defines a number of useful
classes, primarily collections classes that are
useful for working with groups of objects. - (Java in a Nutshell, Orielly)
7Java.io.
- Provides for system input and output through data
streams, serialization and the file system.
8Class
In the real world, you'll often find many
individual objects all of the same kind. There
may be thousands of other bicycles in existence,
all of the same make and model. bicycle was
built from the same set of blueprints and
therefore contains the same components. In
object-oriented terms, we say that your bicycle
is an instance of the class of objects known as
bicycles. A class is the blueprint from which
individual objects are created. (www.java.sun.com
)
class ReadNumbers
Name of the class
Keyword class
9Declaration
the field is accessible only within its own
class.
keyword used to define a variable as a class
variable
we read the user's input
private static BufferedReader stdin new
BufferedReader( new InputStreamReader( System.in
) )
java.io.BufferedReader is used to read
character Strings
convert byte streams to character streams
10The object created
stdin
Class BufferedReader http//java.sun.com/j2se/1.5
.0/docs/api/java/io/BufferedReader.html
Class InputStreamReader http//java.sun.com/j2se
/1.4.2/docs/api/java/io/InputStreamReader.html
11Main method
public static void main(String args)
the method or variable can be accessed by
elements residing in other classes
command-line argument
The main method is similar to the main function
in C and C it's the entry point for your
application and will subsequently invoke all the
other methods required by your program.
12While Loop
while(true) // statements // end of
while-true
Statements inside a while (indicated by ) are
executed repeatedly, till the condition of the
while (the parameter In brackets ()) is false. In
this case, the loop will be executed forever
till something happens inside the loop to
stop the execution.
13Getting out of the Loop
if((number lt 0) (number gt 1000)) break
If the given condition is not met, the break
statement is called. This break will take us
out of the while loop!
14Try and Catch
try //statements catch(the exception to
catch)) // statements
In the try block, you enter the code that might
cause an exception. At the end of this portion
of code, you close it with a catch block. The
catch block will tell your program just what to
do in case the exception actually does occur. If
no exception occurs, then the catch block is
ignored, and your program moves along smoothly.
15Try and Catch
- In this program we are executing in the try
block, the statement - which may throw an exception
- which will take us to the catch block.
stdin.readLine()
java.io.IOException
16readLine() method
// Read a line of text from the user. String
input stdin.readLine()
The BufferedReader class gives us the readLine()
method, and applies buffering to the input
character input stream. Here, the String input
will hold the user input.
17Integer Class
// converts a String into an int value int number
Integer.parseInt( input )
The Integer class wraps a value of the primitive
type int in an object. In addition, this class
provides several methods
Parses the string argument as a signed decimal
integer.
18Questions?
19TwoDPoint.java, TwoDPointTest.java
- how to define a class,
- the notion of a default constructor
- class methods
- the notion of this, etc.
20TwoDPoint.java
TwoDPointTest.java
Creating TwoDPoint Objects here
21Defining Class
- class TwoDPoint
- /// variables, methods
The name of the Java file is the same as that of
the name of the class
22Constructors
- Constructors have one purpose in life to create
an instance of a class. This can also be called
creating an object, as in
TwoDPoint p p new TwoDPoint()
23Constructors
- A java constructor has the same name as the name
of the class to which it belongs. Constructor's
syntax does not include a return type, since
constructors never return a value. - Java provides a default constructor which takes
no arguments and performs no special actions or
initializations, when no explicit constructors
are provided.
24Class Method
- A java method is a series of statements that
perform some repeated task. - It is like a C/C function
- The methods in TwoDPoint.java are
- String getAsString()
- void setX(double value)
- void setY(double value)
- double getX()
- double getY()
25this
- A method uses this to refer to the instance of
the class that is executing the method - Instance of the class the object of the class
in question - Instance variable the variables declared in
the class outside the methods
26Why do we need this?
- The keyword helps us to avoid name conflicts.
- In some case we may declare the name of instance
variable and local variables same. - Now to avoid the confliction between them we use
this keyword.
27Object
// Declare two TwoDPoint objects, p and
q TwoDPoint p TwoDPoint q
28Method Call
// Call the set methods to assign the two //
coordinates of p p.setX(1.5) p.setY(2.5)
29Questions?