JetBrains Academy: Floating point types - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Distance:
Write a program that reads the distance between the two cities in miles and the travel time by bus in hours, and outputs the average speed of the bus.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double distance = scanner.nextDouble();
double travelTime = scanner.nextDouble();
double avgSpeed = distance / travelTime;
System.out.println(avgSpeed);
}
}
Evaluate an expression - 1:
Write a program which reads a double value xx and evaluates the result of x3 + x2 + x + 1
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x = scanner.nextDouble();
double result = Math.pow(x, 3) + Math.pow(x, 2) + x + 1;
System.out.println(result);
}
}
Celsius to Fahrenheit:
Write a program that reads a temperature in Celsius (°C) and shows its equivalent in Fahrenheit temperature (°F).
Use the following formula:
F = C * 1.8 + 32
Where C is a temperature in Celsius, F - is a temperature in Fahrenheit.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double celsius = scanner.nextDouble();
double fahrnheit = celsius * 1.8 + 32;
System.out.println(fahrnheit);
}
}
Difference between two doubles:
Write a program that read two double values and prints the difference between the second and the first one.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double first = scanner.nextDouble();
double second = scanner.nextDouble();
double difference = second - first;
System.out.println(difference);
}
}
Find X:
Write a program that reads three double values a, b, c, and then solving the simplest equation:
a * x + b = c
The program should output the value of x.
It's guaranteed, a is not 0.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double x = c / a - b / a;
System.out.println(x);
}
}
Evaluate an expression - 2:
Write a program that reads four double values a, b, c, d and then evaluates the following expression:
a * 10.5 + b * 4.4 + (c + d) / 2.2
The program should output the result.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
double b = scanner.nextDouble();
double c = scanner.nextDouble();
double d = scanner.nextDouble();
double result = a * 10.5 + b * 4.4 + (c + d) / 2.2;
System.out.println(result);
}
}
Area of a circle:
Given the radius of the circle, you need to find an area of that circle. Use this formula:
S = π R2
Where S - an area of a circle, π - Math.PI
constant, R - radius of the circle.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double radius = scanner.nextDouble();
double area = Math.PI * Math.pow(radius, 2);
System.out.println(area);
}
}
Liquid pressure:
Write a program that reads the density of a liquid and the height of a column, and calculates the liquid pressure.
- We suppose the gravity is 9.8 m/s² (Earth).
- Do not format the result.
Some explanations:
When a person swims under the water, water pressure is felt acting on the person's eardrums. The deeper that person swims, the greater the pressure. The pressure felt is due to the weight of the water above the person. As someone swims deeper, there is more water above the person and therefore greater pressure. The pressure a liquid exerts depends on its depth.
Liquid pressure also depends on the density of the liquid. If someone was submerged in a liquid more dense than water, the pressure would be correspondingly greater. Thus we can say that the depth, density and liquid pressure are directly proportionate. The pressure due to a liquid in liquid columns of constant density or at a depth within a substance is represented by the following formula:
p = ρgh
where:
p is liquid pressure,
g is gravity at the surface of overlaying material,
ρ is density of liquid,
h is height of liquid column.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double liquidDensity = scanner.nextDouble();
double columnHeight = scanner.nextDouble();
double gravity = 9.8;
double liquidPressure = liquidDensity * gravity * columnHeight;
System.out.println(liquidPressure);
}
}