Introduction to programming in java

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.
Lecture 3 Java Basics Lecture3.ppt.
18 File handling1June File handling CE : Fundamental Programming Techniques.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
LAB 10.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
Computer Programming Lab(4).
2-1 CM0551 Week 3 The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
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.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
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.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
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.
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 String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
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 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
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.
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.
© 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[]
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!!
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to Computer Science and Object-Oriented Programming
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Streams & File Input/Output (I/O)
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.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Maha AlSaif Maryam AlQattan
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
BIT115: Introduction to Programming
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Computers & Programming Languages
Introduction to Classes and Methods
A+ Computer Science INPUT.
Introduction to Computer Science and Object-Oriented Programming
A+ Computer Science INPUT.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Object Oriented Programming
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Optional Topic: User Input with Scanner
Presentation transcript:

Introduction to programming in java Lecture 06 Input from keyboard

Introduction Picture taken from Book entitled Introduction to Programming in Java: An interdisciplinary approach

Introduction Picture taken from Book entitled Introduction to Programming in Java: An interdisciplinary approach

Input From Keyboard Java supports three standard input/output streams: System.in (standard input device), System.out (standard output device), and System.err (standard error device). The System.in is defaulted to be the keyboard; while System.out and System.err are defaulted to the console.

Input From Keyboard double i = scanner.nextDouble(); Java SE 5 introduced a new class called Scanner in package java.util to simplify formatted input. Instantiate a Scanner Scanner myScanner = new Scanner(System.in); Read an entire line of text we use nextLine(); String input = myScanner.nextLine(); Read an integer value we use nextInt() int i = scanner.nextInt(); Read floating valueinteger value we use nextDouble() double i = scanner.nextDouble();

Example # 1: Enter your name import java.util.Scanner; public class MyName { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter your full name: "); String name = in.nextLine(); System.out.println(“Wellcome Mr. " + name); }

Example 2: Get Student ID import java.util.Scanner; public class StudentID { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter your student ID : "); int id = in.nextInt(); System.out.println(“Your ID : " + id); }

Example 3: Product of 2 numbers import java.util.Scanner; public class Product { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter first number : "); double x = in.nextDouble(); System.out.print("Enter second number : "); double y = in.nextDouble(); double ans = x * y ; System.out.println( x + “ * “ + y + “ = " + ans); }

Example 3: Name and Age import java.util.*; public class NameAndAge { public static void main(String[] args) { int age; String name; Scanner in = new Scanner(System.in); System.out.println("Enter full name: "); name = in.nextLine(); System.out.println("How old are you? "); age = in. nextInt(); System.out.println(name + '\t' + age); }

Practice Questions Write a program called MyMath which first reads two integer values from the standard input and then compute sum , product, division and subtract of those two numbers and prints the result. For example > Java MyMath Enter 1st number: 10 Enter 2nd number: 5 Sum : 10 + 5 = 15 Minus: 10-5 = 5 Product: 10 * 5 = 50 Division: 10 / 5 = 2

Submission deadline of Assignment No. 1 is 24th Sep 2013 For details: See Lecture 05