CMPS 12B Chapter 1-2: Reading Week 1 Notes

12 Pages
Unlock Document

University of California - Santa Cruz
Computer Science
Darrell Long

CMPS 12B Week 1 Notes Chapters 1 & 2, Prichard/Carrano: Chapter 1 1.1 - Language Basics Comments // - comment line /* - start comment block */ - end comment block Identifiers and Keywords Java is case sensitive identifier - to name various parts of the program Variables Java identifier - represents a memory location that contains a value of a primitive data type Declaring variables - indicate data type, new Primitive Data Types Boolean - boolean - True/False Character - char - use single quotes or Unicode to denote - integral type Integer - byte/short/int/long - signed numbers Floating point - float/double - proper fractions - arithmetic type A wrapper class is available for each primitive data type References A type used to locate an object - left side of equal sign Literal Constants Used to indicate aprticular values within a program Named Constants Constants whose value that don't change. Use final to lock in value Assignments and Expressions How to do math == != <= >= += -= && - and || - or Arrays Collection of elements, items, or values that share the same data type. Direct access data structure [] declaration 2D arrays [][] declaration 1.2 - Selection Statements If Statement Switch statement For multiple choices - why am I just learning this now? Damnit McDowell 1.3 - Iteration Statements The while statement - as long as something is true, do this The for statement - during this interval, do this The do statements - execute something until the expression is false 1.4 - Program Structure Packages - group of related classes Classes - data type that specifies data and methods that are available in scope of class/within class Data Fields - class members that are variables/constants - values you put in functions Access modifiers (use one) public private protected No access modifier Use modifiers (all can be used at once) static final transient volatile Methods Used to implement operations Class Inheritance Reusing code Extending Superclass 1.5 - Useful Java Classes Object Classes - object as root Array Classes - Used to manipulate arrays String Classes 1.6 - Java Exceptions Catching Exceptions - error analysis Exceptions are instances of th class java.lang.Exception Throwing exceptions - indicates that a method might throw an exception; a warning, essentially. 1.7 - Text Input and Output Output print println printf The Console CLass Helps access character-based console device in JVM 1.8 - File Input and Output File definition - sequence of components of the same data type that resides in an auxiliary storage Can exist after program execution terminates/completes Permanent record Text Files - self-explanatory Object Serialization data persistance - saving a file Mechanism for creating persistaent objects Chapter 2 2.1 - Problem Solving and Software Engineering What is Problem Solving? process of taking the statement of a problem and developing a computer program that solves problem. Solution 1. algorithm 2. ways to store data The Life Cycle of Software Development is a continual process; expensive too. Phase 1: Specification - initial purpose Phase 2: Design Phase 3: Risk analysis - is it worth doing? Phase 4: Verification - could it work? Phase 5: Coding Phase 6: Testing Phase 7: Refining the solution What is a Good Solution? What is a good computer program? Efficient use of computer resources Difficulties encountered by users Consequences of misbehaving program A solution is good if the total cost it incurs over all phases of its life cycle is minimal 2.2 - Achieving an Object-Oriented Design Abstraction and Information Hiding Procedural abstraction - separates the purpose of a method from its implementation - specify what to do, not how to do it Data abstraction - specify what you will do with data, not how to do it Object-Oriented Design develop objects that combine data/operations. modular/hierarchical encapsulation - hide inner details Three Principles of OOP 1. Encapsulation: Objects combine data and operations 2. Inheritance: Classes can inherit properties from other classes 3. Polymorphism: Objects can determine appropriate operations at execution time Functional Decomposition Address a task of successively lower levels of detail - break the problem down General Design Guidelines 1. Use OOD and FD together to produce modular solutions. That is, develop abstract data types and algorithms in tandem 2. Use OOD for problems that primarily involve data 3. Use FD to design algorithms for an object's operations 4. Consider FD to design solutions to problems that emphasize algorithms over data 5. Focus on what, not how, when designing both ADTs and algorithms 6. Consider incorporating previously written software components into your design Modeling Object-Oriented Designs Using UML UML - Unified Modeling Language - used to express object-oriented designs Advantages of an Object-Oriented Approach Methodical Ancestor/Descendant classes Easy maintenance and verification cycle 2.3 - A Summary of Key Issues in Programming Modularity Constructing the program - Modularity facilitates programming Debugging the program - Modularity isolates errors Reading the program - Modular programs are easy to read Modifying the program - Modularity isolates modifications Eliminating redundant code - Modularity eliminates redundancies Modifiability - increasing user friendliness Methods - make a program easier to modify Named constants - make a program easier to modify Ease of Use - UI Prompt the user for input Echo the input Label the input Fail-Safe Programming Error in input data - Check for errors in input Error in the program logic - Check for errors in logic Guarding against errors in input data - test for invalid input data Guarding against errors in program logic methods should check their invariants methods should enforce their preconditions methods should check the values of their arguments Style Extensive use of methods - It is difficult to overuse methods Use of private data fields - good for info hiding Error handling - in case of error, methods should return a value or throw an exception, not display message Readability - easy to follow Documentation 1. An initial comment for the program that includes a. Statement of purpose b. Author and date c. Description of the program's input and output d. Description of how to use the program e. Assumptions such as the type of data expected f. Statement of exceptions, that is, what could go wrong g. Brief description of the major classes 2. Initial comments in each class that state its purpose and describe the data containe din the class (constants and variables) 3. Initial comments in each method that state its purpose, preconditions, postconditions, and methods called 4. Comments in the body of each method to explain important features or subtle logic Consider who will read your comments Document it like history - in the moment, not after Debugging Use temporary System.out.println to find logic issues Use special dump methods Chapters 1 & 2, Kernighan/Ritchie: Chaper 1 1.1 - Getting Started Write programs C consists of functions and variables functions constain statements main is special, no arguments, can't be renamed 1.2 - Variables and Arithmetic Expressions Same as Java 1.3 - The for statement Same as Java 1.4 - Symbolic Constants Name your constants 1.5 - Character Input and Output File Copying EOF - end of file Character Counting ++ Line Counting Add one once you get to end of line Word Counting Lots of if statements 1.6 - Arrays arrays are declared by int Ex: int ndight[10]; 1.7 - Functions Used to encapsulate computation Ignore how a job is done shorthand for longer code 1.8 - Arguments - Call by Value all function arguments are passed by value - called function is given the alues of its arguments in temporary variables rather than the originals. When you modify a variable, you're modifying the copy, not original. Fewer extraneous variables 1.9 - Character Arrays Common 1.10 - External Variables and Scope Variables in main are private and local to main Same as Java Chaper 2 2.1 - Variable Names Names are made up of letters and digits; first character must be a letter "_" counts as a letter 2.2 - Data Types and Sizes char - single byte, capable of holding one character int short long float - single-precision floating point double - double-precision floating point 2.3 - Constants integer constant are numbers long constants end with L, ell, or are just too long to be ints Unsigned constants are written with a terminal u or U 2.4 - Declarations All variables must be declared before use Put data type before variable 2.5 - Arithmetic Operators Same as Java 2.6 - Relational and Logical Operators Same as Java 2.7 - T
More Less

Related notes for CMPS 12B

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.