2.51k likes | 3.03k Views
Review. Introduction. 1.2 What Is a Computer?. Computer Performs computations and makes logical decisions Millions / billions times faster than human beings Hardware: physical devices of computer system Computer programs Sets of instructions for which computer processes data
E N D
Review • Introduction
1.2 What Is a Computer? • Computer • Performs computations and makes logical decisions • Millions / billions times faster than human beings • Hardware: physical devices of computer system • Computer programs • Sets of instructions for which computer processes data • Software: programs that run on computers
1.3 Computer Organization Peripherals
1.7 Machine Languages, Assembly Languages and High-Level Languages • Machine language • “Natural language” of computer component • Machine dependent • Assembly language • English-like abbreviations represent computer operations • Translator programs convert to machine language • High-level language • Allows for writing more “English-like” instructions • Contains commonly used mathematical operations • Compiler converts to machine language • Interpreter • Execute high-level language programs without compilation
1.9 History of Java • Objects • Reusable software components that model real-world items • Java • Originally for intelligent consumer-electronic devices • Then used for creating Web pages with dynamic content • Now also used to: • Develop large-scale enterprise applications • Enhance WWW server functionality • Provide applications for consumer devices (cell phones, etc.)
1.10 Java Class Libraries • Classes • Include methods that perform tasks • Return information after task completion • Used to build Java programs • A “blueprint” for creating (instantiating) objects • Java provides class libraries • Known as Java APIs (Application Programming Interfaces)
Review • Operators (ch2)
Fig. 2.11 | Arithmetic operators. • Integer division truncates remainder • 7 / 5 evaluates to 1 • Remainder operator % returns the remainder • 7 % 5 evaluates to 2
2.7 Arithmetic (Cont.) • Operator precedence • Some arithmetic operators act before others (i.e., multiplication before addition) • Use parenthesis when needed • Example: Find the average of three variablesa,b and c • Do not use: a + b + c / 3 • Use: ( a + b + c ) / 3
Fig. 2.12 | Precedence of arithmetic operators. • Example: Find the average of three variablesa,b and c • Do not use: a + b + c / 3 • Use: ( a + b + c ) / 3 • Use parentheses !!!
2.8 Decision Making: Equality and Relational Operators • Condition • Expression can be either true or false • if statement • Simple version in this section, more detail later • If a condition is true, then the body of the if statement executed • Control always resumes after the if statement • Conditions in if statements can be formed using equality or relational operators (next slide)
Figure 2.14 Decision Making: Equality and Relational Operators
3 import java.util.Scanner; // program uses class Scanner 2.5 Import Declarations • Used by compiler to identify and locate classes used in Java programs • All import declarations must appear before the first class declaration in the file. Placing an import declaration inside a class declaration’s body or after a class declaration is a syntax error. • Forgetting to include an import declaration for a class used in your program typically results in a compilation error containing a message such as “cannot resolve symbol.”
Notes on Import Declarations • java.lang is implicitly imported into every program • Default package • Contains classes compiled in the same directory • Implicitly imported into source code of other files in directory • Packages unnecessary if fully-qualified names are used
Software Engineering Observation 3.2 • The Java compiler does not require import declarations in a Java source code file if the fully qualified class name is specified every time a class name is used in the source code. • But most Java programmers consider using fully qualified names to be cumbersome, and instead prefer to use import declarations.
Review • Data Types (ch3)
Primitive Types vs. Reference Types • Types in Java • Primitive • boolean, byte, char, short, int, long, float, double • The AP exam tests int, double, boolean • Reference (sometimes called nonprimitive types) • Objects • Default value of null • Used to invoke an object’s methods
Software Engineering Observation 3.4 • A variable’s declared type (e.g., int, double or some object) indicates whether the variable is of a primitive or a reference type. • If a variable’s type is not one of the eight primitive types, then it is a reference type.
Pop quiz – Intro / Operators / data types AreviewQuizHardwareOperatorsDataTypes.doc
Review • Classes and Objects (ch3)
3.2 Classes, Objects, Methods and Instance Variables • Class provides one or more methods • Method represents task in a program • Describes the mechanisms that actually perform its tasks • Hides from its user the complex tasks that it performs • Method call tells method to perform its task
3.2 Classes, Objects, Methods and Instance Variables (Cont.) • Classes contain one or more attributes • Specified by instance variables. • Carried with the object as it is used • In other words the variable is part of the object when the object is “instantiated”.
3.3 Declaring a Class • Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the .java file-name extension. • keyword public is an access modifier • Class declarations include: • Access modifier • Keyword class • Pair of left and right braces
Instantiating an Object of a Class • Java is extensible • We write classes (“blueprints”). If programmers want to use them in their application, they need to create an object. • Programmers can create or “instantiate” new objects with a particular class blueprint. • Just like declaring variables, we need to declare objects in Java • recall reference data types include objects • Class instance (object) creation expression • Keyword new • Then name of object to create and parentheses
Method Declaration and Call • Keyword public indicates method is available to public • Keyword void indicates no return type • Access modifier, return type, name of method and parentheses comprise method header • Calling a method • Object name, then dot separator (.) • Then method name and parentheses • Method parameters • Additional information passed to a method, comma separated • Supplied in the method call with arguments
UML Class Diagrams • UML class diagrams • Top compartment contains name of the class • Middle compartment contains class’s attributes or instance variables • Bottom compartment contains class’s operations or methods • Plus sign indicates public methods
3.5 Instance Variables, set Methods and get Methods • Variables declared in the body of method • Called local variables • Can only be used within that method • Variables declared in a class declaration • Called fields or instance variables • Each object of the class has a separate instance of the variable
Access Modifiers public and private • private keyword • Used for most instance variables • private variables and methods are accessible only to methods of the class in which they are declared • Declaring instance variables private is known as data hiding (can’t see the variable from an application that uses that object) • Return type • Indicates item returned by method • Declared in method header
Software Engineering Observation 3.3 • Precede every field and method declaration with an access modifier. • As a rule of thumb, instance variables should be declared private and methods should be declared public.
Set (mutator) and get (accessor) methods • private instance variables • Cannot be accessed directly by clients of the object • Use set methods to alter the value – called an mutator or “setter” • Use get methods to retrieve the value – called an accessor or “getter” • Why do have mutators and accessors? • So don’t have to declare everything public (a security violation) • Allows controlled access. May want the user to input a username and password if the application calls a getter.
3.7 Initializing Objects with Constructors • Constructors • Initialize an object of a class • Java requires a constructor for every class • Java will provide a default no-argument constructor if none is provided • Called when keyword new is followed by the class name and parentheses
Pop quiz • Questions: • Identify the access modifier for the GradeBook class. • Identify the name of the instance variable for the GradeBook class • Identify the name of the constructor for the GradeBook class • Identify the name of an accessor in the GradeBook class • Identify the name of a mutator in the GradeBook class • Draw a UML diagram for the GradeBook class • public class GradeBook • { • private String courseName; • public GradeBook( String name ) • { • courseName = name; • } • public void setCourseName( String name ) • { • courseName = name; • } • public String getCourseName() • { • return courseName; • } • public void displayMessage() • { • System.out.printf( “Grade book for • \n%s!\n", getCourseName() ); • } • } //end class GradeBook
Review • Control Structures (ch4)
4.4 Control Structures (Cont.) • Selection Statements • if statement • Single-selection statement • if…else statement • Double-selection statement • switch statement • Multiple-selection statement
4.4 Control Structures (Cont.) • Repetition statements • Also known as looping statements • Repeatedly performs an action while its loop-continuation condition remains true • while statement • Performs the actions in its body zero or more times • do…while statement • Performs the actions in its body one or more times • for statement • Performs the actions in its body zero or more times
4.5 if Statements • if statements (single selection) • Execute an action if the specified condition is true • if…else statement (double selection) • Executes one action if the specified condition is true or a different action if the specified condition is false • Conditional Operator ( ?: ) • Compact alternative
Good Programming Practice 4.4 • Always using braces in an if...else (or other) statement helps prevent their accidental omission, especially when adding statements to the if-part or the else-part at a later time. • To avoid omitting one or both of the braces, some programmers type the beginning and ending braces of blocks before typing the individual statements within the braces.
4.7 while Repetition Statement • while statement • Repeats an action while its loop-continuation condition remains true • Use a counter variable to count the number of times a loop is iterated • Or use Sentinel-controlled repetition • Also known as indefinite repetition • Sentinel value also known as a signal, dummy, flag, termination value • Uses a merge symbol in its UML activity diagram
4.8 Casting • Unary cast operator • Creates a temporary copy of its operand with a different data type • example: (double) will create a temporary floating-point copy of its operand • Converting values to lower types results in a compilation error, unless the programmer explicitly forces the conversion to occur • Place the desired data type in parentheses before the value • example: (int)4.5 • Promotion • Converting a value (e.g. int) to another data type (e.g. double) to perform a calculation • Values in an expression are promoted to the “highest” type in the expression (a temporary copy of the value is made)
4.11 Compound Assignment Operators • Compound assignment operators example: c=c+3; can be written as c+=3; • This statement adds 3 to the value in variable c and stores the result in variable cs
4.12 Increment and Decrement Operators • Unary increment and decrement operators • Unary increment operator (++) adds one to its operand • Unary decrement operator (--) subtracts one from its operand
Review • Control Structures-2 (ch5)
5.3 for Repetition Statement (Cont.) for ( initialization; loopContinuationCondition; increment ) statement; can usually be rewritten as: initialization;while ( loopContinuationCondition ) {statement;increment;}