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

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

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

Building Java Programs. Chapter 1: Introduction to Java Programming

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

Building Java Programs

Building Java Programs Chapter 1

Introduction to Computer Programming

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

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

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

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

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

Building Java Programs Chapter 1

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

Building Java Programs

Welcome to CSE 142! Zorah Fung University of Washington, Spring Building Java Programs Chapter 1 Lecture 1: Introduction; Basic Java Programs

Lecture 1: Basic Java Syntax

CS 112 Introduction to Programming

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

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

Topic 3 static Methods and Structured Programming

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

AP Computer Science Unit 1. Programs

Building Java Programs

CS111: PROGRAMMING LANGUAGE II

Topic 12 more if/else, cumulative algorithms, printf

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

Topic 12 more if/else, cumulative algorithms, printf

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

Full file at

Oct Decision Structures cont d

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Important Java terminology

Building Java Programs

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

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

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Building Java Programs

Methods CSC 121 Spring 2017 Howard Rosenthal

Department of Computer Science Purdue University, West Lafayette

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Week 2: Data and Output

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

Building Java Programs

Question: Total Points: Score:

Midterm Examination (MTA)

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Coding 3. Over & over again!

AP CS Unit 3: Control Structures Notes

CS 101 Spring 2007 Midterm 2 Name: ID:

Chapter 3. Selections

Lecture 8 " INPUT " Instructor: Craig Duckett

What did we talk about last time? Examples switch statements

Programming Exercise 7: Static Methods

Programming with Java

AP Computer Science Unit 1. Writing Programs Using BlueJ

1 Short Answer (10 Points Each)

Building Java Programs

Getting started with Java

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Using Java Classes Fall 2018 Margaret Reid-Miller


Introduction to Java Unit 1. Using BlueJ to Write Programs

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Conditional Execution

Full file at

AP Computer Science Summer Work Mrs. Kaelin

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Methods CSC 121 Fall 2016 Howard Rosenthal

Building Java Programs Chapter 2

Week 6 CS 302. Jim Williams, PhD

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Building Java Programs

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

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

Full file at

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CEN 414 Java Programming

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CSEN 202 Introduction to Computer Programming

! 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

CS 112 Introduction to Programming

Full file at

Advanced if/else & Cumulative Sum

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

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

Chapter 2: Basic Elements of Java

Elementary Programming


Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Basic Computation. Chapter 2

Transcription:

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

Review of Quiz 12 Quiz 12: You are to write a method that calculates the arithmetic mean (i.e.: average = sum of data items divided by the number of items) and the geometric mean (i.e.: the n-th root of the product of the data items where n is the number of data items). The key points being tested in Quiz 12 are your ability to use cumulative techniques introduced in Lecture 11 to find the sum and product of a series of data items. Fill in the missing code below, place the entire program (class) into DrJava, compile and run the code and show your result to Prof. Soderstrand: import java.util.*; // for Scanner public class Quiz12 { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many data items to you wish to process? "); int number = console.nextint(); System.out.println(); processdata(console, number); public static void processdata(scanner console, int number) { System.out.print("Enter " + number); System.out.print(" data items (double or integer) "); System.out.print("separated by spaces: "); double temp; double sum = 0; double product = 1; Place your code here! double arithmeticmean = sum/number; double geometricmean = Math.pow(product, 1.0/number); System.out.print("The arithmetic mean is " + arithmeticmean); System.out.println(" and the geometric mean is " + geometricmean); 4

Review of Quiz 12 The key points to understand from Quiz 12 are as follows: 1. Reading Scanner object input from the console in a for loop: console.nextdouble() 2. Algorithm for finding the sum of a set of data points: double sum = 0; for (i = 1; i <= number; i++) { temp = console.nextdouble(); sum = sum + temp; 3. Algorithm for finding the product of a set of data points: double product = 1; for (i = 1; i <= number; i++) { temp = console.nextdouble(); product = product * temp; 5

Review of Quiz 12 The key points to understand from Quiz 12 are as follows: 4. Combining the two loops into a single loop: double sum = 0; double product = 1; for (i = 1; i <= number; i++) { temp = console.nextdouble(); sum = sum + temp; product = product * temp; 6

Review of Quiz 12 7

Review of Quiz 02 8

Review of Quiz 02 9

Review of Quiz 02 10

Review of Quiz 02 11

Review of Quiz 02 PROBLEM: The tab character \t in output moves the cursor to the next tab stop on the output console. The default setting for tab stops on the DrJava console is 8 spaces. Two tab characters are required after the Gold Ring 12

Review What is the output of the following println statements? System.out.println("\ta\tb\tc"); a b c System.out.println("\\\\"); \\ System.out.println("'"); ' System.out.println("\"\"\""); """ System.out.println("C:\nin\the downward spiral"); C: in he downward spiral 13

Review Write a println statement to produce the line of output: / \ // \\ /// \\\ System.out.println("/ \\ // \\\\ /// \\\\\\"); 14

Review What println statements will generate this output? This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this continent a new nation. System.out.println("This program prints a"); System.out.println("quote from the Gettysburg Address."); System.out.println(); System.out.println("\"Four score and seven years ago,"); System.out.println("our 'fore fathers' brought forth on"); System.out.println("this continent a new nation.\""); 15

Review What println statements will generate this output? A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget: use \" instead of "! '' is not the same as System.out.println("A \"quoted\" String is"); System.out.println("'much' better if you learn"); System.out.println("the rules of \"escape sequences.\""); System.out.println(); System.out.println("Also, \"\" represents an empty String."); System.out.println("Don't forget: use \\\" instead of \"!"); System.out.println("'' is not the same as \""); 16

Review Where to place comments: at the top of each file (a "comment header") at the start of every method (seen in today s lecture) to explain complex pieces of code Comments are useful for: Understanding larger, more complex programs. Multiple programmers working together, who must understand each other's code. 17

Review Example of a wellcommented program: /* Suzy Student, CS 101, Fall 2019 This program prints lyrics about... something. */ public class BaWitDaBa { public static void main(string[] args) { // first verse System.out.println("Bawitdaba"); System.out.println("da bang a dang diggy diggy"); System.out.println(); // second verse System.out.println("diggy said the boogy"); System.out.println("said up jump the boogy"); 18

Review of Quiz 3 19

Review of Quiz 3 20

Review of Quiz 4 Horizontal redundancy cannot be eliminated cones(); squares(); us(); 21

Review of Quiz 4 cones(); squares(); us(); squares(); cones(); 22

Review of Quiz 5 The following is the class Quiz5, the program that calls the five methods that solve the five problems: 23

Review of Quiz 5 Problem 1 Write a Java method problem1 that prints out on the console (8 characters per line each separated by a space) the Unicode special characters for HEX 20 through HEX 47. Method problem1 should initialize the char variable x to a space (i.e.: char x = ) and then print five lines to the console containing the special characters (Hint: use the ++ operator no loops are allowed!). The point of this problem was to demonstrate use of the x++ in a print statement and the location of the Unicode characters. 24

Review of Quiz 5 Problem 1 OUTPUT: The point of this problem was to demonstrate use of the x++ in a print statement and the location of the Unicode characters. 25

Review of Quiz 5 Problem 2 The mod function, %, for integers is fairly easy to understand. It is the remainder when the numerator n is divided by the denominator d. As noted in class, the general form of the mod function, %, is: n%d = (n/d (int)(n/d))*d. In a single println statement, calculate 128 % 33 as integers and compare it to the definition (128.0/33-128/33)*33. In a single println statement, calculate 12.8 % 3.3 and compare it to the definition. In a single println statement, calculate 11.4 % 3 and compare it to the definition. NOTE: The first two lines of your code should print out as follows: n = 128, d = 33, n % d = 29(29.000000000000004) n = 12.8, d = 3.3, n % d = 2.9000000000000012(2.9000000000000017) Why is there a difference between n % d and the definition? The point of this problem was to demonstrate use of the mod function % and the problem with exact numbers in type double. 26

Review of Quiz 5 Problem 2 Solution: The simplest solution is to use values, not variables, in the arithmetic operations directly in the print statement. This way you do not need to define any variables or do anything complicated. The point of this problem was to demonstrate use of the mod function % and the problem with exact numbers in type double. 27

Review of Quiz 5 Problem 2 Solution: The point of this problem was to demonstrate use of the mod function % and the problem with exact numbers in type double. 28

Review of Quiz 5 Problem 3 The point of this problem was to demonstrate use of the pre-increment (++x) and post increment (x++). 29

Review of Quiz 5 Problem 4 Write a single println statement that prints the following line (no loops allowed): 3, 9, 27, 81, 243 The point of this problem was to demonstrate use of the *= operator. 30

Review of Quiz 5 Problem 5 Set an integer x equal to 12345 (int x = 12345;) and then use a single println statement to print each digit out in order with a space in between them (1 2 3 4 5). The point of this problem was to demonstrate use of the integer divide by powers of 10 combined with the mod 10 (% 10) operators to isolate individual digits from a decimal integer. 31

Review of Loops First standard technique to design a for loop that executes ntimes: for (int i = 1; i <= ntimes; i++) { statements here execute ntimes; { Second standard technique to design a for loop that executes ntimes: for (int i = 0; i < ntimes; i++) { statements here execute ntimes; { 32

Review of Loops First nested for loop that prints nlines and ncolumns : for (int i = 1; i <= nlines; i++) { for (int j = 1; j <= ncolumns; j++) { System.out.print("*"); System.out.println(); *********** In this example, *********** *********** nlines = 3 and ncolumns = 10 33

Review of Loops Second nested for loop that prints nlines and ncolumns : for (int i = 0; i < nlines; i++) { for (int j = 0; j < ncolumns; j++) { System.out.print("*"); System.out.println(); *********** In this example, *********** *********** nlines = 3 and ncolumns = 10 34

Review of Loops First nested for loop that prints nlines triangle: for (int i = 1; i <= nlines; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); System.out.println(); * In this example, ** *** nlines = 3 35

Review of Loops Second nested for loop that prints nlines triangle: for (int i = 0; i < nlines; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); System.out.println(); * In this example, ** *** nlines = 3 36

Review of Loops First nested for loop that prints nlines triangle * extended to square using!: for (int i = 1; i <= nlines; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); for (int j = i+1; j <= nlines; j++) { System.out.print("!"); System.out.println(); *!! In this example, **! *** nlines = 3 37

Review of Loops Second nested for loop that prints nlines triangle * extended to square using!: for (int i = 0; i < nlines; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); for (int j = i+1; j < nlines; j++) { System.out.print("!"); System.out.println(); *!! In this example, **! *** nlines = 3 38

Review of Loops We can double the width of any of these figures by printing two character: for (int i = 0; i < nlines; i++) { for (int j = 0; j <= i; j++) { System.out.print("**"); for (int j = i+1; j < nlines; j++) { System.out.print("!!"); System.out.println(); **!!!! In this example, ****!! ****** nlines = 3 39

Review of Quiz 6 40

Review of Quiz 6 Line "\\\\ i "!!" 11 2*i "//" 1 2 3 4 5 6 0 1 2 3 4 5 0 1 2 3 4 5 11 9 7 5 3 1 11 9 7 5 3 1 0 1 2 3 4 5 i 0 1 2 3 4 5 exclamation = a i + b 11 = a 0 + b => b = 11 9 = a 1 + b => a = 9 b = 9 11 = 2 exclamation = 11 2 i 41

Review of Quiz 6 Line "\\\\ i "!!" 11 2*i "//" i 1 0 0 11 11 0 0 2 1 1 9 9 1 1 3 2 2 7 7 2 2 4 3 3 5 5 3 3 5 4 4 3 3 4 4 6 5 5 1 1 5 5 42

Review of Quiz 6 43

Review of Quiz 7 44

Review of Quiz 7 45

Review of Quiz 7 46

Review of Quiz 7 (Solution for drawline();) public static void drawline() { // Prints top and bottom line System.out.print("#"); for (int i = 1; i <= 4*SIZE; i++) { System.out.print("="); System.out.println("#"); 47

Review of Quiz 7 (Solution for bottomhalf();) public static void bottomhalf() { // Prints the contracting pattern of <> for the top half of the figure for (int line = SIZE; line >= 1; line--) { System.out.print(" "); for (int space = 1; space <= (-2 * line + 2*SIZE); space++) { System.out.print(" "); System.out.print("<>"); for (int dot = 1; dot <= (4 * line - 4); dot++) { System.out.print("."); System.out.print("<>"); for (int space = 1; space <= (-2 * line + 2*SIZE); space++) { System.out.print(" "); System.out.println(" "); 48

Review of Quiz 8 49

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 50

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

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

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

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

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 55

Review of Quiz 8 (The entire program) 56

Review of Quiz 8 (The printout) 57

Quiz 8 (Solution) 58

Review of Quiz 9 59

Review of Quiz 9 Key items you need to know from this quiz: How to use the Math class objects Math.round(x) Math.pow(y, m) The rest was done for you in the Quiz problem setup Write the method round1(d, n) such that the double d is rounded to n decimal places Use Math.pow(10, n) to multiply d by 10 n bringing the n th decimal digit to the one s place Use Math.round() to round to the one s place Divide by 10 n to restore the n th decimal digit to its right place 60

Review of Quiz 9 The key to moving the decimal point to the correct place for rounding and then returning it after rounding: The ability to move the decimal point to the right by multiplying 10 n and to the left by dividing by 10 n. Math.pow(10, n) generates the value 10 n we must multiply d by before we round and then divide the result by to get our final answer. 61

Review of Quiz 9 The TWO lines we need to add to the given code are: double scalingfactor = Math.pow(10, n); return Math.round(scalingFactor*d)/scalingFactor; public static double round1(double d, int n) { double scalingfactor = Math.pow(10, n); return Math.round(scalingFactor*d)/scalingFactor; 62

Review of Quiz 9 The entire program: public class RoundTest { public static void main(string[] args) { double x = 123.4567; System.out.print("x = " + x + ", round(x,0) = " + round1(x,0) + ", round(x,1) = " + round1(x,1)); System.out.print(", round(x,2) = " + round1(x,2) + ", round(x,3) = " + round1(x,3)); System.out.println(", round(x,-1) = " + round1(x,-1) + ", round(x,-2) = " + round1(x,-2)); public static double round1(double d, int n) { double scalingfactor = Math.pow(10, n); return Math.round(scalingFactor*d)/scalingFactor; 63

Review of Quiz 9 The entire program including printout: 64

Review of Quiz 9 The entire program including printout: The concept of moving the decimal point through multiplication by powers of 10 is a very useful concept and has many applications in computer programming: Recall that in Quiz 5 Problem 5 we were able to select single digits from an integer by the combination of scaling by a power of 10 and using the mod 10 operator (x % 10) to extract the units digit. This is another use of the important concept of moving the decimal point through multiplication by a power of 10. You should review Quiz 5 Problem 5 along with this Quiz in order to fully understand this important concept. 65

Review of Quiz 10 66

Review of Quiz 10 The key points to understand from Quiz 10 are as follows: 1. The actual parameter in the calling program is a string that will be the prompt printed in the method getinput. 2. The actual parameter from the calling method is copied into the formal parameter (String prompt) in the called method. 3. Thus to print the prompt, the first of the two lines we need is the statement System.out.print(prompt); 4. The statement Scanner console = new Scanner(sytem.in); creates the console object which inherits all the Scanner object method including nextdouble(); 5. Hence, the double value typed by the user on the console can be returned to the calling program with the second statement that we need to provide: 6. The return statement copies the value after the return (in this case the number typed by the user) to a storage location in the computer with the same name as the method: getinput 67

Review of Quiz 10 68

Review of Quiz 10 69

Review of Quiz 11 70

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)); 71

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

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

Review of Quiz 11 74

Assignments for this week 1. Laboratory for Chapter 4 due today (Monday 10/20) IMPORTANT: When you email me your laboratory Word Document, be sure it is all in one file 2. Midterm will be on Monday, 10/13/2014 in class. 3. Complete the take-home portion of the exam before class on Monday 10/13/2014. Email your completed take-home to Prof. Soderstrand in ONE Word document (similar to labs) by the beginning of class Monday 10/13/2014. 4. Be sure to complete Quiz 13 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 75