A Level Java Worksheet 2
A Level Java Worksheet 2
Java Worksheet 2
Basic Data Types
1. Write a Java program to declare two integer variables, one float variable and one string
variable and assign 10, 12.5 and “Java Programming” to them respectively. Then display
their values on the screen.
double y = 12.5;
System.out.println(y);
import java.util.Scanner;
public class two
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
double FD;
double SD;
System.out.println("input a Fahrenheit degree");
FD = sc.nextDouble();
SD = (FD - 32)*5/9;
System.out.println("Celsius degree is" + SD);
}
}
import java.util.Scanner;
public class three
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
double Meters;
double inches;
System.out.println("input the inches");
inches = sc.nextDouble();
Meters = inches/39.3700783;
1
A-Level Java
4. Write a Java program that reads an integer between 0 and 1000 and adds all the digits in the
integer.
import java.util.Scanner;
public class Four
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
int number;
System.out.println("please input number between 1 - 1000");
number = sc.nextInt();
int digit3;
int remainder3;
int digit2;
int remainder2;
int ans;
remainder3 = number%100;
digit3 = (number-remainder3)/100;
remainder2 = remainder3%10;
digit2 = (remainder3-remainder2)/10;
5. Write a Java program to convert minutes into a number of years and days.
import java.util.Scanner;
System.out.println(minutes + " minutes is " + years + " years and " + days
+ " days.");
input.close();
2
A-Level Java
}
}
import java.util.Scanner;
System.out.println(minutes + " minutes is " + years + " years and " + days
+ " days.");
input.close();
}
}
import java.util.Scanner;
8. Write a Java program that accepts two integers from the user and then prints the sum, the
difference, the product, the average, the distance (the difference between the two integers),
the maximum (the larger of the two integers) and the minimum (smaller of the
two integers).
3
A-Level Java
import java.util.Scanner;
10. Write a Java program to takes the user for a distance (in meters) and the time taken (as
three numbers: hours, minutes, seconds), and display the speed, in meters per second,
kilometers per hour and miles per hour (hint: 1 mile = 1609 meters).
import java.util.Scanner;
4
A-Level Java
System.out.print("Minutes: ");
System.out.print("Seconds: ");
scanner.close();