There are two different types of numbers in Java:
-6
, 0
, 1
, 25
, 976
and -500000
.-20.4
, 0.1
, 2.72
, 16.984025
and 1024.0
.The two most common numeric types in Java are int
and double
.
An int
is a 32-bit integer and a double
is a 64-bit floating-point number.
Arithmetic is done using the standard arithmetic operators.
Numbers can be compared using the standard numeric comparison operators (eg. 5 > 4
and 4 <= 5
) and the equality (==
) and inequality (!=
) operators.
Java has two types of numeric conversions:
As an int
has less precision than a double
, converting from an int
to a double
is safe and is thus an implicit conversion.
However, converting from a double
to an int
could mean losing data, so that requires an explicit conversion.
The most basic control flow statement in Java is the if-then statement.
This statement is used to only execute a section of code if a particular condition is true
.
An if-then statement is defined using the if
clause:
class Car {
void drive() {
// the "if" clause: the car needs to have fuel left to drive
if (fuel > 0) {
// the "then" clause: the car drives, consuming fuel
fuel--;
}
}
}
In the above example, if the car is out of fuel, calling the Car.drive
method will do nothing.
The if-then-else statement provides an alternative path of execution for when the condition in the if
clause evaluates to false
.
This alternative path of execution follows an if
clause and is defined using the else
clause:
class Car {
void drive() {
if (fuel > 0) {
fuel--;
} else {
stop();
}
}
}
In the above example, if the car is out of fuel, calling the Car.drive
method will call another method to stop the car.
The if-then-else statement also supports multiple conditions by using the else if
clause:
class Car {
void drive() {
if (fuel > 5) {
fuel--;
} else if (fuel > 0) {
turnOnFuelLight();
fuel--;
} else {
stop();
}
}
}
In the above example, driving the car when the fuel is less then or equal to 5
will drive the car, but it will turn on the fuel light.
When the fuel reaches 0
, the car will stop driving.
In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from 0
(off) to 10
(maximum).
At its lowest speed (1
), 221
cars are produced each hour. The production increases linearly with the speed. So with the speed set to 4
, it should produce 4 * 221 = 884
cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:
1
to 4
: 100% success rate.5
to 8
: 90% success rate.9
: 80% success rate.10
: 77% success rate.You have two tasks.
Implement the CarsAssemble.productionRatePerHour()
method to calculate the assembly line's production rate per hour, taking into account its current assembly line's speed :
CarsAssemble.productionRatePerHour(6)
// => 1193.4
Note that the value returned is a double
.
Implement the CarsAssemble.workingItemsPerMinute()
method to calculate how many working cars are produced per minute:
CarsAssemble.workingItemsPerMinute(6)
// => 19
Note that the value returned is an int
.
Sign up to Exercism to learn and master Java with 25 concepts, 148 exercises, and real human mentoring, all for free.