1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review”...
-
Upload
dorthy-mitchell -
Category
Documents
-
view
216 -
download
0
description
Transcript of 1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review”...
11
Review for exam 1Review for exam 1CS 101CS 101
Aaron BloomfieldAaron Bloomfield
22
Today’s lectureToday’s lecture
An overview of the “review” sections of An overview of the “review” sections of chapters 1-3chapters 1-3
Stop me if you want me to go over Stop me if you want me to go over something in more detail!something in more detail!
33
Material you may not be Material you may not be comfortable with…comfortable with…
ConstructorsConstructors I know there is a lot of confusion about themI know there is a lot of confusion about them
Why you call mutator methods instead of Why you call mutator methods instead of setting the fields directlysetting the fields directly Just know that it’s a good idea to do soJust know that it’s a good idea to do so
ReferencesReferences This will come with time…This will come with time…
44
Chapter 1: IntroChapter 1: Intro
Computers think in bits (1 or 0)Computers think in bits (1 or 0) 00101001 = 8100101001 = 81
Eight bits per byteEight bits per byte 1024 bytes = 1 Kb1024 bytes = 1 Kb 1024 Kb = 1 Mb1024 Kb = 1 Mb 1024 Mb = 1 Gb1024 Mb = 1 Gb
55
Chapter 1: Computer organizationChapter 1: Computer organization
CPU = brainCPU = brainMicroprocessor: computer on a single chipMicroprocessor: computer on a single chipNetwork: when two or more computers are Network: when two or more computers are plugged into one anotherplugged into one another
66
Chapter 1: SoftwareChapter 1: Software
OS: the program that manages a OS: the program that manages a computer’s resourcescomputer’s resourcesProgram: a sequence of instructions that Program: a sequence of instructions that performs some taskperforms some task Performing an instruction is called “executing” Performing an instruction is called “executing”
an instructionan instruction
77
Chapter 1: CompilationChapter 1: Compilation
Translator: translates a program from one language to Translator: translates a program from one language to anotheranotherMachine language: the ones and zeros that a computer Machine language: the ones and zeros that a computer understandsunderstands
A low level language!A low level language!Compiler: a translator which typically translates a high-Compiler: a translator which typically translates a high-level language into a low-level onelevel language into a low-level one
Java is a high-level languageJava is a high-level languageJava’s compiler translates Java code into bytecodeJava’s compiler translates Java code into bytecode
Bytecode is like machine language, but not tied to a specific Bytecode is like machine language, but not tied to a specific machinemachine
A Java bytecode interpreter is used to execute the A Java bytecode interpreter is used to execute the bytecodebytecode
Called a Java Virtual Machine (JVM)Called a Java Virtual Machine (JVM)
88
Chapter 1: TerminologyChapter 1: Terminology
AbstractionAbstraction Similar objects exhibit similar behaviorSimilar objects exhibit similar behavior The ability to do the same “thing” on many objectsThe ability to do the same “thing” on many objects Consider car driving exampleConsider car driving exampleEncapsulationEncapsulation Not revealing how the method does it’s workNot revealing how the method does it’s work Consider String.substring()Consider String.substring() Consider the car radio exampleConsider the car radio exampleModularityModularity Dividing code into smaller pieces (modules), each one Dividing code into smaller pieces (modules), each one
of which is easier to codeof which is easier to code Consider the car radio exampleConsider the car radio example
99
Chapter 1: OOP stuffChapter 1: OOP stuff
OOP languages:OOP languages: Abstract things into the class’ methodsAbstract things into the class’ methods Encapsulate code inside the class’ methodsEncapsulate code inside the class’ methods Use additional method for modularityUse additional method for modularityA (primitive) type is the basic unit of storage in A (primitive) type is the basic unit of storage in JavaJava A type is a template for a variableA type is a template for a variableA class is composed of types (or other classes) A class is composed of types (or other classes) as well as methodsas well as methods A class is a template for an objectA class is a template for an objectCreating a variable/object from a type/class is Creating a variable/object from a type/class is called instantiating the type/classcalled instantiating the type/class
1010
Chapter 1: Problem solving stepsChapter 1: Problem solving steps
AnalysisAnalysis What needs to be done?What needs to be done?DesignDesign How is it going to be done?How is it going to be done?ImplementationImplementation Make it so!Make it so!TestingTesting Does it work correctly?Does it work correctly?
11
Quick surveyQuick survey I feel comfortable with the material in I feel comfortable with the material in
chapter 1chapter 1
1212
Chapter 2: Readable programsChapter 2: Readable programs
Comments are a English textComments are a English text Have a // before them in a Java fileHave a // before them in a Java file
Blank lines make a program easier to readBlank lines make a program easier to readIndentation helps humans identify which Indentation helps humans identify which code is within the methodcode is within the methodKeywords have special meanings in JavaKeywords have special meanings in Java Examples: int, double, class, static, publicExamples: int, double, class, static, public
1313
Chapter 2: IdentifiersChapter 2: Identifiers
Identifiers: programmer-defined namesIdentifiers: programmer-defined names For classes, variables, methods, etc.For classes, variables, methods, etc. Cannot be a keywordCannot 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 Can contain numbers also (but not as the first
character)character)
Good identifiers: radius, width, positionGood identifiers: radius, width, positionBad identifiers: x, y, q, Bad identifiers: x, y, q, the_really_really_long_variable_name_hi_momthe_really_really_long_variable_name_hi_momJava default: theReallyReallyLongVariableNameJava default: theReallyReallyLongVariableName
1414
Chapter 2: Computer bugsChapter 2: Computer bugs
A bug is an error in the programA bug is an error in the programTo debug is to remove bugsTo debug is to remove bugs
1515
Chapter 2: Java classesChapter 2: Java classes
The class keyword is used to start a class The class keyword is used to start a class declarationdeclaration Can be made public (for this course, always Can be made public (for this course, always
do so)do so)
A class is a “template” for an objectA class is a “template” for an object Just as a type is a “template” for a variableJust as a type is a “template” for a variable
1616
Chapter 2: Java methodsChapter 2: Java methodsAll methods have the following syntax:All methods have the following syntax:
modifers type name ( parameters ) { statements }modifers type name ( parameters ) { statements }
Propertiesof the
method
Typethat itreturns
A namefor themethod
Any number(including zero)of parameters
The body ofthe method
(can be empty)
public static void main (String[] args) { ... }
1717
Chapter 2: Program executionChapter 2: Program execution
Java starts executing a program at the Java starts executing a program at the beginning of the main() methodbeginning of the main() methodBraces { } are used to specify where a Braces { } are used to specify where a method begins and endsmethod begins and endsA statement ends when a semicolon is A statement ends when a semicolon is encounteredencountered A statement can span multiple linesA statement can span multiple lines
1818
Chapter 2: Misc stuffChapter 2: Misc stuffA literal character string is a sequence of characters A literal character string is a sequence of characters enclosed by double quotesenclosed by double quotesSystem is the Java class that allows you to access parts System is the Java class that allows you to access parts of the computer systemof the computer system
System.in: access to the keyboardSystem.in: access to the keyboard System.out: access to the monitorSystem.out: access to the monitor
Period is used for selection: Math.roundPeriod is used for selection: Math.round 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 wrongIt means something is wrong
1919
Chapter 2: Escape sequencesChapter 2: Escape sequences
Java provides escape sequences for printing Java provides escape sequences for printing special charactersspecial characters \b\b backspacebackspace \n\n newlinenewline \t\t tabtab \r\r carriage returncarriage return \\\\ backslashbackslash \"\" double quotedouble quote \‘\‘ single quotesingle quote
2020
Chapter 2: Primitive variable typesChapter 2: Primitive variable types
Java has 8 (or so) primitive types:Java has 8 (or so) primitive types: floatfloat doubledouble booleanboolean charchar bytebyte shortshort intint longlong
real numbers
integer numbers
two values: true and falsea single character
Also the void “type”Also the void “type”Can make a variable finalCan make a variable final
2121
Chapter 2: Symbolic names vs. Chapter 2: Symbolic names vs. literal valuesliteral values
Which is easier to enter:Which is easier to enter: Math.PIMath.PI 3.1415926535897933.141592653589793
Entering a symbolic name (i.e. a constant) Entering a symbolic name (i.e. a constant) reduces chances of errorsreduces chances of errorsIt allows for changing the constant later onIt allows for changing the constant later onAre usually finalAre usually final
2222
Chapter 2: References and Chapter 2: References and variablesvariables
A variable is an actual spot in memory that A variable is an actual spot in memory that holds a (primitive type) valueholds a (primitive type) valueA reference is a memory address that A reference is a memory address that points to another spot in memory where points to another spot in memory where the object isthe object is
Variables defined in a class are initialized Variables defined in a class are initialized to a default valueto a default valueVariables defined in a method are not Variables defined in a method are not initialized to a default valueinitialized to a default value
2323
Chapter 2: MathChapter 2: Math
Standard operators: + - * /Standard operators: + - * /Note that / can be either integer division or Note that / can be either integer division or floating-point divisionfloating-point division% computes the remainder% computes the remainderCan provide numbers in decimal or Can provide numbers in decimal or scientific notationscientific notation
2424
Chapter 2: ExpressionsChapter 2: Expressions
Evaluating an expression yields a result and a Evaluating an expression yields a result and a typetype Example: 4/3 yields 1 of type intExample: 4/3 yields 1 of type int Example: 3.5*2.0 yields 7.0 of type doubleExample: 3.5*2.0 yields 7.0 of type doubleBinary operator has two operandsBinary operator has two operands Example: 3+4, 6*3, etc.Example: 3+4, 6*3, etc. Left one is evaluated firstLeft one is evaluated firstUnary operator has one operandUnary operator has one operand Example: -3, etc.Example: -3, etc.Operators have precedenceOperators have precedence For example, * and / are evaluated before + and -For example, * and / are evaluated before + and -
2525
Chapter 2: OverflowChapter 2: Overflow
Consider:Consider:byte b = 100;byte b = 100;b = b * 100;b = b * 100;
A byte can only hold up to +127A byte can only hold up to +127
This is called overflowThis 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;
2626
Chapter 2: OperatorsChapter 2: Operators
Assignment: =Assignment: =Increment (++) and decrement (--)Increment (++) and decrement (--)Consider:Consider:
int i = 5;int i = 5; int i = 5;int i = 5;System.out.println (i++);System.out.println (i++); System.out.println (+System.out.println (++i);+i);
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 = i + 1;i += 1;i += 1;i++;i++;++i++i
There are many such compound operators
2727
Chapter 2: CastingChapter 2: Casting
Casting converts one type to anotherCasting converts one type to anotherExample:Example:
int x = 1;int x = 1;System.out.println ((double) x);System.out.println ((double) x);
double d = 3.4;double d = 3.4;System.out.println ((int) d);System.out.println ((int) d);
2828
Chapter 2: Scanner classChapter 2: Scanner class
Creating one: Creating one: Scanner stdin = new Scanner (System.in)Scanner stdin = new Scanner (System.in) Don’t use Scanner.create()!Don’t use Scanner.create()!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()
29
Quick surveyQuick survey I feel comfortable with the material in I feel comfortable with the material in
chapter 2chapter 2
3030
Chapter 3: IntroChapter 3: Intro
An object variable is really a reference to that objectAn object variable is really a reference to that object
null represents an object variable that points to nothingnull represents an object variable that points to nothing
Once nothing points to an object, Java automatically Once nothing points to an object, Java automatically deletes that objectdeletes that object
Called garbage collectionCalled garbage collection
A final object variable:A final object variable: Only the reference (where it points in memory) is finalOnly the reference (where it points in memory) is final The values in the object can change via member methodsThe values in the object can change via member methods
We use constructors to create objectsWe use constructors to create objects
3131
Chapter 3: StringsChapter 3: Strings
A String is a sequence of charactersA String is a sequence of charactersThe + operator concatenates two StringsThe + operator concatenates two StringsThe += operator appends a StringThe += operator appends a StringFirst character has index 0First character has index 0A String can never be modified once A String can never be modified once created!created!
3232
Chapter 3: String methodsChapter 3: String methods
length()length()substring()substring()indexOf()indexOf()lastIndexOf()lastIndexOf()charAt()charAt()trim()trim()valueOf()valueOf()
3333
Chapter 3: Rectangle classChapter 3: Rectangle class
Represents a Rectangle (for displaying on Represents a Rectangle (for displaying on the screen)the screen)Has height, width, x position, and y Has height, width, x position, and y positionposition Main constructor takes in these 4 arguementsMain constructor takes in these 4 arguements
setLocation() changes the positionsetLocation() changes the positionresize() changes the height and widthresize() changes the height and width
34
Quick surveyQuick survey I feel comfortable with the material in I feel comfortable with the material in
chapter 3chapter 3