CSE Summer Preparatory Course - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

CSE Summer Preparatory Course

Description:

Java Programming Lab and Exercise. Lecture 2. Have you finished the ... As a Java programmer, we do NOT have to memorize all the stuffs provided in the API. ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 37
Provided by: michae107
Category:

less

Transcript and Presenter's Notes

Title: CSE Summer Preparatory Course


1
CSESummer Preparatory Course
  • Using the Java API

2
Revision
  • Introduction to Computers
  • Introduction to Programming
  • Introduction to Java a modern programming
    language
  • Java Syntax Variables and Operations
  • Basic Output
  • Java Programming Lab and Exercise

Lecture 1
3
Revision
  • Getting User Input
  • Storing Text Using String
  • String Processing and Integer
  • The boolean Data Type
  • ifelse
  • while-loop
  • Swapping the value of two variables
  • Java Programming Lab and Exercise

Lecture 2
Have you finished the problems in Lab 2?
4
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

5
The Java API
  • System, String, JOptionPane, etc. are stuffs
    provided in the Java Application Programming
    Interface (API).

System.out.println(...)
String answer
JOptionPane.showInputDialog(...)
6
The Java API Reference Manualhttp//java.sun.com/
j2se/1.5.0/docs/api/
7
The Java API Reference ManualQuick Reference
Using NetBeans
8
Importance of the Java API
  • The Java language itself is already a strong
    programming tool.
  • The API provides extra functionalities and tools,
    however.
  • A single command using the API can perform
    enormous work for us.

9
Using the Java API
  • 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.

10
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.

11
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

12
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.

13
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
) )
14
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.

15
Lab Exercise Using Math with if
16
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

17
Java Primitive Data Types
  • Let's give the full list of the 8 Primitive 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.
18
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

19
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.

20
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 ''

21
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

22
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.

23
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

24
Objects
  • Our world is full of objects.
  • Graphical representation of objects

Object name
Object type
25
Objects
  • Our world is full of objects.
  • Graphical representation of objects

26
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.

27
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
28
More Class/ Object Examples
217-1-1345
Account
29
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.)

30
Class (Object) Type Variables
  • There are already lots of classes defined in the
    Java API.
  • Lets 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")

31
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.

32
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

33
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"

34
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
35
Lab Exercise String Manipulation
36
Any Enquiry?
Write a Comment
User Comments (0)
About PowerShow.com