Java Review Outline. basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Similar documents
Introduction to Programming Using Java (98-388)

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Getting started with Java

Declarations and Access Control SCJP tips

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

COMP1008 Exceptions. Runtime Error

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Good Coding Practices Spring 2018

Object Oriented Programming

Java Errors and Exceptions. Because Murphy s Law never fails

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exception-Handling Overview

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CS 3 Introduction to Software Engineering. 3: Exceptions

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Casting. References. References

CS193j, Stanford Handout #25. Exceptions

Fun facts about recursion

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Pace University. Fundamental Concepts of CS121 1

Exception Handling in C++

(Not Quite) Minijava

Algorithms & Datastructures Laboratory Exercise Sheet 1

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Introduction Basic elements of Java

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Program Correctness and Efficiency. Chapter 2

Checked and Unchecked Exceptions in Java

Exceptions and Design

CS 61B Discussion 5: Inheritance II Fall 2014

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

CSE 331 Software Design & Implementation

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS111: PROGRAMMING LANGUAGE II

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

CS 106 Introduction to Computer Science I

A Short Summary of Javali

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Object-Oriented Programming Design Topic : Exception Programming

Java Review: Objects

About this exam review

THE CONCEPT OF OBJECT

Introduction to Java

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CS 231 Data Structures and Algorithms, Fall 2016

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

COS 126 General Computer Science Spring Written Exam 1

BBM 102 Introduction to Programming II Spring 2017

Exceptions and Error Handling

Array. Prepared By - Rifat Shahriyar

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

1 Shyam sir JAVA Notes

Introduction to Programming (Java) 2/12

Object Oriented Programming

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

BBM 102 Introduction to Programming II Spring Exceptions

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Course Outline. Introduction to java

Announcements. 1. Forms to return today after class:

Fundamentals of Programming Session 12

Programming II (CS300)

CSCI Object Oriented Design: Java Review Errors George Blankenship. Java Review - Errors George Blankenship 1

CSCI Object-Oriented Design. Java Review Topics. Program Errors. George Blankenship 1. Java Review Errors George Blankenship

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Programming Languages and Techniques (CIS120)

A brief introduction to C programming for Java programmers

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

1 Lexical Considerations

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

Programming refresher and intro to C programming

Lab Assignment Three

Object Oriented Programming Exception Handling

Program Fundamentals

CSC Java Programming, Fall Java Data Types and Control Constructs

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

CSE 142 Su 04 Computer Programming 1 - Java. Objects

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

Chapter 13 Exception Handling

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

EECS168 Exam 3 Review

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CS11 Introduction to C++ Fall Lecture 6

Computer Components. Software{ User Programs. Operating System. Hardware

Exception Handling. Chapter 9

CS 11 java track: lecture 1

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT

BASIC ELEMENTS OF A COMPUTER PROGRAM

6.Introducing Classes 9. Exceptions

CS61B, Summer 2002 Exam #1 Barath Raghavan

1B1b Classes in Java Part I

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

COP 3330 Final Exam Review

In this lab we will practice creating, throwing and handling exceptions.

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Transcription:

Java Review Outline basics exceptions variables arrays modulo operator if statements, booleans, comparisons loops: while and for

Java basics write a simple program, e.g. hello world http://www2.hawaii.edu/~esb/2017fall.ics211/helloworl d.java.html how many classes does this program create? how many objects can you find in this program? how many methods are implemented by this program? where does a program begin execution? how do you declare that? what is the file name? how do you print a string?

Java development environment: no IDE edit a text file using an editor which editors have been used by this class? then compile using? then execute using? JDK can compile, JRE (included in JDK) can execute compilation errors are not the same as execution errors: how do they differ?

Java development environment: IDE Integrated Development Environment includes editor, compiler, execution engine Eclipse, jgrasp, NetBeans the Java coding standard helps other people read your program, no matter what IDE you and they use

Java errors humans make mistakes: errare humanum est compiler errors are due to programmer lack of knowledge, to sloppiness, or to hurry run-time errors are due to programmer sloppiness, hurry, or lack of understanding typical run-time errors may be due to unexpected inputs, e.g. a non-numeric string where a number was expected improvements: check values to make sure they fit your expectations, have a sane response if they don t

Java Exceptions (book, Appendix A.11) try block followed by one or more catch blocks try { /* code which may throw exception */ catch (exceptionclass1 variable1) { /* code which handles exception of type exceptionclass1 */ catch (exceptionclass2 variable2) { /* code which handles exception of type exceptionclass2 */ finally { /* code executed after try or catch */

Java Exceptions II example (from the textbook, p. 608) try { // statements that perform file-processing operations catch (IOException ex) { ex.printstacktrace(); // display stack trace System.exit(1); // exit with an error indication an uncaught exception is caught by the runtime system: this is a run-time error (but not all run-time errors result in exceptions) an example showing try/catch and command-line arguments http://www2.hawaii.edu/~esb/2017fall.ics211/substring.java.html another example program using try/catch and GUI I/O http://www2.hawaii.edu/~esb/2017fall.ics211/changedue.java.html

Java variables local variables only have scope from their declaration to the end of the block parameters have scope within the method smaller declarations override larger declarations, but: this is bad programming practice, since a reader may not know which of multiple equally-named variables the programmer is using, e.g. private void countupto (int count) { count = 0; while (count < 1000) { int count = 15; count = count + 3; when there is more than one such variable, this code will refer to the innermost count variable but maybe programmer wants to refer to the outer count variable!

Java arrays arrays are objects that contain other objects, the array elements each element is identified by an integer value in (0...array.size-1), and the array index or subscript like any variable, arrays must be declared unlike integers or booleans, arrays must be initialized, that is, allocated or instantiated, e.g. String [] a = new String [15]; boolean [] bits = new boolean [99]; which creates an array of size 15 with indices 0..14 the expression "a [7]" has what type? see the ChangeDue example

array initialization an array that is not initialized to an allocated value has the special value null, which cannot be indexed initializing the array (the container) is different from initializing each of the elements: the array must be initialized first, then the elements initializing array elements one by one can be as follows coinnames[4] = "half"; This could be done within a loop, e.g. a[i] = i*7; or all can be initialized and allocated at once, e.g. double [] x = {1.0, 2.71, 3.14, 4.0;

array operations the length of an array is one more than the highest usable index int i = 0; while (i < x.length) { x [i] += 1.0; arraycopy can be used to copy elements from one existing array to another existing array: System.arraycopy(sourceArray, sourcepos, destarray, elementcount); destpos, all the element positions must be valid, specifically: sourcepos through sourcepos + elementcount - 1 on sourcearray, and destpos through destpos + elementcount - 1 on destarray

The modulo (%) operator integer division gives two results: a quotient and a remainder for example 11/4 = 2 with a remainder of 3 the quotient is given by / int quotient = 11 / 4; // 2 the remainder is given by %, known as modulo int remainder = 11 % 4; // 3 this code computes a random bit, 0 or 1: Random myrandomgenerator = new Random(); int random = myrandomgenerator.nextint() % 2; modulo will be used throughout this class

example using modulo suppose I want to use an array in a circular fashion so that the last location in the array is followed by the first location in the array For example, suppose the array had values for traffic light signals: String [] lights = { "green", "yellow", "red" ; int lightisnow = 0; then, to cycle through the lights, all we need is: lightisnow = (lightisnow + 1) % lights.length; if light is red (lightisnow has the value 2), the next light will be green (lightisnow has the value 0)

booleans and boolean expressions variables of type boolean have either the value true or the value false expressions of type boolean have either the value true or the value false for example, x < y is either true or false arithmetic values or expressions can be used in comparisons: < <= ==!= >= > for objects, == compares the addresses -- are these two the same object -- whereas the equals method may perform a different comparison (and is usually the one to use) boolean expressions or values can be combined using the logical operators: &&! boolean expressions and values are used to determine which code gets executed when in conditional statements and loops

conditional statements boolean b = false; if (x > y) { a = 3.0; else if (! b) { a = 2.2; else { x = 99; the part after the first if is executed if the first condition is true otherwise the part after the first else is executed in-class exercise: determine the output of this program: http://www2.hawaii.edu/~esb/2013fall.ics211/equals.java.html

loops These two sets of statements are equivalent: int x = 99; while(x > 0) { removebottleofbeerfromwall(); x = x - 1; and for (int x = 99; x > 0; x--) { removebottleofbeerfromwall();

loops, continued a while loop is good when something needs to be done zero or more times: while(x > 0) { x -= y; y = y * 2; do.. while can be used to execute statements one or more times: do { x -= y; y = y * 2; while(x > 0); a for loop is particularly useful with loop variables: for (int i = 0; i < a.length; i++) { a [i] *= 2; what does this program do? http://www2.hawaii.edu/~esb/2013fall.ics211/mysteryprogram1.java.html

loops, continued a while loop is good when something needs to be done zero or more times: while(x > 0) { x -= y; y = y * 2; do.. while can be used to execute statements one or more times: do { x -= y; y = y * 2; while(x > 0); a for loop is particularly useful with loop variables: for (int i = 0; i < a.length; i++) { a [i] *= 2;

summary Java primitives you should already be familiar with anything you are not familiar with, become familiar with, by using the course resources: the TA in lab, the instructor, the course web page, the textbook (Appendix A), and the reference (The Java Language Specification). Be sure you know the material in Appendix A of the textbook. Please skim it and make sure you are comfortable with all the subjects in the appendix.