160 likes | 184 Views
Learn about variable categories, implicit/explicit parameters, number types, constants, arithmetic operations, and static method calls in Java programming. Master the essentials to enhance your coding skills.
E N D
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham
Week 5 Topics 5.1.1 Categories of Variables 5.1.2 Implicit and Explicit Method Parameters 5.2.1 Number Types 5.2.2 Constants 5.2.3 Assignment, Increment, and Decrement 5.2.4 Arithmetic Operations and Mathematical Functions 5.2.5 Calling Static Methods
5.1.1 Categories of Variables • Instance fields • Local variables • Parameter variables All hold values. Difference is lifetime. Instance field will exist so long as there is a reference to the object it belongs to. Parameter and local variables come to life when method is called, and die after call.
5.1.1 Categories of Variables Cont. public class BankAccount { private double balance; public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } . . . balance: instance field amount: parameter variable newBalance: local variable Example: harrysChecking.deposit(500); Method is called, amount is created and set to 500, newBalance is created and set to balance + amount, balance is set to newBalance. After method call, amount and newBalance dies, balance still lives.
5.1.2 Implicit and Explicit Method Parameters public class BankAccount { private double balance; public void deposit(double amount) { double newBalance = this.balance + amount; this.balance = newBalance; } . . . harrysChecking.deposit(500); The amount parameter is an explicit parameter. An instance field in a class method can be denoted as this.instanceFieldName. this.balance is equivalent for this example to “harrysChecking.balance”, that is to say this refers to the implicit object parameter (harrysChecking object). The this keyword is not required, but is a good programming style.
5.2.1 Number Types Cont. int n = 1000000; System.out.println(n * n); // Overflow double f = 4.35; System.out.println(100 * f); // Rounding error (due to binary to decimal conversion, will print 434.99999…) int dollars = 100; double balance = dollars; // OK double balance = 13.75; int dollars = balance; // Error int dollars = (int) balance;// OK because of cast double d = 5/2;// d is 2double d = 5/2.0; // d is 2.5
5.2.2 Constants • A constant is a numeric value that does not change and is used for computations • final denotes that a variable is a constant • static means that the constant belongs to the class (not to an object like an instance field) • OK to make class constants public since the value cannot be changed • Good style to make a constant all caps Example of a method constant: final double QUARTER_VALUE = 0.25; Example of a class constant: public static final double QUARTER_VALUE = 0.25;
5.2.2 Constants Cont. // Example of using a constant in a computation double coins1 = numQuarters * QUARTER_VALUE; // A public static constant can be used by methods // in other classes using the syntax: // className.constantName double coins1 = numQuarters * Coins.QUARTER_VALUE; double circumference = Math.PI * diameter; In above examples Coins is assumed to be a user-defined class and Math is a Java API class
5.2.3 Assignment, Increment, and Decrement • = is the assignment operator • ++ is the increment operator • myVar = myVar + 1; can be expressed as myVar++; • -- is the decrement operator • ++myVar; is a pre-increment • myVar++; is a post-increment • --myVar; is a pre-decrement • myVar--; is a post-decrement
5.2.3 Assignment, Increment, and Decrement Cont. Assuming that myVar equals 10 at the outset of each example statement below: System.out.println(myVar++); // displays 10 System.out.println(++myVar); // displays 11 System.out.println(myVar--); // displays 10 System.out.println(--myVar); // displays 9 Note that the final value of incremented myVar is 11 and decremented myVar is 9. Make sure to understand the “pre vs. post” distinction when doing an increment or decrement as part of an assignment statement! Example: x = myVar++; // stores 10 in x, NOT 11
5.2.4 Arithmetic Operations and Mathematical Functions A modulus B is the remainder of A divided by B. Example: 5%2 resolves to 1. Use parentheses to change the operator precedence. Example: 4 + 2 * 10 resolves to 24 whereas (4 + 2) * 10 = 60
5.2.4 Arithmetic Operations and Mathematical Functions Cont.
5.2.4 Arithmetic Operations and Mathematical Functions Cont. 5 to the 3rd power can be expressed in Java as: int x = Math.pow(5, 3);// x equals 125 The square root of 25 can be expressed in Java as: double x = Math.sqrt(25);// x equals 5 Reminder from a previous slide (worth repeating), integer division truncates the remainder, so use a floating point literal value or cast to a double to avoid this pitfall: double x = 5/2;// x equals 2
5.2.5 Calling Static Methods • The Math class methods per the previous section are examples of calling static methods • A static method call uses the class name rather than an object name • Format is: ClassName.methodName(parameters) • Example Math class static method definition: public static double sqrt(double a) • Classes with static methods only, such as the Java API Math class, are not used to create objects. Think of such classes as general purpose utility classes.
Reference: Big Java 2nd Edition by Cay Horstmann 5.1.1 Categories of Variables (section 3.7 in Big Java) 5.1.2 Implicit and Explicit Method Parameters (section 3.8 in Big Java) 5.2.1 Number Types (section 4.1 in Big Java) 5.2.2 Constants (section 4.2 in Big Java) 5.2.3 Assignment, Increment, and Decrement (section 4.3 in Big Java) 5.2.4 Arithmetic Operations and Mathematical Functions(section 4.4 in Big Java) 5.2.5 Calling Static Methods (section 4.5 in Big Java)