Program Style Console Input and Output

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
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.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Introduction to Java Applications
Input/Output.
Console Input and Output
Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Introduction to Java Applications
CSC 1051 – Data Structures and Algorithms I
Console Input and Output
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
OUTPUT STATEMENTS GC 201.
CSS161: Fundamentals of Computing
Control Statement Examples
Scope, Comments, Code Style, Keyboard Input
CSS161: Fundamentals of Computing
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Classes and Methods
A+ Computer Science INPUT.
Chapter 2 Part 2 Edited by JJ Shepherd
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
CMSC 202 Java Primer 2.
Expressions and Assignment Statements
Anatomy of a Java Program
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
CS2011 Introduction to Programming I Elementary Programming
CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing
Presentation transcript:

Program Style Console Input and Output This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

Techniques of Program Style Naming Constants Spelling Conventions Comments Indenting No redundant code (use of methods) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Naming Constants Declare numbers as named constants: Syntax public static final Type Variable = Constant; Examples public static final int INCHES_PER_FOOT = 12; public static final double RATE = 0.14; Naming Convention: Use all uppercase letters, and designate word boundaries with an underscore. Advantages Changing a value in one place if it must be modified preventing a value from being changed inadvertently CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Spelling Conventions Class names start with an upper letter: FirstProgram Variable, Object, and method names start with a lower letter: answer Punctuation is indicated with an underscore for a constant, otherwise an upper letter: INCHES_PER_FOOT, FirstProgram Identifiers are spelled out in full: ArrayIndexOutOfBoundsException Boolean variables should be named with a statement that will be true when the value of the Boolean variable is true: isPositive, pressureOK Methods tend to start with a verb: equals( ), remove( ), add( ) CSS161: Fundamentals of Computing

Comments and Indenting A line comment begins with the symbols // A block comment begins with the symbol pair /*, and ends with the symbol pair */ Javadoc creates an HTML file that documents a given java program with /** */ Indenting shows the level of nested structures. Self-documenting means the clear structure of a given program with no necessity of // coments. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing More Example /** * Is a CSS161 example program to see javadoc demonstration. * * @author Munehiro Fukuda * @since 01/01/07 * @version 1.0 */ public class Css161Example { * Is the main function. * @param arguments passed to the main function. * @return nothing public static void main( String[] args ) { function( ); // function call } * Repeats printing "Hello!" three times public static void function( ) { System.out.println( "Hello!" ); Javadoc formats these block comments in html. Indenting to show the level of nested structure. A line comment (This example contributes nothing to understanding, though.) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Practice Reformat the following code according to the program style you have learned: public class Circumference { double radius 10.0; public static void main( String[] args ) { double circumference = 2 * 3.141592 * radius; System.out.println( “Circumference of radius 10.0 is “ + circumference ); } CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing System.out.println Syntax System.out.println( Item_1 + Item_2 + … + Last_item ); Items: quoted strings, variables, numbers, or almost any object, which are concatenated together with +. A new line added automatically Examples System.out.println( “Welcome to Java.” ); System.out.println( “Elapsed time = “ + time + “seconds” ); CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing System.out.printf Syntax System.out.printf( “…%X1[.Y1]z1 … %X2[.Y2]z2 …”, primitive_data1, primitive_data2, …); where: X: a digit Y: a digit added with f, e, and g specifier Z: a specifier such as d, f, e, g, s, and c Examples double price = 19.8; String name = “magic apple”; System.out.printf( “$6.2f for each $s.%n”, price, name ); %n: a new line added manually if necessary CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Format Specifiers CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example (1 or 3) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example (2 of 3) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example (3 of 3) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on P67’s Self-Test Exercises 5 ~ 7 with your neighboring classmate. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Scanner Class Convert each keyboard input (delimited by whitespace) into an appropriate data type. Whitespace: blank spaces, tabs, and line breaks Instantiation import java.util.Scanner; Scanner keyboard = new Scanner( System.in ); Major methods Convert an input into a byte, a short, an integer, a long, a float, a double, or a string data type. nextByte( ); nextShort( ); nextInt( ); nextLong( ); nextFloat( ); nextDouble( ); next( ); Get an entire line next( ); Check if the next input can be converted into a byte, a short, an integer, a long a float, a double or a string data type. hasNextByte( ); hasNextShort( ); hasNextInt( ); hasNextLong( ); hasNextFloat( ); hasNextDouble( ); hasNext( ); Check if there is another line to get. hasNextLine( ); CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example (1 of 3) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example (2 of 3) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Example (3 of 3) CSS161: Fundamentals of Computing

Scanner.useDelimiter( ) Change the delimiter from whitespace to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##"); String word1 = keyboard2.next( ); String word2 = keyboard2.next( ); Inputs abc##xyz Values assigned word1: abc Word2: xyz CSS161: Fundamentals of Computing

Pitfall: Dealing with the Line Terminator, '\n' Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); and the input, 2 Heads are better than 1 head. what are the values of n, s1, and s2? \n \n An empty string “” CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Programming Tips Prompt for Input Scanner keyboard = new Scanner( System.in ); System.out.println( “enter the first name” ); String first = keyboard.next( ); System.out.println( “enter the last name” ); String last = keyboard.next( ); Echo Input System.out.print( “the full name is %s %s %n”, first, last ); CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on P86’s Self-Test Exercises 13 ~ 14. Work on P87’s Self-Test Exercises 15 ~ 16. CSS161: Fundamentals of Computing