A simple example of program design

Slides:



Advertisements
Similar presentations
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Advertisements

10-Jun-15 Fibonacci Numbers A simple example of program design.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Fibonacci Numbers A simple example of program design.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Building Java Programs
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Introduction to Computer Programming Counting Loops.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Lesson #6 Modular Programming and Functions.
Some Eclipse shortcuts
Lesson #6 Modular Programming and Functions.
Introduction to Computer Science / Procedural – 67130
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Repetition-Counter control Loop
Lesson #6 Modular Programming and Functions.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Class Structure 16-Nov-18.
Class Structure 28-Nov-18.
Object Oriented Programming (OOP) LAB # 5
Class Structure 7-Dec-18.
Building Java Programs
Class Structure 2-Jan-19.
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
Class Structure 25-Feb-19.
Lesson #6 Modular Programming and Functions.
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Building Java Programs
Building Java Programs
Building Java Programs
Review of Previous Lesson
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

A simple example of program design Fibonacci Numbers A simple example of program design 17-Nov-18

Purpose of this presentation The whole point of this presentation is to give you some idea of how to put together the components we have so far (declarations, assignments, if and while statements) into a working program The example we use is writing a program to compute and display a Fibonacci sequence

Fibonacci sequences A Fibonacci sequence is an infinite list of integers The first two numbers are given Usually (but not necessarily) these are 1 and 1 Each subsequent number is the sum of the two preceding numbers: 1 1 2 3 5 8 13 21 34 55 89 144 ... Let’s write a program to compute these

Starting the Fibonacci sequence We need to supply the first two integers int first = 1; int second = 1; We need to print these out: System.out.print(first + " "); System.out.print(second + " "); We need to compute and print the next number: int next = first + second; System.out.print(next + " ");

Taking the next step We need to compute and print the next number: int next = first + second; System.out.print(next + " "); Now what? We don't want to make up a lot more names If we use a loop, we must reuse names

Preparing for another step This computation gave us our third number: int next = first + second; System.out.print(next + " "); The sequence so far is: first second next To get another number, we need second + next Variable first is no longer useful for anything Let’s move values around so that first + second does the job we need

Preparing to make many steps We need to make these moves: first second next 1 1 2 1 2 3 2 3 5 3 5 8 first second next We can do it like this: first = second; second = next; We can put these statements in a loop and do them as many times as we please

The program so far int first = 1; int second = 1; System.out.print(first + " "); System.out.print(second + " "); while ( ? ? ? ) { // when do we stop? int next = first + second; System.out.print(next + " "); first = second; second = next; }

Deciding when to stop Suppose we stop when we get to a number that’s 1000 or bigger So we continue as long as the number is less than 1000: while (next < 1000) { ... } Question: is the final number printed greater than 1000 or less than 1000? while (next < 1000) { int next = first + second; System.out.print(next + " "); first = second; second = next; }

One other minor detail We have been printing the numbers all on one line We’ll get to 1000 quickly enough, so we won’t have a terribly long line For neatness’ sake, we really ought to end the line (rather than hoping Java does it for us): System.out.println( );

The program so far int first = 1; int second = 1; System.out.print(first + " "); System.out.print(second + " "); while (next < 1000) { int next = first + second; System.out.print(next + " "); first = second; second = next; } System.out.println( ); // oops--a bug

Fixing the bug The first time we see the variable next, it’s in the test of the while loop: while (next < 1000) { next hasn’t been given a value yet next hasn’t even been declared! Solution: declare next up with the other variables, and give it some reasonable initial value

The (fixed) program so far int first = 1; int second = 1; int next = 2; System.out.print(first + " "); System.out.print(second + " "); while (next < 1000) { next = first + second; // "int" was removed System.out.print(next + " "); first = second; second = next; } System.out.println( );

Finishing up We have the commands we need, but we do not have a complete application We need to put the commands into a method We need to put the method into a class The next slide shows the extra stuff we need, but doesn’t explain it

The “box” our program goes in public class Fibonacci { public static void main(String args[ ]) { (code goes here) } }

The complete final program public class Fibonacci { public static void main(String args[ ]) { int first = 1; int second = 1; int next = 2; System.out.print(first + " "); System.out.print(second + " "); while (next < 1000) { next = first + second; System.out.print(next + " "); first = second; second = next; } System.out.println( ); } } Our code

The End “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” --Martin Golding