Review - PowerPoint PPT Presentation

1 / 87
About This Presentation
Title:

Review

Description:

... the program is using. the results of intermediate calculations. usually measured in megabytes (e.g. 256 megabytes of RAM) RAM is short for random access memory ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 88
Provided by: rober860
Category:

less

Transcript and Presenter's Notes

Title: Review


1
Review
2
Memory
  • Memory holds
  • programs
  • data for the computer to process
  • the results of intermediate processing.
  • two kinds of memory
  • main memory
  • auxiliary memory

3
Main memory
  • working memory used to store
  • the current program
  • the data the program is using
  • the results of intermediate calculations
  • usually measured in megabytes (e.g. 256 megabytes
    of RAM)
  • RAM is short for random access memory
  • a byte is a quantity of memory

4
Auxiliary Memory
  • also called secondary memory
  • disk drives, diskettes, CDs, etc.
  • more or less permanent (nonvolatile)
  • usually measured in gigabytes (e.g. 50 gigabyte
    hard drive)

5
Bits, Bytes, and Addresses
  • A bit is a digit with a value of either 0 or 1.
  • A byte consists of 8 bits.
  • Each byte in main memory resides at a numbered
    location called its address.

6
Addresses
7
Storing Data
  • Data of all kinds (numbers, letters, strings of
    characters, audio, video, even programs) are
    encoded and stored using 1s and 0s.
  • When more than a single byte is needed, several
    adjacent bytes are used.
  • The address of the first byte is the address of
    the unit of bytes.

8
Programming Languages
  • High-level languages are relatively to write and
    to understand.
  • Java, Pascal, FORTRAN, C, C, BASIC, Visual
    Basic, etc.
  • Unfortunately, computer hardware does not
    understand high-level languages.
  • Therefore, a high-level language program must be
    translated into a low-level language.

9
Compilers
  • A compiler translates a program from a high-level
    language to a low-level language the computer can
    run.
  • You compile a program by running the compiler on
    the high-level-language version of the program
    called the source program.
  • Compilers produce machine- or assembly-language
    programs called object programs.

10
Compilers, cont.
  • Most high-level languages need a different
    compiler for each type of computer and for each
    operating system.
  • Most compilers are very large programs that are
    expensive to produce.

11
Java Byte-Code
  • The Java compiler does not translate a Java
    program into assembly language or machine
    language for a particular computer.
  • Instead, it translates a Java program into
    byte-code.
  • Byte-code is the machine language for a
    hypothetical computer (or interpreter) called the
    Java Virtual Machine.

12
Java Byte-Code, cont.
  • A byte-code program is easy to translate into
    machine language for any particular computer.
  • A program called an interpreter translates each
    byte-code instruction, executing the resulting
    machine-language instructions on the particular
    computer before translating the next byte-code
    instruction.

13
Compiling, Interpreting, Running
  • Use the compiler to translate the Java program
    into byte-code (done using the compile command).
  • Use the byte-code interpreter for your computer
    to translate each byte-code instruction into
    machine language and to run the resulting
    machine-language instructions (done using the run
    command).

14
Portability
  • After compiling a Java program into byte-code,
    that byte-code can be used on any computer with a
    byte-code interpreter and without a need to
    recompile.
  • Byte-code can be sent over the Internet and used
    anywhere in the world.
  • This makes Java suitable for Internet
    applications.

15
(No Transcript)
16
Class Loader
  • A Java program typically consists of several
    pieces called classes.
  • Each class may have a separate author and each is
    compiled (translated into byte-code) separately.
  • A class loader (called a linker in other
    programming languages) automatically connects the
    classes together.

17
Object-Oriented Programming
  • Our world consists of objects (people, trees,
    cars, cities, airline reservations, etc.).
  • Objects can perform actions which effect
    themselves and other objects in the world.
  • Object-oriented programming (OOP) treats a
    program as a collection of objects that interact
    by means of actions.

18
OOP Terminology
  • Objects, appropriately, are called objects.
  • Actions are called methods.
  • Objects of the same kind have the same type and
    belong to the same class.
  • Objects within a class have a common set of
    methods and the same kinds of data
  • but each object can have its own data values.

19
OOP Design Principles
  • OOP adheres to three primary design principles
  • encapsulation
  • polymorphism
  • inheritance.

20
Errors
  • An error in a program is called a bug.
  • Eliminating errors is called debugging.
  • three kinds or errors
  • syntax errors
  • runtime errors
  • logic errors

21
Syntax Errors
  • grammatical mistakes in a program
  • the grammatical rules for writing a program are
    very strict
  • The compiler catches syntax errors and prints an
    error message.
  • example using a period where a program expects a
    comma

22
Runtime Errors
  • errors that are detected when your program is
    running, but not during compilation
  • When the computer detects an error, it terminates
    the program an prints an error message.
  • example attempting to divide by 0

23
Logic Errors
  • errors that are not detected during compilation
    or while running, but which cause the program to
    produce incorrect results
  • example an attempt to calculate a Fahrenheit
    temperature from a Celsius temperature by
    multiplying by 9/5 and adding 23 instead of 32

24
Compiling a Java Program or Class
  • A Java program consists of one or more classes,
    which must be compiled before running the
    program.
  • You need not compile classes that accompany Java
    (e.g. System and Scanner).
  • Each class should be in a separate file.
  • The name of the file should be the same as the
    name of the class.

25
Compiling and Running
  • Use an IDE (integrated development environment)
    which combines a text editor with commands for
    compiling and running Java programs.
  • When a Java program is compiled, the byte-code
    version of the program has the same name, but the
    ending is changed from .java to .class.

26
Compiling and Running, cont.
  • A Java program can involve any number of classes.
  • The class to run will contain the words
  • public static void main(String args)
  • near the beginning of the file.

27
Primitive Types
  • four integer types (byte, short, int, and long)
  • int is most common
  • two floating-point types (float and double)
  • double is more common
  • one character type (char)
  • one boolean type (boolean)

28
Primitive Types, cont.
29
Assignment Statements
  • An assignment statement is used to assign a value
    to a variable.
  • answer 42
  • The equal sign is called the assignment
    operator.
  • We say, The variable named answer is assigned a
    value of 42, or more simply, answer is assigned
    42.

30
Assignment Statements, cont.
  • Syntax
  • variable expression
  • where expression can be another variable, a
    literal or constant (such as a number), or
    something more complicated which combines
    variables and literals using operators (such as
    and -)

31
Simple Screen Output
  • System.out.println(The count is count)
  • outputs the string literal The count is
    followed by the current value of the variable
    count.

32
Simple Input
  • Sometimes the data needed for a computation are
    obtained from the user at run time.
  • Keyboard input requires
  • import java.util.
  • at the beginning of the file.

33
Simple Input, cont.
  • Data can be entered from the keyboard
    using Scanner keyboard
  • new Scanner(System.in)
  • followed, for example, by
  • eggsPerBasket keyboard.nextInt()
  • which reads one int value from the keyboard and
    assigns it to eggsPerBasket.

34
Assignment Compatibilities
  • A value of one type can be assigned to a variable
    of any type further to the right
  • byte --gt short --gt int --gt long
  • --gt float --gt double
  • but not to a variable of any type further to the
    left.
  • You can assign a value of type char to a variable
    of type int.

35
Type Casting
  • A type cast temporarily changes the value of a
    variable from the declared type to some other
    type.
  • For example,
  • double distance
  • distance 9.0
  • int points
  • points (int)distance
  • (illegal without (int))

36
The Division Operator
  • The division operator (/) behaves as expected if
    one of the operands is a floating-point type.
  • When both operands are integer types, the result
    is truncated, not rounded.
  • Hence, 99/100 has a value of 0.

37
The mod Operator
  • The mod () operator is used with operators of
    integer type to obtain the remainder after
    integer division.
  • 14 divided by 4 is 3 with a remainder of 2.
  • Hence, 14 4 is equal to 2.
  • The mod operator has many uses, including
  • determining if an integer is odd or even
  • determining if one integer is evenly divisible by
    another integer.

38
Concatenation of Strings
  • Two strings are concatenated using the
    operator.
  • String greeting Hello
  • String sentence
  • sentence greeting officer
  • System.out.println(sentence)
  • Any number of strings can be concatenated using
    the operator.

39
Classes
  • A class is a type used to produce objects.
  • An object is an entity that stores data and can
    take actions defined by methods.
  • An object of the String class stores data
    consisting of a sequence of characters.
  • The length() method returns the number of
    characters in a particular String object.
  • int howMany solution.length()

40
Objects, Methods, and Data
  • Objects within a class
  • have the same methods
  • have the same kind(s) of data but the data can
    have different values.
  • Primitive types have values, but no methods.

41
Screen Output
  • Weve seen several examples of screen output
    already.
  • System.out is an object that is part of Java.
  • println() is one of the methods available to the
    System.out object.

42
Keyboard Input
  • Java 5.0 has reasonable facilities for handling
    keyboard input.
  • These facilities are provided by the Scanner
    class in the java.util package.
  • A package is a library of classes.

43
Using the Scanner Class
  • Near the beginning of your program, insert
  • import java.util.
  • Create an object of the Scanner class
  • Scanner keyboard
  • new Scanner (System.in)
  • Read data (an int or a double, for example)
  • int n1 keyboard.nextInt()
  • double d1 keyboard,nextDouble()

44
The if-else Statement
  • A branching statement that chooses between two
    possible actions.
  • syntax
  • if (Boolean_Expression)
  • Statement_1
  • else
  • Statement_2

45
The if-else Statement, cont.
  • example
  • if (count lt 3)
  • total 0
  • else
  • total total count

46
Compound Boolean Expressions
  • Boolean expressions can be combined using the
    and () operator.
  • example
  • if ((score gt 0) (score lt 100))
  • ...
  • not allowed
  • if (0 lt score lt 100)
  • ...

47
Compound Boolean Expressions, cont.
  • Boolean expressions can be combined using the
    or () operator.
  • example
  • if ((quantity gt 5) (cost lt 10))
  • ...
  • syntax
  • (Sub_Expression_1) (Sub_Expression_2)

48
Negating a Boolean Expression
  • A boolean expression can be negated using the
    not (!) operator.
  • syntax
  • !(Boolean_Expression)
  • example
  • (a b) !(a b)
  • which is the exclusive or

49
Using
  • is appropriate for determining if two integers
    or characters have the same value.
  • if (a 3)
  • where a is an integer type
  • is not appropriate for determining if two
    floating points values are equal. Use lt and
    some appropriate tolerance instead.
  • if (abs(b - c) lt epsilon)
  • where b, c, and epsilon are floating point types

50
Using , cont.
  • is not appropriate for determining if two
    objects have the same value.
  • if (s1 s2), where s1 and s2 refer to strings,
    determines only if s1 and s2 refer a common
    memory location.
  • If s1 and s2 refer to strings with identical
    sequences of characters, but stored in different
    memory locations, (s1 s2) is false.

51
Using , cont.
  • To test the equality of objects of class String,
    use method equals.
  • s1.equals(s2)
  • or
  • s2.equals(s1)
  • To test for equality ignoring case, use method
    equalsIgnoreCase.
  • (Hello.equalsIgnoreCase(hello))

52
equals and equalsIgnoreCase
  • syntax
  • String.equals(Other_String)
  • String.equalsIgnoreCase(Other_String)

53
The switch Statement
  • The switch statement is a multiple branch that
    makes a decision based on an integral (integer or
    character) expression.
  • The switch statement begins with the keyword
    switch followed by an integral expression in
    parentheses and called the controlling expression.

54
The switch Statement, cont.
  • A list of cases follows, enclosed in braces.
  • Each case consists of the keyword case followed
    by
  • a constant called the case label
  • a colon
  • a list of statements.
  • The list is searched for a case label matching
    the controlling expression.

55
The switch Statement, cont.
  • The action associated with a matching case label
    is executed.
  • If no match is found, the case labeled default is
    executed.
  • The default case is optional, but recommended,
    even if it simply prints a message.
  • Repeated case labels are not allowed.

56
The switch Statement, cont.
  • The action for each case typically ends with the
    word break.
  • The optional break statement prevents the
    consideration of other cases.
  • The controlling expression can be anything that
    evaluates to an integral type.

57
The switch Statement, cont.
  • syntax
  • switch (Controlling_Expression)
  • case Case_Label
  • Statement(s)
  • break
  • case Case_Label
  • default

58
the while Statement
  • also called a while loop
  • A while statement repeats until a controlling
    boolean expression becomes false.
  • If the controlling boolean expression is false
    initially, the while loop is not executed.
  • The loop body typically contains a statement that
    ultimately causes the controlling boolean
    expression to become false.

59
the while Statement, cont.
  • syntax
  • while (Boolean_Expression)
  • Body_Statement
  • or
  • while (Boolean_Expression)
  • First_Statement
  • Second_Statement

60
The do-while Statement
  • also called a do-while loop
  • similar to a while statement, except that the
    loop body is executed at least once
  • syntax
  • do
  • Body_Statement
  • while (Boolean_Expression)
  • dont forget the semicolon!

61
The do-while Statement, cont.
  • First, the loop body is executed.
  • Then the boolean expression is checked.
  • As long as it is true, the loop is executed
    again.
  • If it is false, the loop is exited.
  • equivalent while statement
  • Statement(s)_S1
  • while (Boolean_Condition)
  • Statement(s)_S1

62
The for Statement
  • A for statement executes the body of a loop a
    fixed number of times.
  • example
  • for (count 1 count lt 3 count)
  • System.out.println(count)
  • System.out.println(Done)

63
The for Statement, cont.
  • syntax
  • for (Initialization, Condition, Update)
  • Body_Statement
  • Body_Statement can be either a simple statement
    or a compound statement in .
  • corresponding while statement
  • Initialization
  • while (Condition)
  • Body_Statement_Including_Update

64
The break Statement in Loops
  • A break statement can be used to end a loop
    immediately.
  • The break statement ends only the innermost loop
    or switch statement that contains the break
    statement.
  • break statements make loops more difficult to
    understand.
  • Use break statements sparingly (if ever).

65
The break Statement in Loops
  • A break statement can be used to end a loop
    immediately.
  • The break statement ends only the innermost loop
    or switch statement that contains the break
    statement.
  • break statements make loops more difficult to
    understand.
  • Use break statements sparingly (if ever).

66
Basic Terminology
  • Objects can represent almost anything.
  • A class defines a kind of object.
  • It specifies the kinds of data an object of the
    class can have.
  • It provides methods specifying the actions an
    object of the class can take.
  • An object satisfying the class definition
    instantiates the class and is an instance of the
    class.

67
Basic Terminology, cont.
  • The data items and the methods are referred to as
    members of the class.
  • We will call the data items associated with an
    object the instance variables of that object
    (i.e. that instance of the class).

68
Method Definitions
  • All method definitions belong to some class.
  • All method definitions are given inside the
    definition of the class to which they belong.
  • If the definition of the method begins with
    public void, it does not return a value.
  • public indicates that use is unrestricted.
  • void indicates that the method does not return a
    value.

69
Method Definitions, cont.
  • The parentheses following the method name contain
    any information the method needs.
  • The first part of the method definition is called
    the heading.
  • The remainder of the method is called the body,
    and is enclosed in braces .
  • Statements or declarations are placed in the body.

70
Method Definitions, cont.
  • The parentheses following the method name contain
    any information the method needs.
  • The first part of the method definition is called
    the heading.
  • The remainder of the method is called the body,
    and is enclosed in braces .
  • Statements or declarations are placed in the body.

71
Local Variables
  • A variable declared within a method is called a
    local variable.
  • Its meaning is local to (confined to) the
    method definition.
  • Variables with the same name declared within
    different methods are different variables.
  • A local variable exists only as long as the
    method is active.

72
The public and private Modifiers
  • The instance variables of a class should not be
    declared public.
  • Typically, instance variables are declared
    private.
  • An instance variable declared public can be
    accessed and changed directly, with potentially
    serious integrity consequences.
  • Declaring an instance variable private protects
    its integrity.

73
The private Modifier
  • The private modifier makes an instance variable
    inaccessible outside the class definition.
  • But within the class definition, the instance
    variable remains accessible and changeable.
  • This means that the instance variable can be
    accessed and changed only via the methods
    accompanying the class.

74
Accessor and Mutator Methods
  • Appropriate access to an instance variable
    declared private is provided by an accessor
    method which is declared public.
  • Typically, accessor methods begin with the word
    get, as in getName.
  • Mutator methods should be written to guard
    against inappropriate changes.

75
Static Methods and Static Variables, cont.
  • Static methods and static variables belong to a
    class and do not require any object.

76
Static Methods
  • Some methods have no meaningful connection to an
    object. For example,
  • finding the maximum of two integers
  • computing a square root
  • converting a letter from lowercase to uppercase
  • generating a random number
  • Such methods can be defined as static.

77
Static Methods, cont.
  • A static method is still defined as a member of a
    class.
  • But, the method is invoked using the class name
    rather than an object name.
  • syntax
  • return_Type Variable_Name
  • Class_Name.Static_Method_Name
  • (Parameters)

78
Putting main in Any Class
  • A class which contains a method main serves two
    purposes
  • It can be run as a program.
  • It can be used to create objects for other
    classes.

79
Putting main in Any Class, cont.
  • A programs main method must be static.
  • A nonstatic method in the same class cannot be
    invoked unless an object of the class is created
    and used as a calling object for the nonstatic
    method.
  • In general, dont provide a method main in a
    class definition if the class will be used only
    to create objects.

80
The Math Class
  • The predefined class Math provides several
    standard mathematical methods.
  • All of these methods are static methods.
  • You do not need to create an object to call the
    methods of the Math class.
  • These methods are called by using the class name
    (Math) followed by a dot and a method name.
  • Return_Value Math.Method_Name(Parameters)

81
The Math Class, cont.
  • Method round returns a number as the nearest
    whole number.
  • If its argument is of type double, it returns a
    whole number of type long.
  • Method floor (ceil) returns the largest
    (smallest) whole number that is less (greater)
    than or equal to its argument.

82
Overloading
  • Weve seen that different classes can have
    methods with the same names.
  • Two or more methods in the same class class can
    be defined with the same name if the parameter
    list can be used to determine which method is
    being invoked.
  • This useful ability is called overloading.

83
Overloading, cont.
  • The number of arguments and the types of the
    arguments determines which method average is
    invoked.
  • If there is no match, Java attempts simple type
    conversions of the kinds discussed earlier.
  • If there is still no match, an error message is
    produced.

84
Constructors
  • When you create an object of a class, often you
    want certain initializing actions performed such
    as giving values to the instance variables.
  • A constructor is a special method that performs
    initializations.

85
Defining Constructors
  • New objects are created using
  • Class_Name Object_Name
  • new Class_Name (Parameter(s))
  • A constructor is called automatically when a new
    object is created.
  • Class_Name (Parameter(s)calls the constructor and
    returns a reference.
  • It performs any actions written into its
    definition including initializing the values of
    (usually all) instance variables.

86
Defining Constructors, cont.
  • Each constructor has the same name as its class.
  • A constructor does not have a return type, not
    even void.
  • Constructors often are overloaded, each with a
    different number of parameters or different types
    of parameters.
  • Typically, at least one constructor, the default
    constructor, has no parameters.

87
Defining Constructors, cont.
  • When a class definition does not have a
    constructor definition, Java creates a default
    constructor automatically.
  • Once you define at least one constructor for the
    class, no additional constructor is created
    automatically.
Write a Comment
User Comments (0)
About PowerShow.com