Back to Week 1
Lab Session

Week 1: Lab — Java Basics

Hands-on Programming Exercises — Ch. 1-5

Progress0 / 9 completed

1Even or Odd

Read an integer and check if it is even or odd using the modulus operator (%).
Expected OutputEnter a number: 7 7 is odd
Your Code
A number is even if number % 2 == 0. Otherwise it's odd. Use nextInt() to read an integer.

2Multiplication Table

Read a number and print its multiplication table (1 through 10) using a for loop.
Expected OutputEnter a number: 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50
Your Code
Use for (int i = 1; i <= 10; i++) and print num + " x " + i + " = " + (num * i) inside the loop.

3Sum of N Numbers

Read a positive integer N and use a while loop to calculate the sum of all integers from 1 to N.
Expected OutputEnter N: 100 Sum from 1 to 100 = 5050
Your Code
Initialize sum = 0 and i = 1. While i <= n, add i to sum and increment i.

4Method: Max of Three

Write a method called maxOfThree that takes 3 integers as parameters and returns the maximum. Call it from main.
Expected OutputEnter three numbers: 15 42 8 The maximum is: 42
Your Code
Define the method outside main but inside the class. Use if statements or Math.max() to find the largest. Don't forget return!

5Method: Is Prime

Write a method called isPrime that takes an integer and returns true if it's prime, false otherwise. Test it by reading a number from the user.
Expected OutputEnter a number: 17 17 is a prime number
Your Code
A prime number is only divisible by 1 and itself. Loop from 2 to n/2 (or Math.sqrt(n)) and check if n % i == 0. If any divides evenly, it's not prime. Return boolean.

6Method: Factorial

Write a method called factorial that takes an integer n and returns n! (n factorial). Print the result from main.
Example: 5! = 5 × 4 × 3 × 2 × 1 = 120
Expected OutputEnter a number: 5 5! = 120
Your Code
Use a for loop from 1 to n, multiplying a running result. Use long as the return type because factorials get large fast! long result = 1; for (int i = 1; i <= n; i++) result *= i;

7Method: Power Calculator

Write a method called power that takes a base and an exponent (both integers) and returns baseexponent without using Math.pow(). Use a loop.
Expected OutputEnter base: 2 Enter exponent: 10 2^10 = 1024
Your Code
Initialize result = 1. Loop exp times, multiplying result *= base each iteration. Don't use Math.pow() — the goal is to practice loops inside methods!

8Number Pattern

Read a number of rows and print a right triangle of stars using nested for loops.
For rows = 5:
*
**
***
****
*****
Expected OutputEnter number of rows: 5 * ** *** **** *****
Your Code
Outer loop: for (int i = 1; i <= rows; i++). Inner loop: for (int j = 1; j <= i; j++) — print a star with System.out.print("*"). After the inner loop, use System.out.println() for a new line.

9Mini Calculator with Switch

Read two numbers and an operator (+, -, *, /). Use a switch statement to compute and display the result. Handle invalid operators and division by zero.
Expected OutputEnter first number: 10 Enter operator (+,-,*,/): * Enter second number: 5 Result: 10.0 * 5.0 = 50.0
Your Code
Read the operator as a char using input.next().charAt(0). Use switch (operator) with cases for each operation. In the '/' case, check if the second number is 0 before dividing.