James Tam Intermediate Pascal to Java examples Pascal To Java: The Transition Documentation Variables and constants Advanced input and output Decision.

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

C Language.
Programming Languages and Paradigms The C Programming Language.
James Tam CPSC 233: Introduction to Computers II Object-oriented programming Object-oriented design And a whole lot ole fun (you’ll have a …)
James Tam CPSC 233: Introduction to Computers II Object-oriented programming Object-oriented design And a whole lot ole fun (you’ll have a …)
James Tam CPSC 233: Introduction to Classes and Objects, Part I Attributes and methods Creating new classes References: Dynamic memory allocation and.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
Program Design and Development
Java Syntax Primitive data types Operators Control statements.
James Tam Introduction To Java Programming You will study the process of creating Java programs and constructs for input, output, branching, looping,
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam CPSC 233: Introduction to Computers II Object-oriented programming Object-oriented design And a whole lot ole fun (you’ll have a …)
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam CPSC 233: Introduction to Computers II Object-oriented programming Object-oriented design And a whole lot ole fun (you’ll have a …)
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Guide To UNIX Using Linux Third Edition
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
String Escape Sequences
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
DAT602 Database Application Development Lecture 5 JAVA Review.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
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.)
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
From C++ to Java A whirlwind tour of Java for C++ programmers.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Computer programming Lecture#2 أ. إلهام باسندوه 1.
Introduction to Java Java Translation Program Structure
August 6, 2009 Data Types, Variables, and Arrays.
CSC 212 Object-Oriented Programming and Java Part 2.
James Tam CPSC 233: Introduction to Computers II Object-oriented programming Object-oriented design And a whole lot ole fun (you’ll have a …)
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
By Mr. Muhammad Pervez Akhtar
James Tam Introduction To Java Programming You will study the process of creating Java programs and constructs for input, output, branching, looping,
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Java Programming Language Lecture27- An Introduction.
Information and Computer Sciences University of Hawaii, Manoa
ECE Application Programming
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
C Basics.
Java Programming: From Problem Analysis to Program Design, 4e
Advanced Programming Behnam Hatami Fall 2017.
Arrays, For loop While loop Do while loop
Chapter 2: Basic Elements of Java
Chapter 2 Programming Basics.
Programming Languages and Paradigms
Presentation transcript:

James Tam Intermediate Pascal to Java examples Pascal To Java: The Transition Documentation Variables and constants Advanced input and output Decision making Loops Homogeneous composite types (arrays) Modularization (functions/procedures and methods)

James Tam Introduction to CPSC 233 Basic Structure Of A Java Program class { public static void main (String[] args) { }

James Tam Introduction to CPSC 233 Documentation / Comments Pascal (* Start of documentation *) End of documentation Java Multi-line documentation /* Start of documentation */ End of documentation Documentation for a single line // Everything until the end of the line is treated as a comment

James Tam Introduction to CPSC 233 Variable Declaration class { public static void main (String[] args) { // Variable declarations occur here }

James Tam Introduction to CPSC 233 Some Simple Types Of Variables In Java TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64 bit signed integer float32 bit signed integer double64 bit signed integer char16 bit Unicode character Boolean1 bit true or false value StringA sequence of characters between double quotes (“”)

James Tam Introduction to CPSC 233 Java Vs. Pascal Variable Declarations Pascal Format: : variable type; Example: num : integer; Java Format: variable type ; Example: long num1; double num2 = 2.33;

James Tam Introduction to CPSC 233 Constants In Java Format final = ; Example final int SIZE = 100;

James Tam Introduction to CPSC 233 The Semicolon In Pascal Pascal Used to separate statements within a block This is okay in Pascal: program test (output); begin writeln("one"); writeln("two") end.

James Tam Introduction to CPSC 233 The Semicolon In Java Java Follows each statement This is not okay in Java: class BadExample { public static void main (String [] argv) { System.out.println("one"); System.out.println("two") }

James Tam Introduction to CPSC 233 Java Variables And Constants: A Small Example class SmallExample { public static void main (String [] argv) { final double PI = 3.14; String str = "Hosta la vista baby!"; long num = 10; double db; db = PI; System.out.print("num=" + num); System.out.println(" db=" + db); System.out.println("Str says..." + str); }

James Tam Introduction to CPSC 233 Output : Some Escape Sequences Escape sequenceCharacter value \tHorizontal tab (5) \nNew line \”Double quote \\Backslash

James Tam Introduction to CPSC 233 Advanced Output Employ the predefined code in TIO To use: (In Unix): Copy all the files in the directory /home/profs/tamj/tio to the directory where your Java program resides (In your Java program include the following statements): import tio.*; FormattedWriter out = new FormattedWriter(System.out);

James Tam Introduction to CPSC 233 Advanced Output (2) StatementEffect out.printf( + …); MUST EVENTUALLY BE FOLLOWED BY A PRINTFLN! Prints contents of field out.printfln(( + …); Prints contents of field and a newline out.setWidth( );Sets the width of a field out.setDigits( );Sets the number of places of precision out.setJustify(out.LEFT); outsetJustify(out.RIGHT); Left or right justify field contents

James Tam Introduction to CPSC 233 Advanced Output: Example import tio.*; class OutputExample { public static void main (String [] argv) { FormattedWriter out = new FormattedWriter(System.out); int num = 123; double db = ; out.setJustify(out.LEFT); out.setWidth(6); out.setDigits(1); out.printf("Start line"); out.printf(num); out.printf(db); out.printf("End of line"); out.printfln(""); }

James Tam Introduction to CPSC 233 Advanced Input Employ the predefined code in TIO To use: (In Unix): Copy all the files in the directory /home/profs/tamj/tio to the directory where your Java program resides (In your Java program include the following statement): import tio.*;

James Tam Introduction to CPSC 233 Advanced Input: Getting The Input Console.in.readInt()Reads in an integer Returns an integer Console.in.readLong()Reads in a long Returns a long Console.in.readFloat()Reads in a float Returns a float Console.in.readDouble()Reads in a double Returns a double Console.in.readLine()Reads in a line Returns a String Console.in.readWord()Reads in a word Returns a String Console.in.readChar()Reads in a character Returns an integer

James Tam Introduction to CPSC 233 Relational Operators OperatorMeaning of the operator <Less than <=Less than, equal to >Greater than >=Greater than, equal to ==Equal to !=Not equal to

James Tam Introduction to CPSC 233 Common Logical Operators &&Logical AND ||Logical OR !Logical NOT

James Tam Introduction to CPSC 233 Decision Making Pascal If-then If-then-else If-then, else-if Case-of Java If If, else If, else-if Switch

James Tam Introduction to CPSC 233 Decision Making: If Format: if (Boolean Expression) Body Example: if (x != y) System.out.println(“X and Y are not equal”); if ((x > 0) && (y > 0)) { System.out.println(); System.out.println("X and Y are positive"); }

James Tam Introduction to CPSC 233 Decision Making: If, Else Format if (Boolean expression) Body of if else Body of else Example if (x < 0) System.out.println(“X is negative”); else System.out.println(“X is non-negative”);

James Tam Introduction to CPSC 233 If, Else-If Format if (Boolean expression Body of If else if (Boolean expression) Body of first else-if ::: else if (Boolean expression) Body of last else-if else Body of else

James Tam Introduction to CPSC 233 If, Else-If (2) Example if (gpa == 4) { System.out.println("A"); } else if (gpa == 3) { System.out.println("B"); } else if (gpa == 2) { System.out.println("C"); }

James Tam Introduction to CPSC 233 If, Else-If (2) else if (gpa == 1) { System.out.println("D"); } else { System.out.println("Invalid gpa"); }

James Tam Introduction to CPSC 233 Alternative To Multiple Else-If’s: Switch Format switch (variable name) { case : Body break; case : Body break; : default: Body } 1 The type of variable can be a byte, char, short, int or long

James Tam Introduction to CPSC 233 Alternative To Multiple Else-If’s: Switch (2) Format switch (variable name) { case ‘ ’: Body break; case ‘ ’: Body break; : default: Body } 1 The type of variable can be a byte, char, short, int or long

James Tam Introduction to CPSC 233 Loops Pascal Pre-test loops For-do While-do Java Pre-test loops For While Pascal Post-test loops Repeat-until Java Post-test loops Do-while

James Tam Introduction to CPSC 233 While Loops Format While (Expression) Body Example int i = 1; while (i <= ) { System.out.println(“How much do I love thee?”); System.out.println(“Let me count the ways: “, + i); i = i + 1; }

James Tam Introduction to CPSC 233 For Loops Syntax: for (initialization; Boolean expression; update control) Body Example: for (i = 1; i <= ; i++) { System.out.println(“How much do I love thee?”); System.out.println(“Let me count the ways: “, + i); }

James Tam Introduction to CPSC 233 Do-While Loops Format: do Body while (Boolean expression); Example: char ch = 'A'; do { System.out.println(ch); ch++; } while (ch != 'K');

James Tam Introduction to CPSC 233 Ending Loops Early: Break import tio.*; class BreakExample { public static void main (String [] argv) { char ch; while (true) { System.out.print("\tType 'Q' or 'q' to Quit program: "); ch = (char) Console.in.readChar(); if ((ch == 'Q') || (ch == 'q')) { break; }

James Tam Introduction to CPSC 233 Skipping An Iteration of A Loop: Continue for (i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; } System.out.println("i=" + i); }

James Tam Introduction to CPSC 233 Homogeneous Composite Types (Arrays) Important points to remember for Java: An array of n elements will have an index of zero for the first element up to (n-1) for the last element The array index must be an integer Declaring arrays involves dynamic memory allocation (references)

James Tam Introduction to CPSC 233 Homogeneous Composite Types (Arrays) Important points to remember for Java: An array of n elements will have an index of zero for the first element up to (n-1) for the last element The array index must be an integer Declaring arrays involves dynamic memory allocation (references)

James Tam Introduction to CPSC 233 References It is a pointer that cannot be dereferenced by the programmer Automatically garbage collected when no longer needed

James Tam Introduction to CPSC 233 References It is a pointer that cannot be dereferenced by the programmer Automatically garbage collected when no longer needed

James Tam Introduction to CPSC 233 Regular pointers (Pascal): Programmer Dereferencing var numPtr : ^ integer; begin new(numPtr); numPtrnumPtr ^

James Tam Introduction to CPSC 233 References (Java): No Programmer Dereferencing = new ; final int SIZE = 4; int [] arr = new int[SIZE]; [0] [1] [3] [2]

James Tam Introduction to CPSC 233 References It is a pointer that cannot be dereferenced by the programmer Automatically garbage collected when no longer needed

James Tam Introduction to CPSC 233 Regular pointers (Pascal): Garbage Collection dispose (numPtr); numPtr := NIL; numPtrnumPtr ^

James Tam Introduction to CPSC 233 Regular pointers (Pascal): Garbage Collection dispose (numPtr); numPtr := NIL; numPtrnumPtr ^

James Tam Introduction to CPSC 233 Regular pointers (Pascal): Garbage Collection dispose (numPtr); numPtr := NIL; numPtrnumPtr ^ NIL

James Tam Introduction to CPSC 233 References (Java): Automatic Garbage Collection Dynamically allocated memory is automatically freed up when it is no longer referenced [0] [1] [3] [2] Reference Dynamic memory

James Tam Introduction to CPSC 233 Determining Array Length class Arr { public static void main (String [] argv) { final int SIZE = 4; int [] arr = new int[SIZE]; int i; for (i = 0; i < SIZE; i++) { arr[i] = i; System.out.println("Element " + arr[i] +"=" +i); } System.out.println(arr); }

James Tam Introduction to CPSC 233 Determining Array Length (2) class Arr { public static void main (String [] argv) { final int SIZE = 4; int [] arr = new int[SIZE]; int i; for (i = 0; i < arr.length ; i++) { arr[i] = i; System.out.println("Element " + arr[i] +"=" +i); } System.out.println(arr); }

James Tam Introduction to CPSC 233 Declaring Arrays Arrays in Java involve a reference to the array Declaring array requires two steps: 1)Declaring a reference to the array 2)Allocating the memory for the array

James Tam Introduction to CPSC 233 Declaring A Reference To An Array Syntax [] ; Example int [] arr; int [][] arr;

James Tam Introduction to CPSC 233 Allocating Memory For An Array Syntax: = new [ ]; Example: arr = new int[SIZE]; arr = new int[SIZE][SIZE];

James Tam Introduction to CPSC 233 Modularizing Programs: Methods (Definition) Syntax: name (, … ) { return ( ); }

James Tam Introduction to CPSC 233 Modularizing Programs: Methods (Definition) Example: static double calculate (double principle, double interest, double time) { return(principle + (principle * interest * time)); }

James Tam Introduction to CPSC 233 Modularizing Programs: Methods (Definition) Example: static double calculate (double principle, double interest, double time) { return(principle + (principle * interest * time)); } Used for a transition!

James Tam Introduction to CPSC 233 Parameter Passing: Review Pascal Value parameters (pass by value) Variable parameters (pass by reference)

James Tam Introduction to CPSC 233 Passing Value Parameters (Pass By Value) A local copy of the parameter is made for the procedure or function procedureName (p1, p2); procedureName (p1, p2: parameter type); begin end; p1 p2 Copy

James Tam Introduction to CPSC 233 Passing Value Parameters (Pass By Value) A local copy of the parameter is made for the procedure or function procedureName (p1, p2); procedureName (p1, p2: parameter type); begin end; (copy of) p1 (copy of) p2 Copy

James Tam Introduction to CPSC 233 Passing Variable Parameters (Pass By Reference) Changes made to the parameter inside the function or procedure will be retained outside of that module. This is done by passing a pointer to the parameter (refer to the parameter in the module and the pointer is automatically dereferenced in order to refer to the original parameter passed in)

James Tam Introduction to CPSC 233 Passing Variable Parameters (Pass By Reference) procedureName (p1, p2); procedureName (var p1, p2: parameter type); begin end; Pointer p1 p2 (Original) p1 (Original) p2

James Tam Introduction to CPSC 233 Parameters To Methods In Java Allowable parameters include: Simple type (boolean, char, integer etc.) Programmer defined types. Simple types can only be passed by value in Java (copy passed in) Composite types (arrays, objects) have a copy of a reference passed in (not exactly pass by reference because the net effect is the same as with simple types)

James Tam Introduction to CPSC 233 Method Return Types In Java Any simple type (boolean, char, integer etc.) Cases where no value is returned (void) Programmer defined types (objects)

James Tam Introduction to CPSC 233 Passing Arrays As Parameters Syntax: ( [] ) Example: void fun (int [] arr) void fun (int arr [])

James Tam Introduction to CPSC 233 Methods In Java: Putting It All Together See the example in Unix under: /home/profs/tamj/233/examples/intro/Modules.java

James Tam Introduction to CPSC 233 Some Common Mistakes 1.Semi-colons e.g., method definitions, last statements in a block of statements 2.Performing an assignment when you want to check for equity e.g., decision making constructs, loops 3.Comments i.e., (* and *) vs /* and */ {}