Date post: | 04-Apr-2018 |
Category: |
Documents |
Upload: | sullivan583 |
View: | 239 times |
Download: | 1 times |
of 12
7/30/2019 Java - External Classes
1/12
Java
Scanner ClassKeyboard Class
7/30/2019 Java - External Classes
2/12
User Interaction
So far when we created a programthere was no human interaction
Our programs just simply showed one
output
In order for users to interact with our
programs we need to use external
classes
7/30/2019 Java - External Classes
3/12
External Classes
External classes are ready madeclasses that are used to allow users to
interact with a program
Two examples of external classes are;
1. The Scanner Class
2. The Keyboard Class
7/30/2019 Java - External Classes
4/12
Using External Classes
In order to use external classes youwould need to let your program know
that you would like to use them
In order to call ALL external classes
you need to use the following code
snippet
import java.util.*;
7/30/2019 Java - External Classes
5/12
Scanner & Keyboard Class
The scanner and keyboard classesare both used to allow users to
interact with the program
They are used to ask users for an
input
The input is normally done using the
actual keyboard
7/30/2019 Java - External Classes
6/12
Scanner Class
The first thing you need to do is createan instance of the Scanner class
Class Creates aninstance of
the class
Name
Constructor
7/30/2019 Java - External Classes
7/12
Scanner Inputs
You could input many different datatypes;
Data Type Method
String String str = s.next();
double double d = s.nextDouble();
long long l = s.nextLong();
short short sh = s.nextShort();
byte byte b = s.nextByte();
float float f = s.nextFloat();
boolean boolean bo = s.nextBoolean();
t t
7/30/2019 Java - External Classes
8/12
Examples
mport ava.ut . canner;
class test {public static void main (String args[]){
//Create an instance of the ScannerScanner s = new Scanner(System.in);
System.out.print("Enter your name : ");//Since the name is a String the String
//has to be usedString name = s.next();
System.out.println("How old are you ? ");//The age can be stored in a longlong age = s.nextLong();
System.out.println("You are "+name+" and you are "+age+"years old.");
}
}
7/30/2019 Java - External Classes
9/12
Keyboard Class
This class is also used to let usersinput data from the keyboard
The user can input the same datatypes we spoke about when we used
the scanner class
7/30/2019 Java - External Classes
10/12
Using the Keyboard Class
Data Type Method
int Keyboard.readInt();
byte Keyboard.readByte();
short Keyboard.readShort();
long Keyboard.readLong();
float Keyboard.readFloat();
double Keyboard.readDouble();
char Keyboard.readChar();
boolean Keyboard.readBoolean();
Keyboard.readString();
7/30/2019 Java - External Classes
11/12
class test {public static void main (String args[]){
System.out.print("Enter your name : ");String name = Keyboard.readString();
System.out.println("How old are you ? ");long age = Keyboard.readLong();
System.out.println("You are "+name+" andyou are "+age+" years old.");
}}
7/30/2019 Java - External Classes
12/12
Which is better to
use?Why?