![Page 1: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/1.jpg)
Java development environment and Review of Java
![Page 2: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/2.jpg)
EclipseTM Intergrated Development Environment (IDE) Running Eclipse:
Warning: Never check the “Use this as the default and do not ask again” box.
![Page 3: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/3.jpg)
The 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
![Page 4: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/4.jpg)
EclipseTM tutorial
Perspective is a set of related Views (windows)
that enable a development specialist to perform
specific tasks
![Page 5: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/5.jpg)
Setting 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.
![Page 6: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/6.jpg)
Setting up preferences in Eclipse
![Page 7: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/7.jpg)
Setting up preferences in Eclipse
![Page 8: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/8.jpg)
Create an Eclipse project
From the Eclipse menu bar select File, New, Project to start the “New Project” wizard.Select “Java Project” and click “Next”.
![Page 9: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/9.jpg)
Create an Eclipse project
![Page 10: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/10.jpg)
Create a Java package
Structure in dot format:
for example: com.tm.tutorial.main
![Page 11: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/11.jpg)
Create Java classes
Create Application class under com.tm.tutorial.main package
![Page 12: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/12.jpg)
Create Java classes
Create Application class under com.tm.tutorial.main package
![Page 13: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/13.jpg)
Implementation of Java class
![Page 14: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/14.jpg)
Implementation of Java class
Generating setters and getters for attributes
![Page 15: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/15.jpg)
Implementation of Java class
Generating constructors
![Page 16: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/16.jpg)
Implementation of Java class
Generating constructors
![Page 17: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/17.jpg)
Compile Java Application
![Page 18: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/18.jpg)
Compile Java Application
![Page 19: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/19.jpg)
Run the application
![Page 20: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/20.jpg)
Run the application
![Page 21: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/21.jpg)
Run the application
![Page 22: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/22.jpg)
Run the application
![Page 23: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/23.jpg)
Run Application (Shorter way)
![Page 24: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/24.jpg)
Project 1 discussion
![Page 25: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/25.jpg)
Algorithm for Main class
Assign a file nameAssign a file name
Create a report objectCreate a report object
Call a method from Report class to produce report from the given data file
Call a method from Report class to produce report from the given data file
![Page 26: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/26.jpg)
Structure of Report class
Report object
-inFile
-currentBook, nextBook….
FileInputFileInput
Book
+printReport-processBook-processGenre-columnHeadings-padL-padR
Datamembers
Methods
![Page 27: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/27.jpg)
Algorithm for printing a report (Report.java)
Open fileOpen file
Printing headingPrinting heading
Read a recordRead a record
processing genre*processing genre*
Print footer of the report
Print footer of the report
Record != null ?Record != null ?
true
false
![Page 28: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/28.jpg)
Algorithm for process a book and a genre
currentBook = record just readCurrent genre = genre of the current book
currentBook = record just readCurrent genre = genre of the current book
Print information about a bookPrint information about a book
Read another bookRead another book
Total price for each genre += price of the current book
Total price for each genre += price of the current book
Start a new genre and go back to processing a new genre
Start a new genre and go back to processing a new genre
Is another book has the same genre ?
Is another book has the same genre ?
true
false
![Page 29: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/29.jpg)
Class exercise
Class Object Primitive Data Type If, Switch While, Do-While
![Page 30: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/30.jpg)
Review of Java fundamentalsTemplate for Class Definition
class {
}
Import StatementsImport Statements
Class CommentClass Comment
Class NameClass Name
Data MembersData Members
Methods(incl. Constructor)
Methods(incl. Constructor)
![Page 31: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/31.jpg)
Review of Java fundamentalsTemplate for Class Definition
class {
}
Import StatementsImport Statements
Class CommentClass Comment
Class NameClass Name
Data MembersData Members
Methods(incl. Constructor)
Methods(incl. Constructor)
![Page 32: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/32.jpg)
Numeric data
Variable declaration:
<data type> <variable_name>;
If more than one variable has the same data type:
<data type> <name1>, <name2>..;
![Page 33: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/33.jpg)
Six 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.4E+38 to 3.4E+38 double:-1.797E+308 to 1.797E+308
![Page 34: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/34.jpg)
Assignment statement
<variable name> = <expression>;
Example:
x =2*5+6-1;
![Page 35: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/35.jpg)
Variable 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.
![Page 36: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/36.jpg)
Variable name (cont.)
Legal identifier:be composed of letters, numbers, _ and $. Identifiers may only begin with a letter, _, or $.
Keyword:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
Variable names begin with a lowercase letter Class names begin with an uppercase letter
![Page 37: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/37.jpg)
Constant and variables
Constant: Value it contains doesn’t change
final int MONTHS_IN_YEAR = 12;
Variables: Value it contains may vary
double loanAmount; loanAmount =0; loanAmount = 1000.99;
![Page 38: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/38.jpg)
Integer 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
![Page 39: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/39.jpg)
Type 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
![Page 40: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/40.jpg)
Type casting (cont.)
Explicit casting (<data type>) <expression> Example:
float result;
result = (float) ((3+5)/6);
and
result = ((float) (5+3))/6;
![Page 41: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/41.jpg)
if (<boolean expression>) <block>;else <block>;
Simple Choice Statement
if (<boolean expression>) single statement;else single statement;
![Page 42: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/42.jpg)
Boolean expression
Boolean expression: is a conditional expression that is evaluated to either true or false.
Conditional expression: is a three part expression:<exp.> <relational operators><exp.>
Boolean expressions can be combined by boolean operators
![Page 43: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/43.jpg)
Relational Operators
Expression Meaninga == b Is a equal to b?a != b Is a not equal to b?a > b Is a greater than b?a < b Is a less than b?a >= b Is a greater than or equal to b?a <= b Is a less than or equal to b?
![Page 44: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/44.jpg)
Boolean operators
&& means AND
|| means OR
! means NOT
![Page 45: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/45.jpg)
The While Loop
while(<boolean expression>){
// Repeat multiple statements.
statement 1
statement 2
statement 3
...
}
![Page 46: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/46.jpg)
The Do Loopdo{ // Repeat multiple statements. statement 1 statement 2 statement 3 ...} while(<boolean expression);
•Note that the statements in the body of the loop are always executed at least one.•Note the final semicolon, which is required.
![Page 47: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/47.jpg)
The 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.
![Page 48: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/48.jpg)
Increment and Decrement Used as a shorthand for add-one-to and
subtract-one-from: value = value+1; value += 1; value++;
Prefix and postfix forms: ++value; --value; value--;
![Page 49: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/49.jpg)
Attributes (Data Member) Declaration
<modifiers> <data type> <name> ;
private String ownerName ;
ModifiersModifiers Data TypeData Type NameName
Note: There’s only one modifier in this example.
Note: There’s only one modifier in this example.
![Page 50: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/50.jpg)
Method Declaration
<modifier> <return type> <method name> ( <parameters> ){
<statements>
}
public void setOwnerName ( String name ) {
ownerName = name;
}
StatementsStatements
ModifierModifier Return TypeReturn Type Method NameMethod Name ParameterParameter
![Page 51: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/51.jpg)
Constructor A constructor is a special method that is executed when a new
instance of the class is created.
public <class name> ( <parameters> ){ <statements> }
public Bicycle ( ) {
ownerName = “Unassigned”;
}StatementsStatements
ModifierModifier Class NameClass Name ParameterParameter
![Page 52: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/52.jpg)
Arguments 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); . . . }
. . . } argument
![Page 53: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/53.jpg)
Arguments 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
![Page 54: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/54.jpg)
Algorithm for Main class
Assign a file nameAssign a file name
Create a report objectCreate a report object
Call a method from Report class to produce report from the given data file
Call a method from Report class to produce report from the given data file
![Page 55: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/55.jpg)
Algorithm for printing a report (Report.java)
Open fileOpen file
Printing headingPrinting heading
Read a recordRead a record
processing a movie*processing a movie*
Print footer of the report
Print footer of the report
Record != null ?Record != null ?
true
false
![Page 56: Java development environment and Review of Java](https://reader030.vdocuments.mx/reader030/viewer/2022013004/56814737550346895db478cd/html5/thumbnails/56.jpg)
Algorithm for process a movie and a genre
currentMovie = record just readCurrent genre = genre of the current movie
currentMovie = record just readCurrent genre = genre of the current movie
Print information about a moviePrint information about a movie
Read another movieRead another movie
Total price for each genre += price of the current movie
Total price for each genre += price of the current movie
Start a new genre and go back to processing a new genre
Start a new genre and go back to processing a new genre
Is another movie has the same genre ?
Is another movie has the same genre ?
true
false