csc1030 hands-on introduction to java
DESCRIPTION
CSC1030 HANDS-ON INTRODUCTION TO JAVA. Loop, Primitive type & Object basics. Three Kinds of Looping Statements. while (condition) { loop_body; } do { loop_body; } while (condition); for (start; condition; update) { loop_body; }. Supplemented by: break; continue;. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/1.jpg)
CSC1030HANDS-ON INTRODUCTION
TO JAVA Loop, Primitive type & Object basics
![Page 2: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/2.jpg)
Three Kinds of Looping Statementswhile (condition) {
loop_body;
}
do {
loop_body;
} while (condition);
for (start; condition; update) {
loop_body;
}
Supplemented by:•break;•continue;
![Page 3: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/3.jpg)
For example: while-loopclass Main{ public static void main (String [ ] args) { int i = 1; // declare a variable
// a while-loop while ( i < 10 ) // while "i < 10 is true" { System.out.println(i); i = i + 1; // increment i }
System.out.println(i); // what will i be?
}}
![Page 4: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/4.jpg)
For example: do-while-loopclass Main{ public static void main (String [ ] args) { int i = 1; // declare a variable
// a do-while-loop do { // do at least once System.out.println(i); i = i + 1; // increment i } while ( i < 10 ); // while "i < 10 is true"
System.out.println(i); // what will i be?
}}
![Page 5: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/5.jpg)
For example: for-loopclass Main{ public static void main (String [ ] args) { int i; // declare a variable
// a for-loop (start; check; update) for ( i = 1; i < 10; i++ ) { // i runs from 1 to 9 System.out.println(i); }
System.out.println(i); // what will i be?
}}
![Page 6: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/6.jpg)
break and continue break: stops a loop right away.
continue: stop the current iteration AND continue the next round!
![Page 7: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/7.jpg)
Outline The Java API Using the Math Class
Lab Exercise: Quadratic Equation Solver Java Primitive Data Types Java Class (Object) Types Using the String Objects
Lab Exercise: String Manipulation
![Page 8: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/8.jpg)
The Java API System, String, JOptionPane, etc.
are stuffs provided in the Java Application Programming Interface (API).
System.out.println(...);
String answer;
JOptionPane.showInputDialog(...);
![Page 9: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/9.jpg)
The Java API Reference Manualhttp://java.sun.com/javase/6/docs/api/
![Page 10: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/10.jpg)
The Java API Reference ManualQuick Reference Using NetBeans
![Page 11: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/11.jpg)
Importance of the Java API The API provides extra functionalities and tools, however.
As a Java programmer, we do NOT have to memorize all the stuffs provided in the API.
However, we should understand, look-up and make use of the API library properly.
Better though, we recite the spelling and usage of some commonly used ones, such as System, String, etc
![Page 12: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/12.jpg)
Contributing to the World-Wide API As a Computer Professional, sometimes we
create something to make a contribution.
Others could then possibly make use of our work.
In such case, we have to well-design, well-test and well-document our contribution.
![Page 13: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/13.jpg)
Outline The Java API Using the Math Class
Lab Exercise: Quadratic Equation Solver Java Primitive Data Types Java Class (Object) Types Using the String Objects
Lab Exercise: String Manipulation
![Page 14: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/14.jpg)
The Math Class Well, System is a Java Class.
String is also a Java Class.
So as JOptionPane.
We are going to make use of another useful and important Java Class, the Math Class.
![Page 15: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/15.jpg)
Using the Math Classclass Main{ public static void main (String [ ] args) { String deg; deg = JOptionPane.showInputDialog("An angle in deg:");
// convert the angle (text input) to a number double angle_in_degree; angle_in_degree = Double.parseDouble( deg );
double angle_in_radian; angle_in_radian = Math.toRadians( angle_in_degree );
System.out.println( "sin(" + deg + ") = " + Math.sin( angle_in_radian ) ); }}
sin(45) = 0.7071067811865475
![Page 16: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/16.jpg)
Some Commonly Used Math Class Members Math.toRadians( 270 )
Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
Math.sin( 3.14159 ) A method giving us the trigonometric sine of an angle in
radian.
Math.sqrt( 2.25 ) A method giving us the positive square root of a value.
![Page 17: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/17.jpg)
Lab Exercise: Using Math with if
![Page 18: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/18.jpg)
Break Time – 15 minutes
![Page 19: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/19.jpg)
Outline The Java API Using the Math Class
Lab Exercise: Quadratic Equation Solver Java Primitive Data Types Java Class (Object) Types Using the String Objects
Lab Exercise: String Manipulation
![Page 20: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/20.jpg)
They are for storing numbers with a decimal point.
They are for storing integers of different range limits.
Let's give the full list of the EIGHT Primitive Data Types in Java:
char boolean
double float
byte short int long
Java Primitive Data Types
![Page 21: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/21.jpg)
What are Primitive Types? Eight build-in types in Java:
byte [-128 127]
short [-32768 32767]
int [-2147483648 2147483647]
long [-9223372036854775808 9223372036854775807 ]
float [±1038 with floating point]
double [±10308 with floating point]
char [‘A’, ‘B’, …, ‘a’, ‘b’, …, ‘0’, ‘1’, …, ‘!’, ‘#’]
boolean [true, false]
![Page 22: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/22.jpg)
Java Primitive Data Types Variables in Java must bear a type.
Basically, we have these 8 types for declaring variables.
They are for storing different kind and different range of data.
![Page 23: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/23.jpg)
The char Data Type char is one of the primitive types
representing a single character.
char aVariable;aVariable = 'a';
char gender = 'F';char grade = 'C';char symbol = '#';
![Page 24: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/24.jpg)
Outline The Java API Using the Math Class
Lab Exercise: Quadratic Equation Solver Java Primitive Data Types Java Class (Object) Types: Quick Revision Using the String Objects
Lab Exercise: String Manipulation
![Page 25: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/25.jpg)
Java Class (Object) Types The primitive data types are good for representing
simple data, such as a number, a character, a true/ false value.
In real life, information is usually not so simple.
Thus, we have to use some structured data types.
They are known as Class (Object) Types in Java.
![Page 26: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/26.jpg)
Object-Oriented Programming Object-oriented Programming (OOP) is one of the
programming paradigms (school of thought, methodology) in computer science.
An object is used to store information as well as to keep methods for handling the information.
Synonyms: Object == Instance == Entity
Class ~= Static ~= Type
![Page 27: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/27.jpg)
Customer
Customer
Bill
Customer
Customer
Michael
Account
Account
012-3-1441
Objects
Our world is full of objects.
Graphical representation of objects
Object ‘type’
Object name
![Page 28: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/28.jpg)
Our world is full of objects.
Graphical representation of objects
Customer
Customer
Bill
Objects
Customer
Customer
Michael
Account
Account
012-3-1441
![Page 29: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/29.jpg)
Modeling Our World We try to model this object world.
Objects can keep data/state and accomplish tasks. e.g.
A drink dispensing machine has a stock of 100 cans.A drink dispensing machine sells Coke.
Inhuman?! Certainly, but it helps us to program a computer in an
organized and manageable manner.
![Page 30: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/30.jpg)
Classes A class (e.g., Customer) is a kind of mold or
template to create objects (e.g., Michael and Bill).
An object is an instance of a class. The object belongs to that class.
Customer
Customer
BillCustom
er
Customer
Michael
Customer
Customer
‘instance-of’/ ‘belongs-to’
Class Object
![Page 31: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/31.jpg)
More Class/ Object Examples
PersonPerson
Michael
PersonPerson
Bill Gates
PersonPerson
Account
Account
019-9-5887AccountAccount
217-1-1345
Account
Account
![Page 32: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/32.jpg)
Object-Oriented Programming We first define classes.
While the program is running, we may create objects from these classes.
We may store information in objects.
We send messages to an object, instruct it to perform a task.
(For example, we send a deposit $250.00 message to an Account object to add $250.00 into the account.)
![Page 33: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/33.jpg)
Class (Object) Type Variables There are already lots of classes defined in the Java API.
Let’s see how to make use of them.String address;address = "CUHK, Shatin, HK";
File aFile;aFile = new File("Hello.zip");
ZipFile aZip;aZip = new ZipFile(aFile);
JButton aButton;aButton = new JButton("Ok");
![Page 34: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/34.jpg)
Class (Object) Type Variables With classes and the objects we created from
the classes, we can represent, store and handle more complex form of data.
For example, we can represent some text using String, we can handle a ZIP file, we can process a JPEG image, we can show a button on the screen, etc.
![Page 35: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/35.jpg)
Outline The Java API Using the Math Class
Lab Exercise: Quadratic Equation Solver Java Primitive Data Types Java Class (Object) Types Using the String Objects
Lab Exercise: String Manipulation
![Page 36: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/36.jpg)
Using the String Objects Usually, an object is created from a class using
the new( ) statement.
The String objects are different. They are privileged to use the double quotes for creation.
String address;address = "CUHK, Shatin, HK";
![Page 37: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/37.jpg)
Using the String Object Methods There are many methods defined for String objects. We can easily find out some properties of a String.
String address;address = "CUHK, Shatin, HK";
char firstLetter;firstLetter = address.charAt(0);
int addressLength;addressLength = address.length();
C
16
![Page 38: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/38.jpg)
Lab Exercise: String Manipulation
![Page 39: CSC1030 HANDS-ON INTRODUCTION TO JAVA](https://reader035.vdocuments.net/reader035/viewer/2022062721/56813790550346895d9f308d/html5/thumbnails/39.jpg)
Any Enquiry?