Introduction to Programming Java Lab 7: Loops 22 February 2013 1 JavaLab7 lecture slides.ppt Ping Brennan

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming Overview of Week 2 25 January Ping Brennan
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
15 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
Introduction to Programming
25 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.
Case study 1: Calculate the approximation of Pi
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
CS150 Introduction to Computer Science 1
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Textbook Problem Java – An Introduction to Problem Solving & Programming, Walter Savitch, pp.219, Problem 08.
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Do-while loop Syntax do statement while (loop repetition condition)
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Intro to Java Day 3 / Loops 1. DAY 3 Java Language Specifications Conditional Loops while do for References: JavaNotes7 pdf bookJavaNotes7 pdf book.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Introduction to Java Primitive Types Operators Basic input and output.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Textbook Problem Java – An Introduction to Problem Solving & Programming, Walter Savitch, pp.217, Problem 04.
Introduction to Programming Python Lab 5: Strings and Output 05 February PythonLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Introduction to programming in java Lecture 21 Arrays – Part 1.
Unit-1 Introduction to Java
for Repetition Structures
Introduction to Programming
CS150 Introduction to Computer Science 1
Introduction to Programming
Lecture Notes – Week 3 Lecture-2
Counting Loops.
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
CSCE 206 Lab Structured Programming in C
Introduction to Programming
Introduction to Programming
CS150 Introduction to Computer Science 1
Introduction To Programming Information Technology , 1’st Semester
CS150 Introduction to Computer Science 1
Introduction to Programming
Introduction to Programming
Introduction to Programming
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
CSCE 206 Lab Structured Programming in C
Presentation transcript:

Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan

Java Project Project Name: JavaLab7 2 Rectangle NumberProperties

Class Rectangle (part 1) Reads a non-negative integer, numberRows, from key board. Prints out numberRows rows of the form Objective –Understand the use of a single for loop to solve problems. Method (i) 3 Input int numberRows=0; Example of input numberRows= 2; Loop One for loop Print out two rows of three asterisks. Output ***

Syntax of the for Statement for (int i = 1; i <= numberRows; i++) { /* more Java statement(s) */ } /* i = 1: initialisation, executed once on entering the loop. i <= numberRows: condition to be checked before each iteration. i++: update is executed after each iteration. */ 4

for (int i=1; i <= numberRows; i++) { System.out.println("***"); } for (int i=1; i <= numberRows; i++) { System.out.println("***"); } True False println ("***"); i <= numberRows ? i <= numberRows ? i=1; End i++; Flowchart of a for Loop Start

Anatomy of Class Rectangle (part 1) import java.util.Scanner; public class Rectangle { public static void main(String[] args) { /* To Do - write code to read numberRows from the key board which is a non-negative number. */ for (int i = 1; i <= numberRows; i++) { System.out.println('***'); } } // end of main } // end of class Rectangle 6

Class Rectangle (part 2) Reads two non-negative integers numberRows and numberColumns from key board. Prints out numberRows rows of asterisks, such that each row contains numberColumns asterisks. Objective - understand the use of nested for loops. Method (ii) 7 Input int numberRows = 0, numberColumns = 0; Example of input numberRows = 2; numberColumns = 4; Loop Nested for loops Print out a matrix (2 rows by 4 columns) of asterisks. Output ****

Anatomy of Class Rectangle (part 2) import java.util.Scanner; public class Rectangle { public static void main(String[] args) { /* Write code to read two non-negative integers, numberRows and numberColumns, from key board */ for (int i = 1; i <= ? ; i++) { for (int j = 1; j <= ?; j++) { // print out an asterisk } System.out.println(); } } // end of main } // end of class Rectangle 8 Which one of the two variables to use?

Class NumberProperties Reads a set of strictly positive integers from keyboard, one at a time. Use a while loop to read these integers. The end of input is indicated by the integer 0. Program prints out: i.the average value (use a variable of type double) ii.the smallest of the values iii.the largest of the values iv.the range, that is one more than the difference between the smallest and the largest values. Add a comment with each number which is printed out, for example: The average value is:... 9

Class NumberProperties (2) Objectives –Set a proper sentinel for the while loop to solve problems. –Apply the Math class methods, Math.min and Math.max Method (in pseudo-code) 10 int count = 0, currentInt= -1, maxValue = 0, minValue = 0; double total = 0.0; while ( currentInt != 0 ) // setting a sentinel to end the loop { currentInt = in.nextInt() ; // in is defined as Scanner type if ( currentInt != 0 ) { /* Write additional code to: (i) add one to count; (ii) calculate the total; (iii) update minValue and maxValue. */ } /* write additional code to find range, average and print results */

Class NumberProperties (3) 11 Input A set of positive integer numbers. For example: Computations 1.double average = ( ) / 4 2. min = min(2, 5, 3, 9) 3. max = max(2, 5, 3, 9) 4. range = (max - min) + 1 Output

Syntax for the while Loop while (boolean condition) { /* Write additional Java statement(s) */ } /* The statement(s) are carried out while the condition is true. */ 12

while (currentInt != 0) { currentInt = in.nextInt(); /* Write statements to solve the problem */ } while (currentInt != 0) { currentInt = in.nextInt(); /* Write statements to solve the problem */ } True False currentInt = in.nextInt(); currentInt != 0 ? currentInt != 0 ? End Write statements to solve problem Flowchart of a while Loop Start

Anatomy of Class NumberProperties public class NumberProperties { public static void main(String[] args) { int count = 0, currentInt = -1, minValue = 0, maxValue =0; double total = 0.0; /* write code to declare Scanner object for keyboard input */ while (currentInt != 0) { currentInt = in.nextInt(); if (currentInt != 0) { /* Code to: add one to count; calculate the total; update minValue and maxValue. */ } } /* Code to find range, average and print results */ } // end of main } // end of class NumberProperties 14