September 17 - Java Lab Notes: Constants, Variables, and Basic Arithmetic
Variable Concepts: Constant vs Variable
The instructor begins by asking whether a value is a constant or a variable.
The answer given is that it is a variable (not a constant).
A constant would be declared with the final keyword in Java, making its value immutable after initialization.
Practical takeaway: if you want a value that must not change, declare it with final, e.g., final double PI = 3.14159; (use uppercase naming for constants as a common convention).
Final Keyword and Constants
Final keyword marks a constant in Java; once assigned, its value cannot be reassigned.
If a variable is not declared final, its value can be changed later in the program.
This distinction is important for maintaining invariants and preventing unintended modifications.
Data Types and Declarations
The example uses the double data type for numbers, indicating decimal values are being used.
When starting with a number, you declare variables like double a; double b; (or initialize them directly).
A typical lab task is to declare two numbers and perform arithmetic operations on them.
Java Statements and Semicolons
In Java, every statement ends with a semicolon (;).
The semicolon signals to the compiler that the statement is complete.
Forgetting the semicolon leads to syntax errors; hence, remember the terminator for each statement.
Lab Setup and Help Resources
IDEs: Visual Studio is acceptable for those already comfortable with it; beginners should use Eclipse as guided by the GTAs.
Support roles in the lab:
Grace: GTA for the course
Pranev: lab technologist (system help)
A third GTA assists with C++ but is available to help on the day of the session
Goal: Attempt to finish the introductory assignment today and ask questions freely; the GTAs are available to help with issues.
Working with Two Numbers: Operations
Task focus: given two numbers, compute the following using the appropriate operators:
Sum: the addition of the two numbers
Difference: subtraction between the two numbers
Multiplication: product of the two numbers
Division: quotient of the two numbers
The instructor emphasizes understanding each operation and applying it to the two numbers provided.
Note on terminology: how to denote the difference was clarified as subtraction.
Naming and Organizing Result Variables
For each operation, use a distinct, descriptive variable name to hold the result:
sum (for addition)
difference (for subtraction)
product (for multiplication)
quotient or division (for division)
If you already used a variable name for one operation, you should use a new name for another operation to avoid confusion.
Example approach: reuse the structure but with new result names (e.g., if you use AMB as a placeholder for the operation, you still assign the result to a properly named variable like sum, difference, etc.).
Operator Notation (AMB) and Practical Tips
The instructor mentions using AMB as a placeholder for the operation, indicating you should apply the actual operators (
+, -, *, /) in code.You don’t need to repeat the operator label for each operation if you’re following a consistent pattern; implement each operation once and assign the result to a new variable.
Formulas to Record (LaTeX)
Let the two numbers be represented by a and b.
Sum: s = a + b
Difference: d = a - b
Product: p = a \,\cdot\, b
Division: q = \dfrac{a}{b}
Quick Practical Outline
Declare two numbers as doubles: a, b \
Initialize or assign values to a and b
Compute results:
s = a + b
d = a - b
p = a \cdot b
q = \dfrac{a}{b}$$
Store each result in descriptive variables: sum, difference, product, quotient
Ensure each statement ends with a semicolon
When in doubt, refer to the GTAs for clarifications and seek help during the lab session
Practical and Real-World Relevance
Understanding constants vs variables is fundamental for writing robust code and maintaining invariants in programs.
Basic arithmetic operations mirror everyday calculations and underpin data processing, algorithms, and numeric computations in software.
Clear variable naming improves readability and maintainability, especially when expanding code to include more operations or user input.
Reflective Notes and Tips
Always verify whether a value should be mutable (variable) or immutable (constant) before coding.
Use descriptive names for both inputs and results to minimize confusion when reviewing or debugging code.
Remember the Java syntax rule: every statement ends with a semicolon.
If you’re new to IDEs, start with Eclipse as recommended by the instructors; switch to VS if you’re more comfortable.
Don’t hesitate to ask questions during the lab to prevent solidifying misconceptions and to ensure you’re aligned with the expected approach.