Java Tutorial » Chapter 10 — Problem Solving & Best Practices

Chapter 10 — Problem Solving & Best Practices

Learn to approach programming challenges with logic, flowcharts, and best practices.

1. Introduction

Thinking Like a Programmer

Before writing any code, a good programmer first thinks about the problem. This chapter is about building that skill. We will do this by:

  • Understanding the Problem: Clearly defining what the input is and what the output should be.
  • Designing a Solution: Using a flowchart to visually map out the steps and logic before coding.
  • Following Best Practices: Writing clean, readable, and efficient code.

Remember, planning is the most important step!

Problem 1

Print Your Name and Address

Write a program that reads your name and address from the input and prints them on the screen.

Sample Input & Output

Sample Input:
Alice
123 Main St
Expected Output:
Name: Alice
Address: 123 Main St

Flowchart

[Start]
  |
  +--->[Input: name, address]
  |
  +--->[Process: Print "Name: " + name]
  |
  +--->[Process: Print "Address: " + address]
  |
  +--->[End]

Best Practices

  • Use descriptive variable names like `userName` and `userAddress`.
  • Use `System.out.println()` to move to the next line after printing the name.
  • Remember to close the `Scanner` object to free up resources.
Problem 2

Print a Hello Message

Write a program that reads a user's name and then prints a personalized "Hello" message.

Sample Input & Output

Sample Input:
Bob
Expected Output:
Hello, Bob!

Flowchart

[Start]
  |
  +--->[Input: name]
  |
  +--->[Process: Print "Hello, " + name + "!"]
  |
  +--->[End]

Best Practices

  • Use the `+` operator to concatenate (join) strings.
  • Be careful with spelling and punctuation in the output string.
Problem 3

Sum of Two Numbers

Write a program that reads two integers from the user and prints their sum.

Sample Input & Output

Sample Input:
5
10
Expected Output:
The sum is: 15

Flowchart

[Start]
  |
  +--->[Input: num1, num2]
  |
  +--->[Process: sum = num1 + num2]
  |
  +--->[Output: "The sum is: " + sum]
  |
  +--->[End]

Best Practices

  • Use separate variables for each input (`num1`, `num2`).
  • Use a third variable (`sum`) to store the result.
Problem 4

Difference of Two Numbers

Write a program that reads two integers and prints the difference between the first and the second number.

Sample Input & Output

Sample Input:
20
5
Expected Output:
The difference is: 15

Flowchart

[Start]
  |
  +--->[Input: num1, num2]
  |
  +--->[Process: difference = num1 - num2]
  |
  +--->[Output: "The difference is: " + difference]
  |
  +--->[End]

Best Practices

  • Be mindful of the order of subtraction. The difference is `num1 - num2`.
Problem 5

All Arithmetic Operations

Write a program that reads two numbers and prints the result of all arithmetic operations (+, -, *, /).

Sample Input & Output

Sample Input:
10
5
Expected Output:
Sum: 15
Difference: 5
Product: 50
Division: 2

Flowchart

[Start]
  |
  +--->[Input: num1, num2]
  |
  +--->[Process: sum = num1 + num2]
  |
  +--->[Process: diff = num1 - num2]
  |
  +--->[Process: prod = num1 * num2]
  |
  +--->[Process: div = num1 / num2]
  |
  +--->[Output: sum, diff, prod, div]
  |
  +--->[End]

Best Practices

  • Perform integer division if you only want a whole number result.
  • Calculate each result in a separate step for clarity.
Problem 6

Evaluate Equations

Write a program that reads a number 'A' and calculates the result of the following equations: a) A²+2AB+B² and b) A³+3AB+2B².

Sample Input & Output

Sample Input:
A=2, B=3
Expected Output:
Result of (a) is: 31
Result of (b) is: 38

Flowchart

[Start]
  |
  +--->[Input: A, B]
  |
  +--->[Process: result1 = A*A + 2*A*B + B*B]
  |
  +--->[Process: result2 = A*A*A + 3*A*B + 2*B*B]
  |
  +--->[Output: result1, result2]
  |
  +--->[End]

Best Practices

  • Break down complex equations into smaller, manageable parts.
  • Use the `Math.pow()` function for powers, e.g., `Math.pow(A, 2)` for A².
Problem 7

Remainder of Division

Write a program that reads two numbers and prints the remainder of their division.

Sample Input & Output

Sample Input:
10
3
Expected Output:
The remainder is: 1

Flowchart

[Start]
  |
  +--->[Input: num1, num2]
  |
  +--->[Process: remainder = num1 % num2]
  |
  +--->[Output: "The remainder is: " + remainder]
  |
  +--->[End]

Best Practices

  • Use the modulo operator (`%`) to find the remainder of a division.
Problem 8

Swapping Two Variables

Write a program that reads two numbers into variables `x` and `y`, then swaps their values. Use a third variable `temp` to help.

Sample Input & Output

Sample Input:
x = 5, y = 10
Expected Output:
After swap: x = 10, y = 5

Flowchart

[Start]
  |
  +--->[Input: x, y]
  |
  +--->[Process: temp = x]
  |
  +--->[Process: x = y]
  |
  +--->[Process: y = temp]
  |
  +--->[Output: x, y]
  |
  +--->[End]

Best Practices

  • Always use a temporary variable (`temp`) to hold one value during the swap.
  • Clearly print the values before and after the swap to verify the result.
Problem 9

Swapping Without a Third Variable

Write a program that reads two numbers into variables `x` and `y` and swaps their values without using a third variable.

Sample Input & Output

Sample Input:
x = 5, y = 10
Expected Output:
After swap: x = 10, y = 5

Flowchart

[Start]
  |
  +--->[Input: x, y]
  |
  +--->[Process: x = x + y]
  |
  +--->[Process: y = x - y]
  |
  +--->[Process: x = x - y]
  |
  +--->[Output: x, y]
  |
  +--->[End]

Best Practices

  • This technique uses arithmetic (addition and subtraction) to swap values.
  • It can be more complex to understand than using a temporary variable.
Problem 10

Student Total & Percentage

Write a program that reads the marks of 5 subjects, calculates the total, and prints the percentage. Assume each subject is out of 100.

Sample Input & Output

Sample Input:
85
90
76
88
95
Expected Output:
Total Marks: 434
Percentage: 86.8

Flowchart

[Start]
  |
  +--->[Input: mark1, mark2, mark3, mark4, mark5]
  |
  +--->[Process: total = sum of all marks]
  |
  +--->[Process: percentage = (total / 500) * 100]
  |
  +--->[Output: total, percentage]
  |
  +--->[End]

Best Practices

  • Use a loop to read the 5 marks to avoid repetitive code.
  • Be careful with floating-point division to calculate the percentage accurately.
Problem 11

Calculate Bill for One Item

Write a program that reads the quantity and rate of an item and calculates the total bill amount.

Sample Input & Output

Sample Input:
Quantity: 5
Rate: 10.50
Expected Output:
Total Bill Amount: 52.50

Flowchart

[Start]
  |
  +--->[Input: quantity, rate]
  |
  +--->[Process: totalBill = quantity * rate]
  |
  +--->[Output: totalBill]
  |
  +--->[End]

Best Practices

  • Use a `double` or `float` for the rate and total bill to handle decimal values.
Problem 12

Calculate Bill for Three Items

Write a program that reads the quantity and rate for 3 different items and calculates the total bill amount.

Sample Input & Output

Sample Input:
Item 1 - Qty: 2, Rate: 15.00
Item 2 - Qty: 1, Rate: 25.50
Item 3 - Qty: 3, Rate: 5.25
Expected Output:
Total Bill Amount: 77.25

Flowchart

[Start]
  |
  +--->[Input: qty1, rate1, qty2, rate2, qty3, rate3]
  |
  +--->[Process: total1 = qty1 * rate1]
  |
  +--->[Process: total2 = qty2 * rate2]
  |
  +--->[Process: total3 = qty3 * rate3]
  |
  +--->[Process: grandTotal = total1 + total2 + total3]
  |
  +--->[Output: grandTotal]
  |
  +--->[End]

Best Practices

  • Use an array or separate variables for each item's quantity and rate.
  • Calculate the subtotal for each item before calculating the grand total.
Problem 13

Calculate Bill with Discount

Write a program that reads the quantity and rate of an item, calculates the total bill, applies a 12.50% discount, and prints the final amount.

Sample Input & Output

Sample Input:
Quantity: 4
Rate: 20.00
Expected Output:
Total Bill: 80.00
Discount (12.50%): 10.00
Net Amount: 70.00

Flowchart

[Start]
  |
  +--->[Input: quantity, rate]
  |
  +--->[Process: totalBill = quantity * rate]
  |
  +--->[Process: discount = totalBill * 0.125]
  |
  +--->[Process: netAmount = totalBill - discount]
  |
  +--->[Output: totalBill, discount, netAmount]
  |
  +--->[End]

Best Practices

  • Represent the discount percentage as a decimal (e.g., 12.50% as 0.125).
  • Clearly show the user the discount amount and the final net amount.
Problem 14

Convert Dollars to Rupees

Write a program that reads an amount in US Dollars and converts it to Indian Rupees. Assume the conversion rate is 1 Dollar = 83 Rupees.

Sample Input & Output

Sample Input:
50
Expected Output:
Amount in Rupees: 4150.00

Flowchart

[Start]
  |
  +--->[Input: dollars]
  |
  +--->[Process: rupees = dollars * 83]
  |
  +--->[Output: rupees]
  |
  +--->[End]

Best Practices

  • Store the conversion rate in a constant variable if it doesn't change.
  • Use `double` for currency to handle decimal values accurately.
Problem 15

Convert Grams to Pounds

Write a program that reads a weight in grams and converts it to pounds. (1 gram = 0.00220462 pounds).

Sample Input & Output

Sample Input:
1000
Expected Output:
Weight in Pounds: 2.20

Flowchart

[Start]
  |
  +--->[Input: grams]
  |
  +--->[Process: pounds = grams * 0.00220462]
  |
  +--->[Output: pounds]
  |
  +--->[End]

Best Practices

  • Use a `double` for the conversion factor and the result for precision.
Problem 16

Convert Centimeters to Feet

Write a program that reads a length in centimeters and converts it to feet. (1 cm = 0.0328084 feet).

Sample Input & Output

Sample Input:
152.4
Expected Output:
Length in Feet: 5.0

Flowchart

[Start]
  |
  +--->[Input: centimeters]
  |
  +--->[Process: feet = centimeters * 0.0328084]
  |
  +--->[Output: feet]
  |
  +--->[End]

Best Practices

  • Be aware of the data types needed for precision (float or double).
Problem 17

Average of Five Numbers

Write a program that reads five numbers and calculates their average.

Sample Input & Output

Sample Input:
10
20
30
40
50
60
Expected Output:
Average: 30.0

Flowchart

[Start]
  |
  +--->[Input: num1, num2, num3, num4, num5]
  |
  +--->[Process: sum = num1 + num2 + num3 + num4 + num5]
  |
  +--->[Process: average = sum / 5]
  |
  +--->[Output: average]
  |
  +--->[End]

Best Practices

  • Use a loop to read the five numbers to make the code cleaner.
  • Be careful with integer division. Cast the sum to a `double` before dividing to get a precise average.
Problem 18

Sum of Digits of a Number

Write a program that reads a 5-digit number and prints the sum of its digits.

Sample Input & Output

Sample Input:
12345
Expected Output:
Sum of digits: 15

Flowchart

[Start]
  |
  +--->[Input: number]
  |
  +--->[Process: sum = 0, temp = number]
  |
  +--->[Loop: While temp > 0]
  |     |
  |     +--->[Process: digit = temp % 10]
  |     |
  |     +--->[Process: sum = sum + digit]
  |     |
  |     +--->[Process: temp = temp / 10]
  |     |
  +--->[End Loop]
  |
  +--->[Output: sum]
  |
  +--->[End]

Best Practices

  • Use the modulo operator (`%`) to extract the last digit of a number.
  • Use integer division (`/`) to remove the last digit from a number.
  • A `while` loop is perfect for processing a number until it becomes 0.
Problem 19

Student Total, Average, and Percentage

Write a program that reads a student's name, roll number, and marks in 5 subjects. Calculate the total marks, average, and percentage. Assume each subject is out of 100.

Sample Input & Output

Sample Input:
John Doe
101
85
90
76
88
95
Expected Output:
Name: John Doe
Roll Number: 101
Total Marks: 434
Average: 86.8
Percentage: 86.8%

Flowchart

[Start]
  |
  +--->[Input: name, rollNo, mark1, mark2, mark3, mark4, mark5]
  |
  +--->[Process: total = sum of all marks]
  |
  +--->[Process: average = total / 5]
  |
  +--->[Process: percentage = (total / 500) * 100]
  |
  +--->[Output: name, rollNo, total, average, percentage]
  |
  +--->[End]

Best Practices

  • Read the student's name first, before the numbers, using `nextLine()`.
  • Use a loop to read the 5 marks.
  • Format the output to be clean and easy to read.