Comp 114 Foundations of Programming Instructor: Prasun Dewan.

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

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
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.
ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Comp 114 Foundations of Programming Instructor: Prasun Dewan (Pr  sün Divän)
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Author: Loh Jianxiong Christopher Editors: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang.
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,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C OMP 401 B ASICS O F S CANNING Instructor: Prasun Dewan (FB 150,
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
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.
Debugging. 2 © 2003, Espirity Inc. Module Road Map 1.Eclipse Debugging  Debug Perspective  Debug Session  Breakpoint  Debug Views  Breakpoint Types.
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 2: Java Fundamentals
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.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
Author: Loh Jianxiong Christopher Contributions: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang, Tong Chun Kit, Tania Chattopadhyay.
Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.
By Mr. Muhammad Pervez Akhtar
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Debugging with Eclipse
Eclipse.
Chapter 7 User-Defined Methods.
Instructor: Sasa Junuzovic
Yanal Alahmad Java Workshop Yanal Alahmad
Important terms Black-box testing White-box testing Regression testing
Chapter 5: Control Structures II
Java Programming: From Problem Analysis to Program Design, 4e
An Introduction to Java – Part I
Important terms Black-box testing White-box testing Regression testing
Advanced Programming Behnam Hatami Fall 2017.
Introduction to Programming in Java
Instructor: Prasun Dewan
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
Comp 110/401 Appendix: Debugging Using Eclipse
Loops CGS3416 Spring 2019 Lecture 7.
Introduction to java Part I By Shenglan Zhang.
More on iterations using
Presentation transcript:

Comp 114 Foundations of Programming Instructor: Prasun Dewan

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 Keyword Predefined Programmer- defined

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'); } Function call Actual parameter Function definition Formal parameter

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

Starting Eclipse

Specifying Projects Directory

No Projects Screen

Adding a New Project

Selecting Java Project

Entering Project Name

Switching to Java Perspective

Empty Project

Adding a Package (Optional Step)

Entering Package Name

Workspace, Project, Package  Folder Worskspace Project Package

Adding a New Class File

Naming the File.java suffix needed!

Empty File

Getting Rid of Welcome Pane

Editing the File

Saving the File

Running the Main Method

Output in Console Window Console

Running Main with Parameters

Setting Run Configuration

Run Configuration

Arguments Tab

Executing Debug

Setting a Break Point Double click where you want program to stop

Executing Debug

Starting the Debugger

Stopping at Breakpoint Debug Perspective Next statement to be executed

Examining Main Parameters Main Parameters

Stepping to next statement

Next Statement

New Output and Local Variable Index gets initialized println() executed

Step Over

Step Into Call stack Called method parameters

Step Return

Resume

Returning to Java Perspective

AReverseUpperCasePrinter

No bold face! No Outline!

Deleting File Will also delete from file system

Renaming

Adding.java suffix

Renamed File No bold face! No Outline!

Downloading ObjectEditor Software

Installing ObjectEditor Software

Must do this for each project!

Installing ObjectEditor Software

Testing ObjectEditor Software Any object instance!

Testing ObjectEditor Graphics

Adding a library in JBuilder

Location of All Libraries Use Internet explorer (not Netscape) to download files Latest library names –oe8.jar –shapes3.jar oe8.jar version 8 of oe.jar Sotware may have lots of bugs! Send me mail for workarounds bugs.

The end The rest are extra slides

Previous Main.class shown

Running Another Program with Parameters

Resume