Java Programming Exercises: Calculations and Conversions
Exercise Listing 2.1: Calculate Circle Area
Write a simple application that computes the area of a circle, knowing that π = 3.14159 and radius = 20.
ComputeArea.java
public class ComputeArea {
public static void main(String[] args) {
double radius; // Declare radius
double area; // Declare area
// Assign a radius
radius = 20; // radius is now 20
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Exercise Listing 2.2: Calculate Circle Area with User Input
Write a Java program that calculates the area of the circle where the radius size is to be given by the user and π = 3.14159.
ComputeAreaWithConsoleInput.java
import java.util.Scanner; // Scanner is in the java.util package
public class ComputeAreaWithConsoleInput {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter a radius
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble();
// Compute area
double area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Exercise Listing 2.3: Calculate Average of Three Numbers
Write a Java program that takes 3 numbers as input, calculates their average, and then prints the average on the screen.
ComputeAverage.java
import java.util.Scanner; // Scanner is in the java.util package
public class ComputeAverage {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter three numbers
System.out.print("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display results
System.out.println("The average of " + number1 + " " +
number2 + " " + number3 + " is " + average);
}
}
Exercise Listing 2.4: Convert Seconds to Minutes and Seconds
Write a program that obtains minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.
DisplayTime.java
import java.util.Scanner;
public class DisplayTime {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer for seconds: ");
int seconds = input.nextInt();
int minutes = seconds / 60;
int remainingSeconds = seconds % 60;
System.out.println(seconds + " seconds is " + minutes +
" minutes and " + remainingSeconds + " seconds");
}
}
Exercise Listing 2.5: Convert Fahrenheit to Celsius
Write a program that converts a Fahrenheit degree to Celsius using the formula celsius = (5/9)(fahrenheit – 32).
FahrenheitToCelsius.java
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble();
// Convert Fahrenheit to Celsius
double celsius = (5.0 / 9) * (fahrenheit - 32);
System.out.println("Fahrenheit " + fahrenheit + " is " +
celsius + " in Celsius");
}
}
Programming Exercise 2.4: Convert Pounds to Kilograms
Write a program that converts pounds into kilograms. The program prompts the user to enter a number in pounds, converts it to kilograms, and displays the result. One pound is 0.454 kilograms.
Exercise_02_04.java
import java.util.Scanner;
public class Exercise_02_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final double KILOGRAMS_PER_POUND = 0.454; // Declare constant
System.out.print("Enter a number in pounds: ");
double pounds = input.nextDouble();
double kilograms = pounds * KILOGRAMS_PER_POUND;
System.out.println(pounds + " pounds is " + kilograms + " kilograms");
}
}