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

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

Building Java Programs

Building Java Programs

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

Building Java Programs

Static Methods & Decomposition

CSc 110, Autumn Lecture 2: Functions

Entering the world of Javatar

Lecture 1: Basic Java Syntax

CSE 142, Summer 2015

CSE 142, Summer 2014

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

Introduction to Computer Programming

Procedural decomposition using static methods

Building Java Programs

Building Java Programs Chapter 1

Building Java Programs

CSE 201 Java Programming I. Smart Coding School website:

Building Java Programs Chapter 1

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

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

Topic 6 loops, figures, constants

Building Java Programs

Building Java Programs

Building Java Programs. Introduction to Java Programming

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

Topic 4 Expressions and variables

CS 112 Introduction to Programming

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

Introduction to Computer Programming concepts using StarLogo TNG

Recap: Assignment as an Operator CS 112 Introduction to Programming

CS 112 Introduction to Programming

Topic 27 classes and objects, state and behavior

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

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

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

Topic 9 More Graphics. Based on slides bu Marty Stepp and Stuart Reges from

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Introduction Jan. 22, Ch 1

COMPUTATIONAL THINKING

Critters. Critter #2 Attack.ROAR Attack.POUNCE Attack.SCRATCH. Critter #1

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

Graded Project. HTML Coding

2.9 Modularization. Department of CSE,Coimbatore

Lab 1: Introduction to Java

QUIZ. What are 3 differences between C and C++ const variables?

LN #6 & 7 (2 Hrs) Modularization 1 & 2 CTPS Department of CSE,Coimbatore

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Building Java Programs

CS 251 Intermediate Programming Coding Standards

QUIZ. Source:

CS 152 Computer Programming Fundamentals Coding Standards

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

Welcome to Python! If you re the type of person who wants to know

Building Java Programs

Topic 8 graphics. -mgrimes, Graphics problem report 134

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

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca


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

Algorithms: The recipe for computation

Chapter Two: Classes and Objects

Advanced if/else & Cumulative Sum

Boolean evaluation and if statements. Making decisions in programs

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

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

Functions: Decomposition And Code Reuse

Building Java Programs

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

CSc 110, Spring 2017 Lecture 3: Expressions, Variables and Loops. Adapted from slides by Marty Stepp and Stuart Reges

CS 351 Design of Large Programs Coding Standards

Lecture 2: Operations and Data Types

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

Object-Oriented Programming in Processing

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

Variables One More (but not the last) Time with feeling

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Ch. 10: Name Control

CS 106A, Lecture 5 Booleans and Control Flow

Functions: Decomposition And Code Reuse

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

CSE 142/143 Unofficial Style Guide

Topic 5 for loops and nested loops

Chapter 2 Author Notes

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether, Ph.D.

Building Java Programs. Introduction to Programming and Simple Java Programs

Designing Forms in Access

CS 106A, Lecture 3 Problem-solving with Karel

University of Washington, CSE 190 M Homework Assignment 9: Remember the Cow (To-Do List)

1 Getting used to Python

COMP 102: Computers and Computing

Mobile App:IT. Methods & Classes

Homework Assignment 2: Java Console and Graphics

CS111: PROGRAMMING LANGUAGE II

Transcription:

Topic 3 static Methods and Structured Programming "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to make it fast." -Joshua Bloch clicker question What is the name of the method that is called when a Java program starts? A. main B. static C. void D. println E. class Based on slides by Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/ 2 Comments comment: A note written in source code by the programmer to describe or clarify the code. Comments are not executed when your program runs. Syntax: comment text, on one line or, comment text; may span multiple lines Examples: Using comments Where to place comments: at the top of each file (a "comment header") at the start of every method (seen later) to explain complex pieces of code Comments are useful for: Understanding larger, more complex programs. Multiple programmers working together, who 3 must understand each other's code. 4

Comments example Program Hygiene - a.ka. Style 5 Provide a structure to the program Eliminate redundant code Use spaces judiciously and consistently Indent properly Follow the naming conventions Use comments to describe code behavior Follow a brace style Good software follows a style guide See links on assignment page 6 Google C++ Style Guide Google C++ Style Guide http://code.google.com/p/google-styleguide/ 7 8

Why Worry About Program Hygiene? Computer Scientists and Software developers spend as much time maintaining code as they do creating new code You should spend time on thinking and coding. You should NOT be wasting time looking for that missing closing brace. 9 Algorithms algorithm: A list of steps for solving a problem. Example algorithm: "Bake sugar cookies" Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven temperature. Set the timer. Place the cookies into the oven. Allow the cookies to bake. Mix ingredients for frosting.... 10 Problems with algorithms lack of structure: Many small steps; tough to remember. redundancy: Consider making a double batch... Mix the dry ingredients. Cream the butter and sugar. Beat in the eggs. Stir in the dry ingredients. Set the oven temperature. Set the timer. Place the first batch of cookies into the oven. Allow the cookies to bake. Set the oven temperature. Set the timer. Place the second batch of cookies into the oven. Allow the cookies to bake. Mix ingredients for frosting.... 11 Structured algorithms structured algorithm: Split solution into coherent tasks. 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 the cookies. Set the oven 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.... 12

Removing redundancy A well-structured algorithm can describe repeated tasks with less redundancy. A program with redundancy 1 Make the cookie batter. Mix the dry ingredients.... 2a Bake the cookies (first batch). Set the oven temperature. Set the timer.... 2b Bake the cookies (second batch). 3 Decorate the cookies.... 13 14 Static methods static method: A named group of statements. denotes the structure of a program eliminates redundancy by code reuse Building complex systems is hard Some of the most complex systems are software systems procedural decomposition: dividing a problem into methods a way to manage complexity Writing a static method is like adding a new command to Java. 15 http://spectrum.ieee.org/computing/software/why-software-fails 16

Using static methods 1. Design the algorithm. Look at the structure, and which commands are repeated. Decide what are the important overall tasks. Good programmers do this BEFORE writing any code Design of an algorithm 2. Declare (write down) the methods. Arrange statements into groups and give each group a name. 3. Call (run) the methods. The program's method executes the other methods to perform the overall task. 17 18 Gives your method a name so it can be executed Syntax: <statement> <statement>... <statement> Example: Declaring a method <name> Syntax: <name> Calling a method Executes the method's code You can call the same method many times if you like. Example: Output: 19 20

Program with static method Final cookie program Output: 21 22 Summary: Why methods? Makes code easier to read by capturing the structure of the program should be a good summary of the program Summary: Why methods? Eliminate redundancy Note: necessarily mean worse code!!! 23 24

Methods calling methods Control flow When a method is called, the program's execution... "jumps" into that method, executing its statements, then "jumps" back to the point where the method was called. Output: 25 26 How many lines of output with visible characaters does the following program produce? Drawing complex figures with static methods reading: 1.5 (Ch. 1 Case Study: ) A. 3 B. 4 C. 8 D. 12 E. 20

Static methods question Write a program to print these figures. Development strategy 29 30 Program version 1 Development strategy 2 31 32

Output structure Program version 2 33 34 Program version 2, cont'd. Development strategy 3 35 36

Output redundancy Program version 3 37 38 Program version 3, cont'd. 39