Information and Computer Sciences University of Hawaii, Manoa

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Hello, world! Dissect HelloWorld.java Compile it Run it.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
COMP More About Classes Yi Hong May 22, 2015.
Imperative Programming
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
The Java Programming Language
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
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.
Flow of Control Part 1: Selection
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CPS120: Introduction to Computer Science Decision Making in Programs.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Introduction to Java Java Translation Program Structure
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
 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
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Information and Computer Sciences University of Hawaii, Manoa
Lecture 4b Repeating With Loops
Dept of Computer Science University of Maryland College Park
Chapter 4 Assignment Statement
Java Primer 1: Types, Classes and Operators
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Control Structures – Selection
Introduction to Programming in Java
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Escape Sequences What if we wanted to print the quote character?
Arrays, For loop While loop Do while loop
Starting JavaProgramming
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
T. Jumana Abu Shmais – AOU - Riyadh
Statement-Level Control Structures
Control Structure Chapter 3.
Chapter 6 – Methods Topics are:
Focus of the Course Object-Oriented Software Development
Chapter 2 Programming Basics.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Control Structure.
Corresponds with Chapter 5
Presentation transcript:

Information and Computer Sciences University of Hawaii, Manoa Midterm Review ICS 111 Cam Moore Information and Computer Sciences University of Hawaii, Manoa

Computer Fundamentals Von Neumann Architecture Fetch, Execute cycle Input Device Output Central Processing Unit Control Unit Arithmetic/Logic Unit Memory Unit

Two Types of Languages Machine Language Binary Specific to CPU High-level Programming Languages Human readable Java, Fortran, C++, COBOL, LISP, etc Must be converted to Machine Language Compiler Interpreter

High-Level Programming Languages Syntax Strict rules about what is and isn’t allowed Semantics The meaning of the program (What it does)

Building Blocks of Programs Data – Variables Names that refer to memory locations Typed, what they can hold Can change value by assignment = Instructions Sequence of execution steps Control Structures – loops and branches Subroutines – named “chunks” of code

Java Syntax Comments Ignored by the computer Very important // Single line /* … */ Multi line /** … */ Javadoc /** * A program to display the message * "Hello World!" on standard output. * @author Cam Moore */ public class HelloWorld { * Prints out Hello World! * @param args not used. public static void main(String[] args) { System.out.println("Hello World!"); } } // end of class HelloWorld

Variables Program data is stored in memory Variables Variables are not the data, but the location of the data Variables have types Assignment statements Memory Location 0 Location 1 Location 2 Location 3 Location 4 Location 5 Location 6 Location 7 Location 8 Location 9 Location 10 Location 11 Location 12 Location N 10001010 00110100 01110111 10100100 11010010 10000110 01001111 10100000 00000010 10100010 00010100 11111010 . . . x interest <variable> = <expression> rate = 0.07; interest = rate * principal;

Java Primitive Types Eight primitive types byte short int long float double char boolean Whole Numbers Floating Point Numbers Single Character (Unicode) true or false

Precedence Rules Highest to lowest Precedence Unary and assignment operators: right-to-left Rest: left-to-right Operator Type Examples Parentheses () Unary operators ++, --, !, unary -, unary +, type-cast Multiplication and Division *, /, % Addition and Subtraction +, - Relational operators <, >, <=, >= Equality and Inequality ==, != Boolean And && Boolean Or || Ternary ? : Assignment operators =, +=, -=, *=, /=, %=

Blocks, Loops and Branches Control Structures Control the flow of programs block while do..while for if switch

Blocks Group statements together Pair of curly braces { } Sequence of statements 0 or more statements Doesn’t affect flow of control Defines scope { <statements> }

Variable Scope Variable Scope: Where a variable is usable Variables are defined in a block Usable inside that block and all sub-blocks Not available outside the block { int x = 3; ... char c = ‘!’; } double c = 3e8;

While Loop Repeat statements over and over Will loop while boolean-exression is true Can lead to infinite loops while (<boolean-expression>) <statement> while (<boolean-expression>) { <statements> } int number = 1; while (number < 6) { // Keep going as long as number < 6 System.out.println(number); number = number + 1; // Go on to next number }

do..while Loop Similar to the while loop The body of the loop will run at least once do <statement> // body of the loop while (<boolean-expression>); do { <statements> // body of the loop } while (<boolean-expression>);

The for Loop initialization done once boolean-expression is evaluated to terminate loop update is done each time through the loop for (<initialization>; <boolean-expression>; <update>) <statement> // body of the loop for (<initialization>; <boolean-expression>; <update>) { <statements> // body of the loop } <initialization>; while(<boolean-expression>) { <statements> // body of the loop <update>; }

Which Loop to Use Know how many time to loop (counting) for loop Don’t know how many times while loop Run the body at least once do…while loop

if Statement Two way branching statement(s)-1 executed if boolean-expression is true statement(s)-2 executed if boolean-expression is false if (<boolean-expression>) <statement-1> else <statement-2> if (<boolean-expression>) { <statements-1> } else { <statements-2>

Multiway Branching Three-way branch if (<boolean-expression-1>) { <statements-1> } else if (<boolean-expression-2>) { <statements-2> else { <statements-3> Three-way branch only one of the statements will execute expression 1: true => statements-1 expression 1: false expression 2: true => statements-2 expression 2: false => statements-3

switch Statement Second branching statement switch (<expression>) { case <constant-1>: <statements-1> break; case <constant-2>: <statements-2> . . // (more cases) case <constant-N>: <statements-N> default: // optional default case <statements-(N+1)> } // end of switch statement

Java Exceptions Java exceptions are represented by objects of type Exception Many different subclasses of Exception NullPointerException IllegalArgumentException NumberFormatException The program “throws” the exception String str = “42”; int x = Integer.parseInt(str); str = “fred”; x = Integer.parseInt(str);

Dealing with Exceptions try..catch If statements-1 throws an exception of type exception-class-name control jumps to statements-2 Else statements-2 is skipped try { <statements-1> } catch (<exception-class-name> <variable-name>) { <statements-2>

Arrays Very basic data structure Data structures are data items chunked together Arrays: Items are arranged as numbered sequence length index starts at 0 All the same type

Array Variables Arrays use [] Creating Arrays String[] nameList; int[] A; double[] prices; nameList[7]; A[0] = 13; prices[prices.length – 1]; nameList = new String[1000]; A = new int[5]; prices = new double[100]; <array-variable> = new <base-type>[<array-length>];

Subroutines Allow us to handle complex programs Consists of instructions for a task Grouped together Named Can be called by the program Can be called by other subroutines Build up the complex solution

Subroutine Definitions modifiers “static” and “public” return-type The type of the returned value or void parameter-list Information passed into the subroutine <type> <parameter-name> pairs separated by commas <modifiers> <return-type> <name> ( <parameter-list> ) { <statements> }

Access Specifiers public Usable by all none Usable by “package” classes private Usable only by the same class Choose either public or private*

Calling Subroutines For static subroutines in the same class in a different class <subroutine-name>(<parameters>); <class-name>.<subroutine-name>(<parameters>);

Parameters Mechanism for passing information to subroutines Part of the interface Type Number Order Get values from outside the subroutine

Formal and Actual Parameters Parameters in subroutine definition Type Name Actual Values set by calling subroutine public static void doTask(int N, double x, boolean test) { // statements to perform the task go here } doTask(17, Math.sqrt(z + 1), z >= 10);

Three Types of Variables Local Declared inside a subroutine Available to the subroutine only Parameters Set outside the subroutine Like local variables in the subroutine Global Declared outside subroutines Available to all*

Return Values Subroutines that returns a value is called a function Functions can only return a value of a specified type Java uses the return statement The type of the expression must match the function definition <modifiers> <return-type> <name> ( <parameter-list> ) { <statements> } return <expression>;

Java Packages Java groups classes into packages Packages can contain Other packages “sub-packages” Two major packages java javax

Using Classes from Packages Two ways to use a class from another package Use the full name of the class Import the class java.awt.Color rectColor; package edu.uhm.ics111; import java.awt.Color; public class Example { private static Color rectColor; . . . }