Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Duplicate local variable sc
at ZellerCongruence.main(ZellerCongruence.java:13)
The Correct Answer and Explanation is :
The error you’re encountering, Duplicate local variable sc, is caused by declaring the same local variable sc more than once within the same scope in your Java program. To resolve this error, you need to ensure that you only declare a variable once within the scope in which it is being used. In this case, it looks like sc is being redeclared in the same method or block.
Here’s how you can fix the issue:
Example Fix:
import java.util.Scanner;
public class ZellerCongruence {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner sc = new Scanner(System.in);
// Take year, month, and day as input from the user
System.out.print("Enter year: ");
int year = sc.nextInt();
System.out.print("Enter month: ");
int month = sc.nextInt();
System.out.print("Enter day: ");
int day = sc.nextInt();
// Adjust month and year for January and February
if (month <= 2) {
month += 12;
year--;
}
// Zeller's congruence formula
int k = year % 100;
int j = year / 100;
int h = (day + (13 * (month + 1)) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;
// Output the result
System.out.println("The day of the week is: " + dayOfWeek(h));
}
// Method to convert Zeller's output to day of the week
public static String dayOfWeek(int h) {
switch (h) {
case 0: return "Saturday";
case 1: return "Sunday";
case 2: return "Monday";
case 3: return "Tuesday";
case 4: return "Wednesday";
case 5: return "Thursday";
case 6: return "Friday";
default: return "";
}
}
}
Explanation of the Code:
- Scanner object initialization: The
Scanner sc = new Scanner(System.in);initializes the scanner to read input from the user. - Input values: The user is prompted to input a year, month, and day.
- Zeller’s Congruence Formula: The formula calculates the day of the week for any given date. This algorithm requires modifications to the month and year if the month is January or February, as those months are treated as the 13th and 14th months of the previous year.
- Modulo operation: The formula calculates the value
h, which corresponds to the day of the week, ranging from 0 (Saturday) to 6 (Friday). - Day of the Week: A switch statement is used to print the corresponding day of the week based on the value of
h.
Resolving the error:
- Duplicate Declaration: The error occurs if the
scvariable is declared multiple times in the same scope. Make sure that you declare it only once and reuse it for the input process.
I’ll generate a visual representation of Zeller’s Congruence formula for you. Let me get that ready!
Here’s the diagram that illustrates Zeller’s Congruence formula for calculating the day of the week. It shows the components of the formula, such as year, month, day, and adjustments for January and February, as well as the calculation of ‘h’. This visual should help you understand how the algorithm works in a step-by-step manner.
Let me know if you need further clarification!
