Objective:
- Understand the concept and importance of Arithmetic Operators and Expressions in Java development.
- Learn how to implement Arithmetic Operators and Expressions using Java's basic syntax.
- Explore practical applications of Arithmetic Operators and Expressions in simple calculations.
- Identify common pitfalls and best practices when working with Arithmetic Operators and Expressions.
- Gain hands-on experience with a complete Java example that demonstrates Arithmetic Operators and Expressions.
Prerequisites:
- Basic understanding of Java programming.
- Familiarity with variables and data types in Java.
- Basic knowledge of how to run a Java program.
What You'll Achieve:
- Develop a solid understanding of Arithmetic Operators and Expressions in Java.
- Implement practical examples that can be applied in real-world scenarios.
- Enhance your skills in basic Java programming and mathematical operations.
Assignment Details
In this assignment, you will create a simple Java program that demonstrates the use of various arithmetic operators and expressions. Follow these steps:
- Create a new Java file named
ArithmeticOperatorsDemo.java. - In the
mainmethod, declare and initialize the following variables:int a = 10;int b = 5;double c = 7.5;
- Perform the following operations and store the results in appropriately named variables:
- Add
aandb - Subtract
bfroma - Multiply
aandb - Divide
abyb - Calculate the remainder when
ais divided byb - Add
aandc - Divide
abyc
- Add
- Create a more complex expression that uses multiple operators and parentheses. For example:
(a + b) * (c - b) / 2 - Use the increment (
++) and decrement (--) operators onaandb. - Print all the results to the console with appropriate labels.
Example Output
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Remainder: 0
Adding int and double: 17.5
Dividing int by double: 1.3333333333333333
Complex expression result: 31.25
a after increment: 11
b after decrement: 4
Starter Code
The ArithmeticOperatorsDemo.java file contains the following starter code:
package academy.javapro.lab;
public class ArithmeticOperatorsDemo {
public static void main(String[] args) {
int a = 10;
int b = 5;
double c = 7.5;
// TODO: Perform arithmetic operations and print results
// TODO: Create and evaluate a complex expression
// TODO: Use increment and decrement operators
}
}Hints
- Remember that when dividing two integers, the result will be an integer (truncated).
- When mixing integer and double operands, the result will be a double.
- Be careful with the order of operations. Use parentheses when in doubt.
- The increment and decrement operators can be used both as prefix (++a) and postfix (a++). They behave differently in expressions.
- When printing results, use descriptive labels to make your output clear and readable.
Submission Instructions
- Fork the repository
- Clone your fork
- Navigate into the repository
- Implement the required methods in both
ArithmeticOperatorsDemoclasses - Test your implementation with various inputs
- Git add, commit, and push to your fork
- Submit a pull request
- Set the title of the pull request to your first name and last name
- In the comment, briefly explain your implementation approach and any challenges you faced
Remember, the goal is to learn and have fun! Don't hesitate to ask for help if you get stuck.