Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1- 3 and 5 Stop me if you want me to go.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Computer Systems and the Java Programming Language.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming: Basic Elements of C++.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Introduction to Java Java Translation Program Structure
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
1 Review for exam 1 CS 101-E Aaron Bloomfield. 2 Announcements Exam this Wed Exam this Wed In CHM 402 (NOT in Clark G004) In CHM 402 (NOT in Clark G004)
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Yanal Alahmad Java Workshop Yanal Alahmad
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
Chapter 2: Basic Elements of Java
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
Fundamentals 2.
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Review for exam 1 CS 101 Aaron Bloomfield

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

3 Material you may not be comfortable with References This will come with time… Using objects, ifs, etc. It all builds over time

4 Chapter 1

5 Intro Computers think in bits (1 or 0) = 81 Eight bits per byte 1024 bytes = 1 Kb 1024 Kb = 1 Mb 1024 Mb = 1 Gb

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

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

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

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

10 OOP stuff OOP languages: 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 A type is a template for a variable A class is composed of types (or other classes) as well as methods A class is a template for an object Creating a variable/object from a type/class is called instantiating the type/class

11 Problem solving steps Analysis What needs to be done? Design How is it going to be done? Implementation Make it so! Testing Does it work correctly?

12 Chapter 2

13 Readable programs Comments are a English text 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 Examples: int, double, class, static, public

14 Identifiers Identifiers: programmer-defined names 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_really_long_variable_name_hi_mom Java default: theReallyReallyLongVariableName

15 Computer bugs A bug is an error in the program To debug is to remove bugs

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

17 Java methods 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){... }

18 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 A statement can span multiple lines

19 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 System.in: access to the keyboard System.out: access to the monitor Period is used for selection: Math.round Given String s, select a method via: s.substring() An exception is when Java “panics” It means something is wrong

20 Escape sequences Java provides escape sequences for printing special characters \bbackspace \nnewline \ttab \rcarriage return \\backslash \"double quote \‘single quote

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

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

23 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

24 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

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

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

27 Operators Assignment: = Increment (++) and decrement (--) 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: i = i + 1; i += 1; i++; ++i There are many such compound operators

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

29 Scanner class Creating one: Scanner stdin = new Scanner (System.in) Don’t use Scanner.create()! Methods: public int nextInt() public short nextShort() public long nextLong() public double nextDouble() public float nextFloat() public String next() public String nextLine() public boolean hasNext()

30 Chapter 3

31 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 Called garbage collection A final object variable: Only the reference (where it points in memory) is final The values in the object can change via member methods We use constructors to create objects

32 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!

33 String methods length() substring() indexOf() lastIndexOf() charAt() trim() valueOf()

34 Rectangle class Represents a Rectangle (for displaying on the screen) Has height, width, x position, and y position Main constructor takes in these 4 arguements setLocation() changes the position resize() changes the height and width

35 Chapter 5

36 Logical expressions Logical expression has values either true or false Java has the boolean type with values true or false Truth table: method to dissect a logical expression

37 Logical operators Three primary logical operators: and, or, not An and operation is only true when both parts are true An or operation is true when either (or both) parts are true A not operation negates (switches) the value of the expression Logical operators: and is &&, or is ||, not is ! Not operator is unary

38 Equality Two equality operators: == and != When comparing objects, == compares the references, not the objects themselves Use the.equals() method, when available, to test for object equality Don't test floating point values for equality! Instead, test for “closeness”

39 Ordering Ordering operators:, =. These only work on primitive types! Relational operators are the equality operators and the ordering operators For booleans, false is less than true For characters, ordering is based on the Unicode numbers of the characters

40 If statements An if statement has the form: if (expression) action An if-else statement has the form: if (expression) action1 else action2 An if-else-if statement is used when there are many tasks to do, depending on the logical expressions

41 Switches A switch statement is useful instead of a long- winded if-else-if block Must always put either break at the end of a switch statement block, or a comment such as '// FALL THRU' The default case means any case not matched by any of the other cases

42 Misc Operator precedence: (p 187) Short-circuit evaluation: left side is evaluated first. If the result can be determined at that point, right side is not evaluated System.exit() will terminate the program immediately Use consistent indentation!