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 Lecture 05 2/10/2014 3

4 Review of Quiz 8 4

5 Review of Quiz 8 Key items you need to know from this quiz: How to pass parameters from the calling program to the called program How to set up a for loop to count the rows How to set up a for loop to count the columns Where to put the three print statements: System.out.print( ) to print the matrix elements System.out.println() to create new line after each row System.out.println() to create new line after each matrix 5

6 Review of Quiz 8 Main method calls the method matrix with the actual parameters to specify each of the three matrices: public class Quiz8 { public static void main (String[] args){ matrix('a', 3, 4); //Print 3x4 a matrix matrix('b', 5, 5); //Print 5x5 b matrix matrix('c', 4, 3); //Print 4x3 c matrix } public static void matrix(char sym, int row, int col) 6

7 Review of Quiz 8 Main method calls the method matrix with the actual parameters to specify each of the three matrices: public class Quiz8 { public static void main (String[] args){ matrix('a', 3, 4); //Print 3x4 a matrix matrix('b', 5, 5); //Print 5x5 b matrix matrix('c', 4, 3); //Print 4x3 c matrix } public static void matrix(char sym, int row, int col) 7

8 Review of Quiz 8 Main method calls the method matrix with the actual parameters to specify each of the three matrices: public class Quiz8 { public static void main (String[] args){ matrix('a', 3, 4); //Print 3x4 a matrix matrix('b', 5, 5); //Print 5x5 b matrix matrix('c', 4, 3); //Print 4x3 c matrix } public static void matrix(char sym, int row, int col) 8

9 Review of Quiz 8 Main method calls the method matrix with the actual parameters to specify each of the three matrices: Actual Parameters public class Quiz8 { public static void main (String[] args){ matrix('a', 3, 4); //Print 3x4 a matrix matrix('b', 5, 5); //Print 5x5 b matrix matrix('c', 4, 3); //Print 4x3 c matrix } Formal Parameters public static void matrix(char sym, int row, int col) 9

10 Review of Quiz 8 The method matrix copies the values of the actual parameters into its formal paramenters: sym, row and col matrix('a', 3, 4); a 3 4 public static void matrix(char sym, int row, int col) { for (int i = 1; i <= row; i++) { //i counts the rows for (int j = 1; j <= col; j++) { //j counts the columns System.out.print(sym + "(" + i + ", " + j + ") "); } //NOTE: this ends the printing of one row System.out.println(); //New line after each row } //NOTE: this ends the printing of the entire matrix System.out.println(); //New line after each matrix } 10

11 Review of Quiz 8 (The entire program) 11

12 Review of Quiz 8 (The printout) 12

13 Quiz 8 (Solution) 13

14 Objects and Classes; Strings (pp ) We ve spent a considerable amount of time discussing the primitive types in Java and how they work, so it s about time that we started talking about objects and how they work. The idea for objects came from the observation that as we start working with new kinds of data (integers, reals, characters, text, etc.), we find ourselves writing a lot of methods that operate on that data. Rather than completely separating the basic operations from the data, it seemed to make sense to include them together. This packaging of data and operations into one entity is the central idea behind objects. An object stores some data and has methods that act on its data. 14

15 Classes and objects (p. 160) class: A program entity that represents either: 1. A program / module, or 2. A type of object. A class is a blueprint or template for constructing objects. Example: The DrawingPanel class (type) is a template for creating many DrawingPanel objects (windows). Java has 1000s of classes. Later (Ch.8) we will write our own. object: An entity that combines data and behavior. object-oriented programming (OOP): Programs that perform their behavior as interactions between objects. 15

16 Objects (p. 160) object: An entity that contains data and behavior. data: behavior: variables inside the object methods inside the object You interact with the methods; the data is hidden in the object. Constructing (creating) an object: Type objectname = new Type(parameters); Calling an object's method: objectname.methodname(parameters); 16

17 Using Class as a Blueprint for Objects (p. 160) A class is like a blueprint of what the object looks like. Once you ve given Java the blueprint, you can ask it to create actual objects that match that blueprint. We refer to the individual objects as instances of the class. Often people use the words instance and object interchangeably. This concept classes as blueprints for objects is difficult to understand in the abstract. Don't worry too much about this in COSC it is the subject of COSC-237 However, to give a brief introduction to what it means and how it works, we ll look at several different pre-defined Java classes. In keeping with our idea of focusing on fundamental concepts first, in this chapter we ll study how to use existing objects that are already part of Java. We will leave the issue of how to define our own new types of objects for COSC

18 Blueprint analogy (p. 160) state: current song volume battery life behavior: power on/off change station/song change volume choose random song ipod blueprint/factory creates ipod #1 state: song = "1,000,000 Miles" volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song ipod #2 state: song = "Letting You" volume = 9 battery life = 3.41 hrs behavior: power on/off change station/song change volume choose random song ipod #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs behavior: power on/off change station/song change volume choose random song 18

19 Classes and objects (p. 160) Using objects differs from using primitive types We ll have to introduce some new syntax and concepts. It would be nice if Java had a consistent model for using all types of data, but Java doesn t. If you want to understand how your programs operate, you ll have to learn two sets of rules: One for primitives A different one for objects. 19

20 Strings as and example of objects (p ) Let s use strings as our example of an object We already know some of the syntax and concepts. Let s look at what we know in the new context of classes and objects. Strings are an example of objects: Strings contain data (the characters) Strings contain behavior (methods this is the new part). 20

21 Strings (pp ) string: An object storing a sequence of text characters. Unlike most other objects, a String is not created with new. String name = "text"; String name = expression; (using the + concatenation) Examples: String name = "Marla Singer"; int x = 3; int y = 5; String point = "(" + x + ", " + y + ")"; 21

22 Behavior: Methods associated with strings (Indexes) Characters of a string are numbered with 0-based indexes: String name = "R. Kelly"; index character R. K e l l y First character's index : 0 Last character's index : 1 less than the string's length The individual characters are values of type char (seen later) 22

23 String methods (p. 166) indexof(str) length() Method name substring(index1, index2) or substring(index1) tolowercase() touppercase() Description index where the start of the given string appears in this string (-1 if not found) number of characters in this string the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string a new string with all lowercase letters a new string with all uppercase letters These methods are called using the dot notation: String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7 23

24 String method examples (p. 166) // index String s1 = "Stuart Reges"; String s2 = "Marty Stepp"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)); // "Reg" String s3 = s2.substring(1, 7); System.out.println(s3.toLowerCase()); // "arty s" Given the following string: // index String book = "Building Java Programs"; How would you extract the word "Java"? NOTE: End is 13, not 12! 24

25 Modifying strings (p. 166) Methods like substring and tolowercase build and return a new string, rather than modifying the current string. String s = "lil bow wow"; s.touppercase(); System.out.println(s); // lil bow wow To modify a variable's value, you must reassign it: String s = "lil bow wow"; s = s.touppercase(); System.out.println(s); // LIL BOW WOW 25

26 Modifying strings (p. 166) String Methods 26

27 Interactive Programs with Scanner (pp ) We have been using System.out.println() and System.out.print() to provide output to the console Java also has a System.in.read() to read a character from the console System.in.read() has the following characteristics: It only reads one character It returns the integer Unicode value of the character As a result, System.in.read() is not a very useful 27

28 Example of System.in.read() Not in text 28

29 Input and System.in interactive program: Reads input from the console. While the program runs, it asks the user to type input. The input typed by the user is stored in variables in the code. Stores Unicode value of character typed Can be tricky; users are unpredictable and misbehave. But interactive programs have more interesting behavior. Scanner: An object that can read input from many sources. Communicates with System.in (the opposite of System.out) Can also read from files (Ch. 6), web sites, databases,... 29

30 Scanner syntax (pp ) The Scanner class is found in the java.util package. import java.util.*; // so you can use Scanner Constructing a Scanner object to read console input: Scanner name = new Scanner(System.in); Example: Scanner console = new Scanner(System.in); 30

31 Scanner methods (pp. 167) Method nextint() nextdouble() next() nextline() Description reads an int from the user and returns it reads a double from the user reads a one-word String from the user reads a one-line String from the user Each method waits until the user presses Enter. The value typed by the user is returned. System.out.print("How old are you? "); // prompt int age = console.nextint(); System.out.println("You typed " + age); prompt: A message telling the user what input to type. 31

32 Scanner example 1 (pp ) import java.util.*; // so that I can use Scanner public class UserInputExample { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How old are you? "); int age = console.nextint(); age 29 years 36 } } int years = 65 - age; System.out.println(years + " years to retirement!"); Console (user input underlined): How old are you? years until retirement! 32

33 Scanner example 2 (pp ) import java.util.*; // so that I can use Scanner public class ScannerMultiply { public static void main(string[] args) { Scanner console = new Scanner(System.in); } } System.out.print("Please type two numbers: "); int num1 = console.nextint(); int num2 = console.nextint(); int product = num1 * num2; System.out.println("The product is " + product); Output (user input underlined): Please type two numbers: The product is The Scanner can read multiple values from one line. Create console as a new scanner object with input from System.in Get first number Get second number Prompt user to enter two numbers User types the two numbers separated by a space (white space) Calculate and print the result 33

34 Input tokens (p. 168) token: A unit of user input, as read by the Scanner. Tokens are separated by whitespace (spaces, tabs, new lines). How many tokens appear on the following line of input? 23 John Smith 42.0 "Hello world" $2.50 " 19"

35 Input tokens (p. 168) 23 John Smith 42.0 "Hello world" $2.50 " 19"

36 Input tokens (p. 168) When a token is not the type you ask for, it crashes. System.out.print("What is your age? "); int age = console.nextint(); Output: What is your age? Timmy java.util.inputmismatchexception at java.util.scanner.next(unknown Source) at java.util.scanner.nextint(unknown Source)... 36

37 Scanner Methods (pp ) Scanner's next method reads a word of input as a String. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); name = name.touppercase(); System.out.println(name + " has " + name.length() + " letters and starts with " + name.substring(0, 1)); Output: What is your name? Chamillionaire CHAMILLIONAIRE has 14 letters and starts with C The nextline method reads a line of input as a String. System.out.print("What is your address? "); String address = console.nextline(); 37

38 Use of console.next() (p ) 23 John Smith 42.0 "Hello world" $2.50 " 19" 38

39 Use of console.nextline() (p ) 23 John Smith 42.0 "Hello world" $2.50 " 19" 39

40 Strings question (pp ) Write a program that outputs a person's "gangsta name." first initial Diddy last name (all caps) first name -izzle Example Output: Type your name, playa: Marge Simpson Your gangsta name is "M. Diddy SIMPSON Marge-izzle" 40

41 // This program prints your "gangsta" name. import java.util.*; public class GangstaName { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("Type your name, playa: "); String name = console.nextline(); // split name into first/last name and initials String first = name.substring(0, name.indexof(" ")); String last = name.substring(name.indexof(" ") + 1); last = last.touppercase(); String finitial = first.substring(0, 1); System.out.println("Your gangsta name is \"" + finitial + ". Diddy " + last + " " + first + "-izzle\""); } } 41

42 NOTE: substring(index) with a single index grabs the substring starting at index through the end of the string. substring(index) is the same as substring(index, name.length()) 42

43 Assignments for this week 1. Laboratory for Chapter 3 due (Monday 10/6) IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 2. Read Section 3.4 for Wednesday (pp ) 3. Be sure to complete Quiz 9 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 43

Building Java Programs Chapter 3

Building Java Programs Chapter 3 Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson 2013. All rights reserved. Redundant recipes Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups

More information

Redundant recipes. Building Java Programs Chapter 3. Parameterized recipe. Redundant figures

Redundant recipes. Building Java Programs Chapter 3. Parameterized recipe. Redundant figures Redundant recipes Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson 2013. All rights reserved. Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups

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

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp! Garfield AP CS User Input, If/Else Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp! Warmup Write a method add10 that takes one integer parameter. Your method should return

More information

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution Topic 11 Scanner object, conditional execution "There are only two kinds of programming languages: those people always [complain] about and those nobody uses." Bjarne Stroustroup, creator of C++ Copyright

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

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education AP Computer Science Return values, Math, and double Distance between points Write a method that given x and y coordinates for two points prints the distance between them If you can t do all of it, pseudocode?

More information

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution https://www.dignitymemorial.com/obituaries/brookline-ma/adele-koss-5237804 Topic 11 Scanner object, conditional execution Logical thinking and experience was as important as theory in using the computer

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 3.4, 4.1, 4.5 2 Interactive Programs with Scanner reading: 3.3-3.4 Interactive programs We have written programs that print console

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

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

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 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-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-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

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

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

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

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

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 8 Lecture 8-1: Classes and Objects reading: 8.1-8.3 self-checks: #1-9 exercises: #1-4 A programming problem Given a file of cities' (x, y) coordinates, which begins with

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

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

Input. Scanner keyboard = new Scanner(System.in); String name;

Input. Scanner keyboard = new Scanner(System.in); String name; Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system.

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: 6.3-6.5 self-check: #7-11 exercises: #1-4, 8-11 Copyright 2009 by Pearson Education Hours question Given a file

More information

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual jump

More information

AP Computer Science A

AP Computer Science A AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive

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

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

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

! definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. ! We often use language like Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is

More information

Lecture 11: Intro to Classes

Lecture 11: Intro to Classes Lecture 11: Intro to Classes Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Classes and objects class: A program entity

More information

Introduction to Java Unit 1. Using BlueJ to Write Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

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

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

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 8 Lecture 18: Classes and Objects reading: 8.1-8.2 (Slides adapted from Stuart Reges, Hélène Martin, and Marty Stepp) 2 File output reading: 6.4-6.5 3 Output to files PrintStream:

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1 self-check: Ch. 4 #2; Ch. 5 # 1-10 exercises: Ch. 4 #2, 4, 5, 8; Ch. 5 # 1-2 Copyright 2009

More information

CS 106A, Lecture 25 Life After CS 106A, Part 1

CS 106A, Lecture 25 Life After CS 106A, Part 1 CS 106A, Lecture 25 Life After CS 106A, Part 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6: File Processing 1 Lecture outline line-based file processing using Scanners processing a file line by line mixing line-based and token-based file processing searching

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

References and objects

References and objects References and objects Arrays and objects use reference semantics. Why? efficiency. Copying large objects slows down a program. sharing. It's useful to share an object's data among methods. DrawingPanel

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Topic 27 classes and objects, state and behavior

Topic 27 classes and objects, state and behavior Topic 27 classes and objects, state and behavior "A 'class' is where we teach an 'object' to behave." -Rich Pattis Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

More information

Lecture 8 " INPUT " Instructor: Craig Duckett

Lecture 8  INPUT  Instructor: Craig Duckett Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment

More information

Unit 4: Classes and Objects Notes

Unit 4: Classes and Objects Notes Unit 4: Classes and Objects Notes AP CS A Another Data Type. So far, we have used two types of primitive variables: ints and doubles. Another data type is the boolean data type. Variables of type boolean

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

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

double float char In a method: final typename variablename = expression ;

double float char In a method: final typename variablename = expression ; Chapter 4 Fundamental Data Types The Plan For Today Return Chapter 3 Assignment/Exam Corrections Chapter 4 4.4: Arithmetic Operations and Mathematical Functions 4.5: Calling Static Methods 4.6: Strings

More information

Topic 14 while loops and loop patterns

Topic 14 while loops and loop patterns Topic 14 while loops and loop patterns "Given enough eyeballs, all bugs are shallow (e.g., given a large enough beta-tester and co-developer base, almost every problem will be characterized quickly and

More information

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

Computer Programming, I. Laboratory Manual. Experiment #5. Strings & Text Files Input

Computer Programming, I. Laboratory Manual. Experiment #5. Strings & Text Files Input Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #5

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

! Theoretical examples: ! Examples from Java: ! data type: A category of data values. " Example: integer, real number, string

! Theoretical examples: ! Examples from Java: ! data type: A category of data values.  Example: integer, real number, string Object types Example object types! So far, we have seen: ( types " variables, which represent data (categorized by " methods, which represent behavior! object: An variable that contains data and 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

Building Java Programs

Building Java Programs Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: 8.1-8.2 2 File output reading: 6.4-6.5 3 Output to files PrintStream: An object in the java.io package that lets you print output

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 6: File Processing Lecture 6-1: File input using Scanner reading: 6.1-6.2, 5.3 self-check: Ch. 6 #1-6 exercises: Ch. 6 #5-7 Input/output ("I/O") import java.io.*; Create

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading: 6.3-6.5 2 Hours question Given a file hours.txt with the following contents: 123 Ben 12.5 8.1 7.6 3.2 456 Greg 4.0 11.6 6.5

More information

Reading Input from Text File

Reading Input from Text File Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest

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

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

Midterms Save the Dates!

Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Errors (Using the Scanner and String Classes) Anatomy of a Java Program Readings This Week s Reading: Ch 3.1-3.8 (Major conceptual

More information

Classes and Objects Part 1

Classes and Objects Part 1 COMP-202 Classes and Objects Part 1 Lecture Outline Object Identity, State, Behaviour Class Libraries Import Statement, Packages Object Interface and Implementation Object Life Cycle Creation, Destruction

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

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

CSE 143 Lecture 3. More ArrayList; object-oriented programming. reading: 10.1;

CSE 143 Lecture 3. More ArrayList; object-oriented programming. reading: 10.1; CSE 143 Lecture 3 More ArrayList; object-oriented programming reading: 10.1; 8.1-8.7 slides created by Marty Stepp http://www.cs.washington.edu/143/ Out-of-bounds Legal indexes are between 0 and the list's

More information

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Arrays Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Can we solve this problem? Consider the following program

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Walter Savitch Frank M. Carrano Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers

More information

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics Strings Strings and their methods Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive vs Object Types

More information

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 5.2 1 2 A deceptive problem... Write a method printletters that prints each letter from a word

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6 Lecture 6-3: Searching Files reading: 6.3-6.5 2 Recall: Line-based methods Method nextline() Description returns the next entire line of input hasnextline() returns true

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

c) And last but not least, there are javadoc comments. See Weiss.

c) And last but not least, there are javadoc comments. See Weiss. CSCI 151 Spring 2010 Java Bootcamp The following notes are meant to be a quick refresher on Java. It is not meant to be a means on its own to learn Java. For that you would need a lot more detail (for

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016 First Name (Print): Last Name (Print): Student Number: The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II 2016 Instructor: Dr. Bowen Hui Tuesday, April 19, 2016 Time: 6:00pm

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Chapter 4: Control Structures I

Chapter 4: Control Structures I Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4: Conditional Execution 1 loop techniques cumulative sum fencepost loops conditional execution Chapter outline the if statement and the if/else statement relational expressions

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information