Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

Similar documents
cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 251 Intermediate Programming Java Basics

Java Basic Programming Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs

Object-Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

AP Computer Science A

CS1004: Intro to CS in Java, Spring 2005

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Course Outline Introduction to C-Programming

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Introduction to Programming Using Java (98-388)

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CS251L REVIEW Derek Trumbo UNM

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming with Java

1B1b Classes in Java Part I

Mr. Monroe s Guide to Mastering Java Syntax

Programming Lecture 3

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CS 251 Intermediate Programming Methods and Classes

Getting started with Java

CompSci 125 Lecture 11

Lecture Set 4: More About Methods and More About Operators

CS171:Introduction to Computer Science II

BM214E Object Oriented Programming Lecture 4

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Week 6: Review. Java is Case Sensitive

Object Oriented Software Design

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CS 177 Week 15 Recitation Slides. Review

Review for Test 1 (Chapter 1-5)

Java Bytecode (binary file)

COMP Primitive and Class Types. Yi Hong May 14, 2015

Basics of Java Programming

ECE2049 E17 Lecture 2: Data Representations & C Programming Basics

Lecture Set 4: More About Methods and More About Operators

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

CS 251 Intermediate Programming Methods and More

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

3. Java - Language Constructs I

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Program Fundamentals

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

1007 Imperative Programming Part II

Introduction to the Java Basics: Control Flow Statements

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Introduction to Object-Oriented Programming

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

The Basic Parts of Java

Loops. CSE 114, Computer Science 1 Stony Brook University

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

204111: Computer and Programming

Chapter 1 Getting Started

PieNum Language Reference Manual

Object-Oriented Programming in Java

Language Fundamentals Summary

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Variables and Functions. ROBOTC Software

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

static String usersname; public static int numberofplayers; private static double velocity, time;

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

More Programming Constructs -- Introduction

Declarations and Access Control SCJP tips

Text Input and Conditionals

A variable is a name for a location in memory A variable must be declared

CS 231 Data Structures and Algorithms, Fall 2016

Unit 2: Java in the small

CS50 Supersection (for those less comfortable)

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Chapter 2: Data and Expressions

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Introduction to Java

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Fall 2017 CISC124 9/16/2017

Accelerating Information Technology Innovation

CS 106 Introduction to Computer Science I

CS 61B Data Structures and Programming Methodology. June David Sun

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Transcription:

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays Midterm Review Kage Weiss Lab 001 TA, SPR 17

Java Basics and Structure Class{ class variables; methods{ method variables; statements;

Java Basics and Structure Code blocks (classes, methods) are defined by braces { Classes: <publicity> class <name> {/* code here*/ Methods: <publicity> <return type> <name> (<parameter1 type> <parameter1 name>,<param2> ) {/* code here*/ Variables only exist in the code block they re defined in: block{ var x; X exists X does not exist anymore

Variables Variables are used to store information of declared data types. <data type> <variable name>; (e.g. int x; ) <data type> <variable name> = <value>; (e.g. int x = 5; ) Variables are assigned data via the assignment operator = e.g. int x = 5; Converting between the types can be done with type casting float y = (float) x; (y now equals 5.0) Variables must be declared before usage. Variables are either primitive data types or object data types

Variables Primitive data types: boolean - true or false - 1 bit char - Unicode Characters (2 bytes) byte - One byte (8 bits) short - Short integer (2 bytes) int - Regular integer (4 bytes) ( 2 31 = (231 1)) long - Large integer (8 bytes) float - Floating point (4 bytes) double - Floating point (8 bytes)

Relational Operators A == B // Is A equal to B? A!= B // Is A not equal to B? A < B // Is A less than B? A > B // Is A greater than B? A <= B // Is A less than or equal to B? A >= B // Is A greater than or equal to B? All of these operations return Booleans, true or false depending on the values of A and B

Assignment Operators x = y; //x now has the value from y x -= y; // same as: x = x - y; x += y; // same as: x = x + y; x *= y; // same as: x = x * y; x /= y; // same as: x = x / y; x %= y; // same as: x = x % y; (for integers x, y) q &&= p; // same as: q = q && p; (for booleans q, p) Increment and decrement: x++; // same as: x = x + 1; x--; // same as: x = x - 1;

Control Structures Branching Branching allows for code with choices and options, and is often used hand-in-hand with variables to clean and optimize code. Switch/case/default if/then, if/else if/else Loops Loops allow you to repeat a set of statements until certain conditions are met.

Branching The main options are if statements and switch statements: switch(test) { case <option>: case <option2>: break; default: if(test) { else if(test2) { else {

If Statements If statements are a simple code block inside a test, if the test fails it won t enter the block. If you place an else (or an else if) it will be evaluated when and only when the test fails. if(x > 0) { x /= 2; else if(x < 0) { x *= -1; else { x = 9; Remember the order of tests matters, if its passes the first it will not check any more.

Switch Statements Switch statements are similar to sets of if/else if/else if/else if/ tests. Based on the result of one test it will chose an option (case) //String x; switch(x) { case Op1 : case Op2 : break; default: If you don t break after a case, the program will fall through into the next code block inside the switch until it hits a break or a return statement.

Branching Remember all tests must be binary (return a Boolean), and don t forget your Boolean operators. A && B // AND : Are both A and B true? A B // OR: Are either A or B true?!a // NOT : Negate value of A (opposite) This can be used to string tests together inside one big test: //int x, y, z; if( x < 0 y > 0 (( z == x ) && ( z == y)) ) { /*Code here*/ Just remember: if it passes an OR or fails an AND before it completes all the tests, it wont bother testing them all.

Loops The main options are for loops and while loops: for(initial value; test; increment) { while(test) { Loops will continue until they fail their test, or until the program either break s or return s. A break will kick the program out of a loop, a return will leave the current method with the value included.

For Loops For loops generally operate on an index: declaring it, testing it, completing the code block, and then changing the index for(int i = 0; i < 9; i++) { System.out.println(i); For loops give you an index (i) to use inside the loop. Enhanced for loops give us a variable value instead of an index, if you know how to use them.

While Loops While loops just keep testing. (So make sure it ll fail the test at some point, or break/return.) //int x, y; while(x!= y) { x += ((y - x)/2); if(x > y) { break; While loops also have a special version. Want to run your block before you test? Use a do { while(test);

Loops Remember that loops will continue until their tests fail, so be sure the test has to fail at some point, or if you want it to loop infinitely, then be sure it ll break or return. Some infinite loops are shown here. for(int i = 100; true; i--) {, with an index while(true) { for( ; ; ) { //a for loop without an index?!?

Methods Methods are small (normally) blocks of code that are useful ore repeated so instead of typing out the full code or rewriting with a bunch of different variables, you use method calls. Methods are defined using the form: <publicity> <return type> <name>(<param1 type> <param1 name>, <param2> ) { /* code here*/ You call a method with the form: <name>(<params>);

Methods If an object has its own methods, you can call the methods for that object by saying <object>.<method>(<params>); For example, if we have a String x, and we want to see if it contains the letter y, we would say: boolean containsy = x.contains( y ); Where x is our object (String), contains is our method, and y is the String parameter we are passing in. Contains returns a Boolean, so we store the value of x.contains( y ) in our variable containsy. If we write our own method: public void methodname() { /*code here*/ We call it by saying: methodname();

Methods If there are two versions of a method that have different parameters, we call this overloading. Both exist, and java will determine which to call based on the parameters you give it in the method call. A method must return the type it declares as its return type.

Methods for Strings String y = x.replace( <String1>, <String2> ); Replaces all instances of String1 in x with String2 x.contains( <String> ) Returns true if <String> exists in x.indexof ( <String> ) Returns the index of the first appearance of <String> x.charat (int i) Returns the character at the index i x.touppercase () Returns the String x in all caps x.substring(int beginindex, int endindex) Returns the String from beginindex to endindex in x

Arrays An array is a variable that is an indexed collection of data. To declare an array, we use the format: <type>[] <name>; When we initialize it we must give it a size, the size for an array cannot change. W initialize an array as such: <type>[] <name> = new <type>[<size>]; For example if I wanted an array, book, of 30 Strings, I would say: String[] book = new String[30]; We can also initialize an array with set values: String[] book = { Arrays, are, fun, and, quite, useful,. ;

Arrays To reference a value in an array, we say <name>[<index>] These values work just like any other variable: book[0] = Words ; Arrays don t come pre-filled with nice values, arrays of numbers contain all 0 s, arrays of reference types contains all null. Remember, arrays indices start at 0, so an array of 30 Strings would have a String at book[0] and at book[29], but not at book[30]. You can always find the length of an array (number of values it hold) by saying <name>.length

Arrays You can also overwrite arrays, because as variables they are references to the information, not the information itself: String [] arr1 = { " Hello", " World" ; String [] arr2 = { " Goodbye", " World" ; arr1 = arr2 ; arr1 and arr2 both now point to the data { " Goodbye", " World" { "Hello", "World" gets thrown away because arr1 no longer references it.

Combinations Combinations of variables, arrays, methods, loops, and classes allow us to accomplish anything possible in java, though if you limit yourself to just these then some things might be very difficult. When writing any program start simple with what you re comfortable doing, and build from there both on your own knowledge and on your program.