Martin T. Press
Main Method and Class Name Printing To Screen Scanner
The Return Type : always void (means returns nothing) The parameters Of the method: The Main Method Away takes a list of type String The main method is the starting point of execution of the Java program. Without a main Method a Class is not runnable by itself. A Java project must have at least one class with a main. Most of the first few labs will have one class. Remember always include public static at the beginning of the method. If this is left out the complier will not recognize it as the “main”.
File Name Sum.java The Public Key word tells the complier that all classes can see it ! I Class Name Sum Every.java file written must have a class name. It must be the same name as the.java file. Failure to name it the same as the file name will cause a compile error.
The System.out.print() and System.out.println() will be the only visual output for the first few labs. It displays text that is inside the ( ). The + operator when used with strings pushes them together. Such that “Hello ”+5 is the same as the String “Hello 5”. This is useful when printing!
Scanner Is the object that allows programs to get Input data from the user on the command line. The two methods we will use in this lab are nextDouble() and nextInt() This is the import statement which tells the complier where the Scanner object is located in the java library. You must always include this when scanner is used. This is a constructor for the Scanner (creates the object). It always takes System.in as a parameter for reading the command line. myScanner.nextDouble() Returns the next double entered by the user(Waits till user hits enter).
This is the output from the previous example of code. Notice that even if an integer is entered the output is still a double for the double. Try it the other way around what happens?
Looks like an error occurred. Last time it worked because of type casting. A good rule of thumb is if the scanner is expecting a type to always input the same type.