Java for C++ Programmers Second Night. Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
New features in JDK 1.5 Can these new and complex features simplify Java development?
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Mr. Wortzman.  So far, we have gotten all our input and written all our output to the console  In reality, this is somewhat uncommon  Instead, we often.
Computer Programming Lab(7).
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Yoshi
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Computer Programming Lab 8.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 17.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Exceptions Handling Exceptionally Sticky Problems.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exceptions & Templates
Recitation 5 Enums and The Java Collections classes/interfaces 1.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 22.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Functions Input and output Lecture 2. Constants #define – is a preprocessor directive Most common use.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
C++ Lesson 1.
Object-Oriented Programming (OOP) Lecture No. 45
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
Haidong Xue Summer 2011, at GSU
Control Statement Examples
null, true, and false are also reserved.
Namespaces, Typedefs & Enums
More On Enumeration Types
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
CSE 303 Lecture 25 Loose Ends and Random Topics
CS250 Introduction to Computer Science II
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Java for C++ Programmers Second Night

Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates vs. Generics –STL vs. JavaSE API

Second Night Agenda Enumerations, Exceptions, Input/Output – enumeration declaration & usage, exception hierarchy, checked vs. unchecked exceptions, throwing & catching exceptions, scanner class, console I/O, file I/O –Discussion –Lab exercises Break Templates vs. Generics, STL vs. JavaSE API – –Discussion –Lab exercises

Enumerated Values in C++ One way to define a set of enumerated values/constants in C++ is as follows… Example usage… const int CLUBS = 0; const int DIAMONDS = 1; const int HEARTS = 2; const int SPADES = 3; // good invocations DrawSuit(CLUBS); DrawSuit(HEARTS); void DrawSuit(int s) { // draws the suit in a GUI }

Enumerated Values in Java Heres the Java port of that C++ code… Everything looks good, right? public class Suit { public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static final int HEARTS = 2; public static final int SPADES = 3; } // good invocations drawCard(Suit.CLUBS); drawCard(Suit.DIAMONDS); static void drawCard(int s) { // draws the suit in a GUI }

Enumerated Values in Java Well, sort of – as long as the user behaves themselves… Using something unbounded like an int or a String can be problematic, need to restrict the available choices // bad invocations drawCard(Suit.HEARTS * 5); drawCard(-516); static void drawCard(int s) { // draws the suit in a GUI }

Enumerations in Java Thankfully, theres a better way to enumerate a set of values, staring in Java 5 there is an enumerated type –Similar to enum construct in C/C++ –Enums are declared similarly to classes public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

Enumerations in Java Now, each constant is strongly typed When something is expecting a suit, we specify a Suit enum type Invalid usage is now caught at compile time // good invocations drawCard(Suit.CLUBS); drawCard(Suit.DIAMONDS); // compiler errors drawCard(Suit.HEARTS * 5); drawCard(-516); static void drawCard(Suit s) { // draws the symbol for the suit in a GUI }

Enumerations in Java Additionally, we can switch on enums… // draws the symbol for the suit in a GUI static void drawCard(Suit s) { switch(s) { case CLUBS: case SPADES: // switch color to black, then draw break; default: // switch color to red, then draw break; }

Enumerations in Java We can also give enums in Java additional members and methods public enum Planet { VENUS(4.8685e24,6051.8e3), EARTH(5.9736e24,6378.1e3), MARS( e24,3397e3); public static final double G = E-11; final double mass; final double radius; Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } double surfaceGravity() { return G * mass / (radius * radius); } double surfaceWeight(double otherMass) { return otherMass * surfaceGravity(); }

Enumerations Example usage, similar to a class… But we get compiler errors, if we try to construct more… // compiler errors Planet.EARTH = new Planet(1.0, 1.0); Planet p = new Planet(1.0, 1.0); // acceptable usage drawPlanet(Planet.EARTH); System.out.println("Surface gravity on earth: "); System.out.println(Planet.EARTH.surfaceGravity());

Exceptions in C++ C++ allows us to throw anything as an exception Here is a modified factorial function which throws the number back if it is less than 0 int factorial(int n) { // check for exceptional case if(n < 0) { throw n; } // computation for normal case int result = 1; for(int i = n; i > 0; i--) { result *= i; } return result; }

Exceptions in C++ Example catching of that exception in C++… int main(int argc, char** argv) { int num = -4; try { cout << "factorial(" << num << "): " << factorial(num) << endl; } catch(int i) { cerr << i << " is not valid" << endl; } return 0; }

Exceptions in C++ We can also throw more complex types, for example, here is a custom exception class which stores an error message… class Exception { private: string m_message; public: Exception(string message) : m_message(message) { }; string GetMessage() { return this->m_message; }; };

Exceptions in C++ Factorial function modified to throw custom exception type… int factorial(int n) { // check for exceptional case if(n < 0) { throw Exception("number must be positive"); } // computation for normal case int result = 1; for(int i = n; i > 0; i--) { result *= i; } return result; }

Exceptions Example catching of that exception in C++… int main(int argc, char** argv) { int num = -4; try { cout << "factorial(" << num << "): " << factorial(num) << endl; } catch(Exception e) { cerr << e.GetMessage() << endl; } return 0; }

Exceptions in Java In Java, we cannot throw primitives or most objects Anything that is thrown must be a Throwable (or a valid subclass) –Though, typically we throw Exceptions (or subclasses) ErrorException RuntimeException Error … … … Throwable Object

Exceptions in Java Heres a Java port of that second example –Were using the built-in Exception class… static int factorial(int n) { // check for exceptional case if(n < 0) { throw new Exception("number must be positive"); } // computation for normal case int result = 1; for(int i = n; i > 0; i--) { result *= i; } return result; }

Exceptions in Java Here is the invoking function in Java, with the try/catch block added… public static void main(String[] args) { int num = -4; try { System.out.println("factorial(" + num + "): " + factorial(num)); } catch(Exception e) { System.err.println(e.getMessage()); }

Exceptions in Java But, if we try to compile this, we get the following error… Why? $ javac.exe Factorial.java Factorial.java:19: unreported exception java.lang.Exception; must be caught or declared to be thrown throw new Exception("number must be positive"); ^ 1 error

Java Exception Hierarchy Java breaks up Exceptions into 2 categories –Unchecked: you do not need to explicitly handle these exceptions –Checked: you must handle these in your code, failure to do so is a compiler error Checked Unchecked ErrorException RuntimeException Error … … … Throwable Object

Handling Checked Exceptions We have one of 2 options… –Handle the exception locally (wrap code in a try/catch block) –Propagate the exception by adding a throws declaration to the method signature

Exceptions in Java The modified factorial function modified to declare an thrown exception… static int factorial(int n) throws Exception { // check for exceptional case if(n < 0) { throw new Exception("number must be positive"); } // computation for normal case int result = 1; for(int i = n; i > 0; i--) { result *= i; } return result; }

Exceptions in Java Other notes about exceptions in Java… –e.printStackTrace() will print out the full stack trace as to where an exception originated –e.getMessage() will give you a detailed message string as to why the exception occurred –Try/catch blocks may also have a finally section with code in it Always executed even if a return or throw is encountered

Console I/O in C++ #include using namespace std; int main() { int n; string s; cout << "What do you want to buy: "; cin >> s; cout << "How many: "; cin >> n; cout << "You want " << n << " " << s << endl; return 0; } Simple C++ class that reads/writes to the console

Console Input/Output in Java Already looked at System.out for print() and println(), though there are many more such as –printf() – C style output (added in Java 5) –write() – byte based output System.err provides the same methods as System.out does, goes to stderr System.in interface is our standard input stream –System.in is an InputStream class and is pretty low level (read bytes) –Typically another class is used to read from System.in

Console Input in Java Scanner class was added in JSE 5 to simplify input… –Has a constructor which takes an InputStream class Scanner(System.in) –Methods are available to read common data types… nextBoolean() nextFloat(), nextDouble() nextShort(), nextInt(), nextLong() next() - reads next String –Corresponding methods to check and see if another value is in stream… i.e. hasNextInt()

Console I/O in Java Java port of our C ++ example… import java.util.Scanner; public class ConsoleIO { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("What do you want to buy: "); String s = console.next(); System.out.print("How many: "); int n = console.nextInt(); System.out.println("You want " + n + " " + s); }

File Input in C++ Example reading integers from a file… #include using namespace std; int main() { int num; fstream in("file.txt", ios::in); while(in >> num) { cout << "read " << num << endl; } in.close(); return 0; }

File Input in Java Java port of C++ example… import java.io.File; import java.util.Scanner; public class FileIO { public static void main(String[] args) { try { Scanner in = new Scanner(new File("file.txt")); while(in.hasNextInt()) { System.out.println("read " + in.nextInt()); } in.close(); } catch (Exception e) { e.printStackTrace(); }

File Output in C++ Output to a file in C++… #include using namespace std; int main() { fstream out("file.txt", ios::out); for(int i = 0; i < 10; i++) { out << i << endl; } out.close(); return 0; }

File Output in Java Java port of C++ example (PrintStream is same class as System.out, so same methods available)… import java.io.PrintStream; public class FileIO { public static void main(String[] args) { try { PrintStream out = new PrintStream("file.txt"); for(int i = 0; i < 10; i++) { out.println(i); } out.close(); } catch (Exception e) { e.printStackTrace(); }

Exercises 1.Write an Direction enum for the 4 cardinal directions (North, South, East & West). Then write (in another class) a walk method which takes a Direction enum as an argument 2.Write a class which reads numbers from one file (the 1 st command line arg) and writes out each number squared to a second file (the 2 nd command line arg)

Break

C++ Templates C++ allows us to define generic (template) types which can hold whatever type we specify Frequently people include the C file at the end of the header file Gotchas… –Compilation –Mixing templated and non-templated code –Spaces with respect to nested templates

C++ Templates Example node header file… #ifndef _NODE_H_ #define _NODE_H_ template class Node { private: T m_data; public: Node(T data); T GetData() const; }; #include "node.C" #endif

C++ Templates Corresponding implementation in C++ #ifndef _NODE_C_ #define _NODE_C_ #include "Node.H" template Node ::Node(T data) { this->m_data = data; } template T Node ::GetData() const { return this->m_data; } #endif

C++ Templates Example usage… #include #include "node.H" using namespace std; int main() { Node n("hamburger"); cout << n.GetData() << endl; return 0; }

Java Generics Java introduced Generics in Java 5 which are similar to C++s templates –Syntax is a bit less burdensome Parts of the Java SE API are capable of using generics –Prior to Java 5, all of these things stored Objects and were thus capable of storing any object what-so-ever (as it had to be a subclass of Object)

Java Generics Port of templated Node class to Java… public class Node { private T data; Node(T data) { this.data = data; } T getData() { return this.data; }

Java Generics Example driver… public class NodeTest { public static void main(String[] args) { Node n = new Node ("hamburger"); System.out.println(n.getData()); }

Generics Gotchas Cannot use primitive types as the generic type… –In C++ this is completely valid… –In Java, this is not… –We can still store an int, char, float or boolean, but we need to do something slightly different Node n(516); Node n = new Node (516);

Wrapper Classes For each of the primitive types, Java provides a wrapper class Wrapper class is an object which stores the given primitive type… –Integer – stores an int primitive –Boolean – stores a boolean primitive –Float – stores a float primitive –Etc… We can declare our generic type to be of the wrapper class type

Wrapper Classes & Generics Example using the Integer wrapper class to store ints In Java 5, primitives are automatically converted to and from the corresponding wrapper type as needed –This is known as auto boxing/un-boxing public class NodeTest { public static void main(String[] args) { Node n = new Node (516); System.out.println(n.getData()); }

Generics Gotchas In C++, you can create an array of a templates type… Java does not permit this, you end up with a compiler error… template void Node ::SomeTemplatedFunction() { T array[10]; } void SomeTemplatedFunction() { T array[10]; }

Generics Bounding Allow a method to take anything that subclasses T Allow only classes that implement the Comparable interface… void foo(Collection items) { /*... */ }

C++ STL The C++ STL provides numerous built-in data types that are used by programmers Some of the more commonly used ones are… –vector –iterators –deque –list –map

Java SE API One thing you will notice about Java is that the Java SE API is huge –Java SE 6 has Nearly 3800 classes, interfaces & enums in the built-in API Bookmark the javadocs, they are your friend… –

Java Collections Framework Java has a robust collections (containers) framework –With Java 5, these have been generified to support arbitrary types with strong typing –There are interfaces provided for many ADTs List, Set, Map, etc… –There are also multiple concrete implementations of many of the data structures, each providing their own advantages… ArrayList, LinkedList, Stack, Vector, etc… –Typically you instantiate a concrete implementation, but maintain a handle on it based on its interface Interface = ImplementationOfInterface ();

C++ Vector Sample C++ Vector usage… #include using namespace std; int main() { vector v; v.push_back(1); v.push_back(20); v.push_back(2009); for(int i = 0, n = v.size(); i < n; i++) { cout << v[i] << endl; } for(vector ::iterator itr = v.begin(); itr != v.end(); itr++) { cout << *itr << endl; } return 0; }

Java Vector Port of the C++ vector example… import java.util.Vector; public class VectorTest { public static void main(String[] args) { Vector v = new Vector (); v.add(1); v.add(20); v.add(2009); for(int i = 0, n = v.size(); i < n; i++) { System.out.println(v.get(i)); } for(int tmp : v) { System.out.println(tmp); }

C++ List Sample C++ Vector usage… #include using namespace std; int main() { list l; l.push_front(1); l.push_front(2); l.push_back(3); l.push_back(4); list ::iterator itr; for (itr = l.begin(); itr != l.end(); itr++) { cout << *itr << endl; } cout << l.front() << endl; l.pop_front(); cout << l.front() << endl; }

Java List Port of the C++ list example… import java.util.LinkedList; import java.util.List; public class ListTest { public static void main(String[] args) { List l = new LinkedList (); l.add(1); l.add(0, 2); l.add(l.size(), 3); l.add(l.size(), 4); for(int i : l) { System.out.println(i); } System.out.println(l.get(0)); l.remove(0); System.out.println(l.get(0)); }

C++ Map Sample C++ map usage… #include using namespace std; int main() { map courses; courses[201] = "Computer Science I"; courses[202] = "Computer Science II"; courses[341] = "Data Structures"; map ::iterator itr; for (itr = courses.begin(); itr != courses.end(); itr++) { cout first second << endl; } return 0; }

Java Map Port of the C++ map example… import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class MapTest { public static void main(String[] args) { Map courses = new TreeMap (); courses.put(201, "Computer Science I"); courses.put(202, "Computer Science II"); courses.put(341, "Data Structures"); Iterator itr; for (itr = courses.keySet().iterator(); itr.hasNext();) { int num = itr.next(); System.out.println(num + ": " + courses.get(num)); }

C++ Deque Sample C++ deque usage… #include using namespace std; int main() { deque d; d.push_front(1); d.push_front(2); d.push_back(3); d.push_back(4); deque ::iterator itr; for (itr = d.begin(); itr != d.end(); itr++) { cout << *itr << endl; } cout << d.front() << endl; d.pop_front(); cout << d.front() << endl; return 0; }

Java Deque Port of the C++ deque example… import java.util.ArrayDeque; import java.util.Deque; public class DequeTest { public static void main(String[] args) { Deque d = new ArrayDeque (); d.addFirst(1); d.addFirst(2); d.addLast(3); d.addLast(4); for (int i : d) { System.out.println(i); } System.out.println(d.getFirst()); d.removeFirst(); System.out.println(d.getFirst()); }

Exercises 1.Create, compile and run a program which puts several Person objects into a Vector and iterates over them and prints each out The members in a Person are up to you, but there should be at least one, and toString should overridden