Building Java Programs

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Advertisements

Building Java Programs Chapter 1 Introduction to Java Programming.
Java Programs + Algorithms
Basic Java programs with println statements. 2 Compile/run a program 1.Write it –code or source code: the set of instructions in a program 2.Compile it.
1 Building Java Programs Introduction to Java Programming Dept. of Computer Science - SSBN Vishnuvardhan.M.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
1 Procedural decomposition using static methods suggested reading:1.4.
1 Procedural decomposition using static methods. 2 Algorithms Recall: An algorithm is a list of steps for solving a problem. What is the algorithm to.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
Building Java Programs Chapter 1 Introduction to Java Programming.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
Building Java Programs
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods, Avoiding Redundancy reading: self-check:
Copyright 2008 by Pearson Education Building Java Programs Chapter 1: Introduction to Java Programming.
1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
Static methods. 2 Algorithms algorithm: a list of steps for solving a problem Example algorithm: "Bake sugar cookies" –Mix the dry ingredients. –Cream.
BUILDING JAVA PROGRAMS CHAPTER 1 INTRODUCTION TO JAVA PROGRAMMING.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 CSE 142 Lecture Notes Introduction These lecture notes are copyright (C) Marty Stepp May not be rehosted, copied, sold, or modified without Marty.
Copyright 2010 by Pearson Education Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
Warm Up As you enter get out a half sheet of loose paper. Write the HelloWorld program on it. Be prepared to correct your code.
Building Java Programs Chapter 1 Introduction to Java Programming.
Drawing complex figures with static methods. 2 Static methods question Write a program to print these figures using methods ______ / \ \ / \______/ \
1 WELCOME TO CSE 142! host: benson limketkai University of Washington, Summer 2007.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Lecture 3: Java Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 WELCOME TO CSE 142! host: benson limketkai University of Washington, Spring 2008.
CS 112 Introduction to Programming Java Primitive Data Types; Arithmetic Expressions Yang (Richard) Yang Computer Science Department Yale University 208A.
Introduction to Java.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
CSc 110, Autumn 2016 Lecture 2: Functions. Review What is the output of the following print statements? print("this class\tis' the \"best\"") Write a.
Lecture 1: Basic Java Syntax
Building Java Programs
Static Methods and Method Calls
CSc 110, Autumn 2017 Lecture 2: Functions.
Building Java Programs
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
CSc 110, Autumn 2017 Lecture 3: Functions.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Topic 3 static Methods and Structured Programming
Building Java Programs
Building Java Programs
Lecture 1: Basic Java Syntax
Building Java Programs
CSc 110, Spring 2018 Lecture 3: Functions.
Building Static Methods
CSc 110, Spring 2018 Lecture 2: Functions.
Lecture 2: Static Methods Expressions reading: 1.4 – 2.1
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 1
Building Java Programs
Chapter 1 Lecture 1-2: Static Methods reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
host: benson limketkai University of Washington, Spring 2007
Building Java Programs
Presentation transcript:

Building Java Programs Chapter 1 Lecture 1-2: Static Methods reading: 1.4 - 1.5

Algorithms algorithm: A list of steps for solving a problem. 2/21/2019 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 for 10 minutes. Place the cookies into the oven. Allow the cookies to bake. Mix ingredients for frosting. ...

Static methods static method: A named group of statements. denotes the structure of a program eliminates redundancy by code reuse procedural decomposition: dividing a problem into methods Writing a static method is like adding a new command to Java. class method A statement method B method C

Gives your method a name so it can be executed 2/21/2019 Declaring a method Gives your method a name so it can be executed Syntax: public static void name() { statement; statement; ... statement; } Example: public static void printWarning() { System.out.println("This product causes cancer"); System.out.println("in lab rats and humans."); }

Executes the method's code 2/21/2019 Calling a method Executes the method's code Syntax: name(); You can call the same method many times if you like. Example: printWarning(); Output: This product causes cancer in lab rats and humans.

Summary: Why methods? Makes code easier to read by capturing the structure of the program main should be a good summary of the program public static void main(String[] args) { } Note: Longer code doesn’t necessarily mean worse code public static void main(String[] args) { } public static ... (...) { Longer code does not mean worse code.

Summary: Why methods? Eliminate redundancy public static void main(String[] args) { } public static void main(String[] args) { } public static ... (...) {

When to use methods Place statements into a static method if: The statements are related structurally, and/or The statements are repeated. You should not create static methods for: An individual println statement that appears once in a program. Only blank lines. Unrelated or weakly related statements. (Consider splitting them into two smaller methods.)

Static methods question Write a program to print these figures using methods. ______ / \ / \ \ / \______/ +--------+ | STOP |

Output structure The structure of the output: initial "egg" figure ______ / \ / \ \ / \______/ +--------+ | STOP | 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: egg teaCup stopSign hat

Output redundancy The redundancy in the output: ______ / \ / \ \ / \______/ +--------+ | STOP | The redundancy in the output: egg top: reused on stop sign, hat egg bottom: reused on teacup, stop sign divider line: used on teacup, hat This redundancy can be fixed by methods: eggTop eggBottom line

A word about style Structure your code properly Eliminate redundant code Use spaces judiciously and consistently Indent properly Follow the naming conventions Use comments to describe behavior of your program and each method

Why style? Programmers build on top of other’s code all the time. You shouldn’t waste time deciphering what a method does. Often times, that other person is you You should spend time on thinking or coding. You should NOT be wasting time looking for that missing closing brace.

Why style? Taylor Swift has a song about it