0% found this document useful (0 votes)
2 views

Java

The document outlines an assignment on Java programming, specifically focusing on a voting system application. It includes user prompts for age verification, voting method selection, candidate choice, and simulates vote counting with random percentages. The author reflects on their learning experience, highlighting the use of conditional statements, loops, and switch statements in programming.

Uploaded by

Möhaméd Cabdí
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java

The document outlines an assignment on Java programming, specifically focusing on a voting system application. It includes user prompts for age verification, voting method selection, candidate choice, and simulates vote counting with random percentages. The author reflects on their learning experience, highlighting the use of conditional statements, loops, and switch statements in programming.

Uploaded by

Möhaméd Cabdí
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

MOHAMED SUGOW ABDI

Reg/No: 22/08501
Assignment 2
Java Programming

Question 1
A)
Here the program prompts the user to enter his age to see if he is eligible for voting.

B)
Here the programs prompts the user select the type of voting he/she wants:
1. In-person voting.
2. Mail-in voting.
3. Online voting.
C)
Here , the program displays the selection that the user has chosen
For instance I selected : ‘’Online Voting’’.
D)
Here, the program prompts the user to select his/her preferred candidate.
I’ve picked “Candidate 2”

E)
Here , the program calculates and displays the percentage of votes each candidate
received based on a randomly generated number of votes per candidate.
The Java Source Code
“/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template

*/package votingsystemproject;

import java.util.Scanner;

import java.util.Random;

/**

* @author nimcx

*/

public class VotingSystem {

public static void main(String[] args) {Scanner scanner = new Scanner(System.in);


// Part 1: Check Voter Eligibility

System.out.print("Enter your age: ");

int age = scanner.nextInt();

if (age >= 18) {

System.out.println("You are eligible to vote.");

// Voting Method Selection

System.out.println("Select a voting method:");

System.out.println("1. In-person Voting");

System.out.println("2. Mail-in Voting");

System.out.println("3. Online Voting");

System.out.print("Enter your choice (1-3): ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

System.out.println("You have selected In-Person Voting.");

break;

case 2:

System.out.println("You have selected Mail-in Voting.");

break;

case 3:

System.out.println("You have selected Online Voting.");

break;
default:

System.out.println("Invalid choice. Please select 1, 2, or 3.");

} else {

System.out.println("You are not eligible to vote.");

scanner.close();

return;

// Part 2: Poll Results Simulation

// 1. For Loop (Print Voter IDs from 1 to 10)

System.out.println("\nVoter IDs from 1 to 10:");

for (int i = 1; i <= 10; i++) {

System.out.println("Voter ID: " + i);

// 2. While Loop (Even numbers from 1 to 20)

System.out.println("\nEven votes counted from 1 to 20:");

int i = 1;

while (i <= 20) {

if (i % 2 == 0) {

System.out.println("Votes counted: " + i);

i++;
}

// 3. Do-While Loop (Candidate Selection)

int candidate;

do {

System.out.print("\nSelect your candidate (1 to 5): ");

candidate = scanner.nextInt();

if (candidate < 1 || candidate > 5) {

System.out.println("Invalid candidate number. Try again.");

} while (candidate < 1 || candidate > 5);

System.out.println("You selected candidate number " + candidate);

// Bonus: Random Vote Calculation

System.out.println("\n--- Vote Percentage ---");

Random rand = new Random();

int[] votes = new int[5];

int totalVotes = 0;

for (int j = 0; j < 5; j++) {

votes[j] = rand.nextInt(100); // Random votes from 0 to 99

totalVotes += votes[j];

for (int j = 0; j < 5; j++) {


double percentage = (votes[j] * 100.0) / totalVotes;

System.out.printf("Candidate %d received %.2f%% of the votes.\n", j + 1, percentage);

scanner.close();

}”

What I’ve learnt:


1. I learned how to use if-else to make decisions, like checking age.
2. I also learned about loops and how they repeat things, like printing numbers.
3. The switch statement helped me choose between different voting methods easily.

You might also like