CS 302 Week 15 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Advertisements

By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
Types in programming languages1 What are types, and why do we need them?
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
Intro to ETEC Java.
Chapter 10 – Exception Handling
Lecture 5: Some more Java!
CSE 190D, Winter 2013 Building Java Programs Chapter 1
Introduction to OO Program Design
Multithreading in Java
CS Week 10 Jim Williams, PhD.
Comp Sci 200 Programming I Jim Williams, PhD.
CS Week 14 Jim Williams, PhD.
CS 302 Week 14 Jim Williams, PhD.
An Introduction to Java – Part I
CS 200 Branches Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS 200 File Input and Output
Computer Science II Exam 1 Review.
CS 200 Using Objects Jim Williams, PhD.
Advanced Programming Behnam Hatami Fall 2017.
CS 200 Command-Line Arguments & Exceptions
CS Week 7 Jim Williams, PhD.
Java Programming Language
CS Week 3 Jim Williams, PhD.
An Introduction to Java – Part I, language basics
CS 200 More Classes Jim Williams, PhD.
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
CS 200 More Primitives, Objects, Branches and Methods
CS Week 3 Jim Williams.
CS 200 Command-Line Arguments & Exceptions
CS 200 Loops Jim Williams, PhD.
CS 200 Additional Topics and Review
CS 302 Week 13 Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Arrays Jim Williams, PhD.
CS 200 Primitives and Expressions
CS 200 Additional Topics and Review
Lecture 11 Objectives Learn what an exception is.
CS 200 Objects and ArrayList
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
CS 200 More Classes Jim Williams, PhD.
CS Week 2 Jim Williams, PhD.
CS 200 Command-Line Arguments & Exceptions
Week 7 CS 302 Jim Williams.
Ben Stanley for gAlpha gALPHA free, four-week venture-creation workshop designed to help entrepreneurially-minded students and technologists create high-growth.
CS 200 Objects and ArrayList
Java Programming: From Problem Analysis to Program Design, 4e
CS302 Week 6 Jim Williams.
CS 200 Additional Topics and Review
CS 240 – Advanced Programming Concepts
Presentation transcript:

CS 302 Week 15 Jim Williams, PhD

This Week Course Evaluations (currently ~40%, survey closes May 5th) Lab: Comparing Playing Cards using Java interfaces Lecture: Review initially exceptions and "everything else". Final Exam: Wednesday, May 10th location

Exception Handling 3 categories of Throwables creating exception (checked or unchecked) 2 ways of handling exceptions _________ checks that a checked exception is handled.

Exception Handling class MyException extends Exception { public class Test { public static void methodE( double value) throws MyException { if ( value < 5.0) throw new MyException(“value < 5.0”); } public static void main( String []args) throws MyException { try { methodE( 1.5); System.out.println(“after”); } catch ( MyException e) { e.printStackTrace(); System.out.println( e.getMessage()); //throw e; } finally { System.out.println(“always done”); System.out.println(“if not caught or is rethrown, not done.”); class MyException extends Exception { MyException(String msg) { super( msg) }

If this compiles, what is true? static void methodB(int i) { if ( i < 3) throw new ExceptionQ(); } public static void main(String[] args) { try { methodB( 3); System.out.print("B"); } catch( ExceptionQ e) { System.out.print("C"); } finally { System.out.print("D"); System.out.print("E"); ExceptionQ is an checked exception CDE is printed D only is printed BDE is printed try it

If methodA throws MyException2 then output is? public static void main(String[] args) throws MyException { try { methodA( 3); System.out.print("B"); } catch( MyException2 e) { System.out.print("C"); } catch( MyException e) { throw e; System.out.print("D"); } System.out.print("E"); BCE CDE CE BD try it

Course Review Basic fluency in Java P1 - sequence, conditions, loops, console I/O P2 - static methods, parameter passing, arrays P3 - classes, instances, instance methods, exceptions, file IO Incremental Development and Testing Tracing code Basic debugging - print statements, breakpoints

Eclipse IDE (Integrated Development Environment) Interpreter Translator Users Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

Programming Errors Syntax/Compile time Runtime & Logic Files Editor (Virtual) Machine Compiler Hello.java Hello.class Computer Files Programmer

Memory int k; Integer m; //where in memory, what values will they hold and when? //if local variables //if parameters //if class variables //if instance variables

Primitives and Wrapper classes Draw a picture of memory and describe each. public static void main( String []args) { int k; Integer m; k = 2; //example of ? m = 3; //example of ? k = m; //example of ? }

Strings and memory class S { public static void main( String []args) { String name; //Where and what is the value? new String(“dog”); //What and where is the value? name = new String(“happy”); //What and where is the value? }

http://kerncountylibrary.org/event/may-the-4th-be-with-you-celebration/

Office Hours Next Week: Tuesday, 9am to 11am

Course Evaluations

What do these do? for ( char ch = 'A'; ch < 'Z'; ch += 3) { System.out.print( ch); } ArrayList<Bug> bugs = new ArrayList<>(); bugs.add( new Bug()); for ( int i = 0; i < bugs.size(); i++) { }

What is print out? static void myPrint( int out) { System.out.println( “i: “ + out); } static void myPrint( String s) { System.out.println( “s: “ + s); …. in main…. int i = 5; myPrint( i + "3" + (3 + 4 * 6) + 4);

Draw a picture of memory, at each time point class Dag { static int smop; Hup h; Dag( int smop, Hup h) { Dag.smop = smop; this.h = h; //4 } class Hup { double gep; Hup( double g) { gep = g; //2 class TestDag { Dag methodB( Hup p) { Dag t; //3 t = new Dag( 2, p); t = new Dag( 3, p); return t; //5 } main() { Dag y; //1 y = methodB( new Hup( 13)); //6 shadowing static vs. non-static instantiation memory (stack & heap) instance variables/fields/attributes class variables constructors tracing OO code

public class Stove { int heat = 5; public boolean equals(Stove s) { return this.heat == s.heat; } public boolean equals(Object o) { return this.heat == ((Stove)o).heat; public static void main( String []args) { Object o = new Stove(); Stove s1 = new Stove(); Stove s2 = new Stove(); System.out.println( s1.equals(o)); System.out.println( o.equals(s1)); System.out.println( s1.equals(s2)); Will this compile? Which equals method overrides Object’s? Are the equals methods overloaded? s1.equals( o) which method is called? o.equals( s1) which method is called? s1.equals( s2) which method is called? Is the explicit cast an upcast or downcast? Is the explicit cast safe? If not, what would safe syntax be?

What is print out? 8 line 3 5 line hello 2 8 line 5 line 3 line 4 line File f = new File( "myfile.txt"); Scanner scnr = new Scanner( f); scnr.nextLine(); int count = 4; if ( scnr.hasNextInt()) count = scnr.nextInt(); else { } for ( int i = 0; i < count; i++) System.out.println( scnr.nextLine()); 8 line 5 line 3 line other 3 hello 2 8 line 5 line 3 line 4 line

What is the bug? class TrackCount extends IntList { class IntList { private ArrayList<Integer> list = new ArrayList<>(); public void add( int i) { list.add( new Integer(i)); } public void add2( int i, int j) { add( i); add( j); class TrackCount extends IntList { int count = 0; public void add( int i) { this.count++; super.add( i); } public void add2( int i, int j) { this.count += 2; super.add2( i, j); //TEST CODE TrackCount tc = new TrackCount(); tc.add(3); tc.add2( 2, 4); System.out.println( "tc.count=" + tc.count);

What is the bug? public class Super { public Super() { overrideMe(); } public void overrideMe() { public final class Sub extends Super { private final Date date; Sub() { date = new Date(); } @Override public void overrideMe() { System.out.println(date); //Test Code Sub sub = new Sub(); sub.overrideMe(); From Joshua Bloch, Effective Java

https://img. clipartfest https://img.clipartfest.com/bdb09f4961c77985d87988f6b04f4eaf_celebrating-50-years-gary-c-celebrating-50-years-clipart_868-804.jpeg

New Sequence Fall 2017 CS200 Structured Programming CS300 OO Programming Basic Data Structures CS400 Advanced Data Structures Software Project Skills Current Sequence: CS302 Structured and OO Programming CS367 Basic and Advanced Data Structures

Good Luck on your Final Exams!