P06 - Keyboard Entry

download P06 - Keyboard Entry

of 5

Transcript of P06 - Keyboard Entry

  • 7/30/2019 P06 - Keyboard Entry

    1/5

    Computer Studies 2013 Syllabus

    Mr. A. Gatt Page 1 of 5

    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.utilpackage. 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);

    To input different data type values, different methods must be used:

    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();

    booleanboolean bo = s.nextBoolean();

    Class

    Creates an instance of

    the class

    Constructor

  • 7/30/2019 P06 - Keyboard Entry

    2/5

    Computer Studies 2013 Syllabus

    Mr. A. Gatt Page 2 of 5

    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.");

    }

    }

  • 7/30/2019 P06 - Keyboard Entry

    3/5

    Computer Studies 2013 Syllabus

    Mr. A. Gatt Page 3 of 5

    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 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();

    String 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.");

    }

    }

  • 7/30/2019 P06 - Keyboard Entry

    4/5

    Computer Studies 2013 Syllabus

    Mr. A. Gatt Page 4 of 5

    Activities

    1 class Bill {

    2 public static void main (String args[]){

    3 String item;

    4 int qty;

    5 double price;

    6 final float VAT = 18;

    7

    8 System.out.print("Enter item :");

    9 item = Keyboard.readString();

    10 System.out.print("Enter quantity :");

    11 qty = Keyboard.readInt();

    12 System.out.print("Enter price :");

    13 price = Keyboard.readDouble();

    14

    15 System.out.println();

    16

    17 double itemTotal = (qty*price);

    18 System.out.println(qty+" "+item+" = Euro "+itemTotal);

    19

    20 double calcVat = (itemTotal * VAT)/100;

    21 System.out.println("VAT = "+calcVat);

    22

    23 double itemAmount = itemTotal + calcVat;

    24 System.out.println("Item Amount + VAT = Euro "+itemAmount);

    25

    26 }

    27 }

  • 7/30/2019 P06 - Keyboard Entry

    5/5

    Computer Studies 2013 Syllabus

    Mr. A. Gatt Page 5 of 5

    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.

    ***