Today's topics - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Today's topics

Description:

... are not part of the Java language per se, but we rely ... Java's conditional statements are. the if statement. the if-else statement. the switch statement ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 34
Provided by: paul313
Category:
Tags: javas | today | topics

less

Transcript and Presenter's Notes

Title: Today's topics


1
Today's topics
  • Java libraries
  • Using class methods (dynamic, static)
  • Generating "random" numbers
  • Math methods
  • Formatting numeric output
  • Very brief introduction to applets
  • Java control structures (part 1)
  • Conditional execution decision structures

2
Class Libraries
  • A class library is a collection of classes that
    we can use when developing programs
  • The Java standard class library is part of any
    Java development environment
  • Its classes are not part of the Java language per
    se, but we rely on them heavily
  • The System class and the String class are part of
    the Java standard class library
  • Other class libraries can be obtained through
    third party vendors, or you can create them
    yourself

3
Packages
  • The classes of the Java standard class library
    are organized into packages
  • Some of the packages in the standard class
    library are

4
The import Declaration
  • When you want to use a class from a package, you
    could use its fully qualified name, e.g.
  • java.util.Random generator new
    java.util.Random()
  • Or you can import the class, and then use just
    the class name
  • import java.util.Random
  • //...
  • Random generator new Random()
  • To import all classes in a particular package,
    you can use the "wildcard character",
  • import java.util.

5
The import Declaration
  • All classes of the java.lang package are imported
    automatically into all programs.
  • That's why we didn't have to import the System or
    String classes explicitly in earlier programs

6
Invoking Methods
  • Methods are defined in the library definition of
    the class.
  • Typically, a method is invoked by an object
    reference.
  • import java.util.Random
  • //...
  • Random generator new Random()
  • int num1 generator.nextInt()
  • int num2 generator.nextInt(10)
  • Dot notation
  • Methods require (), even if there is no
    parameter.
  • float num3 generator.nextFloat()

7
Class Methods
  • Some methods can be invoked through the class
    name, instead of through an object of the class
  • These methods are called class methods or static
    methods
  • The Math class contains many static methods,
    providing various mathematical functions, such as
    absolute value, trigonometry functions, square
    root, etc.
  • temp Math.cos(90) Math.sqrt(delta)

8
Name conflicts
  • What happens when two imported packages have
    classes with the same name?
  • use the fully qualified names.

9
The Keyboard Class
  • The Keyboard class is NOT part of the Java
    standard class library.
  • It is provided by the authors of the textbook to
    make reading input from the keyboard easy.
  • is part of a package called cs1.
  • contains several static methods for reading
    particular types of data.
  • Try some experiments with the Keyboard class

10
Formatting Output
  • The NumberFormat class has static methods that
    return a formatter object
  • double cost 2.59
  • NumberFormat money NumberFormat.getCurrencyInsta
    nce()
  • System.out.println(money.format(cost))
  • Each formatter object has a method called format
    that returns a String with the specified
    information in the appropriate format

This one displays 2.59
11
Formatting Output
  • The DecimalFormat class can be used to format a
    floating point value in generic ways
  • The constructor of the DecimalFormat class takes
    a String that represents a pattern for the
    formatted number

12
Formatting Output
  • For example, you can specify that the number
    should be printed to three decimal places
  • double sideA 14.25681, sideB 1/5.0
  • DecimalFormat myFormat new DecimalFormat(0.
    )
  • System.out.println(myFormat.format(sideA))
  • System.out.println(myFormat.format(sideB))

Displays 14.257 Rounds the value, and uses as
many places as necessary to the left of the
decimal point.
Displays 0.2 Drops trailing zeroes
13
Questions on formatting, etc., ?
  • before we move on to a little bit about Applets

14
Applets
  • A Java application is a stand-alone program with
    a main method (like the ones we've seen so far).
  • A Java applet is a program that is intended to be
    accessed and executed using a web browser
  • An applet also can be executed using the
    appletviewer tool of the Java Software
    Development Kit
  • An applet doesn't have a main method. Instead,
    there are several special methods that serve
    specific purposes

15
Applets
  • The class that defines an applet extends the
    Applet class.
  • "extends" means "inherits from"
  • more later about inheritance
  • An applet is embedded into an HTML file using a
    tag that references the bytecode file of the
    applet class.
  • The bytecode version of the program is
    transported across the web and executed by a Java
    interpreter that is part of the browser.

16
The HTML applet Tag
lthtmlgt ltheadgt lttitlegtThe Einstein
Appletlt/titlegt lt/headgt ltbodygt
ltapplet code"Einstein.class" width350
height175gt lt/appletgt lt/bodygt lt/htmlgt
17
Much more on applets and graphics later
  • Questions on applets before we move on to
    conditional execution?

18
Conditional execution
  • Decision structures

19
Conditional Statements
  • A conditional statement lets us choose which
    statement will be executed next
  • sometimes called selection statements
  • Conditional statements permit basic
    decision-making
  • Java's conditional statements are
  • the if statement
  • the if-else statement
  • the switch statement

20
The if Statement
  • The if statement has the following syntax

if ( condition ) statement
21
The if Statement
  • An example of an if statement

if (sum gt MAX) delta sum -
MAX System.out.println ("The sum is " sum)
First, the condition is evaluated. The value of
sum is either greater than the value of MAX, or
it is not.
If the condition is true, the assignment
statement is executed. If it is false, the
assignment statement is skipped.
Either way, the call to println is executed next.
22
Logic of an if statement
23
Boolean Expressions
  • A condition often uses one of Java's equality
    operators or relational operators, which all
    return boolean results
  • equal to
  • ! not equal to
  • lt less than
  • gt greater than
  • lt less than or equal to
  • gt greater than or equal to
  • Note the difference between the equality operator
    () and the assignment operator ()

24
The if-else Statement
  • An else clause can be added to an if statement to
    make an if-else statement

if ( condition ) statement1 else
statement2
  • If the condition is true, statement1 is executed
    if the condition is false, statement2 is
    executed
  • One or the other will be executed, but not both

25
Logic of an if-else statement
26
Block Statements
  • Several statements can be grouped together into a
    block statement
  • A block is delimited by braces
  • A block statement can be used wherever a
    statement is called for by the Java syntax
  • For example, in an if-else statement, the if
    portion, or the else portion, or both, could be
    block statements

27
Nested if Statements
  • The statement executed as a result of an if
    statement or else clause could be another if
    statement
  • These are called nested if statements
  • An else clause is matched to the last unmatched
    if (no matter what the indentation implies)
  • Braces can be used to specify the if statement to
    which an else clause belongs.

28
The switch Statement
  • The switch statement provides another means to
    decide which statement to execute next
  • The switch statement evaluates an expression,
    then attempts to match the result to one of
    several possible cases
  • Each case contains a value and a list of
    statements
  • The flow of control transfers to statement
    associated with the first value that matches

29
The switch Statement
  • The general syntax of a switch statement is

switch ( expression ) case value1
statement-list1 case value2
statement-list2 case value3
statement-list3 default ... statement-list4

30
The switch Statement
  • Often a break statement is used as the last
    statement in each case's statement list
  • A break statement causes control to transfer to
    the end of the switch statement
  • If a break statement is not used, the flow of
    control will continue into the next case
  • Sometimes this can be appropriate, but usually we
    want to execute only the statements associated
    with one case

31
The switch Statement
  • A switch statement can have an optional default
    case
  • The default case has no associated value and
    simply uses the reserved word default
  • If the default case is present, control will
    transfer to it if no other case value matches
  • Though the default case can be positioned
    anywhere in the switch, usually it is placed at
    the end
  • If there is no default case, and no other value
    matches, control falls through to the statement
    after the switch

32
The switch Statement
  • The expression of a switch statement must result
    in an integral type, meaning an int or a char
  • It cannot be a boolean value, a floating point
    value (float or double), a byte, a short, or a
    long
  • The implicit boolean condition in a switch
    statement is equality - it tries to match the
    expression with a value
  • You cannot perform relational checks with a
    switch statement

33
Questions?
Write a Comment
User Comments (0)
About PowerShow.com