Chapter 2: Java Fundamentals Input and Output statements.

Slides:



Advertisements
Similar presentations
Introduction to Programming Overview of Week 2 25 January Ping Brennan
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
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.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
©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. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Chapter 2: Java Fundamentals Java Program Structure.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Variables, Constants and Built-in Data Types Chapter 2: Java Fundamentals.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Computer Programming Lab(4).
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Chapter 2 Elementary Programming
©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.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Introduction to Programming
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.
Using Java Class Library
String and Scanner CS 21a: Introduction to Computing I First Semester,
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
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.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Introduction to programming in java
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Topic 2 Elementary Programming
Input/Output.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Chapter 3 Java Input/Output.
INPUT STATEMENTS GC 201.
Scope, Comments, Code Style, Keyboard Input
Program Style Console Input and Output
Computers & Programming Languages
Introduction to Classes and Methods
A+ Computer Science INPUT.
Fundamentals 2.
Lec 4: while loop and do-while loop
Chapter 3 Input/Output.
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
CS2011 Introduction to Programming I Elementary Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
The keyboard is the standard input device.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Chapter 2: Java Fundamentals Input and Output statements

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Standard Output Window Using System.out, we can output multiple lines of text to the standard output window. The exact style of standard output window depends on the Java tool you use.

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 3 The println Method We use println instead of print to skip a line. int x = 123, y = x + x; System.out.println( "Hello, Dr. Caffeine.“ ); System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ );

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 4 Standard Input To input primitive data values, we use the Scanner class. 4 steps are needed to be able to use input primitive: –Step 1: import the Scanner class: import Java.util.Scanner; –Step 2 : declaring a reference variable of a Scanner Scanner read ; //we named the object read –Step 3: creating an instance of the Scanner read = new Scanner (System.in); –Step 4: use specific methods to enter data int x = read.nextInt();

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 5 Example 1 import Java.util.Scanner; 2 Scanner input ; // declaring the reference variable of a Scanner 3 int area,length, width; // declaring variables to store entries 4 input = new Scanner (System.in); // creating an instance 5 length = input.nextInt(); //reading the length from the keyboard 6 width = input.nextInt(); //reading the width from the keyboard 7 area = length * width ; // computing the area // displaying the result 8 System.out.println(“the legnth is ”+ lenght); 9 System.out.println(“the width is ”+ width); 10 System.out.println(“the area is ”+ area);

Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 6 MethodExample Scanner input = new Scanner (System.in); nextByte( )byte b = input.nextByte( ); nextDouble( )double d = input.nextDouble( ); nextFloat( )float f = input.nextFloat( ); nextInt( )int i = input.nextInt( ); nextLong( )long l = input.nextLong( ); nextShort( )short s = input.nextShort( ); next() String str = input.next(); Common Scanner Methods