Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.

Slides:



Advertisements
Similar presentations
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
Advertisements

Chapter 2 - Introduction to Java Applications
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
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.
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.
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;
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
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.
Computer Programming Lab(5).
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
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.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
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.
COMPUTER PARTS AND COMPONENTS INPUT DEVICES
Chapter 2: Java Fundamentals
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
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.
 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.
Introduction to Programming
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
Lecture 2 Objectives Learn about objects and reference variables.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Dialog Boxes.
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.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Computer Programming1 Computer Science 1 Computer Programming.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
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.
Console Input and Output JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Introduction to programming in java
CompSci 230 S Programming Techniques
User Input ICS2O.
Building Java Programs
Java Programming Lecture 2
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.
2.5 Another Java Application: Adding Integers
Dialogues and Wrapper Classes
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
Chapter 2 - Introduction to Java Applications
Chapter 3: Introduction to Objects and Input/Output
Introduction to Java Programming
Building Java Programs
Building Java Programs
Building Java Programs
JOptionPane class.
F II 2. Simple Java Programs Objectives
Building Java Programs
Chapter 2: Java Fundamentals cont’d
Optional Topic: User Input with Scanner
Presentation transcript:

Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Warm-Up: Monday, March 3 Keyboard Microphone Scanner Digital Camera Flash/Hard Drive Mouse Joystick/Game Controller Tablet Video Capture Equipment Barcode Readers Touch Screen Fax Machine Buttons

INPUT Pre-AP Computer Science Cycle 5

Using Class Scanner Class Scanner is used to scan the computer for signals from input devices, and record inputs from these devices on your computer –Must import java.util.* library For our programming, the input device we’re most concerned with is the keyboard –Scan for key presses –Record key presses as numbers, letters, strings, etc Java Programming: Guided Learning with Early Objects4

5 Input (Read) Statement The Scanner class puts data into variables from the standard input device –static Scanner console = new Scanner(System.in); Next input is: –Integer: console.nextInt() –Double: console.nextDouble() –String: console.next() or console.nextLine()

Java Programming: Guided Learning with Early Objects6 Variable Initialization Variable can be initialized with a literal value int feet = 35; Variable can be initialized with an input statement int feet = console.nextInt(); Variables initialized with a literal value cannot be changed without editing the source code Variables initialized with an input statement are more flexible

Example A import java.util.*; public class Example_A { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter two integers” + “separated by spaces.”); int feet = console.nextInt(); int inches = console.nextInt(); System.out.println(“feet = “ + feet); System.out.println(“inches = “ + inches); }

Sample Run: Example A Enter two integers separated by spaces feet = 23 inches = 7 Java Programming: Guided Learning with Early Objects8

import java.util.*; public class Example_B { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter first name.”; String firstname = console.next(); System.out.println(“Enter last name.”; String lastname = console.next(); System.out.println(“Enter age.”); int age = console.nextInt(); System.out.println(“Enter weight.”); double weight = console.nextDouble(); System.out.println(“\nName: “ + firstname + “ “ + lastname); System.out.println(“Age: “ + age); System.out.println(“Weight: “ + weight); }

Sample Run: Example B Enter first name. Monica Enter last name. Barrera Enter age. 22 Enter weight Name: Monica Barrera Age: 22 Weight:

Java Programming: Guided Learning with Early Objects11

Warm-Up What is the error in the following input statement? double miles = console.nextInt( ); Java Programming: Guided Learning with Early Objects12

Chapter 0-1 Exam Statistics Java Programming: Guided Learning with Early Objects13

Chapter 0-1 Exam Statistics Java Programming: Guided Learning with Early Objects14

Chapter 0-1 Exam Statistics Period 1 –Average: 85 –Range: 71 – 99 Period 2 –Average: 74 –Range: 38 – 98.5 Combined –Average: 79 –Std Dev: 14.6 Top 10% >= 94.5 Top 25% >=

Dialog Output Boxes Chapter 2

Input/Output Review Used class System.out to output –System.out.print( ) –System.out.println( ) Used class Scanner to use the keyboard to provide input –Had to import the java.util.* packages –Create a new Scanner to check for keypresses static Scanner console = new Scanner(System.in ) –Used the Scanner to accept input console.nextInt( )//for integers console.nextDouble( )//for doubles console.next( )//for Strings 17

Review: Typical Input/Output Structure import java.util.* public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { // prompt the user for what to input // accept the input and store it in a variable // process/manipulate the inputted information //output the answer back to the user } 18

Review: Typical Input/Output Structure import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter a number.”); int number = console.nextInt( ); int newNum = number * 3 + 6; System.out.println(“New number = “ + newNum); } 19

Using Dialog Boxes Besides printing to the console, you can also use a generated user interface (GUI) to communicate with the user –Output AND Input GUI’s make the program look more user- friendly, and less like a computer program Closely resembles every-day computer use Java Programming: Guided Learning with Early Objects20

Java Programming: Guided Learning with Early Objects21 Figure 2-1 Input dialog box prompting the user to input name

Creating Dialog Boxes Dialog boxes are contained in the library package javax.swing.*, so it must be imported at the beginning of every code To create an output dialog box, use the following code: JOptionPane.showMessageDialog(null, “TEXT”, “TITLE”, JOptionPane.INFORMATION_MESSAGE); Java Programming: Guided Learning with Early Objects22

Example Code import javax.swing.*; import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter a message.”); String message = console.next( ); JOptionPane.showMessageDialog(null, message, “Your message”, JOptionPane.INFORMATION_MESSAGE); } 23

Example Code import javax.swing.*; import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter a message.”); String message = console.next( ); JOptionPane.showMessageDialog(null, message, “Your message”, JOptionPane.INFORMATION_MESSAGE); } 24

Types of Messages 25

Code Template – Dialog Boxes import javax.swing.*; import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { //write code here } JOptionPane.showMessageDialog(null, message, “Your message”, JOptionPane.PLAIN_MESSAGE); ERROR_MESSAGE QUESTION_MESSAGE INFORMATION_MESSAGE PLAIN_MESSAGEWARNING_MESSAGE 26

Warm-Up: Friday, March 7 What is a GUI? When using dialog boxes, why do we need to import the javax.swing.* package? When you are done, TURN IN YOUR WARMUPS Java Programming: Guided Learning with Early Objects27

Dialog Boxes - Input

Review Dialog boxes are pop-up boxes that can be used for input or output They are contained in the javax.swing.* package They belong to class JOptionPane Java Programming: Guided Learning with Early Objects29

Creating Dialog Output Boxes Dialog boxes are contained in the library package javax.swing.*, so it must be imported at the beginning of every code To create an output dialog box, use the following code: JOptionPane.showMessageDialog(null, “TEXT”, “TITLE”, JOptionPane.INFORMATION_MESSAGE); Java Programming: Guided Learning with Early Objects30

Creating Dialog Input Boxes The code for input boxes is much shorter Note: As it is input, you need to set a variable equal to the command in order to save what the user types in String str = JOptionPane.showInputDialog(“text”); Java Programming: Guided Learning with Early Objects31

Example String str; str = JOptionPane.showInputDialog(“Enter your name and press OK”); Java Programming: Guided Learning with Early Objects32

Dialog Input Returns Strings When using the dialog box for input, it always returns the input as a String, EVEN if what you’ve inputted is a number. That means, any number inputted into the dialog box must be converted to a number before you can use it to perform math Java Programming: Guided Learning with Early Objects33

Converting String to Int (Parsing) Integer.parseInt(stringName); int number = Integer.parseInt(“56”);  56 String number = “3678”; int Num = Integer.parseInt(number);  3678 Java Programming: Guided Learning with Early Objects34

Converting String to Float (Parsing) Float.parseFloat(stringName); float decimal = Float.parseFloat(“5.4”);  5.4 String number = “36.51”; float decimal = Float.parseFloat(number);  Java Programming: Guided Learning with Early Objects35

Review str = JOptionPane.showInputDialog(“Enter your name and press OK”); int Num = Integer.parseInt(number); float decimal = Float.parseFloat(number); Java Programming: Guided Learning with Early Objects36