Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,

Slides:



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

Computer Programming Lab(7).
Introduction to Objects and Input/Output
GUIs Part 4 CS221 – 4/17/09. Professional Assignments Assignment #2 – Download and install Visual Studio 2008 Professional Trial Software –
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
© 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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Io package as Java’s basic I/O system continue’d.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Java Packages and Libraries M Taimoor Khan
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 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.
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.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
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.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
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.
Inheritance A Review of Objects, Classes, and Subclasses.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
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.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Input and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Introduction to programming in java
Java Basic.
Building Java Programs
Input/Output.
Chapter 2 More on Math More on Input
TemperatureConversion
Java Classes, Objects, and Events: A Preview
Classes, Libraries & Packages
INPUT STATEMENTS GC 201.
BIT115: Introduction to Programming
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to Java part 2
Java Methods Objects and Classes Start on Slide 30 for day 2
Building Java Programs
Java Methods Objects and Classes Start on Slide 30 for day 2
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Presentation transcript:

Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

4-2 Libraries l Java programs are usually not written from scratch. l There are hundreds of library classes for all occasions. l Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — newer GUI package Swing

4-3 import l Full library class names include the package name. For example: java.awt.Color javax.swing.JButton import statements at the top of your program let you refer to library classes by their short names: import javax.swing. JButton ;... JButton go = new JButton ("Click here"); Fully-qualified name

4-4 import (cont’d) You can import names for all the classes in a package by using a wildcard. * : import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages

4-5 Console Output l The standard output stream for Java is called System.out. We will use System.out along with the print and println methods to write output to the screen. println adds a newline on the end of what you print, print does not. l Note that you do not need to import any library since Java.lang defines System and is automatically imported into all classes.

4-6 Console Output (cont’d) Examples of writing to the screen: system.out.print(“Enter the sides of a triangle”);//prints a string literal system.out.println(“Done ”+ count + ” words.”);//would look like: //Done: 23 words //then go to newline

4-7 Console Input In order to prompt the user for input we are going to use the Scanner class. In order to use the Scanner class you must include the statement import java.util.Scanner; at the top of your program.

4-8 Console Input (cont’d) To open the standard input stream for reading keyboard input use Scanner console = new Scanner(System.in); l You must include a statement like the one above before you can begin getting information from the keyboard. l Note that console is the name that you give the input stream (you can give it any name).

4-9 Console Input (cont’d) Examples for reading data from the keyboard : int n = console.nextInt(); // reads an integer double x = console.nextDouble(); // reads a double String word = console.next(); // reads a contiguous string of // non-whitespace characters String line = console.nextLine(); // reads an entire line including whitespace