Ray Cuschieri

Ray Cuschieri is a Senior Lecturer I with the department of Computing & I.T. at the University of Malta Junior College. He obtained a BSc in Maths & Computing, after which he did a PGCE course to gain a teaching warrant. He started teaching computer-related topics at the Institute of Tourism Studies in 1992 and in 1996 he joined the University Junior College teaching Advanced Level and Intermediate Computing. Mr Cuschieri also serves as a paper marker for Computing Examinations of a major examination board.

 

 

Resources for an introduction to JAVA programming textbook

Keyboard class

import java.io.*;
 public class Keyboard {
     public static String readString() {
         BufferedReader br;
         try {
             br = new BufferedReader(new InputStreamReader(System.in));
             return br.readLine();
         } catch (Exception e) {}
         return null;
     }
     public static int readInt() {
         return Integer.parseInt(readString());
     }
     public static byte readByte() {
         return Byte.parseByte(readString());
     }
     public static short readShort() {
         return Short.parseShort(readString());
     }
     public static long readLong() {
         return Long.parseLong(readString());
     }
     public static float readFloat() {
         return Float.parseFloat(readString());
     }
     public static double readDouble() {
         return Double.parseDouble(readString());
     }
     public static char readChar() {
         return readString().charAt(0);
     }
     public static boolean readBoolean() {
         return Boolean.parseBoolean(readString());
     }
 }

EasyScanner class

import java.util.*;
public class EasyScanner {
    public static int readInt() {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        return i;
    }
    public static double readDouble() {
        Scanner sc = new Scanner(System.in);
        double d = sc.nextDouble();
        return d;
    }
    public static String readString() {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        return s;
    }
    public static char readChar() {
        Scanner sc = new Scanner(System.in);
        char c = sc.next().charAt(0);
        return c;
    }
}
EasyScanner.java

Appendix 2 – Solutions

2.1   Paper 1 – Type question

    • total, num
    • for loop
    • public, class, int, for
  1. It adds the value of num to the variable total
  2. total = 0
  3. 10 numbers
  4. It asks the user to enter ten integer numbers, finds their total and displays it on screen.
  5. public class Total {
    public static void main (String args[]) {
    int total=0, num, i=0;
    while(i < 10) {
    System.out.println(“Enter a number:”);
    num = Keyboard.readInt();
    total+ = num;
    i++;
    }
    System.out.println(“Answer is ” + total);
    }
    }

2.2   Paper 2A – Type question

  1. A class defines a new data type which once defined can be used to create objects of that type.
  2. public class Ellipse {
    double radius1, radius2;double Calc_Area( ) {
    return (Math.PI * radius1 * radius2);
    }
    }
  3. public class Shapes {
    public static void main (String args[]) {Ellipse e1 = new Ellipse();
    Ellipse e2 = new Ellipse();e1.radius1 = 1 + Math.random() * 10;
    e1.radius2 = 1 + Math.random() * 10;
    e2.radius1 = 1 + Math.random() * 10;
    e2.radius2 = 1 + Math.random() * 10;System.out.println(“Ellipse 1 has radii :”+ e1.radius1 +” and ” + e1.radius2);
    System.out.println(“Ellipse 2 has radii :”+ e2.radius1 +” and ” + e2.radius2);System.out.println(“The area of Ellipse 1 is : ” + e1.Calc_Area());
    System.out.println(“The area of Ellipse 2 is : ” + e2.Calc_Area());
    }
    }

2.3   Paper 2B – Type question

  1. A class defines a new data type which once defined can be used to create objects of that type.
  2. public class Square {
    int length;
    void displayArea() {
    System.out.println(“Area of Square : ” + (length*length));
    }
    void displayPerimeter() {
    System.out.println(“Perimiter of Square : ” + (4 * length));
    }
    }
  3. public class Shapes {
    public static void main (String args[]) {
    Square s = new Square();
    s.length = 5;
    s.displayArea();
    s.displayPerimeter();
    }
    }

Download files (see contents of zip file below)

Code shown above

KeyboardClass.java

KeyboardClass.class

EasyScanner.class

Solution 1

Time.java

Solution 2

Numbers.java

Solution 3

Pizza.java

Solution 4

Calc.java

UseCalc.java

Solution 5

BMI.java

Solution 6

Circle.java

useCircle.java