Program Style Console Input and Output This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing
Techniques of Program Style Naming Constants Spelling Conventions Comments Indenting No redundant code (use of methods) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Naming Constants Declare numbers as named constants: Syntax public static final Type Variable = Constant; Examples public static final int INCHES_PER_FOOT = 12; public static final double RATE = 0.14; Naming Convention: Use all uppercase letters, and designate word boundaries with an underscore. Advantages Changing a value in one place if it must be modified preventing a value from being changed inadvertently CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Spelling Conventions Class names start with an upper letter: FirstProgram Variable, Object, and method names start with a lower letter: answer Punctuation is indicated with an underscore for a constant, otherwise an upper letter: INCHES_PER_FOOT, FirstProgram Identifiers are spelled out in full: ArrayIndexOutOfBoundsException Boolean variables should be named with a statement that will be true when the value of the Boolean variable is true: isPositive, pressureOK Methods tend to start with a verb: equals( ), remove( ), add( ) CSS161: Fundamentals of Computing
Comments and Indenting A line comment begins with the symbols // A block comment begins with the symbol pair /*, and ends with the symbol pair */ Javadoc creates an HTML file that documents a given java program with /** */ Indenting shows the level of nested structures. Self-documenting means the clear structure of a given program with no necessity of // coments. CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing More Example /** * Is a CSS161 example program to see javadoc demonstration. * * @author Munehiro Fukuda * @since 01/01/07 * @version 1.0 */ public class Css161Example { * Is the main function. * @param arguments passed to the main function. * @return nothing public static void main( String[] args ) { function( ); // function call } * Repeats printing "Hello!" three times public static void function( ) { System.out.println( "Hello!" ); Javadoc formats these block comments in html. Indenting to show the level of nested structure. A line comment (This example contributes nothing to understanding, though.) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Practice Reformat the following code according to the program style you have learned: public class Circumference { double radius 10.0; public static void main( String[] args ) { double circumference = 2 * 3.141592 * radius; System.out.println( “Circumference of radius 10.0 is “ + circumference ); } CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing System.out.println Syntax System.out.println( Item_1 + Item_2 + … + Last_item ); Items: quoted strings, variables, numbers, or almost any object, which are concatenated together with +. A new line added automatically Examples System.out.println( “Welcome to Java.” ); System.out.println( “Elapsed time = “ + time + “seconds” ); CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing System.out.printf Syntax System.out.printf( “…%X1[.Y1]z1 … %X2[.Y2]z2 …”, primitive_data1, primitive_data2, …); where: X: a digit Y: a digit added with f, e, and g specifier Z: a specifier such as d, f, e, g, s, and c Examples double price = 19.8; String name = “magic apple”; System.out.printf( “$6.2f for each $s.%n”, price, name ); %n: a new line added manually if necessary CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Format Specifiers CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example (1 or 3) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example (2 of 3) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example (3 of 3) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Self-Test Exercises Work on P67’s Self-Test Exercises 5 ~ 7 with your neighboring classmate. CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Scanner Class Convert each keyboard input (delimited by whitespace) into an appropriate data type. Whitespace: blank spaces, tabs, and line breaks Instantiation import java.util.Scanner; Scanner keyboard = new Scanner( System.in ); Major methods Convert an input into a byte, a short, an integer, a long, a float, a double, or a string data type. nextByte( ); nextShort( ); nextInt( ); nextLong( ); nextFloat( ); nextDouble( ); next( ); Get an entire line next( ); Check if the next input can be converted into a byte, a short, an integer, a long a float, a double or a string data type. hasNextByte( ); hasNextShort( ); hasNextInt( ); hasNextLong( ); hasNextFloat( ); hasNextDouble( ); hasNext( ); Check if there is another line to get. hasNextLine( ); CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example (1 of 3) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example (2 of 3) CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Example (3 of 3) CSS161: Fundamentals of Computing
Scanner.useDelimiter( ) Change the delimiter from whitespace to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##"); String word1 = keyboard2.next( ); String word2 = keyboard2.next( ); Inputs abc##xyz Values assigned word1: abc Word2: xyz CSS161: Fundamentals of Computing
Pitfall: Dealing with the Line Terminator, '\n' Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); and the input, 2 Heads are better than 1 head. what are the values of n, s1, and s2? \n \n An empty string “” CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Programming Tips Prompt for Input Scanner keyboard = new Scanner( System.in ); System.out.println( “enter the first name” ); String first = keyboard.next( ); System.out.println( “enter the last name” ); String last = keyboard.next( ); Echo Input System.out.print( “the full name is %s %s %n”, first, last ); CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing Self-Test Exercises Work on P86’s Self-Test Exercises 13 ~ 14. Work on P87’s Self-Test Exercises 15 ~ 16. CSS161: Fundamentals of Computing