Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program with test cases that determines whether or not an election

ID: 3709550 • Letter: W

Question

Write a java program with test cases that determines whether or not an election occurred in a given year between 1845 and 2016.

In 1845, the United States Congress adopted a uniform day on which the States were to choose their Electors. This day would be the Tuesday following the first Monday in November in years divisible by four (Click here to find out more: https://uselectionatlas.org/INFORMATION/INFORMATION/dates.php) Links to an external site..

We would like to write a function "bool WasElectionYear(int year)" that:

- Returns true if the year was an election year.

- Returns false if the year was not an election year.

- Throws an exception if the year was before the first election year.

Identify the minimum set of test cases and list them in order together with their expected output. For the Input insert a year (i.e. 2016), for the output, insert: true, false, or Exception.

For example,

2016 -> True

Explanation / Answer

import java.util.Scanner;

public class Driver {

public static boolean WasElectionYear(int year) {

try {

if (year < 1848)

throw new Exception();

else{

int flag = 0;

for(int i = 1848; i <= 2016; i = i + 4){

if(i == year){

flag = 1;

break;

}

}

if(flag == 0)

return false;

else

return true;

}

}

catch (Exception e) {

System.out.println("Exception Caught. Invalid Year ");

return false;

}

}

public static void main(String[] args){

Scanner in = new Scanner(System.in);

System.out.println("Enter the year: ");

int year = in.nextInt();

boolean result = WasElectionYear(year);

if(result)

System.out.println("Valid year");

else

System.out.println("Invalid year");

}

}

**Comment for any further queries.?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote