Review for exam 1 CS 101 Aaron Bloomfield

  • Slides: 38
Download presentation
Review for exam 1 CS 101 Aaron Bloomfield 1

Review for exam 1 CS 101 Aaron Bloomfield 1

Today’s lecture An overview of the “review” sections of chapters 1 -3 Stop me

Today’s lecture An overview of the “review” sections of chapters 1 -3 Stop me if you want me to go over something in more detail! 2

Material you may not be comfortable with… Constructors n I know there is a

Material you may not be comfortable with… Constructors n I know there is a lot of confusion about them Static vs. non-static n We haven’t gotten to that yet Being able to create your own class n n But you should be able to follow along when creating a class is explained For HW J 3, creating the Line class is explained in steps Why you call mutator methods instead of setting the fields directly n Just know that it’s a good idea to do so Why instance variables are supposed to be private n Again, know that it’s a good idea to do so 3

Chapter 1: Intro Computers think in bits (1 or 0) n 00101001 = 81

Chapter 1: Intro Computers think in bits (1 or 0) n 00101001 = 81 Eight bits per byte n n n 1024 bytes = 1 Kb 1024 Kb = 1 Mb 1024 Mb = 1 Gb 4

Chapter 1: Computer organization CPU = brain Microprocessor: computer on a single chip Network:

Chapter 1: Computer organization CPU = brain Microprocessor: computer on a single chip Network: when two or more computers are plugged into one another 5

Chapter 1: Software OS: the program that manages a computer’s resources Program: a sequence

Chapter 1: Software OS: the program that manages a computer’s resources Program: a sequence of instructions that performs some task n Performing an instruction is called “executing” an instruction 6

Chapter 1: Compilation Translator: translates a program from one language to another Machine language:

Chapter 1: Compilation Translator: translates a program from one language to another Machine language: the ones and zeros that a computer understands n A low level language! Compiler: a translator which typically translates a highlevel language into a low-level one n Java is a high-level language Java’s compiler translates Java code into bytecode n Bytecode is like machine language, but not tied to a specific machine A Java bytecode interpreter is used to execute the bytecode n Called a Java Virtual Machine (JVM) 7

Chapter 1: Terminology Abstraction n Similar objects exhibit similar behavior The ability to do

Chapter 1: Terminology Abstraction n Similar objects exhibit similar behavior The ability to do the same “thing” on many objects Consider car driving example Encapsulation n Not revealing how the method does it’s work Consider String. substring() Consider the car radio example Modularity n n Dividing code into smaller pieces (modules), each one of which is easier to code Consider the car radio example 8

Chapter 1: OOP stuff OOP languages: n n n Abstract things into the class’

Chapter 1: OOP stuff OOP languages: n n n Abstract things into the class’ methods Encapsulate code inside the class’ methods Use additional method for modularity A (primitive) type is the basic unit of storage in Java n A type is a template for a variable A class is composed of types (or other classes) as well as methods n A class is a template for an object Creating a variable/object from a type/class is called instantiating the type/class 9

Chapter 1: Problem solving steps Analysis n What needs to be done? Design n

Chapter 1: Problem solving steps Analysis n What needs to be done? Design n How is it going to be done? Implementation n Make it so! Testing n Does it work correctly? 10

Quick survey n a) b) c) d) I feel comfortable with the material in

Quick survey n a) b) c) d) I feel comfortable with the material in chapter 1 Very well With some review, I’ll be good Not really Not at all 11

Chapter 2: Readable programs Comments are a English text n Have a // before

Chapter 2: Readable programs Comments are a English text n Have a // before them in a Java file Blank lines make a program easier to read Indentation helps humans identify which code is within the method Keywords have special meanings in Java n Examples: int, double, class, static, public 12

Chapter 2: Identifiers: programmer-defined names n n For classes, variables, methods, etc. Cannot be

Chapter 2: Identifiers: programmer-defined names n n For classes, variables, methods, etc. Cannot be a keyword Must start with a letter (or _ or $) Can contain numbers also (but not as the first character) Good identifiers: radius, width, position Bad identifiers: x, y, q, the_really_long_variable_name_hi_mom Java default: the. Really. Long. Variable. Name 13

Chapter 2: Computer bugs A bug is an error in the program To debug

Chapter 2: Computer bugs A bug is an error in the program To debug is to remove bugs 14

An optical illusion 15

An optical illusion 15

Chapter 2: Java classes The class keyword is used to start a class declaration

Chapter 2: Java classes The class keyword is used to start a class declaration n Can be made public (for this course, always do so) A class is a “template” for an object n Just as a type is a “template” for a variable 16

Chapter 2: Java methods All methods have the following syntax: modifers type name (

Chapter 2: Java methods All methods have the following syntax: modifers type name ( parameters ) { statements } Properties of the method Type that it returns public static void A name for the method main Any number The body of (including zero) the method of parameters (can be empty) (String[] args) {. . . } 17

Chapter 2: Program execution Java starts executing a program at the beginning of the

Chapter 2: Program execution Java starts executing a program at the beginning of the main() method Braces { } are used to specify where a method begins and ends A statement ends when a semicolon is encountered n A statement can span multiple lines 18

Chapter 2: Misc stuff A literal character string is a sequence of characters enclosed

Chapter 2: Misc stuff A literal character string is a sequence of characters enclosed by double quotes System is the Java class that allows you to access parts of the computer system n n System. in: access to the keyboard System. out: access to the monitor Period is used for selection: Math. round n Given String s, select a method via: s. substring() An exception is when Java “panics” n It means something is wrong 19

Chapter 2: Escape sequences Java provides escape sequences for printing special characters n n

Chapter 2: Escape sequences Java provides escape sequences for printing special characters n n n n b n t r \ " ‘ backspace newline tab carriage return backslash double quote single quote 20

Chapter 2: Primitive variable types Java has 8 (or so) primitive types: n n

Chapter 2: Primitive variable types Java has 8 (or so) primitive types: n n n n float double boolean char byte short int long real numbers two values: true and false a single character integer numbers Also the void “type” Can make a variable final 21

Chapter 2: Symbolic names vs. literal values Which is easier to enter: n n

Chapter 2: Symbolic names vs. literal values Which is easier to enter: n n Math. PI 3. 141592653589793 Entering a symbolic name (i. e. a constant) reduces chances of errors It allows for changing the constant later on Are usually final 22

Chapter 2: References and variables A variable is an actual spot in memory that

Chapter 2: References and variables A variable is an actual spot in memory that holds a (primitive type) value A reference is a memory address that points to another spot in memory where the object is Variables defined in a class are initialized to a default value Variables defined in a method are not initialized to a default value 23

Chapter 2: Math Standard operators: + - * / Note that / can be

Chapter 2: Math Standard operators: + - * / Note that / can be either integer division or floating-point division % computes the remainder Can provide numbers in decimal or scientific notation 24

Chapter 2: Expressions Evaluating an expression yields a result and a type n n

Chapter 2: Expressions Evaluating an expression yields a result and a type n n Example: 4/3 yields 1 of type int Example: 3. 5*2. 0 yields 7. 0 of type double Binary operator has two operands n n Example: 3+4, 6*3, etc. Left one is evaluated first Unary operator has one operand n Example: -3, etc. Operators have precedence n For example, * and / are evaluated before + and 25

Chapter 2: Overflow Consider: byte b = 100; b = b * 100; A

Chapter 2: Overflow Consider: byte b = 100; b = b * 100; A byte can only hold up to +127 This is called overflow n Java does not tell you that this happened! Underflow: b -= b*100; 26

Chapter 2: Operators Assignment: = Increment (++) and decrement (--) Consider: int i =

Chapter 2: Operators Assignment: = Increment (++) and decrement (--) Consider: int i = 5; System. out. println (i++); System. out. println (++i); System. out. println (i); There are 4 ways to add 1 to an int: i = i + 1; i += 1; i++; ++i There are many such compound operators 27

Chapter 2: Casting converts one type to another Example: int x = 1; System.

Chapter 2: Casting converts one type to another Example: int x = 1; System. out. println ((double) x); double d = 3. 4; System. out. println ((int) d); 28

Chapter 2: Scanner class Creating one: Scanner stdin = new Scanner (System. in) n

Chapter 2: Scanner class Creating one: Scanner stdin = new Scanner (System. in) n Don’t use Scanner. create()! Methods: n n n n public int next. Int() public short next. Short() public long next. Long() public double next. Double() public float next. Float() public String next. Line() public boolean has. Next() 29

Quick survey n a) b) c) d) I feel comfortable with the material in

Quick survey n a) b) c) d) I feel comfortable with the material in chapter 2 Very well With some review, I’ll be good Not really Not at all 30

Chapter 3: Intro An object variable is really a reference to that object null

Chapter 3: Intro An object variable is really a reference to that object null represents an object variable that points to nothing Once nothing points to an object, Java automatically deletes that object n Called garbage collection A final object variable: n Only the reference (where it points in memory) is final n The values in the object can change via member methods We use constructors to create objects 31

Chapter 3: Strings A String is a sequence of characters The + operator concatenates

Chapter 3: Strings A String is a sequence of characters The + operator concatenates two Strings The += operator appends a String First character has index 0 A String can never be modified once created! 32

Chapter 3: String methods length() substring() index. Of() last. Index. Of() char. At() trim()

Chapter 3: String methods length() substring() index. Of() last. Index. Of() char. At() trim() value. Of() 33

Chapter 3: Rectangle class Represents a Rectangle (for displaying on the screen) Has height,

Chapter 3: Rectangle class Represents a Rectangle (for displaying on the screen) Has height, width, x position, and y position n Main constructor takes in these 4 arguements set. Location() changes the position resize() changes the height and width 34

Chapter 3: Variable scoping A variable is visible within the block it is declared

Chapter 3: Variable scoping A variable is visible within the block it is declared in n Called the “scope” of the variable public class Scoping { int z This instance variable is visible anywhere in the Scoping class public static void foo (int x) { //. . . } public static void bar () { //. . . This parameter is visible only in the foo() method } public static void main (String[] args) { int y; //. . . } This local variable is visible until the end of the main() method } 35

Quick survey n a) b) c) d) I feel comfortable with the material in

Quick survey n a) b) c) d) I feel comfortable with the material in chapter 3 Very well With some review, I’ll be good Not really Not at all 36

Quick survey n a) b) c) d) The pace of the lecture for this

Quick survey n a) b) c) d) The pace of the lecture for this slide set was… Fast About right A little slow Too slow 37

Quick survey n a) b) c) d) How interesting was the material in this

Quick survey n a) b) c) d) How interesting was the material in this slide set? Be honest! Wow! That was SOOOOOOO cool! Somewhat interesting Rather boring Zzzzzz 38