0% found this document useful (0 votes)
7 views3 pages

School Election Vote Simulation Code

The document compares the implementation of a school election voting simulation in Java and Python, highlighting differences in syntax, ease of use, and readability. While Python's simpler syntax allowed for quicker coding and debugging, Java provided clearer structure but required more setup. Overall, both languages effectively demonstrated control structures and logic, reinforcing algorithmic thinking and problem-solving skills.

Uploaded by

nagpalsanjna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

School Election Vote Simulation Code

The document compares the implementation of a school election voting simulation in Java and Python, highlighting differences in syntax, ease of use, and readability. While Python's simpler syntax allowed for quicker coding and debugging, Java provided clearer structure but required more setup. Overall, both languages effectively demonstrated control structures and logic, reinforcing algorithmic thinking and problem-solving skills.

Uploaded by

nagpalsanjna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java:

// Randomly generate votes. Uses core language features only ([Link] allowed).
// Output: single line with the winner's name (e.g., "Alice")

public class SchoolElection {


public static void main(String[] args) {
String[] candidates = {"Alice", "Ben", "Clara", "David", "Eva"};
int[] counts = new int[[Link]]; // parallel array for tallies
[Link] rnd = new [Link](); // allowed basic randomness

// Simulate up to 200 votes, stopping early if someone gets >100.


for (int i = 0; i < 200; i++) {
int pick = [Link]([Link]); // pick index 0.4
counts[pick] += 1;

// Early stopping: if this candidate now has more than 100 votes, declare winner
immediately.
if (counts[pick] > 100) {
[Link](candidates[pick]);
return;
}
}

// No early winner: find highest total


int maxVotes = counts[0];
for (int i = 1; i < [Link]; i++) {
if (counts[i] > maxVotes) {
maxVotes = counts[i];
}
}

// Count how many are tied for max and collect their indices
int tieCount = 0;
for (int i = 0; i < [Link]; i++) {
if (counts[i] == maxVotes) tieCount++;
}

if (tieCount == 1) {
// Unique winner — print the candidate with maxVotes
for (int i = 0; i < [Link]; i++) {
if (counts[i] == maxVotes) {
[Link](candidates[i]);
return;
}
}
} else {
// Tie: choose uniformly at random among tied candidates
int[] tiedIndices = new int[tieCount];
int idx = 0;
for (int i = 0; i < [Link]; i++) {
if (counts[i] == maxVotes) {
tiedIndices[idx++] = i;
}
}
int chosen = tiedIndices[[Link]([Link])];
[Link](candidates[chosen]);
}
}
}

Python
# Randomly generate votes using Python's random module.
# Output: single line with the winner's name (e.g., "Alice")

import random

def run_election():
candidates = ["Alice", "Ben", "Clara", "David", "Eva"]
counts = {name: 0 for name in candidates} # simple dictionary for tallies

# Simulate up to 200 votes (random). Stop early if someone exceeds 100 votes.
for _ in range(200):
pick = [Link](candidates)
counts[pick] += 1
if counts[pick] > 100: # early stop condition (strictly more than 100)
print(pick)
return

# No early winner: determine the highest total


max_votes = None
for name in candidates:
if max_votes is None or counts[name] > max_votes:
max_votes = counts[name]

# Collect all candidates tied with max_votes


tied = []
for name in candidates:
if counts[name] == max_votes:
[Link](name)

if len(tied) == 1:
print(tied[0])
else:
# Tie-breaker: choose uniformly at random among tied candidates
print([Link](tied))

if __name__ == "__main__":
run_election()

Perceived difficulty
Python felt easier and faster due to its simple syntax. Java needed more setup and typing
but gave clearer structure.
Control structures
Loops and conditions were simpler in Python. Java was more verbose but made logic
explicit. Early stopping was easier in Python using return.
Manual code vs built-ins
It was tempting to use helpers like Counter, but writing logic manually improved
understanding of loops, counting, and conditions.
Time taken
Java took longer due to syntax and type declarations. Python was quicker to write and
debug.
Marks and skills
Both showed loops, conditionals, and logic clearly, meeting all criteria for full marks.
Readability
Python is more readable and concise; Java is longer but clear and structured.
Language features
Python made randomness and tie handling simpler; Java required more lines but gave good
control.
Overall learning
The task proved that problem-solving is language-neutral and strengthened algorithmic
thinking, control flow understanding, and logical reasoning.

Resources:
Used ChatGPT to refine the code.

You might also like