Chapter 4 Practice cont.. Practice with nested loops 1.What will be the output of the following program segment: 1.for (int i = 1; i <5; i++) { for (int.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Building Java Programs
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
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.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CS1101: Programming Methodology Aaron Tan.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Program 6 Any questions?. System.in Does the opposite of System.out.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Chapter 2: Java Fundamentals
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Exercise 1 Introduction to C# CIS Code the C# program to prompt the user to key in 12 integer values from the keyboard. If a value containing.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Exercise 3 Example> Write a C function that implements /* input : integer value n output : */ int f ( int n ) { int i, sum=0; for (i=1;i
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
int [] scores = new int [10];
Escape sequence Certain characters, preceded by a backslash ( \ ), are known as escape sequences They are used to display certain characters, or as display.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
For Monday Read WebCT quiz 18.
Chapter 5 Repetition.
Python I/O.
Introduction to Classes and Methods
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 6 Decision Making and Looping
Reading and Writing Text Files
Building Java Programs
CS110D Programming Language I
Suppose I want to add all the even integers from 1 to 100 (inclusive)
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Building Java Programs
The keyboard is the standard input device.
Building Java Programs
Building Java Programs
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs
Unit 1: Intro Lesson 4: Output.
Building Java Programs
Chapter 4: Loops and Iteration
Presentation transcript:

Chapter 4 Practice cont.

Practice with nested loops 1.What will be the output of the following program segment: 1.for (int i = 1; i <5; i++) { for (int j = 1; j < 5; j++) System.out.print (2*i+j + “ “); System.out.println(); } 2.for (int i = 1; i <5; i++) { for (int j = 0; j <= i; j++) System.out.print (2*i+j + “ “); System.out.println(); }

Practice with File input and output 1.Write code that does the following: opens a file named MyName.txt, writes your name to the file, then closes the file. 2.Write code that does the following: opens a file named MyName.txt, reads the first line from the file and displays it, then closes the file. 3.A text file contains a sequence of integers, each in a separate line. Write code that reads this file and displays the sum of all numbers in the file.

Practice with Random class 1.Write code to display a random number in the interval [0, 1). 2.Write code to display a random number in the interval [0, 100). 3.Write code to display a random number in the interval [13, 19] (teenager) 4.Write code to display a random letter [A..Z] 5.Write code to display a string of 5 random letters.

Sample Problems 1.The sum S(n) is defined as: S(n) = 1 + ½ + 1/3 + … + 1/n. Write a program that reads a number M from keyboard, then displays the smallest value of n so that S(n) >= M. 2.Write a program to calculate the square root of a non-negative number n, using the while loop. Compare the result with the Math.sqrt() method. See instructions in class.