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

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

Object Oriented Programming. Java-Lecture 1

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

1.5 Input and Output

1.5 Input and Output. Input and Output. Digital Michelangelo Project. Input and Output. Input devices. Output devices.

1.5 Input and Output. Input and Output. Terminal. Input and Output. Input devices. Output devices.

Full file at

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

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

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

1.5 Input and Output. Introduction to Computer Science Sedgewick and Wayne Copyright 2007

1.1 Basic Programming Model 1.2 Data Abstrac9on

2.2 Input and Output. Input and Output. Digital Michelangelo Project. Terminal. Input devices. Output devices.

Example: Monte Carlo Simulation 1

Introduction to Computer Programming

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

COMP 110 Programming Exercise: Simulation of the Game of Craps

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Input. Scanner keyboard = new Scanner(System.in); String name;

CS111: PROGRAMMING LANGUAGE II

CS 101 Spring 2007 Midterm 2 Name: ID:

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

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

Getting started with Java

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

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

AP Computer Science Unit 1. Programs

Software Practice 1 Basic Grammar

STUDENT LESSON A7 Simple I/O

Full file at

COMP-202 Unit 4: Programming with Iterations

Welcome to the Primitives and Expressions Lab!

Conditional Programming

Controls Structure for Repetition

Exercise (Revisited)

2.4 Input and Output. Input and Output. Standard Output. Standard Output Abstraction. Input devices. Output devices. Our approach.

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

2.8. Decision Making: Equality and Relational Operators

Introduction to Java Applications

Simple Java Input/Output

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

File Processing in Java

Top-Down Program Development

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Introduction to Java Applications; Input/Output and Operators

Introduction to Computer Science Unit 2. Exercises

Question 1 [20 points]

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Text User Interfaces. Keyboard IO plus

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Midterm Examination (MTA)

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

Loops. CSE 114, Computer Science 1 Stony Brook University

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

1007 Imperative Programming Part II

Programming with Java

CSCI 355 Lab #2 Spring 2007

CS11 Java. Fall Lecture 1

Programming Language Basics

St. Edmund Preparatory High School Brooklyn, NY

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

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

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

Tutorial 03. Exercise 1: CSC111 Computer Programming I

APCS Semester #1 Final Exam Practice Problems

Course Outline. Introduction to java

Chapter 2: Data and Expressions

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

1 Short Answer (10 Points Each)

Introduction to Computer Programming

Object-Oriented Programming in Java

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CSC 1051 Data Structures and Algorithms I

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Java Coding 3. Over & over again!

CMPT 125: Lecture 4 Conditionals and Loops

COMP 202 Java in one week

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Debugging [continued]

Introduction to Classes and Objects

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Fundamentals of Programming Data Types & Methods

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

AP CS Unit 3: Control Structures Notes

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

5) (4 points) What is the value of the boolean variable equals after the following statement?

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

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

C Sc 227 Project 1: Three Main Methods

Example Program. public class ComputeArea {

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

Warm-Up: COMP Programming with Iterations 1

Chapter 2: Data and Expressions

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

2.5 Another Application: Adding Integers

CS 101 Fall 2006 Midterm 1 Name: ID:

AP COMPUTER SCIENCE A

Transcription:

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

INPUT AND OUTPUT Input devices Keyboard Mouse Hard drive Network Digital camera Microphone Output devices. Display Speakers Hard drive Goal. Java programs that interact with the outside world. Java Libraries support these interactions We use the Operating System (OS) to connect our program to them Network Printer MP3 Player

WHAT HAVE WE SEEN SO FAR? Command-line input. Example: read an integer N as command-line argument. Standard output. The OS output stream for text By default, standard output is sent to Terminal. Example: System.out.println() goes to standard output. 1. public class RandomSeq { 2. public static void main(string[] args) { 3. int N = Integer.parseInt(args[0]); 4. int i = 0; 5. while (i < N) { 6. System.out.println(Math.random()); 7. i++; 8. } 9. } 10. }

STANDARD INPUT AND OUTPUT

FORMATTING OUTPUT There is too much to cover in one slide, so here is a link to help Basics Use System.out.printf() or System.out.format() Their first argument is a string. Each time a % appears in the string, it is a directive to substitute it for a variable value. Attach each value after the string (comma separated) System.out.printf( Hello %s, World ); Use \n in the string to add a new line % %s String %b Boolean %d Integer %f Float/double Etc. Examples System.out.printf( My int: %d, a); System.out.printf( My float: %f, d);

FORMATTING OUTPUT The power of printf! Can control field width how many characters are used to output item Can right justify text Example %5d always uses 5 characters to output an integer. Beginning would be white space, not zeroes 1. public class PlayWithFormat { 2. public static void main(string args[]) { 3. System.out.printf( %5.7f\n, 4. Double.parseDouble(args[0])); 5. } 6. } Can also do the same on other types. Floats can determine number of decimal places: %5.7f means 5 characters before the decimal and 7 after The possibilities become infinite

COMMAND-LINE INPUT VS. STANDARD INPUT Command-line input. Use command-line input to read in a few user values. Not practical for many user inputs. Input entered before program begins execution. Standard input. The OS stream for input By default, standard input is received from Terminal window. Input entered while program is executing.

STANDARD INPUT Use Scanner. Helpful links: API, Tutorial Basics: Declare and initialize a scanner like: Scanner scanner = new Scanner(System.in); Then use it like in the API: double d = scanner.nextdouble(); Usually you should prompt the user with System.out.print() to request input 1. import java.util.scanner; 2. public class ReadSentence { 3. public static void main(string[] args) { 4. System.out.print( Enter sentence: ); 5. Scanner s = new Scanner(System.in); 6. String sentence; 7. while(s.hasnext()) 8. sentence += s.next(); 9. System.out.println( Your sentence: + s); 10. } 11. } Note - hasnext() will return true until it sees Control+d.

DECODING YOUR TEXTBOOK Your textbook uses their own Java libraries StdIn.java and StdOut.java. These are synonymous (BUT NOT EXACTLY THE SAME) as Scanner and System.out respectively Please do not use StdIn or StdOut! These are not necessary libraries

REDIRECTION AND PIPING

REDIRECTING STANDARD OUTPUT Redirecting standard output. Use OS directive to send standard output to a file for permanent storage (instead of terminal window). % java RandomSeq 1000 > data.txt redirect stdout

REDIRECTING STANDARD INPUT Redirecting standard input. Use OS directive to read standard input from a file (instead of terminal window). % java Average < data.txt 0.4947655567740991 redirect stdin

CONNECTING PROGRAMS Piping. Use OS directive to make the standard output of one program become the standard input of another. Another interesting item: Can pipe directly to command line arguments with xargs, e.g., pipe stdout of RandomSeq to stdin of Average java RandomSeq 10 xargs java AverageCMDLine % java RandomSeq 1000000 java Average 0.4997970473016028

EXERCISE IN TRIPLETS Yes you have to be with someone! Starters: You work for JLDiablo Consultants Inc., which specializes in making software for Casino games (Cha-ching! $$$$). A new casino in Reno needs a slot game called Binary Slots 101010. How it works: A player enters a bet of their choice Three Boolean values are randomly generated If they are all true, then the player earns twice their money back!

EXERCISE WHERE TO BEGIN When developing programs Always think first! Sketch out solution, i.e., plan Implement solution Test solution Repeat! Called iterative development Test Plan Implement

EXERCISE START THE PROGRAM 1. public class BinarySlots101010 { 2. public static void main(string[] args) { 3. System.out.println( Welcome to Binary Slots 101010!\n\n\n ); 4. } 5. }

EXERCISE GET BET 1. import java.util.scanner; 2. public class BinarySlots101010 { 3. public static void main(string[] args) { 4. System.out.println( Welcome to Binary Slots 101010!\n\n\n ); 5. 6. System.out.print( Please enter your bet: ); 7. Scanner scanner = new Scanner(System.in); 8. double bet = scanner.nextdouble(); 9. System.out.printf( Your bet is $.2f\n\n, bet); 10. } 11. } Recall, it is good style to name variables descriptively What happens if you don t enter a double?

EXERCISE GET BET ROBUSTLY 1. import java.util.scanner; 2. public class BinarySlots101010 { 3. public static void main(string[] args) { 4. System.out.println( Welcome to Binary Slots 101010!\n\n\n ); 5. 6. System.out.print( Please enter your bet: ); 7. Scanner scanner = new Scanner(System.in); 8. while(!scanner.hasnextdouble()) { 9. System.out.println( Please enter a valid bet: ); 10. Scanner.next(); //Remember to eat up (read) bad input 11. } 12. double bet = scanner.nextdouble(); 13. System.out.printf( Your bet is $.2f\n\n, bet); 14. } 15. }

EXERCISE GAME LOGIC 1. import java.util.scanner; 2. public class BinarySlots101010 { 3. public static void main(string[] args) { 4. System.out.println( Welcome to Binary Slots 101010!\n\n\n ); 5. 6. System.out.print( Please enter your bet: ); 7. Scanner scanner = new Scanner(System.in); 8. while(!scanner.hasnextdouble()) { 9. System.out.println( Please enter a valid bet: ); 10. Scanner.next(); //Remember to eat up (read) bad input 11. } 12. double bet = scanner.nextdouble(); 13. System.out.printf( Your bet is $.2f\n\n, bet); 13. System.out.println( Spinning.match all to win!\n ); 14. boolean a = Math.random() < 0.5, 15. b = Math.random() < 0.5, 16. c = Math.random() < 0.5; 17. System.out.println( Binary slots: + a + + b + + c + \n ); 18. 19. if(a && b && c) 20. System.out.printf( You win 2x your bet! You won $%.2f\n, 2*bet); 21. else 22. System.out.println( Sorry you lose ); 23. } 24. }

EXERCISE Until the end of lab work with your team to improve the slots game. Possibilities: Allow multiple bets without restarting the program Track the user s total money amount and allow them to cash out (leave the machine) Modify game to have more Boolean values and allow different winning amount, i.e., if two are matched you get 1.2*bet, 4 matched you get 8*bet, or whatever works for you. Use Unicode characters to allow more than two symbols Make sure to protect all inputs (while and if statements) Always start with planning your program modification, then implement, and then test (ensure it works!) Save this for program for next week in your Box!