CSC 142 H 1 CSC 142 Standard input/output. CSC 142 H 2 Console Ouput  Use the static PrintStream object out in System System.out.println("Hello, world");

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
Chapter 2 - Introduction to Java Applications
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
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.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
05/09/2015SJF L31 F21SF Software Engineering Foundations Formatting Converting numbers to Strings and vice versa Monica Farrow EM G30
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
Chapter 3 Java Input/Output.
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.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
Introduction to programming in java
Important Java Classes CSIS 3701: Advanced Object Oriented Programming.
Java Programming Lecture 2
Console Input and Output
Outline Creating Objects The String Class Packages Formatting Output
CSC1401 Input and Output (and we’ll do a bit more on class creation)
2.5 Another Java Application: Adding Integers
Interactive Standard Input/output
Chapter 3 Java Input/Output.
Console Input and Output
Chapter 2 - Introduction to Java Applications
Chapter 3 Input/Output.
Object Oriented Programming
The keyboard is the standard input device.
Object based programming in Java
Object based programming in Java
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
F II 2. Simple Java Programs Objectives
Presentation transcript:

CSC 142 H 1 CSC 142 Standard input/output

CSC 142 H 2 Console Ouput  Use the static PrintStream object out in System System.out.println("Hello, world");  Recall: use + to concatenate Strings //If name is a String variable System.out.print ("I am "+name); //Warning: + means concatenation or addition //java reads the expression left to right. //If the 2 operands are numbers, do an //addition. If one of them is a String, do //a concatenation (making the number a String) System.out.print("1+3="+1+3); //prints 1+3=13 System.out.print(1+3+"=1+3"); //prints 4=1+3 System.out.print("1+3="+(1+3)); //prints 1+3=4

CSC 142 H 3 Console Input  In System, BufferedInputStream object in  as is, to input a stream of bytes  can be made into an object to read Strings directly. But some knowledge about exceptions is required.  Instead use a class where all of the work is already done

CSC 142 H 4 Classes for input  Scanner (in java.util) Scanner input = new Scanner(System.in); System.out.print(“Enter your age: “); int i = input.nextInt(); // other methods: nextDouble, // nextLine (for a string),...  Input (in uwcse.io) Input input = new Input(); int i = input.readInt(“Enter your age: “); // other methods: readDouble, readString,...

CSC 142 H 5 Output using GUI objects  Many graphics objects can display text (check the documentation). Two examples:  JOptionPane class (in javax.swing) JOptionPane.showMessageDialog ( null,"Hello, \nWorld!","This is the title", JOptionPane.INFORMATION_MESSAGE); parent window or null if no parent //last 2 actual parameters may be omitted  In a GWindow object: window // Display "Hello" at location (30,60) window.add(new TextShape("Hello",30,60));

CSC 142 H 6 Input using GUI objects (1)  JOptionPane class (in javax.swing): Hard way String s = JOptionPane.showInputDialog ( null,"Your age? ","Age input", JOptionPane.QUESTION_MESSAGE); // Always get a String (possibly null). // To extract the int, use parseInt in Integer. int age = Integer.parseInt(s); // parseInt gives an error if s doesn't have an // integer format. Fix: use exceptions

CSC 142 H 7 Input using GUI objects (2)  In Input from uwcse.io : easy way Input input = new Input(); int age = input.readIntDialog("Age?");

CSC 142 H 8 Formatting output (1)  Use classes from java.text  we have seen DecimalFormat DecimalFormat d = new DecimalFormat("0.00"); // 2 digits after the decimal point String s = d.format(3.136); //s is "3.14"  for a currency amount NumberFormat c; c = NumberFormat.getCurrencyInstance(); String s = c.format( ); //s is "$1,001.34" (in the US)  Many other classes, e.g. for a date: DateFormat  Check the documentation

CSC 142 H 9 Formatting output (2)  Use formatting strings (convenience for C programmers) String s = String.format(“%3.2f”, 3.136); //s is 3.14 System.out.printf(“%3.2f”, 3.136); // prints 3.14 System.out.printf(“$%,.2f”, ); // prints $1,  Check the documentation (look for printf in java.io.PrintStream)