©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Odds and Ends  Formatted Output  Random numbers.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

CS110 Programming Language I
Introduction to Computing Science and Programming I
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Sample Development Loan Calculator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 6 Repetition Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Confirmation Dialog Formatting Output.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: *Sample Development Loan Calculator.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement repetition control.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
Chapter Chapter 6 Repetition Statements. Objectives Understand repetition control (loop ) statements in Java: while statement. do-while statement.
Chapter 6 Repetition. Topics Some additional operators –increment and decrement –assignment operators Repetition –while –do-while –for Random numbers.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6 Repetition Statements.
Chapter 3b Standard Input and Output Sample Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 Pearson Education, Inc. All rights reserved Formatted Output.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
OUTPUT STATEMENTS GC 201.
The Random Class and its Methods
System.out.println for console output
Building Java Programs
INPUT & OUTPUT scanf & printf.
Repetition Statements
Introduction to Java Applications
Presentation transcript:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Odds and Ends  Formatted Output  Random numbers

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Recall the Carpet Pricing Example  The price table for carpets ranging in size from 11 x 5 to 20 x 25 ft. whose unit price is $15 per sq. ft.  The outer for statement ranges from the first row (width = 11) to the last row (width = 20).  For each repetition of the outer for, the inner for statement is executed, which ranges from the first column (length = 5) to the fifth (length = 25).

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Output is a Table  But the numbers don't line up properly

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Formatting Output  To align values with varying numbers of digits, we must vary the number of spaces in front of the values.  The idea behind formatted output is to allocate the same amount of space for the output values and align the values within the allocated space.  We call the space occupied by an output value the field. The number of characters allocated to a field is the field width.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Aligning Columns of Numbers  How to place a varying number of spaces to align the output values. Hyphen is used here to indicate the blank space.  One approach to determine how many spaces each number takes up and then pad with spaces to make it the the desired width.  Better to use the Formatter class from Java 1.5.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Formatter class  A Formatter is created with a PrintStream. Formatter fmt = new Formatter( System.out);  The format method is used with a format string that tells how to position the output values. fmt.format( "%8d", price); The format string is kind of like the 'picture' we used for DecimalFormat and DateFormat objects.  Formatter is in the java.util package.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The format method  The format method is what we call a variatic method It can take a variable number of arguments (one or more) There must be a format string The format string must contain a picture for each of the other arguments The format string can contain text that will also show up in the output

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Format Codes  % starts the picture for a value  The number after the % is the field width  The letter at the end is the type of the output value d => int s => String f => double in this case the number looks like x.y where the x is the total field width and the y is the number of digits after the decimal point

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Some Examples Formatter formatter = new Formatter( System.out); int num1 = 34, num2 = 9, num3; num3 = num1 + num2; formatter.format( "%3d + %3d = %5d\n", num1, num2, num3); String name = "John"; formatter.format( "Hello, %s!\n", name); GregorianCalendar day = new GregorianCalendar( 1776, 6, 4) formatter.format( "%1$tB %1$te, %1$tY\n", day);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Other format methods  The signature is the same as the one in the Formatter class  PrintStream has a format method System.out.format( formatString, varList);  String has a static format method that returns a String String result = String.format( formatString, varList);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Loan Tables  The goal of this exercise is to design a program that generates a loan table. The table will compare different monthly payments for a set loan amount, with varying loan periods in each column and different interest rates in each row.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Loan Tables  The start method can be expressed as tell the user what the program does; prompt the user "Do you want to generate a loan table?"; while (the user says YES) input the loan amount; generate the loan table; prompt the user "Do another loan table?";

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pseudocode  The start method was expressed in pseudocode.  Pseudocode is an informal language used to express an algorithm.  It is useful in clarifying the purpose or function of the program without being tied down to the syntactic rules of a particular programming language.  Also useful for describing algorithms that may be implemented in many different languages.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Loan Tables  Other methods in the program: describeProgram: tells the user what the program does if the user requests it. getLoanAmount: gets the loan amount from the user. generateLoanTable: generates the loan table.  To compute the monthly loan payment, reuse the Loan class defined in Chapter 4.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Random Number Generation  The method Math.random is called a pseudorandom number generator and returns a number of type double that is greater than or equal to 0.0 but less than 1.0.  The generated number is called a pseudorandom number because it is not truly random.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Random Integers  The number returned from the random method ranges from 0.0 up to (but not including) 1.0.  If we want to generate random integers, we must perform a conversion so the number will fall within the desired range.  Use the formula: Y = {X * (max – min + 1)} + min  where X is the number returned by random.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Random Integers  The formula expressed in Java: //assume correct values are assigned to //'max' and 'min' int randomNumber = (int) (Math.floor(Math.random() * (max-min+1)) + min);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Random Number Example *Ch6TestRandomGenerator generates N random numbers between 1 and 4 to simulate the suit of a drawn card.  It keeps one counter for each suit, and increments the matching counter after a random number is generated.  At the end of the generation, it prints the ratio count/N.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Random Class  The Random class is part of the java.util package  It provides methods that generate pseudorandom numbers of various types float nextFloat() int nextInt() int nextInt(n) produces integer in range 0 to n-1