Task 1:
Integer Distance
In this question, you will write a program that reads two integers as command-line arguments and calculates the distance between them. We will define this distance in the standard arithmetic manner, namely as the difference between the larger and the smaller of the two numbers.
For example:
You will calculate this distance in two ways:
Using the methods Math.min() and Math.max().
Using the methods Math.abs().
Using Math.min() and Math.max()
You will make a class, this time called Distance1. You will also need to create a main() method, i.e. with the declaration:
public static void main( String[] args ){...}
As previously, we will need to read two integers from the command line, using the method Integer.parseInt(), and then store the values in variables.
In order to calculate the larger and smaller of the two values, the Java Math library provides the methods Math.max(int a, int b) and Math.min( int a, int b). For example Math.max(1, 4) would return 4, whilst Math.min(12, 45) would return 12.
Your program should print out the result of subtracting the smaller number from the larger one.
Using Math.abs()
An alternative way to calculate this distance is to subtract the first number, whatever it is, from the second number. We can then see if this result is positive or negative.
if it is positive, we use that number as the distance
if it is negative, we invert the sign so it is positive and use that number as the distance.
Java provides a method for performing this calculation, namely Math.abs() (absolute value).
Write a class, Distance2, which use the Math.abs() method instead of the methods Math.min(), Math.max() to calculate the distance between the two command line arguments.
Task 2:
In this question, you will write a program that reads two floating-point numbers from the command line and returns the larger of the two. By default, a floating point number in Java is of type double, and this is the type we will use here.
When we took in arguments of type int from the command line in previously, we had to convert them from strings using Integer.parseInt(). Analogously, we will use the method Double.parseDouble() to convert a String into double .
Write your program to perform the required functionality. Your class should be called LargestDouble. Your session at the terminal should something like this:
c:\oop\week2>javac LargestDouble.java
c:\oop\week2>java LargestDouble 34.3 23.2 34.3
c:\oop\week2>java LargestDouble 45.1 45.2 45.2
Note To print out a double, you can still the use the function System.out.printf()
Note You program may display many more numbers past the decimal point in the output limit them to two decimal places.
Task 3:
Quadratic Equations
we will write a program that solves quadratic equations, a type of equation commonly seen in science and engineering.
Quadratic equations are equations of the form:
Ax2+Bx+C=0
where we are given values for A, B, C and want to solve for x.
We can rearrange and solve for x using the following formula:
x=−B±B2−4AC−−−−−−−−−√2A
The ± operator tells us that we should expect two solutions, the first when we use a plus, and the second when we use a minus.
Warning The solution of quadratic equations can include imaginary numbers. For this exercise, we are only interested in equations with real solutions. If you try to make up random inputs for A, B, C, then you will probably not create equations with real solutions, and your program will most likely crash. The values of A, B, C that will lead to a real solution will satisfy the inequation B2−4AC≥0.
Write a program QuadraticSolver to read three floating point numbers from the command line, representing the values of A, B, C, and then use the values of these arguments to solve for x. Your program should print the two solutions for x on separate lines. Eg:
c:\oop\week2>java QuadraticSolver 1.0 0.0 -1.0
1.0
-1.0
c:\oop\week2>java QuadraticSolver 1.0 -3.0 2.0
2.0
1.0
c:\oop\week2>java QuadraticSolver 1.0 4.0 3.0
-1.0
-3.0
c:\oop\week2>java QuadraticSolver 1.0 10.0 0.0
0.0
-10.0
Note To calculate square roots, you can use the method Math.sqrt().