CS 180 Final Study Guide PDF

Title CS 180 Final Study Guide
Author Ruth Baldwin
Course Programming I
Institution Purdue University
Pages 28
File Size 181.4 KB
File Type PDF
Total Downloads 74
Total Views 148

Summary

Final Study Guide...


Description

1

CS 180 Final Study Guide Concepts: 1. Java Basics/Foundations(2-5) 2. Data Types(5-7) 3. Selection(8) 4. Repetition(8-9) 5. Methods and Classes(9-11) 6. Arrays(11-13) 7. File I/O(13-14) 8. Exceptions(14-15) 9. Interfaces/Inheritance(15-16) 10. Concurrency(17-18) 11. Network I/O(18-19) 12. GUI’s(19-23) 13. Polymorphism(23-24) 14. Dynamic Data Structures(24-26) 15. Recursion(26-28)

2

Java Basics/Foundations 1. Algorithm a. Step by step instructions to solve a problem b. Correct, complete, and ends 2. Abstraction a. Creation of a concept from specific examples 3. Von Neuman architecture a. All modern computers have this structure b. structure i. Input/output devices 1. User interacts with the computer 2. Interacts with cpu ii. CPU 1. Carries out instructions to process information 2. Interacts with memory and I/O devices iii. Memory 1. Stores information to be processed and programs 2. Interacts with CPU 4. Memory and storage sizes Unit

Size

Bytes

Practical Measure

byte

8 bits

1

Single character

kilobyte

1024 bytes

1024

Paragraph of text

megabyte

1024 kilobytes

1048576

A minute of music

gigabyte

1024 megabytes

1073741824

Half an hour of video

terabyte

1024 gigabytes

1099511627776

80% of human memory capacity

5. Number systems a. Positional b. Value of the digit relates to its distance from decimal point c. Bases- 2, 8, 10, or 16 d. Each position multiplies or divides by the base 6. Converting a. Divide number by base

3

b. Prepend remainder result c. Replace number by quotient d. Repeat until quotient is 0 e. Ex. base 2-divide by 2 and take remainder 7. Finite precision a. Numbers are stored with bits b. Bits are organized into bytes c. 1 byte = 8 bits d. Bytes organized into words 4-8bytes e. Only represent ints that fit into those bits f. Signs i. Left most bit is the sign ii. 0 means positive 1 means negative iii. Called two's complement iv. Converting positive to negative 1. Convert positive to binary 2. Flip 0s to 1s 3. Add 1 8. Storage a. Largest positive number to be stored is 2^(N-1)-1 i. 127 b. Largest negative number to be stored is -2^(N-1)-1 i. -128 c. Overflow i. Sum of two numbers is too much d. Underflow i. Difference of two numbers is too negative e. Java ignores overflow and underflow 9. Class basics a. Public static void main(String[] args) is where execution begins b. Variables in a class are local variables c. Methods i. The first line of a method is the header ii. Can be executed with object name, method name, and arguments iii. Can return values or be void iv. Can have parameters or none 10. Software development a. Understand the problem b. Develop a solution c. Implement the solution

4

d. Test the solution e. Maintenance f. Writing software i. Programming language 1. High level-complex instructions 2. Assembly-simple instructions 3. Machine-0s and 1s ii. Compilers and Interpreters translate 1. They change high level language to machine or bytecode then machine 2. Interpreters execute it on cpu iii. IDE’s 1. Allow editing of program files 2. Compiling debugging and executing 11. Tools for abstraction a. Object oriented programming b. Classes i. Gives name to abstraction ii. Creates templates for objects iii. Objects are instances of abstraction iv. Variables and methods are details of abstraction c. Variable i. Names and stores quantities d. Method i. Names and defines operation on class or object 12. Basics of writing programs a. Class i. Name of file ii. Methods iii. Execution at main b. Command line input and output i. Scanners ii. System.out.print c. Syntax i. // and /**/ for comments ii. White space ignored iii. ; to separate or end statements iv. {} group statements v. Do not use reserved words vi. Import to access classes

5

d. System class i. System.in for input ii. System.out for output iii. Command line systems iv. Variables reference standard input and output e. Formatting i. Camel case for variables ii. Upper camel case for classes iii. 4 blanks for indentation iv. Case matters when running

Data Types 1. Programs work with values-values are literals-stored in variables a. Literals are data values i. Cannot change ii. Stored in variable b. Variables are the names i. They represent a location in memory ii. Contents of location can vary iii. Must be declared and given a type 1. Defines methods to be used 2. Allocates space for variable to store a value 2. Types a. Formal definition b. Set of values with set operations c. Primitive i. Built into language ii. Boolean,byte,short,int,long,double,float,char iii. Occupy enough bits/bytes to store value d. Reference i. Defined by user ii. Objects,Strings iii. Hold a reference or pointer to object iv. Extensible v. Defined with class declaration vi. Created by new operator vii. Methods in class

6

viii.

Allocates space for reference to object

3. int

4.

5.

6.

7.

8.

a. Range from - 2,147,483,648  to 2,147,483,648 b. Stored in 4 bytes=32 bits c. Int is a reserved word d. -,/,+,=,% operands e. If result is a fraction it will be 0 due to truncation Double a. Real number - can use math operations b. 64 bits c. Double precision Float a. Real number - can use math operations b. 32 bits c. Single precision Char a. Characters b. 16 bits per character c. Asii has a number per each character d. ‘’ e. Methods i. toUpper/LowerCase ii. isUpper/LowerCase iii. isDigit/Letter/WhiteSpace Boolean i. True or false ii. Logical operations && || ^(xor) ! iii. Created via comparison operators iv. Result of logical operators String a. Sequence of characters b. Reference type c. Can be from length 0 to 2,147,483,647 d. Methods i. Concat ii. toUpperCase iii. Length iv. Substring v. Many more e. Can be created with new

7

f. Can hold literals g. Immutable-operations do not change value unless declared h. Can be formatted i. %s for string ii. %d for integers/byte/long/short iii. %f for double/float iv. %10(type) for adding characters/padding v. %.2f to format decimal 9. Final keyword a. Variables cannot be changed after declaration b. Declared before or in constructor 10. Storing values a. byte: 8 bits (-128 to 127) b. short: 16 bits (-32,768 to 32,767) c. int: 32 bits (-2,147,483,648 to 2,147,483,647) d. long: 64 bits (18 digits) 11. Operations a. Usual math order(like a calculator) b. multiplication/division c. addition/subtraction d. Bitwise e. Logical 12. Casting a. Promotion i. Types when combined will be promoted to certain values b. Convert from one type to another i. Upcast-more bits ii. Downcast-less bits-dangerous less precision 13. Any value can be obtained from a string a. May return a NumberFormatException 14. Constructor a. Constructs object when created b. Called by new operator c. Initializes variables 15. Fields a. Variables in class definition that make up the object b. Initialized in constructor using this.value = value; 16. Var a. Value is assumed by declaration b. Can be confusing

8

Selection 1. Sequential execution a. Each statement is executed in order b. Unless there is an error/exception all statements are executed c. Control flows sequentially d. If there are methods it jumps to methods then returns e. Selection statements disrupt this flow 2. If statements a. Uses boolean evaluations as conditionals i. Comparative statements/logical operands b. The code contained within the block will execute if the conditional is true c. Can use else if to have multiple conditions d. Else is default if if is not true e. If doesn't always have to have an else 3. Short circuit evaluation a. With logical operators if the result is determined it will skip evaluating the rest of the conditional 4. Tips a. Operations can fail when they shouldn’t so be precise b. Ensure you use braces to disambiguate conditionals 5. Switch statements a. Useful instead of having multiple else if statements b. Use cases to check c. Always use break statements to avoid fall through d. Default is the last one to catch cases that don't match ones provided e. Use commas if multiple cases should have one operation (case 1, 2, 3 -> 43;) 6. Bitwise a. Different from logical operations b. &-and c. ^-exclusive or-has to be true or false d. |-inclusive or- could be both

Repetition 1. Parts of repetition a. Conditional

9

2.

3.

4.

5.

6.

7. 8.

b. Part to repeat i. Ensure something changes each time the code is repeated ii. Nothing will happen/infinite loop if nothing changes Indefinite a. Loop until done b. It’s unclear how many loops will be needed Definite a. Set amount of loops till done b. You know how many loops needed c. Counter till set Loops a. While loop i. Check boolean conditional ii. repeat while boolean is true b. Do while loop i. Executes at least once ii. Checks while conditional at the end of stuff contained in loop c. For loop i. Executes for a set amount of times ii. increment/decrement iii. Starts from any desired value d. For-Each i. Iterates over a set of data list, array, iterable class, etc. Pre/Post Decrement a. X++ increments by one, x is original x until declared b. ++X increments by one, x is now set to x + 1 c. Same for -- and -=/+= Infinite Loop a. Something is written in the code so where loop runs infinitely b. Stack overflow will eventually occur/you run out of memory Skipped Loop a. Skipping a desired value in loop Fencepost error a. Using less/greater than or equal in for loop

Methods and Classes 1. Classes a. Includes

10

i.

2.

3.

4.

5.

methods -operations on the object or class 1. Can be static or non static ii. Fields -attributes of an object or class 1. With limits-accessible to methods of the class 2. Can be static or non static b. Static vs non static i. Static means of the class ii. Non static means of the object iii. Static means shared by all methods iv. Non static has its own instance in memory or location for the field Variables a. Instance - non static fields in class declaration b. Class - static fields in class declaration c. Local - in method or block d. Parameters - in method declaration Methods a. Parameterized block of code that may or may not return a value b. Exists within a class c. Can reduce redundancy d. Readable e. Modularity - independent testing f. Must list return type i. Void or data type g. Has a name h. Parameters - empty or not i. Variable local to the method ii. Argument is passed to the parameters and must match parameter type iii. Arguments are copied to parameters i. Running i. Runs from start to finish ii. Returns value to call site Designing a class a. Think about making values/methods static or non static i. Do you want to do actions with or on the object?/make attributes of the object - non static ii. Shared by objects of the class/operate on static variables Extent/Scope a. Scope - where variable is visible i. Variable lifetime same as scope b. Extent - how long value is kept

11

c. Object lasts until it's no longer accessible 6. By value a. Parameter passing b. Value is a reference to the object c. Called method does not modify variable where reference is stored d. Can modify object it references 7. Overloading a. Signature i. Name and parameter types ii. Must have unique signatures 1. Can have same name but different parameters b. Based on arguments passed the compiler chooses which method/construction to access 8. This a. Specify which variable or constructor to call b. Matches parameters to decide what to call 9. Encapsulation a. Hiding implementation from other programmers to separate work b. Ensures that certain necessary fields/methods are unchanged by other programmers to avoid issues with code 10. Access modifiers a. Private - only methods in class can access i. Make fields private ii. Make accessor and setters public to control the change b. Public - any method can access i. Make methods public ii. Final fields can be public c. Protected - only methods in class or subclass or package can access d. Package-private - default when not specified - methods in the class or classes in the package can access

Arrays 1. Data Structures Characteristics a. Contents i. Heterogeneous data values ii. Homogeneous data values b. Size

12

2.

3.

4.

5.

6.

i. static/fixed ii. dynamic /changeable c. Access of elements i. Sequential ii. Random Arrays a. Homogeneous b. Static c. Random access d. Reference types e. datatype(String)[] name (array) = new (datatype)String[size] i. You can also use var array = new String[size] Initialization a. You can put values into the array using String[] array = new String[]{“hi”, bye”} b. Default values i. Reference types - null ii. Integers - 0 iii. Real type - 0.0 c. Must initialize at declaration Two Dimensional Arrays a. int [ROWS][COLUMNS] b. You can have however many dimensions you want i. This takes up a lot of space though c. Can be ragged i. Not every rows/column has to have the same amount of values ii. Remember to check the rows length when iterating with nested for loops Varargs a. A method that can be called with 0-any amount of arguments b. func() c. Varargs parameter should be last in the parameters d. Specified by 3 periods in parameters/data type (double...a) ArrayLists a. Can hold any reference type b. ArrayList c. Dynamic d. You can wrap any primitive class to use (Integer, Double, etc) e. Methods i. Add - (i, e) if you want to specify index ii. Get iii. Contains

13

iv. Remove v. set - (i, e) to specify index vi. Size f. Must be imported g. Can be initialized with a size but doesn’t have to be

File I/O 1. Files a. Good for storing information in case of a program crash b. Can be easily accessed and transferred between programs 2. File class a. Abstraction for file b. File name/path is os dependent c. 3 layers of abstraction for i/o d. Can be i. Opened 1. Has to be opened before read 2. Allows os to establish buffers ii. Read 1. Retrieves data from file to be used 2. Method signatures to identify the data type of data transferred iii. Written to 1. Transferred data from program to file 2. Method signatures to identify the data type of data transferred 3. Can be created if doesn't exist/must be opened to write iv. Marked for position 1. Byte address in file allows program to have a marked place a. Can back up again b. Can start at beginning v. Closed 1. Any operations/buffers being used are freed 2. Stops managing the file 3. Buffering a. Ensures no memory is taken up by program operation b. Can be slow with lots of data c. Only reads a certain amount at input then waits to write d. Close buffers when done 4. File I/O a. Low-level

14

i. ii.

Byte oriented transfer of data FileOutputStream/FileInputStream 1. File name included in FOS parameters b. High-level i. Primitive types ii. DataOutputStream/DataInputStream 1. Builds on fos they must be used together c. Object(SERIALIZATION) i. Java objects ii. Must include implements Serializable iii. ObjectOutputStream/ObjectInputStream d. All data is stored as a sequence of bytes! e. Reminders i. All data must be written in same order as it was read in-OptionalDataException can occur 5. Writing to files a. PrintWriter b. PrintStream c. BufferedWriter 6. Reading to files a. FileReader/BufferedReader(work together)

Exceptions 1. Exception general a. Write code without worrying about checking for errors b. Exceptions are thrown when errors are found, execution stops c. Java searches for an exception handler i. Starts with method where occurred ii. Then searches through caller and caller’s caller 2. Catching a. Surround code possible to throw exceptions with try catch statements i. Try executing block of code ii. Catch specified type of exception and handle it b. You can put throws statements in the method header and you won’t have to catch it c. When catching multiple exceptions do it in order of lowest subclass to highest d. Try with resources can be used to close resources automatically 3. Finally

15

a. After catch b. Always executes no matter what 4. Exception class a. Exceptions are objects b. Either an instance of the class or a subclass c. Created using new d. getMessage() or printStackTrace() to help identify cause of exception e. Hierarchy i. Exception ii. IOException/Created exception iii. FileNotFoundException/Runtime iv. Arithmetic/NumberFormat/IndexOutOfBounds 5. Unchecked/Checked Exceptions a. Runtime and its subclass are unchecked i. Program or JVM error(null pointer, arithmetic, etc) ii. Program will crash b. All others are checked i. You must check for them ii. Try catch or throw iii. Recoverable

Inheritance/Interfaces 1. Interface a. Two systems interact b. One system will define interface the other uses it Ex. a java class provides a form of an interface 2. What does an interface have/do? a. Has methods mainly(public/specified return type) b. There's a class that defines the interface and ones that implement it c. Default methods have bodies(implementation) d. No method bodies/no fields except constants i. Fields are implicitly public, final, static 1. They can be declared ii. Methods are implicitly public 3. Classes that implement the interface must a. Have implementations for all methods in the interface(except default) b. Declare the interface it implements

16

4. Multiple interfaces a. Classes can implement multiple interfaces b. Implements interface, another interface { 1. Inheritance a. Best for overlapping functionality b. Share methods/fields c. Super/parent class and sub/child class 2. How does inheritance work a. Each class can extend one other class b. Hierarchical system c. Inherits all fields and methods d. Be sure to initialize fields from the super class in the constructor i. super(variable, another variable); ii. This is called constructor chaining iii. Inserted by default if there is a parameterless constructor iv. Must be done first v. this() can also be used 3. Object class a. Every class inherits the object class b. clone(), equals(), and toString() are all methods from the object class 4. Subclass rules a. Private fields cannot be accessed by subclasses i. Protected allows subclass access of fields ii. Use accessors and mutators instead 5. Overriding/Overloading a. Overriding i. If in the superclass there is a method with the same name and signature as a subclass method ii. Subclass method will be executed instead iii. Change the behavior of method iv. If method is not overridden that method of superclass is still available to subclass(super.method()) b. Overloading i. Methods have the same name but different signatures 6. InstanceOf a. Can be used to determine if a class inherits another class 7. Interfaces to know a. Serializable-marks a class as something that will be transferred over a stream, such as a FileOutputStream or a Socket connection.

17

b. Iterable-a class that can be iterated over, in a loop for example

Concurrency 1. History a. One program had to run at a time b. Windowed system gives illusion of all running at the same time c. Time slicing-letting each program run for a a few hundred milliseconds 2. CPU a. Cpus were having to run too fast as programs became more complex b. This was an issue because batteries would heat up c. Solution was to put more cpus on one chip (cores) d. Time slicing is still used 3. Threads a. Programs can have multiple threads running simultaneously b. Faster searching c. Sequential i. Thread goes through program d. Concurrent i. Multiple threads running through the program e. Method...


Similar Free PDFs