Basic Operations jgrasp debugger Writing Programs & Checkstyle Suppose you wanted to write a computer game to play "Rock, Paper, Scissors". How many combinations are there? Is there a tricky way to represent the options to make figuring out the result easier? 67
Primitive Data Types boolean stores true (1) or false (0) values byte holds integer in one byte = 8 bits char holds a single character short, int, long hold integers of varying sizes float, double hold real numbers (floating point) Variables have 4 features: name, type, location in memory and value (once assigned) 68
Integer Representation literal values are whole numbers: 8, 23, -102 base ten values are converted to binary left-most bit is for the sign 0 means positive 1 means negative each type (byte, short, int, long) has different size in memory int is used most commonly 69
Character Representation char literals must be in single quotes: 'A', '4', '+' each character is assigned an integer code full set is Unicode System extension of original ASCII system decimal integer codes are converted to binary 'A' to 'Z' have consecutive codes (65-90) 'a' to 'z' have consecutive codes (97-122) '0' to '9' have consecutive codes (48-57) Section 3.2 in textbook 70
Real Number Representation float literals must have f: 10f, 23.342F, -102.3f double literals (more common) can be floating: 10.3, -100.23,.023345 or scientific notation: 1.03e1, -1.0023e2,.23345e-1 stored internally in two parts: 123.45 =.12345e3 12345 is mantissa converted to binary 3 is exponent converted to binary double has twice as many mantissa bits as a float, so you get more precision 71
Arithmetic Operations addition: val1 + val2 subtraction: val1 val2 multiplication: val1 * val2 division result depends on operand types: int1 / int2 gives a whole number result (div) if float or double as one operand, you get a real number result division modulus for integers: val1 % val2 gives whole number remainder after val1 / val2 72
Evaluating Expressions The data type of each binary operation result depends on the data type of the operands The "lower" number type operand is implicitly converted to the "higher" number type operand The result is the same type as the "higher" For example: int + int => int int float => float double / int => double Operator precedence (next slide) determines the order of the operations in a mixed expression 73
Operators have precedence () do inside parentheses first - negation, casting: (int), (double), (char) etc. * multiplication, / division, % mod (remainders) + addition, - subtraction = assignment Section 2.9 in textbook 74
Misc. data items and operator combinations escape sequences for special characters: \" \\ \n \t final for named constants assignment combinations: +=, -=, *=, /=, %= ++, -- in prefix or postfix mode 75
Arithmetic Activity Look at code/arithmetic.java for examples of type conversions and uses of arithmetic operators. Download the file. Use the jgrasp debugger to step through and annotate each line of code (next to the //) with the values that are being assigned. Submit your annotated arithmetic.java file on Blackboard in Classwork Submission as c02. 76
Writing Java code [Make sure your pseudocode is handy!] Start with outer structure: class, main, other methods? Declare some variables Get input, display (incorrect) output Fill in the missing pieces, one task at a time. Compile, [checkstyle] and run at each step. 77
Java program template import java.util.scanner; /** Documentation. */ public class ClassName { /** Documentation. * @param args (not used) */ public static void main(string[] args) { // executable statements } } // other method definitions can go here or above main 78
Recall: Executable statements Variable declarations Assignment statements Method calls Decision statements Loops 79
Variable Declarations For primitives and strings: type identifier; // no initialization type ident = value; // with initialization type ident1, ident2, ident3; // all same type type ident1 = value1, ident2, ident3 = value3; // ident2 was not initialized For class types (objects): ClassName varname; // no initialization ClassName varname = new ClassName(params); 80
Assignment statements A single '=' symbol is used We pronounce it "gets" Store value in varname: varname = value; Evaluate expression and store result in varname: varname = expression; Expressions can be any valid combination of operators, variables, literal values and method calls 81
Method calls Can vary depending on where and how the method is defined. If static and defined in same class: methodname(params) If static and defined in another class: ClassName.methodName(params) Math.round(value) If belongs to a class and requires an object: objectvar.methodname(params) System.out.println("hello"); mystring.charat(0) 82
Checkstyle Tool to ensure that our code conforms to a standard style (similar to the Java API code) Specific style for this course is in file http://cs.jhu.edu/~joanne/cs107/check107.xml See notes on homepage and hw2 to download, configure and use with jgrasp Advantage is it will help you learn to be better programmers and make debugging easier! 83
Checkstyle Details how to install & configure running on arithmetic.java common errors missing javadoc comment (must start with /**) file/classname must start with Capital letter { must be on same line as the block control statement indentation use jgrasp control structure diagram to fix magic numbers use final to fix read documentation linked from main course page 84
Homework Review Solutions for #1 Homework 2 start the formulas 85