Download presentation
Presentation is loading. Please wait.
Published byLynette Willis Modified over 9 years ago
1
1 Review for exam 1 CS 101-E Aaron Bloomfield
2
2 Announcements Exam this Wed Exam this Wed In CHM 402 (NOT in Clark G004) In CHM 402 (NOT in Clark G004) Open-book Open-book Although it’s doubtful that the book will help much Although it’s doubtful that the book will help much Sample text on website Sample text on website Lab quiz this week Lab quiz this week Must be done from 7-9 this Sunday evening Must be done from 7-9 this Sunday evening Let me know if this presents a problem Let me know if this presents a problem Next HW assigned today, due next week Next HW assigned today, due next week Good to have it done for the exam, though Good to have it done for the exam, though
3
3 Today’s lecture An overview of the “review” sections of chapters 1-3 An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over something in more detail! Stop me if you want me to go over something in more detail!
4
4 Chapter 1: Intro Computers think in bits (1 or 0) Computers think in bits (1 or 0) 0101001 = 81 0101001 = 81 Eight bits per byte Eight bits per byte 1024 bytes = 1 Kb 1024 bytes = 1 Kb 1024 Kb = 1 Mb 1024 Kb = 1 Mb 1024 Mb = 1 Gb 1024 Mb = 1 Gb
5
5 Chapter 1: Computer organization CPU = brain CPU = brain Microprocessor: computer on a single chip Microprocessor: computer on a single chip Network: when two or more computers are plugged into one another Network: when two or more computers are plugged into one another
6
6 Chapter 1: Software OS: the program that manages a computer’s resources OS: the program that manages a computer’s resources Program: a sequence of instructions that performs some task Program: a sequence of instructions that performs some task Performing an instruction is called “executing” an instruction Performing an instruction is called “executing” an instruction
7
7 Chapter 1: Compilation Translator: translates a program from one language to another Translator: translates a program from one language to another Machine language: the ones and zeros that a computer understands Machine language: the ones and zeros that a computer understands A low level language! A low level language! Compiler: a translator which typically translates a high- level language into a low-level one Compiler: a translator which typically translates a high- level language into a low-level one Java is a high-level language Java is a high-level language Java’s compiler translates Java code into bytecode Java’s compiler translates Java code into bytecode Bytecode is like machine language, but not tied to a specific machine Bytecode is like machine language, but not tied to a specific machine A Java bytecode interpreter is used to execute the bytecode A Java bytecode interpreter is used to execute the bytecode Called a Java Virtual Machine (JVM) Called a Java Virtual Machine (JVM)
8
8 Chapter 1: Terminology Abstraction Abstraction Similar objects exhibit similar behavior Similar objects exhibit similar behavior The ability to do the same “thing” on many objects The ability to do the same “thing” on many objects Consider car driving example Consider car driving example Encapsulation Encapsulation Not revealing how the method does it’s work Not revealing how the method does it’s work Consider String.substring() Consider String.substring() Consider the car radio example Consider the car radio example Modularity Modularity Dividing code into smaller pieces (modules), each one of which is easier to code Dividing code into smaller pieces (modules), each one of which is easier to code Consider the car radio example Consider the car radio example
9
9 Chapter 1: OOP stuff OOP languages: OOP languages: Abstract things into the class’ methods Abstract things into the class’ methods Encapsulate code inside the class’ methods Encapsulate code inside the class’ methods Use additional method for modularity Use additional method for modularity A (primitive) type is the basic units of storage in Java A (primitive) type is the basic units of storage in Java A type is a template for a variable A type is a template for a variable A class is composed of types (or other classes) as well as methods A class is composed of types (or other classes) as well as methods A class is a template for an object A class is a template for an object Creating a variable/object from a type/class is called instantiating the type/class Creating a variable/object from a type/class is called instantiating the type/class
10
10 Chapter 1: Problem solving steps Analysis Analysis What needs to be done? What needs to be done? Design Design How is it going to be done? How is it going to be done? Implementation Implementation Make it so! Make it so! Testing Testing Does it work correctly? Does it work correctly?
11
11 Chapter 2: Readable programs Comments are a English text Comments are a English text Blank lines make a program easier to read Blank lines make a program easier to read Indentation helps humans identify which code is within the method Indentation helps humans identify which code is within the method Keywords have special meanings in Java Keywords have special meanings in Java Examples: int, double, class, static, public Examples: int, double, class, static, public
12
12 Chapter 2: Identifiers Identifiers: programmer-defined names Identifiers: programmer-defined names For classes, variables, methods, etc. For classes, variables, methods, etc. Cannot be a keyword Cannot be a keyword Must start with a letter (or _ or $) Must start with a letter (or _ or $) Can contain numbers also (but not as the first character) Can contain numbers also (but not as the first character) Good identifiers: radius, width, position Good identifiers: radius, width, position Bad identifiers: x, y, q, the_really_really_long_variable_name_hi_mom Bad identifiers: x, y, q, the_really_really_long_variable_name_hi_mom Java default: theReallyReallyLongVariableName Java default: theReallyReallyLongVariableName
13
13 Chapter 2: Computer bugs A bug is an error in the program A bug is an error in the program To debug is to remove bugs To debug is to remove bugs First bug: 1945 First bug: 1945 Grace Murray Hopper found it Grace Murray Hopper found it In the Harvard University Mark II Aiken Relay Calculator In the Harvard University Mark II Aiken Relay Calculator
14
14 Chapter 2: Computer bugs
15
15 Chapter 2: Java classes The class keyword is used to start a class declaration The class keyword is used to start a class declaration Can be made public (for this class, always do so) Can be made public (for this class, always do so) A class is a “template” for an object A class is a “template” for an object Just as a type is a “template” for a variable Just as a type is a “template” for a variable
16
16 Chapter 2: Java methods All methods have the following syntax: All methods have the following syntax: modifers type name ( parameters ) { statements } Properties of the method Type that it returns A name for the method Any number (including zero) of parameters The body of the method (can be empty) public staticvoidmain(String[] args){... }
17
17 Chapter 2: static vs. non-static variables A static variable (called a class variable): A static variable (called a class variable): Has only ONE instance, regardless of the number of objects created Has only ONE instance, regardless of the number of objects created Even zero! Even zero! Can be accessed by either the class name or the object name Can be accessed by either the class name or the object name A non-static variable (called a member of instance variable): A non-static variable (called a member of instance variable): Has one instance for EACH object created Has one instance for EACH object created Can ONLY be accessed by the object name Can ONLY be accessed by the object name
18
18 Chapter 2: Java methods 2 When a method is called, the associated actions are executed When a method is called, the associated actions are executed A class method is associated with a class (rather than an object) A class method is associated with a class (rather than an object) They are static They are static Can be called by the object name or class name Can be called by the object name or class name A member (or instance) method is associated with an object A member (or instance) method is associated with an object They are not static They are not static
19
19 Chapter 2: static and non- static rules Non-static fields and methods can ONLY be accessed by the object name Non-static fields and methods can ONLY be accessed by the object name Static fields and methods can be accessed by Either the class name or the object name Static fields and methods can be accessed by Either the class name or the object name Non-static methods can refer to BOTH static and non-static fields Non-static methods can refer to BOTH static and non-static fields Static methods can ONLY access static fields Static methods can ONLY access static fields
20
20 Chapter 2: Program execution Java starts executing a program at the beginning of the main() method Java starts executing a program at the beginning of the main() method Braces {} are used to specify where a method begins and ends Braces {} are used to specify where a method begins and ends A statement ends when a semicolon is encountered A statement ends when a semicolon is encountered A statement can span multiple lines A statement can span multiple lines
21
21 Chapter 2: Misc stuff A literal character string is a sequence of characters enclosed by double quotes A literal character string is a sequence of characters enclosed by double quotes Cannot span multiple lines Cannot span multiple lines System is the Java class that allows you to access parts of the computer system System is the Java class that allows you to access parts of the computer system System.in: access to the keyboard System.in: access to the keyboard System.out: access to the monitor System.out: access to the monitor Period is used for selection: Circle.Pi Period is used for selection: Circle.Pi Given String s, select a method via: s.substring() Given String s, select a method via: s.substring() An exception is when Java “panics” An exception is when Java “panics” It means something is wrong It means something is wrong
22
22 Chapter 2: Escape sequences Java provides escape sequences for printing special characters Java provides escape sequences for printing special characters \bbackspace \bbackspace \nnewline \nnewline \ttab \ttab \rcarriage return \rcarriage return \\backslash \\backslash \"double quote \"double quote \'single quote \'single quote
23
23 Chapter 2: Primitive variable types Java has 8 (or so) primitive types: Java has 8 (or so) primitive types: float float double double boolean boolean char char byte byte short short int int long long real numbers integer numbers two values: true and false a single character Also the void “type” Also the void “type” Can make a variable final Can make a variable final
24
24 Chapter 2: Symbolic names vs. literal values Which is easier to enter: Which is easier to enter: Math.PI Math.PI 3.141592653589793 3.141592653589793 Entering a symbolic name reduces chances of errors Entering a symbolic name reduces chances of errors It allows for changing the constant later on It allows for changing the constant later on
25
25 Chapter 2: Terminology Variable types Variable types Static variable in a class Static variable in a class Non-static variable in a class Non-static variable in a class Variable in a method Variable in a method Method types Method types Static method Static method Non-static method Non-static method class variable instance or member variable (local) variable class method (instance or member) method Not initialized to any value Initialized to default value
26
26 Chapter 2: References and variables A variable is an actual spot in memory that holds a (primitive type) value 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 A reference is a memory address that points to another spot in memory where the object is
27
27 Chapter 2: Math Standard operators: + - * / Standard operators: + - * / Note that / can be either integer division or floating-point division Note that / can be either integer division or floating-point division % computes the remainder % computes the remainder Can provide numbers in decimal or scientific notation Can provide numbers in decimal or scientific notation
28
28 The US Gov’t and War Munitions
29
29 Chapter 2: Expressions Evaluating an expression yields a result and a type Evaluating an expression yields a result and a type Example: 4/3 yields 1 of type int Example: 4/3 yields 1 of type int Example: 3.5*2.0 yields 7.0 of type double Example: 3.5*2.0 yields 7.0 of type double Binary operator has two operands Binary operator has two operands Example: 3+4, 6*3, etc. Example: 3+4, 6*3, etc. Left one is evaluated first Left one is evaluated first Unary operator has one operand Unary operator has one operand Example: -3, etc. Example: -3, etc. Operators have precedence Operators have precedence For example, * and / are evaluated before + and - For example, * and / are evaluated before + and -
30
30 Chapter 2: Overflow Consider: Consider: byte b = 100; b = b * 100; A byte can only hold up to +128 A byte can only hold up to +128 This is called overflow This is called overflow Java does not tell you that this happened! Java does not tell you that this happened! Underflow: b -= b*100; Underflow: b -= b*100;
31
31 Chapter 2: Operators Assignment: = Assignment: = Increment (++) and decrement (--) Increment (++) and decrement (--) Consider: Consider: int i = 5;int i = 5; System.out.println (i++);System.out.println (++i); System.out.println (i);System.out.println (i); There are 4 ways to add 1 to an int: There are 4 ways to add 1 to an int: i = i + 1; i += 1; i++;++i There are many such compound operators
32
32 Chapter 2: Casting Casting converts one type to another Casting converts one type to another Example: Example: int x = 1; System.out.println ((double) x); double d = 3.4; System.out.println ((int) d);
33
33 Chapter 2: Scanner class Creating one: Creating one: Scanner stdin = Scanner.create(System.in) Scanner stdin = new Scanner (System.in) Methods: Methods: public int nextInt() public int nextInt() public short nextShort() public short nextShort() public long nextLong() public long nextLong() public double nextDouble() public double nextDouble() public float nextFloat() public float nextFloat() public String next() public String next() public String nextLine() public String nextLine() public boolean hasNext() public boolean hasNext()
34
34 Chapter 3: Intro An object variable is really a reference to that object An object variable is really a reference to that object null represents an object variable that points to nothing null represents an object variable that points to nothing Once nothing points to an object, Java automatically deletes that object Once nothing points to an object, Java automatically deletes that object Called garbage collection Called garbage collection A final object variable: A final object variable: Only the reference (where it points in memory) is final Only the reference (where it points in memory) is final The values in the object can change via member methods The values in the object can change via member methods We use constructors to create objects We use constructors to create objects
35
35 Chapter 3: Strings A String is a sequence of characters A String is a sequence of characters The + operator concatenates two Strings The + operator concatenates two Strings The += operator appends a String The += operator appends a String First character has index 0 First character has index 0 A String can never be modified once created! A String can never be modified once created!
36
36 Chapter 3: String methods length() length() substring() substring() indexOf() indexOf() lastIndexOf() lastIndexOf() charAt() charAt() trim() trim() valueOf() valueOf()
37
37 Chapter 3: Rectangle class Represents a Rectangle (for displaying on the screen) Represents a Rectangle (for displaying on the screen) Has height, width, x position, and y position Has height, width, x position, and y position Main constructor takes in these 4 arguements Main constructor takes in these 4 arguements setLocation() changes the position setLocation() changes the position resize() changes the height and width resize() changes the height and width
38
38 DeCSS stuff Look at http://www-2.cs.cmu.edu/~dst/DeCSS/Gallery/ Look at http://www-2.cs.cmu.edu/~dst/DeCSS/Gallery/ Or do a Google search for “decss gallery” Or do a Google search for “decss gallery”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.