Procedural decomposition using static methods

Similar documents
Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs. Introduction to Java Programming

Building Java Programs

Topic 3 static Methods and Structured Programming

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods?

CS 112 Introduction to Programming

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

CSc 110, Autumn Lecture 2: Functions

CSE 142, Summer 2015

Building Java Programs

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

Static Methods & Decomposition

Lecture 1: Basic Java Syntax

CSE 142, Summer 2014

Introduction to Computer Programming

Building Java Programs Chapter 1

Entering the world of Javatar

clicker question Comments Using comments Topic 3 static Methods and Structured Programming

Building Java Programs Chapter 1

Getting Started With Java

CSE 201 Java Programming I. Smart Coding School website:

Building Java Programs. Introduction to Programming and Simple Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

A Unicorn Named Sparkle Word Search. A New Friend for Sparkle Coloring Sheet. A Unicorn Named Sparkle s First Christmas Coloring Sheet

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Topic 7 parameters. Based on slides from Marty Stepp and Stuart Reges from

Graded Project. HTML Coding

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

CSc 110, Autumn 2016 Lecture 6: Parameters. Adapted from slides by Marty Stepp and Stuart Reges

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

CS106A, Stanford Handout #25. Methods 2

COMPUTATIONAL THINKING

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

Building Java Programs Chapter 2

Building Java Programs. Chapter 1: Introduction to Java Programming

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

Building Java Programs Chapter 2

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

The diagram below is only partially complete. Use the terms in the word list to complete the empty boxes. Make dinner

the rules The Goal Get all three of your monkeys around the board and into the Banana Grove before anyone else can!

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Building Java Programs

Final Exam 2, CS154. April 25, 2010

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

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Algorithms: The recipe for computation

Primitive data, expressions, and variables

CSE 131 Introduction to Computer Science Fall Exam II

Warmup : Name that tune!

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CIS 110: Introduction to Computer Programming

Grade 7 Math LESSON 14: MORE PROBLEMS INVOLVING REAL NUMBERS TEACHING GUIDE

Lesson 36: for() Loops (W11D1)

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Building Java Programs

Building Java Programs

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

COMP 102: Computers and Computing

AP Computer Science Unit 1. Writing Programs Using BlueJ

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

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

AP Computer Science Unit 1. Writing Programs Using BlueJ


Algorithmic Thinking: Loops and Conditionals

The cuisine package. Ben Cohen. 2000/08/01, v0.5

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

CSE 131 Introduction to Computer Science Fall Exam II

Google Home. List of Basic Commands

Early Learning Center Games

Introduction to Java Unit 1. Using BlueJ to Write Programs

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

St. Edmund Preparatory High School Brooklyn, NY

Hour of Code: Teacher Guide

Two figures that have the exact same shape, but not necessarily the exact same size, are called similar

Repetition with for loops

Lesson 166 (Pages 27-29)

CPU. Monitor. Keyboard. Moz: Good questions! Let us learn about each of these parts.

Module 2: Choice and Iteration

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Metamorphosis of Information. Chapter 2: Metamorphosis of Information. What is Information? What is Information? Chapter 2. The Computer Continuum 1

Chapter Legal intliterals: 22, 1, and d Results of intexpressions:

Hang a sign OMG. So many emotions! We feel a great party is in the future. Send reminder (sent by Corporate) and post event on Facebook.

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Recap: Assignment as an Operator CS 112 Introduction to Programming

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CSCI 1103: Introduction

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

Writing a Basic Program in Python

Symmetry Students complete symmetric figures that are partially given and create their own symmetric figures.

What is a Function? What are functions good for?

EXECUTION OF METHODS

CS 170 Java Programming 1. Week 5: Procedures and Functions

Unit: Rational Number Lesson 3.1: What is a Rational Number? Objectives: Students will compare and order rational numbers.

Transcription:

Algorithms Procedural decomposition using static methods Recall: An algorithm is a list of steps for solving a problem. What is the algorithm to bake sugar cookies? 1 2 The Bake sugar cookies algorithm Structured algorithm Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven for the appropriate temperature. Set the timer. Place the cookies into the oven. Allow the cookies to bake. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies. 1. Make the cookie batter. Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. 2. Bake cookies. Set the oven for the appropriate temperature. Set the timer. Place the cookies into the oven. Allow the cookies to bake. 3. Add frosting and sprinkles. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies. Observation: Structured algorithms are easier to understand. 3 4

How do we bake a double batch? Redundancy in programs Unstructured: Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven Set the timer. Place the cookies into the oven. Allow the cookies to bake. Set the oven Set the timer. Place the cookies into the oven. Allow the cookies to bake. Mix the ingredients for the frosting. Spread frosting and sprinkles onto the cookies. Structured: 1. Make the cookie batter. 2a. Bake the first batch of cookies. 2b. Bake the second batch of cookies. 3. Add frosting and sprinkles. Observation: Structured algorithms eliminate redundancy. public class FraggleRock { System.out.println("Dance your cares away,"); System.out.println("Worry's for another day."); System.out.println("Let the music play,"); System.out.println("Down at Fraggle Rock."); System.out.println("Dance your cares away,"); System.out.println("Worry's for another day."); System.out.println("Let the music play,"); System.out.println("Down at Fraggle Rock."); 5 6 How do we structure the program? Worry s for another day! static method: A group of statements given a name. To use a static method: ( recipe 1. define it (write down the Write a group of statements and give it a name. ( recipe 2. call it (cook using the Tell our program to execute the method. public class FraggleRock { public static void singchorus() { System.out.println("Dance your cares away,"); System.out.println("Worry's for another day."); System.out.println("Let the music play,"); System.out.println("Down at Fraggle Rock."); singchorus(); singchorus(); 7 8

Declaring a static method Calling a static method The syntax for declaring a static method (writing down the recipe): public class <class name> {... public static void <method name>() { <statement>; <statement>; <statement>; Static method example 9 The syntax for calling a static method (cooking using the recipe): <method name>(); Methods calling methods 10 Declaring a static method public static void printaffirmation() { System.out.println("I am good enough!"); System.out.println("I am smart enough!"); System.out.println("People like me!"); ( times Calling a static method (possibly multiple printaffirmation(); printaffirmation(); Output I am good enough! I am smart enough! People like me! I am good enough! I am smart enough! People like me! 11 One static method can call another: public class MethodsExample { message1(); message2(); System.out.println("Done with main."); public static void message1() { System.out.println("This is message1."); public static void message2() { System.out.println("This is message2."); message1(); System.out.println("Done with message2."); Output: This is message1. This is message2. This is message1. Done with message2. Done with main. 12

Control flow of methods When a method is called, the execution "jumps" into that method executes all of the method s statements "jumps" back to the statement after the method call Control flow of methods This is message1. This is message2. This is message1. Done with message2. public class MethodsExample { Done with main. message1(); message2(); Output: public static void message1() { System.out.println("This is message1.");... public static void message2() { System.out.println("This is message2"); message1(); System.out.println("Done with message2."); public static void message1() { System.out.println("This is message1."); 13 14 Summary: To use or not to use Example: Figure drawing Yes No Statements that are related to each other (structure). Statements that are repeated (redundancy). Individual statements occurring only once and not related to other statements Unrelated or weakly-related statements Consider splitting the method into two smaller methods. Blank lines Blank println statements can go in the main method. Write a program to print the figures. Use static methods to capture structure and and eliminate redundancy. 15 16

Version 1: Unstructured Version 1: Unstructured Create an empty program with a skeletal header and main method. Copy the expected output into it, surrounding each line with System.out.println syntax. Run and verify that it produces the correct output. // Suzy Student, CSE 142, Autumn 2047 // This program prints several assorted figures. // public class Figures1 { System.out.println(" "); System.out.println(" \"); System.out.println("\"); System.out.println("\"); System.out.println(" \"); System.out.println("\"); System.out.println(" \"); System.out.println(""); System.out.println(" "); System.out.println(" \"); System.out.println("\"); System.out.println(""); System.out.println("\"); System.out.println(" \"); System.out.println(" "); System.out.println(" \"); System.out.println("\"); System.out.println(""); 17 18 Version 2: Structured with redundancy Version 2: Structured with redundancy Identify the overall structure of the output, and divide the main method into several static methods based on this structure. Identify the overall structure of the output, and divide the main method into several static methods based on this structure. The structure of the output: initial "egg" figure second "teacup" figure third "stop sign" figure fourth "hat" figure This structure can be represented by methods: drawegg drawteacup drawstopsign drawhat 19 20

Version 2: Structured with redundancy Version 2: Structured with redundancy // Suzy Student, CSE 142, Autumn 2047 // Prints several assorted figures, with methods for structure. // public class Figures2 { drawegg(); drawteacup(); drawstopsign(); drawhat(); // Draws a figure that vaguely resembles a stop sign. public static void drawstopsign() { System.out.println(" "); System.out.println(" \"); System.out.println("\"); System.out.println(""); System.out.println("\"); System.out.println(" \"); // Draws a figure that vaguely resembles an egg. public static void drawegg() { System.out.println(" "); System.out.println(" \"); System.out.println("\"); System.out.println("\"); System.out.println(" \"); // Draws a figure that vaguely resembles a teacup. public static void drawteacup() { System.out.println("\"); System.out.println(" \"); System.out.println(""); // Draws a figure that vaguely resembles a hat. public static void drawhat() { System.out.println(" "); System.out.println(" \"); System.out.println("\"); System.out.println(""); Version 3: Structured without redundancy 21 Version 3: Structured without redundancy 22 Further divide the program to eliminate all redundancy. Further divide the program to eliminate all redundancy. The redundancy: ( purple ) top half of egg ( green ) bottom half of egg ( yellow ) divider line This redundancy can be fixed by methods: draweggtop draweggbottom drawline 23 24

Version 3: Structured without redundancy Version 3: Structured without redundancy // Suzy Student, CSE 142, Autumn 2047 // Prints several figures, with methods for structure and redundancy. public class Figures3 { drawegg(); drawteacup(); drawstopsign(); drawhat(); // draws redundant part that looks like the top of an egg public static void draweggtop() { System.out.println(" "); System.out.println(" \"); System.out.println("\"); // draws redundant part that looks like the bottom of an egg public static void draweggbottom() { System.out.println("\"); System.out.println(" \"); 25 // Draws a figure that vaguely resembles an egg. public static void drawegg() { draweggtop(); draweggbottom(); // Draws a figure that vaguely resembles a teacup. public static void drawteacup() { draweggbottom(); System.out.println(""); // Draws a figure that vaguely resembles a stop sign. public static void drawstopsign() { draweggtop(); System.out.println(""); draweggbottom(); // Draws a figure that vaguely resembles a hat. public static void drawhat() { draweggtop(); System.out.println(""); 26 Comments: Example Comments: How-to /* Suzy Student CS 101, Fall 2019 This program prints lyrics from my favorite song! */ public class MyFavoriteSong { /* Runs the overall program to print the song on the console. */ sing(); // Separate the two verses with a blank line sing(); // Displays the first verse of the theme song. public static void sing() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); Do not describe the syntax/statements in detail. Instead, provide a short English description of the observed behavior when the method is run. Example: // This method prints the lyrics to the first verse // of my favorite TV theme song. // Blank lines separate the parts of the verse. public static void verse1() { System.out.println("Now this is the story all about how"); System.out.println("My life got flipped turned upside-down"); System.out.println("And I'd like to take a minute,"); System.out.println("just sit right there"); System.out.println("I'll tell you how I became the prince"); System.out.println("of a town called Bel-Air"); 27 28

Exercise Exercise Write a program that prints the following output to the console. Use static methods as appropriate. I do not like my email spam, I do not like them, Sam I am! I do not like them on my screen, I do not like them to be seen. I do not like my email spam, I do not like them, Sam I am! Write a program that prints the following output to the console. Use static methods as appropriate. Lollipop, lollipop Oh, lolli lolli lolli Lollipop, lollipop Oh, lolli lolli lolli Call my baby lollipop 29 BBBBB B B BBBBB B B BBBBB AAAA AAAAAA N N NNN N N NNN N N AAAA AAAAAA N N NNN N N NNN N N AAAA AAAAAA Write a program to print the block letters spelling "banana". Use static methods to capture structure and and eliminate redundancy. 30