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

Lab Java

The document describes a Java program that implements a multi-threaded voting and counting simulation. It creates three Vote threads to populate a vote vector with randomly generated votes. It then creates three Count threads to independently tally the votes for each candidate by traversing the vote vector. The results are printed, indicating the vote counts for each candidate and determining the winner.

Uploaded by

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

Lab Java

The document describes a Java program that implements a multi-threaded voting and counting simulation. It creates three Vote threads to populate a vote vector with randomly generated votes. It then creates three Count threads to independently tally the votes for each candidate by traversing the vote vector. The results are printed, indicating the vote counts for each candidate and determining the winner.

Uploaded by

satyamoorthy m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Code-

MultiThreadVote.java-

import elections.Vote;

import elections.count;

import java.util.Vector;

public class MultiThreadVote {

public static void main(String[] args) {

Vector votevec = new Vector(240); // creating a vote array for 240 votes

Vote a = new Vote(1, votevec);

a.start();
Vote b = new Vote(2, votevec);

b.start();

Vote c = new Vote(3, votevec);

c.start();

try{

a.join();

b.join();

c.join();

System.out.println("Voting has ended!");

}catch(Exception e){System.out.println(e);}

count ac = new count(1, votevec);

count bc = new count(2, votevec);

count cc = new count(3, votevec);

ac.start();

bc.start();

cc.start();

try{

ac.join();

bc.join();
cc.join();

System.out.println("Counting has ended!");

}catch(Exception e){System.out.println(e);}

int av = ac.count;

int bv = bc.count;

int cv = cc.count;

System.out.println("elections.Vote Vector:" + "\n" + votevec);

System.out.println(av + " votes for A");

System.out.println(bv + " votes for B");

System.out.println(cv + " votes for C");

if(av >= bv && av >= cv){

if(av == bv || av == cv)

System.out.println("Tie in elections!");

else

System.out.println("A has won the elections!");

else if(bv >= av && bv >= cv){

if(av == bv || bv == cv)

System.out.println("Tie in elections!");

else

System.out.println("B has won the elections!");

}
else if(cv >= av && cv >= bv){

if(cv == bv || cv == av)

System.out.println("Tie in elections!");

else

System.out.println("C has won the elections!");

Vote.java-

package elections;

import java.util.Random;

import java.util.Vector;

public class Vote extends Thread{

Random rand = new Random();

int max = 750;

int min = 100;

int v, s;

Vector vec;

public Vote(int v, Vector vec)

this.v = v;

this.vec = vec;
}

public void run() {

try

// while voting print id

while(vec.size() < 240) {

System.out.println("Thread " + this.getId() + " is Voting");

vec.add(v);

s = rand.nextInt((max - min) + 1) + min;

System.out.println("Thread " + this.getId() + " is sleeping for " + s);

Thread.sleep(s);

catch(InterruptedException e)

System.out.println("Voting Exception: " + e);

}
Count.java-

package elections;

/**

* @author batch1

*/

import java.util.Vector;

public class count extends Thread{

Vector vec;

int k, i;

public int count = 0;

public count(int k, Vector vec){

this.k = k;

this.vec = vec;

@Override

public void run(){

try{

for(i = 0; i < vec.capacity(); i++){

if(vec.elementAt(i).equals(k))

count++;
}

catch(Exception e){

System.out.println(e);

Output-
Counting has ended!

elections.Vote Vector:

[3, 2, 1, 1, 2, 3, 3, 2, 1, 2, 1, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 2, 3, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 1, 3, 2, 1, 3, 2, 1, 1, 3, 2,
3, 1, 3, 1, 2, 3, 1, 2, 1, 2, 1, 3, 1, 2, 3, 2, 1, 2, 3, 1, 2, 3, 2, 1, 3, 2, 3, 1, 3, 3, 2, 1, 3, 1, 2, 2, 1, 3, 2, 2, 1, 2, 3,
2, 3, 1, 2, 3, 1, 2, 3, 2, 1, 3, 1, 2, 3, 2, 1, 3, 1, 3, 2, 1, 2, 1, 3, 3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1, 2, 3, 1, 3, 2, 1, 3, 1,
2, 2, 3, 1, 3, 2, 1, 3, 2, 3, 2, 1, 3, 2, 1, 3, 2, 3, 1, 2, 3, 2, 3, 1, 1, 2, 3, 2, 1, 2, 1, 2, 3, 2, 3, 1, 2, 1, 3, 1, 2, 3, 2,
2, 1, 1, 3, 1, 2, 2, 3, 1, 2, 1, 3, 2, 3, 2, 1, 3, 1, 2, 1, 3, 1, 3, 1, 2, 3, 1, 3, 2, 1, 2, 3, 2, 3, 1, 3, 2, 3, 1, 1, 2, 3, 2,
1, 3, 2, 1, 2, 3, 2, 3, 1, 2, 2, 3, 2, 1, 3, 1, 3, 1, 2, 3, 1, 2, 3, 1, 2]

80 votes for A

83 votes for B

77 votes for C

B has won the elections!

BUILD SUCCESSFUL (total time: 20 seconds)

You might also like