Resources for the textbook An Introduction to JAVA Programming
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
2.2 Paper 2A – Type question
- A class defines a new data type which once defined can be used to create objects of that type.
- public class Ellipse { double radius1, radius2;double Calc_Area( ) { return (Math.PI * radius1 * radius2); } }
- 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
- A class defines a new data type which once defined can be used to create objects of that type.
- 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)); } }
- 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