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

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.

Exercise (Revisited)

Example: Monte Carlo Simulation 1

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

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

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

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

IT 4043 Data Structures and Algorithms

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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.

4. Static Data Structures

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

Chapter 2: Complexity Analysis

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

Zheng-Liang Lu Java Programming 45 / 79

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

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

Data Structures Brett Bernstein. Lecture 1: Introduction and a Review/Enrichment of CS 101

Lecture 5: Methods CS2301

Example: Fibonacci Numbers

Algorithms and Data Structures

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Array. Prepared By - Rifat Shahriyar

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

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

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

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

IEEE Floating-Point Representation 1

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

Give one example where you might wish to use a three dimensional array

CS 61B Asymptotic Analysis Fall 2018

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

3. Java - Language Constructs I

Binary Search. Roland Backhouse February 5th, 2001

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

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

Section 2.2 Your First Program in Java: Printing a Line of Text

Getting started with Java

Dynamic Data Structures. John Ross Wallrabenstein

Algorithms and Data Structures

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

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

CEN 414 Java Programming

Introduction to Programming Using Java (98-388)

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Midterm Examination (MTA)

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

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

Data Structures and Algorithms

CHAPTER 7 OBJECTS AND CLASSES

Object Oriented Programming. Java-Lecture 1

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

The return Statement

Total Score /1 /20 /41 /15 /23 Grader

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Fundamentals of Programming Data Types & Methods

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Computational Expression

CHAPTER 7 OBJECTS AND CLASSES

Instance Members and Static Members

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

CS:3330 (22c:31) Algorithms

AP Computer Science Unit 1. Programs

CS 101 Spring 2007 Midterm 2 Name: ID:

Unit 4. Input/Output Functions

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Array. Array Declaration:

Section 05: Midterm Review

Repetition with for loops

Lecture 6 Introduction to Objects and Classes

DATA STRUCTURES CHAPTER 1

CSE 142 Su01 Final Exam Sample Solution page 1 of 7

Contents. 8-1 Copyright (c) N. Afshartous

Arrays. Eng. Mohammed Abdualal

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Run-time Environments - 3

Introduction to Analysis of Algorithms

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

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

COE318 Lecture Notes Week 6 (Oct 10, 2011)

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

Object Oriented Methods : Deeper Look Lecture Three

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Outline and Reading. Analysis of Algorithms 1

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Full file at

Short Notes of CS201

Instructor: Eng.Omar Al-Nahal

Programming refresher and intro to C programming

Object Oriented Programming. Java-Lecture 6 - Arrays

Asymptotic Analysis of Algorithms

CS201 - Introduction to Programming Glossary By

Transcription:

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 AP Computer Science A: Java Programming 144 / 162

Example Multiplication table Write a program which displays the multiplication table. Zheng-Liang Lu AP Computer Science A: Java Programming 145 / 162

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 AP Computer Science A: Java Programming 146 / 162

By default, a floating-point value is displayed with 6 digits after the decimal point. Zheng-Liang Lu AP Computer Science A: Java Programming 147 / 162

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 AP Computer Science A: Java Programming 148 / 162

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 AP Computer Science A: Java Programming 149 / 162

Exercise: Coupled Loops Zheng-Liang Lu AP Computer Science A: Java Programming 150 / 162

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 } Zheng-Liang Lu AP Computer Science A: Java Programming 151 / 162

Analysis of Algorithms First, there may exist some algorithms for the same problem, which are supposed to be correct. 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 AP Computer Science A: Java Programming 152 / 162

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. Zheng-Liang Lu AP Computer Science A: Java Programming 153 / 162

For example, 8n 2 3n + 4 O(n 2 ) (tight bound). We say 8n 2 3n + 4 O(n 3 ) and 8n 2 3n + 4 / O(n). Zheng-Liang Lu AP Computer Science A: Java Programming 154 / 162

Growth Rates for Fundamental Functions 2 2 See Table 4.1 and Figure 4.2 in Goodrich and etc, p. 161. Zheng-Liang Lu AP Computer Science A: Java Programming 155 / 162

All in all, O-notation describes the asymptotic 3 upper bound of complexity of the algorithm. So O-notation is widely used to classify algorithms by how they respond to changes in input size. 4 Time complexity Space complexity 3 The asymptotic sense is that the input size n grows toward infinity. 4 Actually, there are Θ, θ, o, Ω, and ω which are used to classify algorithms. Zheng-Liang Lu AP Computer Science A: Java Programming 156 / 162

References https://en.wikipedia.org/wiki/game_complexity https://en.wikipedia.org/wiki/p_versus_np_problem Zheng-Liang Lu AP Computer Science A: Java Programming 157 / 162

1 class Lecture5 { 2 3 "Arrays" 4 5 } Zheng-Liang Lu AP Computer Science A: Java Programming 158 / 162

Arrays An array stores a large collection of data which is of the same type. 1... 2 // assume the size variable exists above 3 T[] A = new T[size]; 4 // this creates an array of T type, referenced by A 5... T can be any data type. This statement comprises two parts: Declaring a reference Creating an array Zheng-Liang Lu AP Computer Science A: Java Programming 159 / 162

Variable Declaration for Arrays In the left-hand side, it is a declaration for an array variable, which does not allocate real space for the array. In reality, this variable occupies only a certain space for the reference to an array. 5 If a reference variable does not refer to an array, the value of the variable is null. 6 In this case, you cannot assign elements to this array variable unless the array object has already been created. type. 5 Recall the stack and the heap in the memory layout. 6 Moreover, this holds for any reference variable. For example, the Scanner Zheng-Liang Lu AP Computer Science A: Java Programming 160 / 162

Creating A Real Array All arrays of Java are objects. As seen before, the new operator returns the memory address of that object. Recall that the type of reference variables must be compatible to that of the array object. The variable size must be a positive integer for the number of elements. Note that the size of an array cannot be changed after the array is created. 7 7 You can try the ArrayList class. See any textbook for data structures. Zheng-Liang Lu AP Computer Science A: Java Programming 161 / 162

Consider this array: 1 int[] A = new int[3]; Arrays in Memory The array is allocated contiguously in the memory. Note that all arrays are zero-based indexing. 8 (Why?) So we have A[0], A[1], and A[2]. 8 Same in C, C++, and more languages. Zheng-Liang Lu AP Computer Science A: Java Programming 162 / 162