Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from 1-1000? // This may require a lot of typing int sum = 1 +

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Building Java Programs
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Topic 12 more if/else, cumulative algorithms, printf Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Chapter 4 Conditional Execution.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
Building Java Programs
Building Java Programs Chapter 4
Conditional Execution
Conditional Execution
CSc 110, Autumn 2016 Lecture 12: Advanced if/else; Cumulative sum
Repetition-Counter control Loop
Building Java Programs
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Building Java Programs
Topic 12 more if/else, cumulative algorithms, printf
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Using Objects (continued)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Cumulative algorithms

2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = ; System.out.println("The sum is " + sum); What if we want the sum from 1 - 1,000,000? Or the sum up to any maximum? –How can we generalize the above code?

3 Cumulative sum loop int sum = 0; for (int i = 1; i <= 1000; i++) { sum = sum + i; } System.out.println("The sum is " + sum); cumulative sum: A variable that keeps a sum in progress and is updated repeatedly until summing is finished. –The sum in the above code is a cumulative sum. –Must be declared outside the loops that update them, so that they will still exist after the loop

4 Cumulative product This cumulative idea can be used with other operators: int product = 1; for (int i = 1; i <= 20; i++) { product = product * 2; } System.out.println("2 ^ 20 = " + product); –How would we make the base and exponent adjustable via a “power” method?

5 Scanner and cumul. sum We can do a cumulative sum of user input: // Build Scanner object // Get 100 #’s from user and sum them

6 Cumulative sum question Modify the Receipt program from before –Prompt for how many people, and each person's dinner cost. –Use static methods to structure the solution. 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? 30.0 Person #4: How much did your dinner cost? Subtotal: $75.0 Tax: $6.0 Tip: $11.25 Total: $92.25

7 Cumulative sum answer // This program enhances our Receipt program using a cumulative sum. import java.util.*; public class Receipt2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); double subtotal = computeMealSubtotal(console); printResults(subtotal); } // Prompts for number of people and returns total meal subtotal. public static double computeMealSubtotal(Scanner console) { System.out.print("How many people ate? "); int people = console.nextInt(); double subtotal = 0.0; // cumulative sum for (int i = 1; i <= people; i++) { System.out.print("Person #" + i + ": How much did your dinner cost? "); double personCost = console.nextDouble(); subtotal = subtotal + personCost; // add to sum } return subtotal; }...

8 Cumulative answer, cont'd.... // Calculates total owed, assuming 8% tax and 15% tip public static void printResults(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); }

9 if/else, return question Write a method countFactors that returns the number of factors of an integer. –countFactors(24) returns 8 because 1, 2, 3, 4, 6, 8, 12, and 24 are factors of 24. Solution: // Returns how many factors the given number has. public static int countFactors(int number) { int count = 0;... return count; }