Question
Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks!

Write a program that displays three buttons with t
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Voting System

Client.Java

******************************************************************************

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.io.InputStreamReader;
import java.rmi.NotBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;



public class VoteClient
{

   public static int voter;
   public static int votes;
   public static String [][]candidate;
   public static int [][]voter;
   public static void main(String[] args)
   {
   try
       {
           VoteSystem c = (VoteSystem)Naming.lookup("rmi://localhost/VotingService");
int temp=c.initialise() ;
           if(temp==0)
               System.out.println("Succesfull");
           else
               System.out.println("Failed");  
       System.out.println( "Welcome");

           while(true)
           {
             
               System.out.println(" 1.Register \n 2.Cast a Vote \n 3.candidate List \n 4.Vote Count \n 5.Exit");
            
           System.out.println("Enter your choice");
             try
               {
                   InputStreamReader isr = new InputStreamReader(System.in);
                   BufferedReader br = new BufferedReader(isr);
                   String s1 = br.readLine();
               int input1=Integer.parseInt(s1);
                   switch(input1)
                   {
                       case 1: System.out.println("Enter your voterid");
                       String s2 = br.readLine();
                   int input2=Integer.parseInt(s2);
                       int temp1=c.register(input2);
                       if(temp1==0)
                           System.out.println(" Successfully registered");
                       else if(temp1==1)
                           System.out.println(" Voterid also exists!");
                       else
                           System.out.println("error try again!!");
                       break;

                       case 2:   System.out.println("Enter the name of the candidate:");
                       String s3= br.readLine();
                       System.out.println("Enter your VoterID:");
                       String s4= br.readLine();
                       int input3=Integer.parseInt(s4);
                       int temp2=c.castvote(s3.toUpperCase(),input3);
                       if(temp2==0)
                           System.out.println("Successfuly voted");
                       else if(temp2==1)
                           System.out.println("candidate added successfully and voted");
                       else if(temp2==2)
                           System.out.println("Voter not registered Please register");
                       else if(temp2==3)
                           System.out.println("You have already voted. duplicate votes not counted.");
                       else if(temp2==4)
                           System.out.println("Error please try again");
                       break;

                       case 3: String [][]temp5=c.candlist();
                           if(temp5[0][0]==null)
                           {
                               System.out.println(" no candidate exists ");
                               break;
                           }
                           for (int i1=0; i1<temp5.length; i1++)
                           {
                               if(temp5[i1][0]==null)
                               {
                                   break;
                               }
                               for (int j1=0; j1<temp5[i1].length; j1++)
                               {
                                   if(temp5[i1][j1]!=null)
                                  System.out.print(" " + temp5[i1][j1]);
                                   else  
                                       break;
                               }
                  
                           System.out.println("");
                          }

                           break;
  
                       case 4:System.out.println("Enter the name of the candidate:");
                       String s5= br.readLine();
                       int temp3=c.votecount(s5.toUpperCase());
                       if(temp3!=-1)
                           System.out.println("total vote for selected cand:"+temp3);
                       else
                           System.out.println("cand not present");
                       break;
  
                       case 5: System.exit(0);
                       default: System.out.println("invalid input");
          
                   }
               }
               catch(Exception ex)
               {
                   System.out.println("Please enter a valid input");
                   continue;
               }
  
           }      
       }
   catch (MalformedURLException murle)
       {
           System.out.println(" ");
       System.out.println("MalformedURLException");
           System.out.println(murl);
   }
       catch (RemoteException rem)
       {
           System.out.println("");
           System.out.println("RemoteException");
           System.out.println(rem);
       }
       catch (NotBoundException nbe)
       {
           System.out.println();
           System.out.println("NotBoundException");
       System.out.println(nbe);
       }
       catch (java.lang.ArithmeticException ae)
       {
           System.out.println();
       System.out.println("java.lang.ArithmeticException");
       System.out.println(ae);
   }
        catch(IOException ex)
       {
           System.out.println(ex);
       }

   }
}

*****************************************************************************************

Server.Java

*****************************************************************************************************

import java.rmi.Naming;

public class VoteServer
{

   public VoteServer()
   {
      try
       {
          VoteSystem c = new VoteImpl();
          Naming.rebind("rmi://localhost:1099/VotingService", c);
       }
       catch (Exception e)
       {
       System.out.println("Exception: " + e);
       }
   }

   public static void main(String args[])
   {
       new VoteServer();
   }
}

*******************************************

Implementation.java

******************************************************************

import java.util.*;
import java.lang.*;

public class VoteImpl extends java.rmi.server.UnicastRemoteObject implements VoteSystem
{
  
   public VoteImpl() throws java.rmi.RemoteException
   {
   super();
   }

   public int initialise() throws java.rmi.RemoteException
   {
       try
       {
           System.out.println ("intialize with zero");
           VoteClient cc= new VoteClient();
           cc.voter=0;
           cc.votes=0;
           cc.candidate = new String[10][2];
           cc.voter=new int[100][2];
           return 0;
       }
       catch(Exception ex)
       {
       return 1;
       }
   }

   public int register(int voterid) throws java.rmi.RemoteException
   {
       try
       {
           VoteClient cc1= new VoteClient();
           int i=0;
           System.out.println ("Registering");
           while(cc1.voter[i][0]!='\0')
           {
               if(cc1.voter[i][0]==voterid)
               {
                   System.out.println ("voter id info already exists");
                   return 1;
               }
           i++;
           }

           System.out.println ("successfully registered");
           cc1.voter++;
           cc1.voter[i][0]=voterid;
           cc1.voter[i][1]=0;
           return 0;
       }
       catch(Exception ex)
       {
           System.out.println (ex);
           return 2;
       }  
   }


   public int castvote(String s,int votid) throws java.rmi.RemoteException
   {
       try
       {
           int m=0,n=0;
           VoteClient cc2=new VoteClient();
           while(cc2.voter[m][0]!='\0')
           {
               if(cc2.voter[m][0]==votid)
               {
                   if( cc2.voter[m][1]==0)
                   {
                       while(cc2.candidate[n][0]!=null)
                       {
                           if(cc2.candidate[n][0].equals(s))
                           {
                               int t=Integer.parseInt(cc2.candidate[n][1]);
                               t++;
                               cc2.candidate[n][1]=Integer.toString(t);
                               System.out.println("vote count incresed by one for the selesvted candidates");  
                               return 0;     
                           }  
                           n++;
                       }
                       cc2.candidate[n][0]=s;
                       cc2.candidate[n][1]="1";
                       cc2.voter[i][1]=1;
                       System.out.println("new candidates added and set vote count set to 1");
                       return 1;         
                   }
                   else return 3;
               }
               i++;
           }
           System.out.println("voter id not found");
           return 2;
       }
       catch(Exception ex)
       {
           System.out.println(ex);
           return 4;
       }          
   }


   public String[][] candidateslist() throws java.rmi.RemoteException
   {
       VoteClient cc3=new VoteClient();
       System.out.println("Sending candidates list");
       return(cc3.candidate);
   }


   public int votecount(String name) throws java.rmi.RemoteException
   {
       try
       {
           int n=0;
           VoteClient cc3=new VoteClient();
           while(cc3.voter[n][0]!='\0')
           {
               if(cc3.candidate[n][0].equals(name))
               {
                   System.out.println(" vote count for candidates requested ");
                   int t=Integer.parseInt(cc3.candidate[n][1]);
                   return(t);
               }
               j++;
           }
           System.out.println(" i m here");
           int t1=-1;
           return(t1);
       }
       catch(Exception Ex)
       {
           return -1;
       }
   }

}

************************************************************************************

System.java

**************************************************************************************

public interface VoteSystem extends java.rmi.Remote
{

   public int initialise() throws java.rmi.RemoteException;

   public int register(int voterid) throws java.rmi.RemoteException;

   public int castvote(String name,int voterid) throws java.rmi.RemoteException;

   public String[][] candidateslist() throws java.rmi.RemoteException;  

   public int votecount(String name) throws java.rmi.RemoteException;
   
}

**************************************************************

How to run
1. First compile all Java files
2. Run rmiregistry in your linux machine
3. Run command java VoteServer
4. Open another terminal
5. And run java VoteClient.

Add a comment
Know the answer?
Add Answer to:
Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME,...

    Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME, as specified by the prompt! DO NOT use user input of 1, 2, 3 etc. User input must come from clicking the buttons. Please be sure to test your program. Thank you! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate...

  • Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints th...

    Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints the string in reverse order. (Tip: Once the string has been entere aved, retrieve and display characters starting from the end of the string). Write a Java program that has 3 buttons. Clicking the first button shows the message "See no evil", clicking the second button shows the message "Hear no evil", clicking the third button shows the message "Speak...

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • Please write a simple Slideshow program in Java GUI. There are three buttons in the bottom...

    Please write a simple Slideshow program in Java GUI. There are three buttons in the bottom Panel. Open: you can choose multiple pictures. Start: starts the slideshow and change the picture in two seconds. Stop: you can stop the slideshow.

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Hi, Hi, I need someone to write a program in Phython 3.6, and please I will...

    Hi, Hi, I need someone to write a program in Phython 3.6, and please I will appreciate if the code is error free and indented correctly. The output would be preferable. thanks Write a GUI program that translates the Latin words to English. The window should have three buttons, one for each Latin word. When the user clicks a button, the program displays the English translation in a label. 3. Miles Per Gallon Calculator Write a GUI program that calculates...

  • In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST...

    In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST use GUI, that means all user input must be through the windows. The "first window" is the window on the top left of the following picture. Add 5 more items to be purchased. Search is accessed from second window, top right. Thw third window is the bottom left, and the fourth window is the bottom right. The program MUST CONTAIN EVERYTHING IN THE PICTURES....

  • This is python3. Please help me out. Develop the following GUI interface to allow the user...

    This is python3. Please help me out. Develop the following GUI interface to allow the user to specify the number of rows and number of columns of cards to be shown to the user. Show default values 2 and 2 for number of rows and number of columns. The buttons "New Game" and "Turn Over" span two column-cells each. Fig 1. The GUI when the program first starts. When the user clicks the "New Game" button, the program will read...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT