Computer Programming Methodology Input and While Loop

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
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.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Computer Programming Lab(4).
Computer Programming Lab(5).
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Java for Beginners University Greenwich Computing At School DASCO
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
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.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
CSC111 Quick Revision.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
using System; namespace Demo01 { class Program
Computer Programming Methodology File Input
Repetition.
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
The for-loop and Nested loops
An Introduction to Java – Part I, language basics
Java Variables, Types, and Math Getting Started
Java so far Week 7.
Introduction to Java Brief history of Java Sample Java Program
Building Java Programs
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Random Numbers while loop
CSS161: Fundamentals of Computing
Computer Science Club 1st November 2019.
Presentation transcript:

Computer Programming Methodology Input and While Loop COS 130 Computer Programming Methodology Input and While Loop

Demo Input Program

New Issues How to read data into the program How many times to loop In this case from the key board How many times to loop Don’t know in advance how many numbers the user will type in How to go between characters and int

How to read data – Scanner one of many possibilities import java.util.Scanner; public class Avg { public static void main(String args[]) { Scanner kb = new Scanner(System.in); int num = 0; double sum = 0; int cnt = 0; // more stuff . . . while(num != -1) { System.out.print("enter number: "); num = kb.nextInt(); } This finds the library This creates a Scanner object This specifies the keyboard or redirected file Reads and converts characters to int

Open Loop - while (not counted) while(num != -1) { // do stuff }

Open Loop - while (not counted) boolean done = false; while(!done) { // do stuff }

while can count too int cnt = 0; while(cnt < 10) { // do stuff cnt = cnt + 1; } for(int cnt = 0; cnt < 10; cnt = cnt + 1) { // do stuff }

When to use which loop for – use when the number of repeations is known before the loop starts while – use when the number of repeations is not known before the loop starts

Another way to convert string to int All Java primitives (int, double, etc.) have a corresponding “wrapper” class which contain “helper” methods. int -> Integer double -> Double long -> Long boolean -> Boolean

Another way to convert string to int int num = Integer.parseInt(“123”); double num = Double.parseDouble(“1.23”);

Example of use String word = kb.next(); if("quit".equals(word)) { // code to quit } else { num = Integer.parseInt(word); }

Class vs. Object Notice the difference between: String name = “John Doe”; int len = name.length(); And int num = Integer.parseInt(“123”);