COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Size: px
Start display at page:

Download "COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand"

Transcription

1 COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

2 COSC 236 Web Site You will always find the course material at: or or From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

3 3

4 Review of Quiz 11 4

5 Review of Quiz 11 The key points to understand from Quiz 11 are as follows: 1. The actual parameter in the calling program is a string that contains the Scanner input entered by the user. 2. The actual parameter from the calling method is copied into the formal parameter in the called method (this is the text to be reversed). 3. The three key concepts tested in Quiz 11: a. Using String Method.toUpperCase() to convert the text to upper case b. Setting up a for loop to count down from the last character (text.length()-1) to the first character in the string c. Using String Method.charAt(i) or.substring(i, i+1) to access a single character in text and print it out using System.out.print(text.charAt(i)); 5

6 Review of Quiz 11 (using text.charat(i)) 6

7 Review of Quiz 11 (using text.substing(i, i+1)) 7

8 Review of Quiz 11 8

9 4.1 if/else statement (pp ) Factoring if/else code (pp ) factoring: Extracting common/redundant code. Can reduce or eliminate redundancy from if/else code. Example: if (a == 1) { System.out.println(a); x = 3; b = b + x; } else if (a == 2) { System.out.println(a); x = 6; y = y + 10; b = b + x; } else { // a == 3 System.out.println(a); x = 9; b = b + x; } Slide from last lecture UPDATE System.out.println(a); x = 3 * a; if (a == 2) { y = y + 10; } b = b + x; 9

10 Programmers commonly face problems that require them to create, edit, examine, and format text. Collectively, we call these tasks text processing. Text Processing: Editing and formatting strings of text. In this section: We look in more detail at the char primitive type Introduce a new command called System.out.printf. Both of these tools are very useful for text-processing tasks. 10

11 Type char (pp ) char : A primitive type representing single characters. A String is stored internally as an array of char String s = "Ali G."; index value 'A' 'l' 'i' ' ' 'G' '.' It is legal to have variables, parameters, returns of type char surrounded with apostrophes: 'a' or '4' or '\n' or '\'' char letter = 'P'; System.out.println(letter); // P System.out.println(letter + " Diddy"); // P Diddy 11

12 The charat method (p. 262) The chars in a String can be accessed using the charat method. charat(n) accepts an int n index parameter and returns the char at that index String food = "cookie"; char firstletter = food.charat(0); // 'c' System.out.println(firstLetter + " is for " + food); You can use a for loop to print or examine each character. String major = "CSE"; for (int i = 0; i < major.length(); i++) { // output: char c = major.charat(i); // C System.out.println(c); // S } // E 12

13 Comparing char values (p. 262) You can compare chars with ==,!=, and other operators: String word = console.next(); char last = word.charat(word.length() - 1); if (last == 's') { System.out.println(word + " is plural."); } // prints the alphabet for (char c = 'a'; c <= 'z'; c++) { System.out.print(c); } 13

14 Differences between char and String (p. 262) 14

15 Differences between == and.equals (p. 262) The == detects that they are not the same object so == gives a false The.equals detects that they contain the same string so.equals gives a true 15

16 Comparing strings (p. 262) Relational operators such as < and == fail on objects. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name == "Barney") { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); } This code will compile, but it will not print the song. == compares objects by references (seen later), so it often gives false even when two Strings have the same letters. 16

17 The equals method (p. 262) Objects are compared using a method named equals. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name.equals("barney")) { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); } Technically this is a method that returns a value of type boolean, the type used in logical tests. 17

18 String test methods (p. 262) Method equals(str) equalsignorecase(str) startswith(str) endswith(str) contains(str) Description whether two strings contain the same characters whether two strings contain the same characters, ignoring upper vs. lower case whether one contains other's characters at start whether one contains other's characters at end whether the given string is found within this one String name = console.next(); if (name.startswith("prof")) { System.out.println("When are your office hours?"); } else if (name.equalsignorecase("stuart")) { System.out.println("Let's talk about meta!"); } 18

19 char vs. String (p. 262) "h" is a String, but 'h' is a char (each has Unicode 104, but they are different) A String is an object; it contains methods. String s = "h"; s = s.touppercase(); // "H" int len = s.length(); // 1 char first = s.charat(0); // 'H' A char is primitive; you can't call methods on it. char c = 'h'; c = c.touppercase(); s = s.charat(0).touppercase(); What is s + 1? What is c + 1? What is s + s? What is c + c? // ERROR // ERROR s + 1 = h1 c + 1 = 105 s + s = hh c + c =

20 char vs. int (pp ) Each char is mapped to an integer value internally Called an ASCII value 'A' is 65 'B' is 66 ' ' is 32 'a' is 97 'b' is 98 '*' is 42 Mixing char and int causes automatic conversion to int. 'a' + 10 is 107, 'A' + 'A' is 130 To convert an int into the equivalent char, type-cast it. (char) ('a' + 2) is 'c' 20

21 Cumulative Text Algorithms (pp ) Cumulative algorithms accumulate a sum or a product We saw these in last lecture for finding the sum and product of a set of numbers We can also do accumulation algorithms with text Count the number of times something appears in text How many times a character appears How many times an alphabetic letter appears How many times a number appears And many similar counting algorithms Concatenate characters to build up a string 21

22 Cumulative Text Algorithms (pp ) Method to count the number of times a character appears in a line of text: public static int count(string text, char c) { int found = 0; for (int i = 0; i < text.length(); i++) { if (text.charat(i) == c) { found++; } } return found; } 22

23 Cumulative Text Algorithms (pp ) Some useful methods for text processing: 23

24 Cumulative Text Algorithms (pp ) Method to count the number of alphabetic character that appear in a line of text: public static int countletters(string phrase) { int count = 0; for (int i = 0; i < phrase.length(); i++) { char ch = phrase.charat(i); if (Character.isLetter(ch)) { count++; } } return count; } 24

25 Cumulative Text Algorithms (pp ) Method to do cumulative concatenation in order to reverse a line of text: public static String reverse(string phrase) { String result = ""; for (int i = 0; i < phrase.length(); i++) { result = phrase.charat(i) + result; } return result; } Compare this algorithm to the algorithm we used in the Quiz 11 solution discussed at the beginning of today s lecture. 25

26 Formatting text with printf (pp ) 26

27 Formatting text with printf (pp ) 27

28 Formatting text with printf (pp ) 28

29 Formatting text with printf (pp ) 29

30 Formatting text with printf (pp ) 30

31 Formatting text with printf (pp ) 31

32 Formatting text with printf (pp ) 32

33 Formatting text with printf (pp ) 33

34 Formatting text with printf (pp ) 34

35 Formatting text with printf (pp ) System.out.printf("format string", parameters); A format string can contain placeholders to insert parameters: %d integer %f real number %s string these placeholders are used instead of + concatenation Example: int x = 3; int y = -17; System.out.printf("x is %d and y is %d!\n", x, y); // x is 3 and y is -17! printf does not drop to the next line unless you write \n 35

36 printf width (pp ) %Wd %-Wd %Wf... integer, W characters wide, right-aligned integer, W characters wide, left-aligned real number, W characters wide, right-aligned for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 10; j++) { System.out.printf("%4d", (i * j)); } System.out.println(); // to end the line } Output:

37 printf precision (pp ) %.Df real number, rounded to D digits after decimal %W.Df real number, W chars wide, D digits after decimal %-W.Df real number, W wide (left-align), D after decimal double gpa = ; System.out.printf("your GPA is %.1f\n", gpa); System.out.printf("more precisely: %8.3f\n", gpa); Output: your GPA is 3.3 more precisely:

38 printf question (pp, ) Modify our Receipt program to better format its output. Display results in the format below, with $ and 2 digits after. Example log of execution: How many people ate? 4 Person #1: How much did your dinner cost? Person #2: How much did your dinner cost? 15 Person #3: How much did your dinner cost? 25.0 Person #4: How much did your dinner cost? Subtotal: $70.00 Tax: $5.60 Tip: $10.50 Total: $

39 printf answer (partial) (pp ) // Calculates total owed, assuming 8% tax and 15% tip public static void results(double subtotal) { double tax = subtotal *.08; double tip = subtotal *.15; double total = subtotal + tax + tip; // System.out.println("Subtotal: $" + subtotal); // System.out.println("Tax: $" + tax); // System.out.println("Tip: $" + tip); // System.out.println("Total: $" + total); } } System.out.printf("Subtotal: $%.2f\n", subtotal); System.out.printf("Tax: $%.2f\n", tax); System.out.printf("Tip: $%.2f\n", tip); System.out.printf("Total: $%.2f\n", total); 39

40 4.4 Conditional Execution (pp ) Catching Divide by Zero Errors ( ) Because Java uses the IEEE standard for floating point numbers a divide by zero does not cause and error: IEEE defines 0.0/0.0 as NaN (Not a Number) IEEE defines a positive number divided by zero as Infinity IEEE defines a negative number divided by zero as Infinity However, IEEE does not specify what to do with integer divides by zero Java will issue an exception (execution error) and stop running on a divide by zero Java provides Try/Catch as one method to deal with exceptions 40

41 4.4 Conditional Execution (pp ) Catching Divide by Zero Errors ( ) 41

42 4.4 Conditional Execution (pp ) Catching Divide by Zero Errors ( ) Try/Catch: Appendix p

43 4.4 Conditional Execution (pp ) Good Programming Practice ( ) Good programming practice is to avoid execution errors Check input values to be sure they are within limits Check preconditions before calling any method Check postconditions before exiting any methods Three different ways of checking BEST: Check using Java conditionals (if/then/else) GOOD: Use Try/Catch to deal with exceptions FAIR: Have your program throw an exception when conditions are not met POOR: Don t check and rely on Java exceptions 43

44 4.4 Conditional Execution (pp ) Good Programming Practice ( ) Factorial method Accepts an integer n and produces the factorial (1x2x3x4x x(n-1)xn) Includes a test for negative n Throws an exception for negative n Three different ways of checking BEST: Check for negative n before calling the method (if/then/else) GOOD: We could use Try/Catch to deal with exception FAIR: Allow the method to throw the exception on negative n POOR: Don t check and in this case you will get an incorrect result for negative n 44

45 4.4 Conditional Execution (pp ) Factorial Method ( ) NOTE: A comma after the % token will format comas in the printout. 45

46 4.4 Conditional Execution (pp ) Factorial Method ( ) 46

47 Assignments for this week 1. Laboratory for Chapter 4 due Monday 10/20 IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 2. Wednesday we will review for Midterm 1 look over all the quizzes in preparation. 3. Be sure to complete Quiz 12 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 47

Building Java Programs Chapter 4

Building Java Programs Chapter 4 Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson 2013. All rights reserved. The if statement Executes a block of statements only if a test is true if (test) { statement;...

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-3: Strings; char; printf; procedural design reading: 3.3, 4.3, 4.5 1 Strings reading: 3.3 1 Strings string: An object storing a sequence of text characters. Unlike

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, 4.3-4.4 self-check: Ch. 4 #12, 15 exercises: Ch. 4 #15, 16 videos: Ch. 3 #3 1 Objects and classes object: An entity that contains: data

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-3: Strings; char reading: 3.3, 4.3 1 Strings reading: 3.3 Objects object: An entity that contains data and behavior. data: variables inside the object behavior:

More information

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

More information

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

More information

Topic 23 arrays - part 3 (tallying, text processing)

Topic 23 arrays - part 3 (tallying, text processing) Topic 23 arrays - part 3 (tallying, text processing) "42 million of anything is a lot." -Doug Burger (commenting on the number of transistors in the Pentium IV processor) Copyright Pearson Education, 2010

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming Sierpinski Valentine http://xkcd.com/543/ 1 CS 112 Introduction to Programming Switch; Text Processing Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email:

More information

Sierpinski Valentine.

Sierpinski Valentine. Sierpinski Valentine http://xkcd.com/543/ 1 CS 112 Introduction to Programming Switch; Text Processing Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email:

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming File as Input; Exceptions; while loops; Basic Arrays Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

Sierpinski Valentine. q Our journey of introducing conditionals

Sierpinski Valentine.  q Our journey of introducing conditionals Sierpinski Valentine http://xkcd.com/543/ CS 112 Introduction to Programming Switch; Text Processing Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email:

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 Loops with if/else if/else statements can be used with

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else; Cumulative sum; reading: 4.2, 4.4-4.5 2 Advanced if/else reading: 4.4-4.5 Factoring if/else code factoring: Extracting common/redundant code.

More information

Lecture 8: The String Class and Boolean Zen

Lecture 8: The String Class and Boolean Zen Lecture 8: The String Class and Boolean Zen Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Strings string: An object

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright

More information

Topic 13 procedural design and Strings

Topic 13 procedural design and Strings Topic 13 procedural design and Strings Ugly programs are like ugly suspension bridges: they're much more liable to collapse than pretty ones, because the way humans (especially engineerhumans) perceive

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Text processing. Readings: 4.4

Text processing. Readings: 4.4 Text processing Readings: 4.4 1 Characters char: A primitive type representing single characters. Individual characters inside a String are stored as char values. Literal char values are surrounded with

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

CSE 373. Data Types and Manipulation; Arrays. slides created by Marty Stepp

CSE 373. Data Types and Manipulation; Arrays. slides created by Marty Stepp CSE 373 Data Types and Manipulation; Arrays slides created by Marty Stepp http://www.cs.washington.edu/373/ University of Washington, all rights reserved. 1 Numeric data type kind of number memory (bits)

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Building Java Programs. Chapter 2: Primitive Data and Definite Loops Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

More information

Text processing. Characters. The charat method. Fun with char! char vs. String. Text processing. Readings: 4.4 (pg ) 'h' is a char

Text processing. Characters. The charat method. Fun with char! char vs. String. Text processing. Readings: 4.4 (pg ) 'h' is a char Characters Text processing Readings: 4.4 (pg. 235 237) char: A primitive type representing single characters. Individual characters inside a String are stored as char values. Literal char values are surrounded

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Variables; Type Casting; Using Variables in for Loops Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

More on variables and methods

More on variables and methods More on variables and methods Robots Learning to Program with Java Byron Weber Becker chapter 7 Announcements (Oct 12) Reading for Monday Ch 7.4-7.5 Program#5 out Character Data String is a java class

More information

CSE 303 Lecture 8. Intro to C programming

CSE 303 Lecture 8. Intro to C programming CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, 2.2-2.4, 2.6, 3.1, 5.1, 7.1-7.2, 7.5.1-7.5.4, 7.6-7.9, Ch. 8; Programming in C Ch. 1-6 slides created by Marty Stepp http://www.cs.washington.edu/303/

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Lecture 2: Operations and Data Types

Lecture 2: Operations and Data Types Lecture 2: Operations and Data Types Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set

More information

Advanced if/else & Cumulative Sum

Advanced if/else & Cumulative Sum Advanced if/else & Cumulative Sum Subset of the Supplement Lesson slides from: Building Java Programs, Chapter 4 by Stuart Reges and Marty Stepp (http://www.buildingjavaprograms.com/ ) Questions to consider

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Variables reading: 2.2 self-check: 1-15 exercises: 1-4 videos: Ch. 2 #2 2 Receipt example What's bad about the

More information

Building Java Programs Chapter 2

Building Java Programs Chapter 2 Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set of data values. Constrains the operations that can

More information

Midterm Examination (MTA)

Midterm Examination (MTA) M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and

More information

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw. Building Java Programs Chapter 2 bug Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. 2 An Insect Software Flaw 3 4 Bug, Kentucky Bug Eyed 5 Cheesy Movie 6 Punch Buggy

More information

Building Java Programs Chapter 2

Building Java Programs Chapter 2 Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. bug 2 An Insect 3 Software Flaw 4 Bug, Kentucky 5 Bug Eyed 6 Cheesy Movie 7 Punch Buggy

More information

-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/ Topic 15 boolean methods and random numbers "It is a profoundly erroneous truism,

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination Tuesday, November 4, 2008 Examiners: Mathieu Petitpas [Section 1] 18:30

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements Outline Problem: How do I make choices in my Java program? Understanding conditional statements Remember: Boolean logic

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements Outline Problem: How do I make choices in my Java program? Understanding conditional statements Remember: Boolean logic

More information

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8 Epic Test Review 1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4 Write a line of code that outputs the phase Hello World to the console without creating a new line character. System.out.print(

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Primitive Data, Variables, and Expressions; Simple Conditional Execution Unit 2, Part 1 Primitive Data, Variables, and Expressions; Simple Conditional Execution Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Overview of the Programming Process Analysis/Specification

More information

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP Primitive and Class Types. Yi Hong May 14, 2015 COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

More information

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue General Loops in Java Look at other loop constructions Very common while loop: do a loop a fixed number of times (MAX in the example) int

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CS 5010: Programming Design Paradigms!

CS 5010: Programming Design Paradigms! CS 5010: Programming Design Paradigms! Fall 2017 Lecture 2: Whirlwind Tour of Java Tamara Bonaci t.bonaci@northeastern.edu Administrivia 1! First assignment due on Monday, Sept 18 by 6pm! Code walkthroughs

More information

CIS October 19, 2017

CIS October 19, 2017 CIS 1068 October 19, 2017 Administrative Stuff String methods due tomorrow Boston Accent Reading: up to chapter 5 Midterms Last Time midterm discussion guessing game Legal Identifiers Ch33zyHaX0R cous

More information

DATA TYPES AND EXPRESSIONS

DATA TYPES AND EXPRESSIONS DATA TYPES AND EXPRESSIONS Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment Mathematical

More information

Fall 2017 CISC124 9/16/2017

Fall 2017 CISC124 9/16/2017 CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems

More information

char ch = astring; //where astring is a String..illegal char ch = A ; //illegal

char ch = astring; //where astring is a String..illegal char ch = A ; //illegal 13-1 Character type and types can t be stored into each other. The following lines of code are : char ch = astring; //where astring is a String..illegal char ch = A ; //illegal String x = xchar; //where

More information

CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum. Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum Adapted from slides by Marty Stepp and Stuart Reges Factoring if/else code factoring: Extracting common/redundant code. Can reduce or eliminate

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

CS 163/164 Exam 2 Review

CS 163/164 Exam 2 Review CS 163/164 Exam 2 Review Review from first exam What does this print? String s = marco polo ; System.out.println(s.substring(0,3)); mar Print the predefined double variable d with 9 decimal place precision

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

CS 106A, Lecture 5 Booleans and Control Flow

CS 106A, Lecture 5 Booleans and Control Flow CS 106A, Lecture 5 Booleans and Control Flow suggested reading: Java Ch. 3.4-4.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5

More information

MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner 1 Lecture outline console input with Scanner objects input tokens Scanner as a parameter to a method cumulative

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 The if/else statement Executes one block if a test is true,

More information

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam Seat Number Name CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam This is a closed book exam. Answer all of the questions on the question paper in the space provided. If

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

5) (4 points) What is the value of the boolean variable equals after the following statement?

5) (4 points) What is the value of the boolean variable equals after the following statement? For problems 1-5, give a short answer to the question. (15 points, ~8 minutes) 1) (4 points) Write four Java statements that declare and initialize the following variables: A) a long integer with the value

More information

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) Overview (5) Topics Output: printf Input: scanf Basic format codes More on initializing variables 2000

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture 2- Primitive Data and Stepwise Refinement Data Types Type - A category or set of data values. Constrains the operations that can be performed on data Many languages

More information

Question: Total Points: Score:

Question: Total Points: Score: CS 170 Exam 1 Section 001 Fall 2014 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than

More information

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture) COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1 Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Declaration and Memory

Declaration and Memory Declaration and Memory With the declaration int width; the compiler will set aside a 4-byte (32-bit) block of memory (see right) The compiler has a symbol table, which will have an entry such as Identifier

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11 Administration Exceptions CS 99 Summer 2000 Michael Clarkson Lecture 11 Lab 10 due tomorrow No lab tomorrow Work on final projects Remaining office hours Rick: today 2-3 Michael: Thursday 10-noon, Monday

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

CSc 110, Autumn Lecture 11: Strings. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn Lecture 11: Strings. Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Autumn 2016 Lecture 11: Strings Adapted from slides by Marty Stepp and Stuart Reges Strings string: a type that stores a sequence of text characters. name = "text" name = expression Examples:

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long Primitive Types Four integer types: byte short int (most common) long Two floating-point types: float double (most common) One character type: char One boolean type: boolean 1 2 Primitive Types, cont.

More information

CSS 161 Fundamentals of Compu3ng. Flow control (2) October 10, Instructor: Uma Murthy

CSS 161 Fundamentals of Compu3ng. Flow control (2) October 10, Instructor: Uma Murthy CSS 161 Fundamentals of Compu3ng Flow control (2) October 10, 2012 Instructor: Uma Murthy Outline Reminders: HW 2 due Monday Today: Errata Review condi3onals Boolean expressions (3.2) Loops (3.3) CSS 161:

More information

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13

Programming Projects: 2.1, 2.3, 2.4, 2.7, 2.8, 2.13 Chapter 2 Objects and Primitive Data - AP Chapter Objectives Define the difference between primitive data and objects. Declare and use variables. Perform mathematical computations. Create objects and use

More information

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem. 60-140-1 Lecture for Thursday, Sept. 18, 2014. *** Dear 60-140-1 class, I am posting this lecture I would have given tomorrow, Thursday, Sept. 18, 2014 so you can read and continue with learning the course

More information

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

What did we talk about last time? Examples switch statements

What did we talk about last time? Examples switch statements Week 4 - Friday What did we talk about last time? Examples switch statements History of computers Hardware Software development Basic Java syntax Output with System.out.print() Mechanical Calculation

More information