JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Similar documents
Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Reading Input from Text File

Loops. CSE 114, Computer Science 1 Stony Brook University

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Arrays. Eng. Mohammed Abdualal

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Midterm Examination (MTA)

Eng. Mohammed Alokshiya

Eng. Mohammed S. Abdualal

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Eng. Mohammed S. Abdualal

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Java Coding 3. Over & over again!

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

2.8. Decision Making: Equality and Relational Operators

Object Oriented Programming. Java-Lecture 1

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Programming with Java

Introduction to the Java Basics: Control Flow Statements

Bjarne Stroustrup. creator of C++

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS111: PROGRAMMING LANGUAGE II

ITERATION WEEK 4: EXMAPLES IN CLASS

Building Java Programs

Date: Dr. Essam Halim

Control Statements: Part 1

Methods. Eng. Mohammed Abdualal

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Loops. Repeat after me

Building Java Programs

Computer Programming: C++

CS1150 Principles of Computer Science Loops (Part II)

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

St. Edmund Preparatory High School Brooklyn, NY

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Building Java Programs

Chapter 4 Loops. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

Computer Programming, I. Laboratory Manual. Experiment #5. Strings & Text Files Input

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Loops / Repetition Statements

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Control Structures: if and while A C S L E C T U R E 4

Object Oriented Programming. Java-Lecture 6 - Arrays

Building Java Programs

CS141 Programming Assignment #6

Array. Array Declaration:

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

CMPT 125: Lecture 4 Conditionals and Loops

Computer Programming: C++

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Chapter 2 ELEMENTARY PROGRAMMING

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

A token is a sequence of characters not including any whitespace.

Introduction to Java Applications; Input/Output and Operators

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Full file at

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

Building Java Programs

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

Chapter 3: Classes and Objects. CSC2010 Lab 9. Java Programming. Copyright 2000 W. W. Norton & Company. All rights reserved. FROM THE BEGINNING

Topic 11 Scanner object, conditional execution

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

H212 Introduction to Software Systems Honors

Combined Assignment Operators. Flow of Control. Increment Decrement Operators. Operators Precedence (Highest to Lowest) Slide Set 05: Java Loops

3chapter C ONTROL S TATEMENTS. Objectives

JAVA PROGRAMMING LAB. ABSTRACT EXTRA LAB, In this Lab you will learn working with recursion, JRX, Java documentations

Topic 11 Scanner object, conditional execution

Lab Exercise 1. Objectives: Part 1. Introduction

JAVA Ch. 4. Variables and Constants Lawrenceville Press

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

CSE 1223: Exam II Autumn 2016

Question: Total Points: Score:

CS Computers & Programming I Review_01 Dr. H. Assadipour

Introduction to Java Applications

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Chapter 5 Lab Methods

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CSC 1051 Data Structures and Algorithms I

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Building Java Programs

H212 Introduction to Software Systems Honors

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Lecture 1 Java SE Programming

Transcription:

Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop JAVA PROGRAMMING LAB Eng. Mustafa J. Dalloul Computer Programming Lab 18 October 2016

Suppose that you need to display a string (e.g., Welcome to Java) a hundred times. It would be tedious to have to write the following statement a hundred times: System. out. println("welcome to java"); System. out. println("welcome to java"); 100 times.. { System. out. println("welcome to java"); So, how do you solve this problem? while A while loop executes statements repeatedly while the condition is true. The syntax for the while loop is: And the flow chart is: The flow chart in figure (a) show how the while loop work. while we have a true continuation condition, the statements in while body will be executed. So we can solve the introduced problem by using while loop as illustrated in figure (b). 1

Example: Find the sum for listed input numbers Code: Scanner s = new Scanner(System.in); double sum = 0; System.out.print("Enter a list of numbers (ends if 0):"); double n = s.nextdouble(); while (n!= 0) { sum += n; n = s.nextdouble(); System.out.println("The sum of numbers is: " + sum); Example: (The 3n + 1 Problem) consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n = 1. For example, the following sequence of numbers will be generated for n = 22: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Write a program to read a positive integer n and generate a sequence of numbers until 1. Code: Scanner input = new Scanner(System.in); int n = input.nextint(); while (n > 1) { System.out.print(n + " "); if (n % 2 == 0) n /= 2; else n = n * 3 + 1; System.out.println(n); 2

Example: Write a program to read a file contains unknown number of lines, each one contains exactly two integers A and B. For each line, print the sum of both numbers, in the following format: Line #N: S Where N is the line number, and S is the sum of two numbers in that line. Sample input: Code: import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class Main { public static void main(string[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("input.txt")); int a, b, line = 1; while (input.hasnext()) { a = input.nextint(); b = input.nextint(); System.out.println("Line #" + line + ": " + (a + b)); // OR // System.out.printf("Line #%d: %d\n", line, a + b); line++; T1: Repeat Addition Quiz Write a program that prompts the user to enter an answer for a question on addition of two single digits. The program will exit if and only if the user enter a true answer. 3

Code: int number1 = (int)(math.random() * 10); int number2 = (int)(math.random() * 10); int result = number1+number2; Scanner s = new Scanner(System.in); System.out.print("What is "+number1+"+"+number2+"?"); int answer = s.nextint(); while (answer!= result) { System.out.print("Wrong answer! Try again. "); System.out.print("What is "+number1+"+"+number2+"?"); answer = s.nextint(); System.out.println("You got it!"); FOR MORE SEE LISTING 5.4 FROM THE BOOK AT PAGE (164) THIS EXAMPLE IS VERY VERY IMPUTRANT, PLEASE READ AND UNDERSTAND IT 4

do-while A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. The do-while loop is a variation of the while loop. Its syntax is: And the flow chart is: Example: Find the sum for listed input numbers Code: Scanner s = new Scanner(System.in); double sum = 0; System.out.print("Enter a list of numbers (ends if 0):"); double n = s.nextdouble(); do { sum += n; n = s.nextdouble(); while (n!= 0); System.out.println("The sum of numbers is: " + sum); When I Use do-while? Use a do-while loop if you have statements inside the loop that must be executed at least once, as in the case of the do-while loop in the preceding Test Do While program. These statements must appear before the loop as well as inside it if you use a while loop. 5

for A for loop has a concise syntax for writing loops. It can be used to simplify the preceding loop as: Instead of The syntax of a for loop is: And the flow chart is: A for loop generally uses a variable to control how many times the loop body is executed and when the loop terminates. This variable is referred to as a control variable. The initial action often initializes a control variable, the action-after-eachiteration usually increments or decrements the control variable, and the loopcontinuation-condition control variable tests whether the control variable has reached a termination value. For example, the following for loop prints Welcome to Java! a hundred times:s 6

int i; for (i = 0; i < 100; i++) { System.out.println("Welcome to Java!"); Notes: 1- The control variable must be declared inside the control structure of the loop or before the loop. a. If you will use the control variable only inside a for loop structure, you need to declare it in the for loop structure. b. If you will use the control variable outside the for loop structure, you need to declare it outside the for loop. 2- The initial-action in a for loop can be a list of zero or more comma-separated variable declaration statements or assignment expressions. 3- The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. 4- If the loop-continuation-condition in a for loop is omitted, it is implicitly true. For example (a),(b) and (c): Example: (Count Uppercase Letters) write a program that counts how many uppercase letters exist in a given string. Code: Scanner input = new Scanner(System.in); String line = input.nextline(); int count = 0; for (int i = 0; i < line.length(); i++) { if (Character.isUpperCase(line.charAt(i))) { count++; System.out.println(count); 7

1- An integer N greater than 1 is prime if its only positive divisor is 1 or itself. In other words, (N % X!= 0, for all integers X where 1 < X < N). Write a program that reads an integer and check if it is a prime number or not. 2- Write a program that prompts the user to enter a string and displays the characters at odd positions. 3- Write a program that asks the user to type 10 integers and writes the smallest value. 8

Netbeans Tips & Tricks for + TAB Use this shortcut for writing for loop header quickly. Write: for, then press TAB button and see the result. 9