Exercise (Revisited)

Similar documents
Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Example: Monte Carlo Simulation 1

Nested Loops. A loop can be nested inside another loop.

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Arithmetic Compound Assignment Operators

switch-case Statements

Arithmetic Compound Assignment Operators

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

++x vs. x++ We will use these notations very often.

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

Repetition with for loops

IEEE Floating-Point Representation 1

Fundamentals of Programming Data Types & Methods

Exercise. Write a program which allows the user to enter the math grades one by one (-1 to exit), and outputs a histogram.

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Midterm Examination (MTA)

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

Example. Write a program which generates 2 random integers and asks the user to answer the math expression.

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Zheng-Liang Lu Java Programming 45 / 79

Values in 2 s Complement

Some Sample AP Computer Science A Questions - Solutions

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

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

CS1150 Principles of Computer Science Loops (Part II)

Recap: Assignment as an Operator CS 112 Introduction to Programming

Introduction to the Java Basics: Control Flow Statements

Topic 6 Nested for Loops

1. Find the output of following java program. class MainClass { public static void main (String arg[])

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS 112 Introduction to Programming

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression.

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order).

CHAPTER 7 OBJECTS AND CLASSES

Introduction to Java & Fundamental Data Types

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Primitive Data, Variables, and Expressions; Simple Conditional Execution

CEN 414 Java Programming

Fundamentals of Programming Session 8

Program Control Flow

Program Control Flow

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

Introduction to Programming Using Java (98-388)

Object Oriented Programming. Java-Lecture 1

Building Java Programs Chapter 2

Example: Fibonacci Numbers

CHAPTER 7 OBJECTS AND CLASSES

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

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

The return Statement

A Foundation for Programming

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

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Top-Down Program Development

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

Introduction to Computer Programming

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Chapter 4: Control structures. Repetition

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Recursion 1. Recursion is the process of defining something in terms of itself.

CS141 Programming Assignment #5

Lesson 36: for() Loops (W11D1)

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Object Oriented Software Design

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

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Chapter 4: Control structures

Jump Statements. ˆ The break statement exits a for or while loop completely.

Getting started with Java

Java Classes: Math, Integer A C S L E C T U R E 8

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Functions. Lecture 6 COP 3014 Spring February 11, 2018

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

Binary Search. Roland Backhouse February 5th, 2001

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

AP Computer Science Unit 1. Programs

Repetition CSC 121 Fall 2014 Howard Rosenthal

Elementary Programming

CSEN 202 Introduction to Computer Programming

Transcription:

Exercise (Revisited) Redo the cashier problem by using an infinite loop with a break statement. 1... 2 while (true) { 3 System.out.println("Enter price?"); 4 price = input.nextint(); 5 if (price <= 0) break; 6 total += price; 7 } 8 System.out.println("Total = " + total); 9... Zheng-Liang Lu Java Programming 138 / 154

Another Example: Compounding Write a program which determines the holding years for an investment doubling its value. Let balance be the current amount, goal be the goal of this investment, and r be the annual interest rate. Then this investment should take at least n years so that the balance of the investment can double its value. Recall that the compounding formula is given by balance = balance (1 + r/100). Zheng-Liang Lu Java Programming 139 / 154

1... 2 int r = 18; // 18% 3 int balance = 100; 4 int goal = 200; 5 6 int years = 0; 7 while (balance <= goal) { 8 balance = (1 + r / 100.0); 9 years++; 10 } 11 12 System.out.println("Balance = " + balance); 13 System.out.println("Years = " + years); 14... Zheng-Liang Lu Java Programming 140 / 154

1... 2 int years = 0; // should be declared here; scope issue 3 for (; balance <= goal; years++) { 4 balance = (1 + r / 100.0); 5 } 6... 1... 2 int years = 1; // check this initial value 3 for (; true; years++) { 4 balance = (1 + r / 100.0); 5 if (balance > goal) break; 6 } 7... A for loop can be an infinite loop by setting true or simply leaving empty in the condition statement. An infinite for loop with an if-break statement is equivalent to a normal while loop. Zheng-Liang Lu Java Programming 141 / 154

Equivalence: while and for Loops (Concluded) In general, a for loop may be used if the number of repetitions is known in advance. If not, a while loop is preferred. Zheng-Liang Lu Java Programming 142 / 154

Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started anew. Zheng-Liang Lu Java Programming 143 / 154

Example Multiplication table Write a program which displays the multiplication table. Zheng-Liang Lu Java Programming 144 / 154

Formatting Console Output You can use System.out.printf () to display formatted output on the console. 1... 2 double amount = 1234.601; 3 double interestrate = 0.00528; 4 double interest = amount interestrate; 5 System.out.printf("Interest = %4.2f", interest); 6... Zheng-Liang Lu Java Programming 145 / 154

By default, a floating-point value is displayed with 6 digits after the decimal point. Zheng-Liang Lu Java Programming 146 / 154

Multiple Items to Print Items must match the format specifiers in order, in number, and in exact type. If an item requires more spaces than the specified width, the width is automatically increased. By default, the output is right justified. You may try the plus sign (+), the minus sign (-), and 0 in the middle of format specifiers. Say % + 8.2f, % 8.2f, and %08.2f. Zheng-Liang Lu Java Programming 147 / 154

1... 2 public static void main(string[] args) { 3 for (int i = 1; i <= 9; ++i) { 4 for (int j = 1; j <= 9; ++j) { 5 System.out.printf("%3d", i j); 6 } 7 System.out.println(); 8 } 9 } 10... Zheng-Liang Lu Java Programming 148 / 154

Exercise: Coupled Loops Zheng-Liang Lu Java Programming 149 / 154

1 public class PrintStarsDemo { 2 public static void main(string[] args) { 3 // case (a) 4 for (int i = 1; i <= 5; i++) { 5 for (int j = 1; j <= i; j++) { 6 System.out.printf(" "); 7 } 8 System.out.println(); 9 } 10 11 // case (b), (c), (d) 12 // your work here 13 } 14 } Zheng-Liang Lu Java Programming 150 / 154

Analysis of Algorithms First, there may exist some algorithms for the same problem. Then we compare these algorithms. The first question is, Which one is more efficient? (Why?) We focus on the growth rate of the running time or space requirement as a function of the input size n, denoted by f (n). Zheng-Liang Lu Java Programming 151 / 154

O-notation 1 In math, O-notation describes the limiting behavior of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions. f (n) O(g(n)) as n if and only if there is a constant c > 0 and a real number n 0 such that f (n) c g(n) n n 0. (1) Note that O(g(n)) is a set featured by some simple function g(n). Hence f (n) O(g(n)) is equivalent to say that f (n) is one instance of O(g(n)). 1 See any textbook for data structures and algorithms or https://en.wikipedia.org/wiki/big_o_notation. Zheng-Liang Lu Java Programming 152 / 154

For example, 8n 2 3n + 4 O(n 2 ). We could say that 8n 2 3n + 4 O(n 3 ) and 8n 2 3n + 4 / O(n). Zheng-Liang Lu Java Programming 153 / 154

Common Fundamental Functions 2 2 See Table 4.1 and Figure 4.2 in Goodrich and etc, p. 161. Zheng-Liang Lu Java Programming 154 / 154