CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we now now that this argument is an array of Strings allows information to be passed into the application 2 Constants A constant is a variable that holds the same value during its entire existence the constant must be initialized with that value when it is declared The compiler will issue an error if you try to change the value of a constant In Java, we use the final modifier to declare a constant EX: final int MIN_HEIGHT = 100; 3 Why Use Constants? Constants give meaning to otherwise unclear literal values ( magic numbers ) For example, MIN_HEIGHT conveys more meaning than the literal 100 Constants facilitate program maintenance If a constant value changes and is used in multiple places, its value need only be updated in one place Constants tell the compiler that a value should not change, avoiding inadvertent errors by other programmers 4 3 4
Instance Variable Constants Sometimes an instance variable is a constant For example, every array object has an instance variable length that holds the number of elements in the array length is declared with final, since the size of an array can t change once it is created It s OK to make constant instance variables public because they re constants external users can t change them 5 5 Instance Variable Access Instance variables can be accessed using the dot operator syntax: objname.varname EX: if we have an array named grades, the number of elements in grades is given by grades.length 6 6 EX: Array Output Loop EX: output all of the elements in an array nums for (int i = 0; i < nums.length; i++) Output.showMessage( i + : + nums[i]); note that we don t need to know ahead of time how many elements are in nums 7 Simple Output: The println Method The println method can be used to produce text output System.out.println ("Whatever you are, be a good one."); object method name single String parameter The System.out object represents a destination (the monitor screen) to which we can send output The System.out object is automatically created for your program 8
Outputting Values We can use String concatenation and the println method to output values System.out.println (num1 + plus + num2 + is + sum); 9 Text Input The Scanner class provides convenient methods for reading input values of various types A Scanner object can be set up to read input from various sources, including the user typing values on the keyboard Keyboard input is represented by the System.in object 10 Creating a Scanner Object The Scanner class is part of the java.util package, and therefore must be imported by your programs The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); 11 Reading Keyboard Input Once created, the Scanner object can be used to invoke various input methods, such as: String answer = scan.nextline(); The nextline method reads all of the input until the end of the line is found The Scanner class provides methods for inputting integers, doubles, etc. from the keyboard 12
Often we perform an operation on a variable, and then store the result back into that variable Java provides assignment operators to simplify that process For example, the statement is equivalent to num += count; num = num + count; 13 There are many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y 14 The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-min) % num; is equivalent to result = result / ((total-min) % num); 15 The behavior of some assignment operators depends on the types of the operands If the operands to the += operator are strings, the assignment operator performs string concatenation The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding operator (+) 16
Lecture 25 Vocabulary constant magic numbers final modifier assignment operators 17