Arithmetic and IO 25 August 2017
Submissions you can submit multiple times to the homework dropbox file name: uppercase first letter, Yourlastname0829.java the system will use the last submission before the deadline mail size to confirm a file was attached
Variables variables must be declared before they can be used unlike Ada, variables in Java are not declared all at the top of a block they are declared inside the code, as late as possible, immediately where they are needed variable identifiers must begin with a lowercase letter, and use camelcase a variable may be initialized when it s declared, or not, depending on need (do not initialize when unnecessary) a constant is like a variable, except a constant must be initialized when it is declared its value can never change it is denoted by the reserved word final its identifier is in SNAKE CASE
Primitive Conversion and Casting you can convert amongst types much more easily than in Ada but much more difficult than in Python some conversions are automatic, some require a typecast lossless conversions are automatic double conversion factor = 12.34F; long accululator = 12; integer to floating is automatic, even if lossy float circumference = 9123456789123456789L; // ugh!
Primitive Conversion and Casting all other conversions either require a typecast or are illegal int quotient = 10.0 / 3.0; // illegal int quotient = (int)(10.0 / 3.0); // ok no conversion or cast is legal to or from boolean
Arithmetic the biggest deal is keeping integer and floating point arithmetic straight if all operands are integers, the result is an integer if one or two operands are floating (float or double), the result is floating there is only one division operator, / if both operands are integers, the operation is (truncating) integer division if either operand is floating, the result is floating
Increment and Decrement the most common operations in all of computing are increment-by-one and decrement-by-one Java has pre- and post-increment and -decrement operators int x = 5; x++; double d = 10.2; ++d;
Increment and Decrement what are the values of x, y, and z after this code? int x = 5; x--; int y = x--; int z = --x;
Increment and Decrement what are the values of x, y, and z after this code? int x = 5; x--; int y = x--; int z = --x; x is 2 y is 4 z is 2
Retrieve - Modify - Store the second-most common operation Awkward Newbie x = x + 5; Correct Professional Way x += 5; works for all binary arithmetic operators special trap for Ada folks: x /= 2;
Integer Division and Remainder 7/3 =
Integer Division and Remainder 7/3 = 2 7/ 3 =
Integer Division and Remainder 7/3 = 2 7/ 3 = 2 7 % 3 =
Integer Division and Remainder 7/3 = 2 7/ 3 = 2 7 % 3 = 1 7 % 3 =
Integer Division and Remainder 7/3 = 2 7/ 3 = 2 7 % 3 = 1 7 % 3 = the behavior of % is very confusing if either operand is negative different languages define it differently Java: 7 % 3 = 1 Python: 7 % 3 = 2 Ada: two different operators, mod and rem don t try to memorize know what you want, test to make sure it works
Math Computations beyond simple 4-function arithmetic, Java has the Math library it is built-in, so you don t need to import it a few examples: Math.sqrt(x) returns the square root of x Math.exp(x) returns the value e x
Console Input and Output in CS260, you will write graphical programs in CS181, all our programs use text for input and output all input will come from keyboard file all output will go to screen file
Output output is relatively simple System. }{{}}{{} out class object. print }{{} method.( }{{} foo ); argument the argument can be a primitive or object value the argument can be literal, variable, constant, or expression the print method does not add a newline the println method adds a newline at the end
Simple Formatting for spacing, tabular output, and floating values, we often want to format the output Java uses the C-style printf method with a format specifier and a list of values System.out.println("2.0 / 3.0 is: " + 2.0 / 3.0); 2.0 / 3.0 is: 0.6666666666666666 System.out.printf("2.0 / 3.0 is: %1.3f\n", 2.0 / 3.0); 2.0 / 3.0 is: 0.667
Multiple Values System.out.printf("My name is %s and I'm %d years old.\n", name, age); My name is Fred and I'm 12 years old.
Input input is a bit more complicated Java was invented to be a language for graphical programs the assumption was that most input would come from graphical widgets like this: Java has a pre-defined input object named in but it s awkward to use
Input instead, for text input, we use a helper at the top of the file we do Library Import import java.util.scanner; and then in the code we do Create Scanner Object Scanner in = new Scanner(System.in); and finally use the scanner object like this Using Scanner int age = in.nextint();
Example discuss Volume2.java from text section 2.3.2