1 / 245

Review

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

Télécharger la présentation

Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review • Introduction

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

  3. 1.3 Computer Organization Peripherals

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

  5. 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.)

  6. 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)

  7. Fig. 1.1 | Typical Java development environment.

  8. 1.13 Typical Java Development Environment (Shell Window)

  9. 1.13 Typical Java Development Environment (IDE)

  10. Review • Operators (ch2)

  11. Fig. 2.11 | Arithmetic operators. • Integer division truncates remainder • 7 / 5 evaluates to 1 • Remainder operator % returns the remainder • 7 % 5 evaluates to 2

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

  13. 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 !!!

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

  15. Figure 2.14 Decision Making: Equality and Relational Operators

  16. Fig. 2.5 | Some common escape sequences.

  17. 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.”

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

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

  20. Review • Data Types (ch3)

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

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

  23. Pop quiz – Intro / Operators / data types AreviewQuizHardwareOperatorsDataTypes.doc

  24. Review • Classes and Objects (ch3)

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

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

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

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

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

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

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

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

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

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

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

  36. UML Diagram with Constructors

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

  38. Review • Control Structures (ch4)

  39. 4.4  Control Structures (Cont.) • Selection Statements • if statement • Single-selection statement • if…else statement • Double-selection statement • switch statement • Multiple-selection statement

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

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

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

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

  44. 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)

  45. Fig. 6.5 | Promotions allowed for primitive types.

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

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

  48. Review • Control Structures-2 (ch5)

  49. Fig. 5.3 | for repetition statement

  50. 5.3 for Repetition Statement (Cont.) for ( initialization; loopContinuationCondition; increment ) statement; can usually be rewritten as: initialization;while ( loopContinuationCondition ) {statement;increment;}

More Related