Java ExA1
Errajae Abdellah
Exercise #1 — User Input + Output (Beginner)
Concepts:
• Using Scanner to accept user input
• Printing results using [Link]()
Task:
Write a Java program that:
1. Ask the user for their name and age.
2. Prints a message like:
3. Hello [name]! You are [age] years old.
4. If the age is 18 or older, print “You’re an adult.”
Otherwise, print “You’re still a minor.”
Example Output:
Enter your name: Alice
Enter your age: 17
Hello Alice! You are 17 years old.
You're still a minor.
Exercise #2 — Arithmetic + User Input + Conditions
Concepts:
• User input (Scanner)
• Arithmetic operations (+, -, *, /)
• If statements (decision making)
Task:
Write a Java program that acts as a Simple Grade Calculator.
1. Ask the user to enter three test scores (e.g., between 0 and 100).
2. Calculate the average score.
3. Print the average like this:
4. Your average score is: 82.3
5. Then use an if-else statement to print the grade:
o 90–100 → A
o 80–89 → B
o 70–79 → C
o 60–69 → D
o Below 60 → F
Example Output:
Enter score #1: 80
Enter score #2: 90
Enter score #3: 75
Your average score is: 81.7
You got a B!
Exercise #3 — Loops + User Input + Arithmetic +
Conditions
Concepts introduced:
• while or do-while loop
• Continue running the program until the user chooses to stop
• Reuse previous skills: input, arithmetic, conditionals
Task: "Repeatable Grade Calculator"
You’ll upgrade your last program so that it:
1. Asks the user for three test scores.
2. Calculates and displays the average and letter grade (same logic as before).
3. Then asks:
4. Do you want to calculate another average? (yes/no):
5. If the user types "yes", repeat the process.
If "no", say goodbye and end the program.
Example Output:
Enter score #1: 90
Enter score #2: 80
Enter score #3: 85
Your average score is: 85.0
You got a B!
Do you want to calculate another average? (yes/no): yes
Enter score #1: 100
Enter score #2: 95
Enter score #3: 92
Your average score is: 95.7
You got an A!
Do you want to calculate another average? (yes/no): no
Goodbye!
Exercise #4 — Methods + Loops + User Input + Conditions
Concepts introduced:
• Methods (functions) to avoid repeating code
• Passing arguments to methods
• Returning values from methods
Task: “Grade Calculator with Methods”
1. Create a method called calculateAverage that:
o Accepts three scores as parameters
o Returns the average
2. Create a method called getLetterGrade that:
o Accepts the average
o Returns the corresponding letter grade as a String
3. In main():
o Ask the user for three scores
o Call calculateAverage()
o Call getLetterGrade()
o Print the results
o Ask if the user wants to calculate again (looping like before)
Exercise #5 — Arrays + Loops + Methods + User Input
Concepts introduced
• Arrays: store multiple values of the same type
• For loops: iterate over arrays
• Reuse methods we already created (calculateAverage and getLetterGrade)
Task: “Class Grade Calculator”
1. Ask the user how many students they want to enter.
2. For each student:
o Ask for three test scores
o Store the average in an array
o Store the letter grade in another array
3. After all students’ scores are entered, print a summary table:
Student 1: Average = 85.0, Grade = B
Student 2: Average = 92.3, Grade = A
...
Hints
• Use two arrays:
double[] averages = new double[numberOfStudents];
String[] grades = new String[numberOfStudents];
• Use a for loop to iterate over each student and store the values:
for(int i = 0; i < numberOfStudents; i++) {
// get scores
// calculate average
// calculate grade
// store in arrays
• Then use another for loop to print the summary table.