Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr  sün Divän)

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

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C OMP 401 I NTRODUCTION Instructor: Prasun Dewan.
C OMP 110 M ORE T YPES Instructor: Jason Carter. 2 P RIMITIVE T YPES int, double, boolean, long, short, float, byte char.
C OMP 110 A RRAYS Instructor: Jason Carter. 2 O UTLINE for loops Arrays.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Comp 114 Foundations of Programming Instructor: Prasun Dewan.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays –Collections (Set, Database, History) Inheritance –inheriting ancestor’s traits –inheriting benefactor’s assets –inheriting instance members( methods/variables)
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
More on Conditionals Miscellaneous (Side effects) Style issues Early returns.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Scanning Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program char.
Review Java.
Chapter 8 Arrays and Strings
Object-based Scanning Redo the scanning solution Monolithic, single class solution –does UI and scanning No reuse of code Another scanning problem to.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
***** 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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 8 Arrays and Strings
C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150,
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Introduction to Java Java Translation Program Structure
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
By Mr. Muhammad Pervez Akhtar
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Definition of the Programming Language CPRL
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Instructor: Sasa Junuzovic
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Yanal Alahmad Java Workshop Yanal Alahmad
Conditional Execution
Chapter 6 More Conditionals and Loops
Java Programming: From Problem Analysis to Program Design, 4e
An Introduction to Java – Part I
Advanced Programming Behnam Hatami Fall 2017.
Instructor: Prasun Dewan
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
Comp 110/401 Appendix: Debugging Using Eclipse
Chapter 2 Programming Basics.
Introduction to java Part I By Shenglan Zhang.
More on iterations using
Presentation transcript:

Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr  sün Divän)

Hello World

Topics Assumed & Reviewed Types –int, double, char, String Variables, constants, expressions Arrays Assignment, conditionals, loops Procedures/Functions/Subroutines/Methods Parameters/arguments

Hello World package warmup; public class AHelloWorldGreeter { public static void main (String[] args) { System.out.println ("Hello World"); } Array of user-supplied arguments main header directory/library

Main Arguments user-supplied argument

Main Arguments package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); } First argument args[0] args[1] Second argument...

Main Arguments user- supples no argument package warmup; public class AnArgPrinter { public static void main (String[] args) { System.out.println (args[0]); } program refers to argument array element exception!

Safe Arg Printer

Safe Arg Printer (edit in class) package warmup; public class ASafeArgPrinter { public static void main (String[] args) { //args.length gives number of elements in args array. }

Safe Arg Printer package warmup; public class ASafeArgPrinter { public static void main (String[] args) { if (args.length = = 1) System.out.println (args[0]); else { System.out.println("Illegal no of arguments:" + args.length + ". Terminating program"); System.exit(-1); }

If-else Statement if ( ) else

If-Else Statement true false

Compound Statement if (args.length = = 1) System.out.println (args[0]); else { System.out.println("Illegal no of arguments:" + args.length + ". Terminating program"); System.exit(-1); }

Nested if-else public static char toLetterGrade (int score) { if (score >= A_CUTOFF) return 'A'; else if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; else return 'F'; }

Nested If-Else if (score >= A_CUTOFF) return 'A'; else if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; else return 'F';

Nested If-Else

If Statement if (args.length = = 1) System.out.println (”args[0]”); if ( ) ;

Printing Multiple Arguments

Printing Multiple Arguments (edit in class) package warmup; public class AnArgsPrinter { public static void main(String[] args){ }

Printing Multiple Arguments package warmup; public class AnArgsPrinter { public static void main(String[] args){ int argNo = 0; while (argNo < args.length) { System.out.println(args[argNo]); argNo++; }

If Vs While Statement if ( ) ; while ( ) ;

if Statement true false

while Statement true false

while loop true false

Scanning Problem

char Constants char {letters, digits, operators...} ‘a’ ‘A‘ ‘1’ ‘< ‘ ‘’ 16 bits ‘ ‘’’ ‘\’’ Escape sequence ‘\n’ newline ‘ ‘\’ ‘\\’

Useful Escape Sequences

Ordering Characters ‘’‘a’…. position in ordered character list ordinal number (integer code) ….

Ordering Characters ‘’‘a’…. ‘b’‘c’‘z’…. ‘’‘A’…. ‘B’‘C’‘Z’…. ‘’‘0’…. ‘1’‘2’‘3’…. ‘a’ > ‘b’  false ‘B’ > ‘A’  true ‘4’ > ‘0’  true ‘0’ > ‘’  true ‘a’ > ‘A’  ??? ‘a’ > ‘0’  ???

Converting between Characters and their Ordinal Numbers (int) ‘a’  ordinal number of ’a’ (char) 55  character whose ordinal number is 55 (int) ‘’  0 0 (char) 0  ‘’ (int) ‘d’  ??? (char) 1  ??? (char) -1 (int) ‘c’ - (int) ‘a’  2 2 ‘c’ - ‘a’  2 2 Implicit cast to wider type (char) (‘c’ - 2)  ‘a’ (char) (‘A’ + 2)  ‘C’ (char) (‘C’ - ‘A’ + ‘a’)  ‘c’

String constants String{sequences of characters} “hello” “123” “hello 123” “a” variable size ‘a’ “” “hello\n\n123” “\”“\\” Object Type

Accessing String Components String s = “hello world”; s[0] s[1]... s.charAt(0)  ‘h’ s.charAt(1)  ‘e’ s.charAt(-1) s.charAt(11) StringIndexBounds exceptiom index s.length()  11 “ ”.length()  1 1  0 0

Accessing SubString “hello world”.substring(4,7)  s.charAt(beginIndex).. s.charAt(endIndex-1) s.substring(beginIndex, endIndex)  “o w” “hello world”.substring(4,4)  “” “hello world”.substring(7,4) StringIndexBounds exceptiom

String Processing int i = 0; while (i < s.length()) { System.out.println (s.charAt(i)); i++; } prints each character on separate line

Dissecting a Loop int i = 0; while (i < s.length()) { System.out.println (s.charAt(i)); i++; } loop condition loop body

Finer-grained Dissection int i = 0; while (i < s.length()) { System.out.println (s.charAt(i)); i++; } loop condition real body Resetting loop variable initalizing loop variables for (int i=0; i<s.length(); i++) System.out.println(s.charAt(i));

Meaning of For Loop S1; while ( E) { S3; S2; } for (S1; E; S2) S3 for (; E; S2) S3 while ( E) { S3; S2; } for (; E; ) S3 while ( E) { S3; } for (; ; ) S3 while ( true) S3;

Scanning Problem

Scanning Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program

Scanning JohnF.Kenndye token Input stream Token Stream token

Algorithm JohnF.Kenndye marker 0 Output: J

Algorithm JohnF.Kenndye marker 1 Output: J String inputLine

Algorithm JohnF.Kenndye marker 2 Output: J

Algorithm JohnF.Kenndye marker 5 Output: JF

Algorithm JohnF.Kenndye marker 6 Output: JF

Algorithm JohnF.Kenndye marker 8 Output: JFK

Algorithm JohnF.Kenndye marker 9 Output: JFK

Algorithm JohnF.Kenndye marker 14 Output: JFK

Solution (edit in class) package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ }

Solution package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } System.out.println("Upper Case Letters:"); int index = 0; while (index < args[0].length()) { if (isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index)); index++; } System.out.println(); } public static boolean isUpperCase(char c) { return (c >= 'A') && (c <= 'Z'); }

Printing Scanned Characters Backwards

Strings = Char Sequences JohnF.Kenndye hello String{sequences of characters} 143l0

Other Sequences as Predefined Types {sequences of integers} 80 JFKFDR {sequence of Strings} JCBCRRGB {sequences of doubles} IntSequence DoubleSequence StringSequence

Other Sequences as Array Types {sequences of integers} 80 {sequence of strings} {sequences of doubles} int[] double[] String[] JFKFDRJCBCRRGB

Initializing Array Declarations int[] assignmentScores = {100, 98, 99, 100, 90 80}; double[] gpas = {3.8, 3.1, 3.7, 3.1, 3.6, 3.9};String[] initials = {“JFK”, “FDR”, “JC”, “BC”, “GW”, “WW”}; JFKFDRJCBCGWWW assignmentScores Array Type Element Type Array Literal Array Variable

Array Operations String[] initials = {“JFK”, “FDR”, “JC”, “BC”, “GW”, “WW”}; JFKFDRJCBCGWWW initials.length  6 initials[0] initials[initials.length - 1] initials[initials.length]  ArrayIndexOutOfBoundsException public named constant initials[0] = “HT” HT initials[initials.length] = “HT”  ArrayIndexOutOfBoundsException Array Instance Size Fixed

Array Types have Variable-Size int[] int[] assignmentScores = {100, 98, 99, 100, 99, 80}; assignmentScores assignmentScores = {60, 40, 50}; assignmentScores

Uninitializing Array Declaration int[] assignmentScores; assignmentScores null assignmentScores = {60, 40, 50}; assignmentScores

Array Elements Uninitialized int[] assignmentScores = new int[3]; assignmentScores 000

Printing Scanned Characters Backwards

Variable-Size Collection filled part unfilled part current size maximum size

3 J F K sizearray Variable-Size Collection filled part unfilled part current size maximum size

Variable-Size Collection static final int MAX_CHARS = 7; static char[] upperCaseLetters = new char[MAX_CHARS]; static int numberOfUpperCaseLetters = 0;

Solution package warmup; public class AReverseUpperCasePrinter { static final int MAX_CHARS = 5; static char[] upperCaseLetters = new char[MAX_CHARS]; static int numberOfUpperCaseLetters = 0; public static void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } int index = 0; System.out.println("Upper Case Letters:"); while (index < args[0].length()) { if (isUpperCase(args[0].charAt(index))) { System.out.print(args[0].charAt(index)); storeChar(args[0].charAt(index)); } index++; } System.out.println(); printReverse(); }

Solution (contd.) public static void storeChar(char c) { if (numberOfUpperCaseLetters == MAX_CHARS) { System.out.println("Too many upper case letters. Terminating program. "); System.exit(-1); } upperCaseLetters[numberOfUpperCaseLetters] = c; numberOfUpperCaseLetters++; } public static void printReverse() { System.out.println("Upper Case Letters in Reverse:"); for (int index =numberOfUpperCaseLetters - 1; index >= 0; index--) { System.out.print(upperCaseLetters[index]); }

Creating JBuilder Project

Adding a Class

Editing the Class

Saving the Class

Running the Class

Viewing the Results

Running with arguments

Setting a break point

Running in debugging mode

Stopping at breakpoint

Examining the call stack

Navigating main parameters

Stepping to next statement (F8)

Stepping to next statement

Variable values

Stepping Over

Stepping Into (F9)

Stepping Out

Resuming normal execution

Program termination