C++ -> Java - A High School Teacher's Perspective

Slides:



Advertisements
Similar presentations
Interfaces.
Advertisements

Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
OOP Languages: Java vs C++
Java and C++, The Difference An introduction Unit - 00.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Session 7 Methods Strings Constructors this Inheritance.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
C++ -> Java - A High School Teacher's Perspective David B. Levine and Alyce Brady (modified by Mike Scott)
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Chapter VII: Arrays.
Pointers and Dynamic Arrays
Topic: Classes and Objects
Computer Organization and Design Pointers, Arrays and Strings in C
C# for C++ Programmers 1.
Sixth Lecture ArrayList Abstract Class and Interface
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 11 Inheritance and Polymorphism
CS2006- Data Structures I Chapter 5 Linked Lists I.
Java Programming Language
Lecture 2: Data Types, Variables, Operators, and Expressions
Exceptions, Interfaces & Generics
Introduction to Classes
Instructor: Ioannis A. Vetsikas
Interfaces and Inheritance
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Allocation
Chapter 9 Inheritance and Polymorphism
Introduction to Classes
null, true, and false are also reserved.
Interface.
7 Arrays.
Java Programming Language
Introduction to Java Programming
Pointers, Dynamic Data, and Reference Types
Interfaces.
CS201: Data Structures and Discrete Mathematics I
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
OO Design with Inheritance
Core Concepts.
7 Arrays.
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
CIS 199 Final Review.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 11 Inheritance and Encapsulation and Polymorphism
Review for Midterm 3.
SPL – PS1 Introduction to C++.
Java Basics – Arrays Should be a very familiar idea
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

C++ -> Java - A High School Teacher's Perspective David B. Levine and Alyce Brady 9/22/2018

Today’s Approach Consider a few representative topics in each language Show examples Make comparisons 9/22/2018

Control Structures in C++ Counting Odd Elements in a 20-element array int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) oddCount++; } 9/22/2018

Control Structures in Java Counting Odd Elements in a 20-element array int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) oddCount++; } 9/22/2018

Variable declarations in C++ // Primitives int x; double d; bool done; // Objects Coin penny; Dice cube(6); 9/22/2018

Variable declarations in Java // Primitives int x; double d; boolean done; // Objects Coin penny = new Coin(); Dice cube = new Dice(6); 9/22/2018

Variable declarations in Java // Primitives int x; double d; boolean done; // Objects Coin penny = new Coin(); Dice cube = new Dice(6); 9/22/2018

Key difference in Java Variables are references to objects, not objects Variable declaration merely creates reference, not object Dice cube; Must construct objects explicitly Dice cube = new Dice(6); Can have multiple references to same object 9/22/2018

Member function calls in C++ penny.flip(); robot.moveTo(newLoc); 9/22/2018

Method invocation in Java penny.flip(); robot.moveTo(newLoc); 9/22/2018

Parameter Passing C++ Java Value (default) Reference (if specified) const Reference (if specified) Java Value (for primitives) Value (for object references, so looks like by reference) In Java, object REFERENCES are passed as parameters, not the objects themselves. References, like primitives, are passed by value. A method can follow a reference , though, and modify the object. (Note: since the reference is passed by value, the called method cannot change WHICH object the reference points to (cannot modify the value of the reference) in a way that the calling method would see the change.) 9/22/2018

Classes and Objects C++ Java 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!) 9/22/2018

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(String message) 9/22/2018

Hello World in C++ #include <iostream.h> int main() { cout << “Hello world” << endl; return 0; } 9/22/2018

Hello World in Java public class Hello { public static void main() System.out.println(“Hello world”); } 9/22/2018

Strings in C++ (AP C++) apstring s; apstring s1 = “Hello, ”; apstring s2(“World”); apstring s3 = s2; cout << s1 + s3; 9/22/2018

Strings in Java String s; String s1 = “Hello, ”; String s2 = new String(“World”); String s3 = s2; System.out.println(s1 + s3); 9/22/2018

Strings are Unusual in Java String constants look like those in C/C++ String s1 = “Hello, ”; String is only class with operator overloading System.out.println(s1 + s3); 9/22/2018

I/O (A Key Difference) C++ Java cin/cout, streams text-based (graphics are an add-on) may be tested on the exam Java System.out.println(String message); graphical i/o: forms, applets, text fields, etc industrial-strength class hierarchy USE WHATEVER YOUR BOOK SUGGESTS! 9/22/2018

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; O(1) access through “get” 9/22/2018

Linked Structures Java has NO pointers! Or nothing but pointers! Java does have linked structures, e.g. linear linked lists, binary trees 9/22/2018

Searching a linked list in C++ while (p != NULL) { if (key == p->data) return p; p = p->next; } 9/22/2018

Searching a linked list in Java while (p != null) { if (key == p.data()) // if primitives return p; p = p.next(); } // or “if (key.equals(p.data()))” // if key and p.data() are objects 9/22/2018

Out with the old (and how it changes) const – more uses than MacGyver’s knife Operator overloading delete – to clean up memory management Java final – a Pascal/Ada-like use of const NO operator overloading Automatic garbage collection 9/22/2018

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 through implements keyword 9/22/2018

Overall Changes (one man’s opinion) 9/22/2018