This exercise continues with writing static methods, as discussed in the warm-up exercises. In addition, it introduces another method for Java programs to take input data.
Write a program IsTriangular containing a static method isTri(), with the following signature:
public static boolean isTri(double a, double b, double c)
The function isTri() should return the value true if the lengths a, b and c could represent the lengths of a triangle; i.e., no single length is greater than or equal to the sum of the others. (You need not check that the lengths are all \( \geq 0 \).)
Next, test that your program gives correct output by adding a main() method to the class IsTriangular. However, this time we will not try to get the input by parsing command-line arguments; the reason is that you should be using IntelliJ by now, and as you may have noticed, setting execution arguments in IntelliJ is rather laborious. Instead, we will use a class from the Java library called Scanner which can be found in the package java.util. This class allows us to parse three double values which can be entered in the IntelliJ console (or the shell if you are running the program from the command-line).
Here is some example code that shows how to define the body of main() to do this:
import java.util.Scanner; public class IsTriangular { // TODO implement function isTri here public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); double a = stdIn.nextDouble(); double b = stdIn.nextDouble(); double c = stdIn.nextDouble(); if (isTri(a, b, c)) { System.out.printf("%s, %s and %s could be the lengths of a triangle\n", a, b, c); } else { System.out.println("Not a triangle."); } stdIn.close(); } }
Note that we do not use the args parameter of main() to get the input. Instead, the values that you type at the console will be available to your code via the class Scanner. You can then call this class’s method nextDouble() to parse the values into doubles and set the variables a, b, c.
Your program will stop at the first call to nextDouble() and wait for your input. Select the console in IntelliJ and type a number. When typing at the console, you can either separate the values by whitespace or by hitting <return> after each one. After you have entered three values, the program will carry on executing the next lines in the program.
In order to use the Scanner class, you will have to import it from the Java API using the corresponding import statement.
Scanner is generally able to translate text into other data types such as integers or doubles. It takes the text from an input stream of data which can have different forms. In order to read user input from the console, the input stream needs to be the operating system's standard input stream which is represented in Java by the static member System.in. Note the similarity to your operating system's standard output stream System.out which you use to write to the console. Please have a look at this article for more detailed information about using the standard streams in Java.
It is good coding practice to close data streams after you no longer need them. Therefore the .close() method is called at the end of the program. Be careful not to close the input stream if you still need it later or your program will crash.
The Scanner class can be used for more than just reading doubles from the command line. It is also possible to read in other data values, parse Strings or read in files. You can check out the corresponding Java API or a suitable internet tutorial on how to do that.
An automated test has been created for this exercise: IsTriangularTest.java.