390 likes | 478 Views
Learn about three kinds of looping statements in Java (while, do-while, for), primitive and object data types, the Java API, and using the Math class with lab exercises. Understand the importance of the Java API in programming and learn about contributing to world-wide API development.
E N D
CSC1030HANDS-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;
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? } }
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? } }
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? } }
break and continue • break: stops a loop right away. • continue: stop the current iteration AND continue the next round!
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
The Java API • System, String, JOptionPane, etc. are stuffs provided in the Java Application Programming Interface (API). System.out.println(...); String answer; JOptionPane.showInputDialog(...);
The Java API Reference Manualhttp://java.sun.com/javase/6/docs/api/
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
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.
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
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.
Using the Math Class sin(45) = 0.7071067811865475 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 ) ); } }
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.
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
Java Primitive Data Types • Let's give the full list of the EIGHTPrimitive Data Types in Java: • char boolean double float byte short int long They are for storing numbers with a decimal point. They are for storing integers of different range limits.
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]
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.
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 = '#';
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
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.
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
012-3-1441 Bill Michael Customer Customer Account Objects • Our world is full of objects. Graphical representation of objects Object name Object ‘type’
Bill 012-3-1441 Michael Customer Customer Account Objects • Our world is full of objects. Graphical representation of objects
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.
Customer Bill Michael Customer Customer 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. Object Class ‘instance-of’/ ‘belongs-to’
Bill Gates Person Person Account Michael 019-9-5887 Person Account More Class/ Object Examples 217-1-1345 Account
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.)
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");
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.
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
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";
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