Title: Java development environment and Review of Java
1Java development environment and Review of Java
2EclipseTM Intergrated Development Environment
(IDE)
Warning Never check the Use this as the default
and do not ask again box.
3The importance of workspace
- This is where you will find all your java files.
- You can switch from one workspace to another.
- You need to define the workspace first (where you
want to put your files) before click on OK
4EclipseTM tutorial
Perspective is a set of related Views (windows)
that enable a development specialist to perform
specific tasks
5Setting up preferences in Eclipse
If you did not download the JDK earlier in this
tutorial, and your existing JRE does not appear
in the Installed JREs preference, you must
add it yourself.
6Setting up preferences in Eclipse
7Setting up preferences in Eclipse
8Create an Eclipse project
From the Eclipse menu bar select File, New,
Project to start the New Project wizard. Select
Java Project and click Next.
9Create an Eclipse project
10Create a Java package
- Structure in dot format
- for example com.tm.tutorial.main
11Create Java classes
Create Application class under com.tm.tutorial.mai
n package
12Create Java classes
Create Application class under com.tm.tutorial.mai
n package
13Implementation of Java class
14Implementation of Java class
Generating setters and getters for attributes
15Implementation of Java class
Generating constructors
16Implementation of Java class
Generating constructors
17Compile Java Application
18Compile Java Application
19Run the application
20Run the application
21Run the application
22Run the application
23Run Application (Shorter way)
24Project 1 discussion
25Algorithm for Main class
26Structure of Report class
-inFile -currentBook, nextBook .
Data members
FileInput
Book
Report object
printReport -processBook -processGenre -columnHea
dings -padL -padR
Methods
27Algorithm for printing a report (Report.java)
Open file
Print footer of the report
processing genre
28Algorithm for process a book and a genre
currentBook record just read Current genre
genre of the current book
Start a new genre and go back to processing a new
genre
29Class exercise
- Class
- Object
- Primitive Data Type
- If, Switch
- While, Do-While
30Review of Java fundamentals
Template for Class Definition
31Review of Java fundamentals
Template for Class Definition
32Numeric data
- Variable declaration
- ltdata typegt ltvariable_namegt
- If more than one variable has the same data type
- ltdata typegt ltname1gt, ltname2gt..
33Six numerical data types
- byte -128 to 127
- short-32768 to 32767 (-215 to 215-1)
- int -231 to 231-1
- long -263 to 263-1
- float -3.4E38 to 3.4E38
- double-1.797E308 to 1.797E308
34Assignment statement
- ltvariable namegt ltexpressiongt
- Example
- x 256-1
35Variable names
- It must be a legal identifier which is an
unlimited series of characters that begins with a
letter. - It must not be a keyword, a boolean literal (true
or false), or the reserved word null. - It must be unique within its scope.
36Variable name (cont.)
- Legal identifierbe composed of letters, numbers,
_ and . Identifiers may only begin with a
letter, _, or . - Keyword
- http//java.sun.com/docs/books/tutorial/java/nuts
andbolts/_keywords.html - Variable names begin with a lowercase letter
- Class names begin with an uppercase letter
37Constant and variables
- Constant
- Value it contains doesnt change
- final int MONTHS_IN_YEAR 12
- Variables
- Value it contains may vary
- double loanAmount
- loanAmount 0
- loanAmount 1000.99
38Integer division and type casting
- Integer division
- Integer/integer integer, 7/2 3
- Integer/double double, 7/2.0 3.5
- Double/integer double, 7.0/2 3.5
- Type casting a process that converts a value of
one data type to another data type. Implicit
casting - Explicit casting
39Type casting (cont.)
- Implicit casting
- Operand is converted from a lower to a higher
precision - Higher precision a data type with a larger range
of values - Double has a higher precision than float
- Int has a higher precision than short
- Operand can be a constant, variable, method call
or another arithmetic expression
40Type casting (cont.)
- Explicit casting
- (ltdata typegt) ltexpressiongt
- Example
- float result
- result (float) ((35)/6)
- and
- result ((float) (53))/6
41Simple Choice Statement
if (ltboolean expressiongt) ltblockgt else
ltblockgt
if (ltboolean expressiongt) single
statement else single statement
42Boolean expression
- Boolean expression is a conditional expression
that is evaluated to either true or false. - Conditional expression is a three part
expression - ltexp.gt ltrelational operatorsgt ltexp.gt
- Boolean expressions can be combined by boolean
operators
43Relational Operators
44- Boolean operators
- means AND
- means OR
- ! means NOT
45The While Loop
while(ltboolean expressiongt) // Repeat
multiple statements. statement 1
statement 2 statement 3 ...
46The Do Loop
do // Repeat multiple statements.
statement 1 statement 2 statement 3
... while(ltboolean expression)
- Note that the statements in the body of the loop
are always executed at least one. - Note the final semicolon, which is required.
47The For-Loop Outline
// Repeat multiple statements. for(initialization
condition post-body update) // Statements
to be repeated. statement 1 statement 2
statement 3 ...
- Commonly used with increment
- and decrement operators.
48Increment and Decrement
- Used as a shorthand for add-one-to and
subtract-one-from - value value1
- value 1
- value
- Prefix and postfix forms
- value
- --value
- value--
49Attributes (Data Member) Declaration
Modifiers
Data Type
Name
private String ownerName
Note Theres only one modifier in this example.
50Method Declaration
Modifier
Return Type
Method Name
Parameter
public void setOwnerName ( String name
) ownerName name
Statements
51Constructor
- A constructor is a special method that is
executed when a new instance of the class is
created.
Modifier
Class Name
Parameter
public Bicycle ( ) ownerName
Unassigned
Statements
52Arguments and Parameters
- An argument is a value we pass to a method.
- A parameter is a placeholder in the called method
to hold the value of the passed argument.
class Account . . . public void
add(double amt) balance balance
amt . . .
class Sample public static void
main(String arg) Account acct
new Account() . . .
acct.add(400) . . . . . .
53Arguments and Parameters
- An argument is a value we pass to a method.
- A parameter is a placeholder in the called method
to hold the value of the passed argument.
class Account . . . public void
add(double amt) balance balance
amt . . .
class Sample public static void
main(String arg) Account acct
new Account() . . .
acct.add(400) . . . . . .
parameter
54Algorithm for Main class
55Algorithm for printing a report (Report.java)
Open file
Print footer of the report
processing a movie
56Algorithm for process a movie and a genre
currentMovie record just read Current genre
genre of the current movie
Start a new genre and go back to processing a new
genre