C++ -> Java - A High School Teacher's Perspective David B. Levine and Alyce Brady (modified by Mike Scott)

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

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Basic Elements of C++ Chapter 2.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Review of C++ Programming Part II Sheng-Fang Huang.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
Java and C++, The Difference An introduction Unit - 00.
Data Structures Using C++ 2E
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
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.
Pointers OVERVIEW.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
Session 7 Methods Strings Constructors this Inheritance.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Written by: Dr. JJ Shepherd
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Topic: Classes and Objects
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter Topics The Basics of a C++ Program Data Types
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
C++ -> Java - A High School Teacher's Perspective
Basic Elements of C++ Chapter 2.
Java Programming Language
7 Arrays.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

C++ -> Java - A High School Teacher's Perspective David B. Levine and Alyce Brady (modified by Mike Scott)

AP Computer ScienceAP Computer ScienceC++ to Java2 Motivation Question at a College Board Workshop “How much (and what) will change when I switch from teaching C++ to teaching Java?”

AP Computer ScienceAP Computer ScienceC++ to Java3 Today’s Approach Consider a few representative topics in each language Show examples Make comparisons

AP Computer ScienceAP Computer ScienceC++ to Java4 Java Primitive Data Types Data TypeCharacteristicsRange byte 8 bit signed integer short 16 bit signed integer int 32 bit signed integer-2,147,483, ,147,483,648 long 64 bit signed integer-9,223,372,036,854,775, ,223,372,036,854,775,808 float 32 bit floating point number + 1.4E-45 to E+38 double 64 bit floating point number + 4.9E-324 to E+308 boolean true or false NA, note Java booleans cannot be converted to or from other type char 16 bit, UnicodeUnicode character, \u0000 to \uFFFF

AP Computer ScienceAP Computer ScienceC++ to Java5 Java Operators Basic Assignment: = Assignment Operators: +=, -=, *=, /=, %= Arithmetic Operators: +, -, *, /, %(remainder) integer, floating point, and mixed arithmetic and expressions increment and decrement operators: ++, -- Relational Operators: >, >=, <, <=, ==, != Logical Operators: &&, ||, ! Bitwise Operators, Ternary Operator

AP Computer ScienceAP Computer ScienceC++ to Java6 Control Structures in C++ int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; } Counting Odd Elements in a 20-element array

AP Computer ScienceAP Computer ScienceC++ to Java7 Control Structures in Java int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; } Counting Odd Elements in a 20-element array

AP Computer ScienceAP Computer ScienceC++ to Java8 Variable declarations in C++ // Primitives int x; double d; bool done; // Objects Coin penny; Dice cube(6);

AP Computer ScienceAP Computer ScienceC++ to Java9 Variable declarations in Java // Primitives int x; double d; boolean done; // Objects Coin penny = new Coin(); Dice cube = new Dice(6);

AP Computer ScienceAP Computer ScienceC++ to Java10 Variable declarations in Java // Primitives int x; double d; boolean done; // Objects Coin penny = new Coin(); Dice cube = new Dice(6);

AP Computer ScienceAP Computer ScienceC++ to Java11 Key difference in Java Object Variables are references (pointers) to objects, not objects Variable declaration merely creates a place to store a reference, not an object Dice cube; Must construct objects explicitly Dice cube = new Dice(6); Can have multiple references to same object - sharing

AP Computer ScienceAP Computer ScienceC++ to Java12 Pointers in Java Pointers exist in Java, but in a very restricted form. int* intPtr; // syntax error The programmer does not choose which variables are pointers in Java. Pointers to primitive data types cannot be declared. Variables who data type are non primitive (arrays, Strings, all types of objects) are always pointers Must dynamically allocate objects using the new operator and call to a constructor Rectangle rect1 = new Rectangle() Dereferencing occurs automatically with dot operator. or array access []

AP Computer ScienceAP Computer ScienceC++ to Java13 Variable Initialization Java void test() { int x; int y = x * 2; // syntax error } public class Test { int x; // instance variable // x set to 0 when // object created } C++ void test() { int x; int y = x * 2; // warning, but program // can will run }

AP Computer ScienceAP Computer ScienceC++ to Java14 Member function calls in C++ penny.flip(); robot.moveTo(newLoc);

AP Computer ScienceAP Computer ScienceC++ to Java15 Method invocation in Java penny.flip(); robot.moveTo(newLoc);

AP Computer ScienceAP Computer ScienceC++ to Java16 Parameter Passing C++ Value (default) Reference (if specified) const Reference (if specified) Java Value (for primitives) Value(for references!, so has reference like behavior)

AP Computer ScienceAP Computer ScienceC++ to Java17 Parameter Passing Java All parameters are passed by value. Parameters is always a copy of the argument it is sent. no modifiers on parameters void test(int x, int y) C++ Parameters can be passed by value (a copy) or reference (the actual variable) or constant reference (the actual variable, but it cannot be changed.) no modifier & const &

AP Computer ScienceAP Computer ScienceC++ to Java18 Classes and Objects C++ public/private/protected data and methods Specify interfaces through.h files Topic can be ignored (albeit unadvisedly) Java public/private/protected data and methods (declare visibility of each separately) Specify external view through documentation Topic CANNOT be ignored (though some try!) best to work with Objects from the start

AP Computer ScienceAP Computer ScienceC++ to Java19 Key difference in Java Everything is tied to a class Almost everything is tied to an object Some data and methods are tied to classes, not objects public static void main(String[] args) Math.abs(int i) System.out.println(StringExpression)

AP Computer ScienceAP Computer ScienceC++ to Java20 Hello World in C++ #include int main() { cout << “Hello world” << endl; return 0; }

AP Computer ScienceAP Computer ScienceC++ to Java21 Hello World in Java public class Hello { public static void main(String [ ] args) { System.out.println(“Hello world”); }

AP Computer ScienceAP Computer ScienceC++ to Java22 Strings in C++ (AP C++) apstring s; apstring s1 = “Hello, ”; apstring s2(“World”); apstring s3 = s2; cout << s1 + s3;

AP Computer ScienceAP Computer ScienceC++ to Java23 Strings in Java String s; String s1 = “Hello, ”; String s2 = new String(“World”); String s3 = s2; System.out.println(s1 + s3);

AP Computer ScienceAP Computer ScienceC++ to Java24 Strings are Special in Java String constants look like those in C/C++ String s1 = “Hello, ”; + operator is overloaded for Strings - String concatenation System.out.println(s1 + s3);

AP Computer ScienceAP Computer ScienceC++ to Java25 I/O (A Key Difference) C++ cin/cout, streams text-based (graphics are an add-on) may be tested on the exam Java System.out.println(StringExpression); graphical i/o: forms, applets, text fields, etc industrial-strength class hierarchy USE WHATEVER YOU ARE COMFORTABLE WITH!

AP Computer ScienceAP Computer ScienceC++ to Java26 Standard Output Done via a method (function) call int x = 12; System.out.print( "x is " + x ); // no carriage return System.out.println("x is still " + x ); // println adds carriage return Note the + operator. If either operand of + is a String the + performs string concatenation. the other operand is automatically converted to a String

import java.io.InputStreamReader; import java.io.BufferedReader; public class ReadAndEchoNumber { public static void main(String[] args) {InputStreamReader iReader = new InputStreamReader(System.in); BufferedReader bReader = new BufferedReader(iReader); System.out.print("Enter a number and press return: "); try { String result = bReader.readLine(); double num = Double.parseDouble(result); System.out.println("You entered: " + num); } catch(Exception e) { System.out.println("An Exception occurred while trying to" + " read and output your number"); System.out.println("The Exception is " + e.toString() ); } GACK!! Standard Input

AP Computer ScienceAP Computer ScienceC++ to Java28 Input Using a Window JOptionPane is a built in class from the Java Standard Library Advantages a standard class students make a window! Disadvantages calling static methods must still parse numbers

AP Computer ScienceAP Computer ScienceC++ to Java29 JOptionPane Example import javax.swing.*; public class Inputex { public static void main(String[] args) { String name = JOptionPane.showInputDialog("Please enter your name:"); JOptionPane.showMessageDialog(null, "Hello " + name ); //System.out.println("Hello " + name ); String ans = JOptionPane.showInputDialog("How old are you " + name + "?"); int age = Integer.parseInt( ans ); JOptionPane.showMessageDialog(null, name + ", you will be " + (age + 1) + " your next birthday."); //System.out.println( name + ", you will be " //+ (age + 1) + " your next birthday."); }

AP Computer ScienceAP Computer ScienceC++ to Java30 Input using a Third Party Class Lots of teachers have put together simple IO classes A good one is EasyReader / EasyWriter from Skylight Publishing Advantages practice using objects easy to use can also read from files Disadvantage not standard

AP Computer ScienceAP Computer ScienceC++ to Java31 EasyReader Example public class Inputex2 { public static void main(String[] args) { EasyReader keyboard = new EasyReader(); System.out.print("Please enter your name:"); String name = keyboard.readLine(); System.out.println("Hello " + name ); System.out.print("How old are you " + name + "?"); int age = keyboard.readInt(); System.out.println( name + ", you will be " + (age + 1) + " your next birthday."); } }

AP Computer ScienceAP Computer ScienceC++ to Java32 Linear Aggregate Structures C++ arrays No bounds checking; can’t resize C++ vectors (apvector) Bounds checking; resizeable Java arrays Bounds checking; can’t resize Java ArrayLists Bounds checking; auto-sized; more methods; heterogeneous; fast access through “get”

AP Computer ScienceAP Computer ScienceC++ to Java33 Arrays Java has a built in array type square brackets used to denote array variable int[] list; list = new int[10]; // elements init to 0 for(int i = 0; i < list.length; i++) list[i] = i * i * i; int x = 12; list = new int[x]; // old data lost int[] list2 = {1, 2, 3, 4, 5}; can declare array of any type String[] stringList = new String[5];

Pointers Again String[] stringList = new String[5]; stringList is an array of 5 String object variables "Variables who type are non primitive (arrays, Strings, all types of objects) are always pointers." String[] stringList; stringList ?? stringList = new String[5]; stringList null null null nullnull

AP Computer ScienceAP Computer ScienceC++ to Java35 Array Index Out of Bounds Java int x = 20; int[] list = new int[10]; list[x] = 20; // runtime error // Exception C++ int x = 20; int list[10]; list[x] = 20; // code compiles and runs int *list2 = new int[20]; list2[40] = 100; delete[] list2;

AP Computer ScienceAP Computer ScienceC++ to Java36 Linked Structures Java has NO pointers?? Java does have linked structures, e.g. linear linked lists, binary trees Actually nothing but pointers! But it is not the programmer's choice

AP Computer ScienceAP Computer ScienceC++ to Java37 while (p != NULL) { if (key == p->data) return p; p = p->next; } Searching a linked list in C++

AP Computer ScienceAP Computer ScienceC++ to Java38 Searching a linked list in Java while (p != null) { if (key == p.data()) // or key.equals( p.data() ) // if key is an object return p; p = p.next(); }

AP Computer ScienceAP Computer ScienceC++ to Java39 Out with the old (and how it changes) C++ const – more uses than MacGyver’s knife Operator overloading delete – to clean up memory management templates to achieve genericity Java final – a Pascal/Ada-like use of const NO operator overloading Automatic garbage collection polymorphism and inheritance to achieve genericity

AP Computer ScienceAP Computer ScienceC++ to Java40 Garbage Collector Used Java int[] list = new int[10]; delete not part of language. Garbage collector runs when necessary during program execution. Programmer not responsible for memory management. No need to write destructors. C++ int *list2 = new int[20]; // delete[] list2; No delete causes memory leak. Programmers spends lots of time dealing with memory management issues.

AP Computer ScienceAP Computer ScienceC++ to Java41 In with the New (and what it used to be) C++ Inheritance – It’s there, but it’s a pain and generally not taught Multiple implementations of Abstract Data Types – through classes only Java Inheritance (extends) – It’s there and it’s easy Multiple implementations of Abstract Data Types - through interfaces and the implements keyword

AP Computer ScienceAP Computer ScienceC++ to Java42 One Pet Peeve: The Dreaded Assignment Java int x = 210; int y = 2 * 105; if( x = y ) x++; // x = y causes syntax // error // the one catch boolean test = false; if(test = true) System.out.print('Y'); C++ int x = 210; int y = 2 * 104; cout << x << endl; if( x = y ) x++; cout << endl << x; Produces output of Huh???!??!?

AP Computer ScienceAP Computer ScienceC++ to Java43 Overall Changes (one man’s opinion)