CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS 1 Copyright: FALL 2014 Illinois Institute of Technology_ George Koutsogiannakis.
How to Create a Java program CS115 Fall George Koutsogiannakis.
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.
18 File handling1June File handling CE : Fundamental Programming Techniques.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Computer Programming Lab(4).
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.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
CS1101: Programming Methodology Aaron Tan.
From BlueJ to NetBeans SWC 2.semester.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
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.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
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.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Dialog Boxes.
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.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
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.
Computer Programming1 Computer Science 1 Computer Programming.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
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.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CompSci 230 S Programming Techniques
Chapter 2 Clarifications
INTERMEDIATE PROGRAMMING USING JAVA
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Input/Output.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
Message, Input, Confirm, and Specialized Dialogs
Something about Java Introduction to Problem Solving and Programming 1.
BIT115: Introduction to Programming
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Introduction to Classes and Methods
Message, Input, and Confirm Dialogs
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
JOptionPane class.
Random Numbers while loop
Consider the following code:
Presentation transcript:

CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1

SUMMARY OF TECHNIQUES FOR RECEIVING INPUT FROM USER There are 3 techniques that we will discuss: – Using the command line This technique is used when we start the execution of the program. We use it to provide a set of inputs right after the command to the java interpreter using a DOS window. i.e. >java MyProgram Nick 3 inputs are provided here the value 2, the value 2.3 and Nick. The program will capture the 3 inputs always as Strings 2

Using the command line to provide input The program has to be written as follows: public class MyProgram { public static void main(String[] args) { //write the code to capture the first input from the //command line – in our example the value 2 (as String) String a=args[0]; // because the value is expected to be an int data type and //not a String we need to convert the String to an int by //parsing it (using the Integer wrapper class) 3

Using the command line to provide input int x=Integer.parseInt(a); //The int 2 is now stored in x // now proceed to capture the second input String b=args[1]; // Now the value 2.3 is stored as a String in b // Since we expect it to be a double we need to convert //(parse) the String to a double data type double y=Double.parseDouble(b); // The double 2.3 is now stored in y // now proceed to capture the third input value String c=args[2]; // The value Nick is store din c. No need to parse it since we expect it to be a //String. other lines of code as needed by the program to process the above inputs 4

Using the command line to provide input Drawbacks of this technique – The inputs are fixed in value and also as far as the number of inputs. They can’t be changed once the program starts execution using this technique (you can change the inputs using a different technique i.e Scanner). – The inputs are always captured as Strings. We need to know the data type involved in order to parse it from a String to that data type. 5

Using the Scanner Object – Using the Scanner to capture input from keyboard This technique is used after the program starts execution. Some times we want some lines of code to be executed first and then we want the program to stop and wait for the user to type on the keyboard input values. The program needs to prompt the user as to when to start typing on the keyboard. The program needs to notify the user if a line of data is to be typed or if one data type at a time needs to be typed. The program resumes execution and captures the input typed when the user presses the Enter key. 6

Using the Scanner Object Example Import java.util.Scanner; public class MyProgram { public static void main(String[] args) { //assume that some lines of code that do something are written first //now we want to have the user enter some input values from the //keyboard. //First let us create a Scanner object and tell it to monitor the keyboard Scanner scan=new Scanner(System.in) //System.in means monitor the keyboard. 7

Using the Scanner Object //Next let us notify the user to enter dat System.out.println(“Please enter your age as an integer number”); // The program stops execution and waits for the user to type the //data and press Enter. The user can type a number like 19 and then //presses Enter. // The program needs to capture the value entered by the user. Since we //expect an int data type we would capture it as an int int x= scan.nextInt(); //now the value 19 is stored in location x. We can continue with other code to process the input captured. Notice that the Scanner class can capture other data types (see the API). 8

Using the JOptionPane Object – Using the JOptionPane to capture input from keyboard This technique uses a Graphical presentation to the user. A box appears on the screen and the user types the input on that box. The user presses the OK button of the Box and the program capture s the input typed. Although it is beyond the scope of the course we will do it in the practice exercise 6. You will be given the code to use. This technique is used to capture input from the user after the program starts execution. It is similar to using the scanner except that the instructions to the user and the output of the program can be presented on a graphical component called a box, instead on a DOS window. 9