Download presentation
Presentation is loading. Please wait.
Published byAlbert Snow Modified over 8 years ago
1
CSC1030 HANDS-ON INTRODUCTION TO JAVA Loop, Primitive type & Object basics
2
Three Kinds of Looping Statements while (condition) { loop_body; } do { loop_body; } while (condition); for (start; condition; update) { loop_body; } Supplemented by: break; continue;
3
For example: while-loop class 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? }
4
For example: do-while-loop class 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? }
5
For example: for-loop class 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? }
6
break and continue break: stops a loop right away. continue: stop the current iteration AND continue the next round!
7
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
8
The Java API System, String, JOptionPane, etc. are stuffs provided in the Java Application Programming Interface (API). System.out.println(...); String answer; JOptionPane.showInputDialog(...);
9
The Java API Reference Manual http://java.sun.com/javase/6/docs/api/
10
The Java API Reference Manual Quick Reference Using NetBeans
11
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
12
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.
13
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
14
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.
15
Using the Math Class class 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
16
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.
17
Lab Exercise: Using Math with if
18
Break Time – 15 minutes
19
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
20
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: charboolean doublefloat byteshortintlong Java Primitive Data Types
21
What are Primitive Types? Eight build-in types in Java: byte [-128 127] short [-32768 32767] int [-2147483648 2147483647] long [ - 9223372036854775808 9223372036854775807 ] float [±10 38 with floating point] double [±10 308 with floating point] char [‘A’, ‘B’, …, ‘a’, ‘b’, …, ‘0’, ‘1’, …, ‘!’, ‘#’] boolean [true, false]
22
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.
23
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 = '#';
24
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
25
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.
26
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
27
Customer Bill Customer Michael Account 012-3-1441 Objects Our world is full of objects. Graphical representation of objects Object ‘type’ Object name
28
Our world is full of objects. Graphical representation of objects Customer Bill Objects Customer Michael Account 012-3-1441
29
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.
30
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 Bill Customer Michael Customer ‘instance-of’/ ‘belongs-to’ Class Object
31
More Class/ Object Examples Person Michael Person Bill Gates Person Account 019-9-5887 Account 217-1-1345 Account
32
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.)
33
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");
34
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.
35
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
36
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";
37
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
38
Lab Exercise: String Manipulation
39
Any Enquiry?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.