Computer Studies 2013 Syllabus

Keyboard Entry
Reading data from a keyboard. Numeric and String input of data.

Until now, the user did not interact with the program; the program just showed one output based on the programmers coding. In order for the user to input values which can vary, external classes have to be used. There are the Scanner Class and the Keyboard Class.

Scanner Class
In order to use the Scanner class, it has to be imported first. This can be done by importing the java.util package. So at the very beginning of the program one must type:
import java.util.Scanner; (this imports that Scanner class only)

OR
import java.util.*; (imports all utilities)

Then in order to get input, an instance of the Scanner class must be constructed by using

Scanner s = new Scanner(System.in);

Class Creates an instance of the class To input different data type values, different methods must be used: Data Type String double long short byte float boolean
Mr. A. Gatt

Constructor

Method
String str = s.next(); double d = s.nextDouble(); long l = s.nextLong(); short sh = s.nextShort(); byte b = s.nextByte(); float f = s.nextFloat(); boolean bo = s.nextBoolean();

Page 1 of 5

Computer Studies 2013 Syllabus

Example of using the Scanner Class:

import java.util.Scanner; class test { public static void main (String args[]){ //Create an instance of the Scanner Scanner s = new Scanner(System.in); System.out.print("Enter your name : "); //Since the name is a String the String //has to be used String name = s.next();

System.out.println("How old are you ? "); //The age can be stored in a long long age = s.nextLong(); System.out.println("You are "+name+" and you are "+age+" years old."); } }

Mr. A. Gatt

Page 2 of 5

Computer Studies 2013 Syllabus

Keyboard Class
Another non-standard class which can be used is the keyboard class; this offers methods to read date of different types. Since this is a third-party class, the class file has to be placed in the same folder to the program being created. The Keyboard class can be used as follows:

Data Type int byte short long float double char boolean String

Method
Keyboard.readInt(); Keyboard.readByte(); Keyboard.readShort(); Keyboard.readLong(); Keyboard.readFloat(); Keyboard.readDouble(); Keyboard.readChar(); Keyboard.readBoolean(); Keyboard.readString();

So, the previous program can be modified as follows:

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+" and you are "+age+" years old."); } }

Mr. A. Gatt

Page 3 of 5

Computer Studies 2013 Syllabus

Activities
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 } } double itemAmount = itemTotal + calcVat; System.out.println("Item Amount + VAT = Euro "+itemAmount); double calcVat = (itemTotal * VAT)/100; System.out.println("VAT = "+calcVat); double itemTotal = (qty*price); System.out.println(qty+" "+item+" = Euro "+itemTotal); System.out.println(); System.out.print("Enter item :"); item = Keyboard.readString(); System.out.print("Enter quantity :"); qty = Keyboard.readInt(); System.out.print("Enter price :"); price = Keyboard.readDouble(); class Bill { public static void main (String args[]){ String item; int qty; double price; final float VAT = 18;

Mr. A. Gatt

Page 4 of 5

Computer Studies 2013 Syllabus

The output generated by this program is:
Enter item :Peas Enter quantity :2 Enter price :0.45 2 Peas = Euro 0.9 VAT = 0.162 Item Amount + VAT = Euro 1.062

1. What is the name of the class? 2. Identify a String variable. 3. What data type is variable qty declared as? 4. In which line is a constant declared? 5. What word identifies the constant? 6. To what variable is the constant initialised? 7. How many items are there in this bill? 8. Identify one line which outputs a prompt. 9. Identify one line in which the user is expected to input data. 10. Which line displays a blank line? 11. Identify one line which does an arithmetic calculation. 12. In which line is Vat being calculated? 13. Which line outputs the Vat? 14. Why is VAT declared as a constant? 15. Which line is outputting the cost of the items bough including the VAT? 16. Develop the application so that the user enters 3 items rather than one. Add the amounts of the three items and output the total Bill.

***

Mr. A. Gatt

Page 5 of 5

Sign up to vote on this title
UsefulNot useful