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

This code is from C++ so i want to know on how it looks in java format or let\'s

ID: 3778324 • Letter: T

Question

This code is from C++ so i want to know on how it looks in java format or let's just say can you translate it from C++ to java?

#include <iostream>

#include <fstream>

#include <stdlib.h>    

#include <time.h>

using namespace std;

string candidate[6];

int state1[10],vote1[10];

class candidates

{

    public:

    void countvote(string name)

    {

        ifstream up;

        int a=0,sum[10]={0};

        up.open("file.txt");

        while(!up.eof())

        {

            up >> candidate[a]>>state1[a]>>vote1[a];

            if(candidate[a]==name)

            {

               sum[a]=sum[a]+vote1[a];

            }

           

         }

         cout<<" candidate "<<name<<" get"<<" "<<sum[0];

        up.close();

    }

    void update(int s, int v)

    {

        ifstream up;

        string c;

        int a=0;

        cout<<" enter the candidate name:";

        cin>>c;

        up.open("file.txt");

        while(!up.eof())

        {

           up >> candidate[a]>>state1[a]>>vote1[a];

           if((candidate[a]==c)&&(state1[a]==s))

           {

               vote1[a]=v;

               cout<<candidate[a]<<" "<<state1[a]<<" "<<vote1[a];

           }

           a++;

       

         }

       

         up.close();

    }

   

};

int main()

{

    ifstream myCourse;

   

    int i=0;

    srand (time(NULL));

    myCourse.open("myfile.txt");

    cout << " Candidates name:" ;

    while(!myCourse.eof())

    {

        myCourse >> candidate[i];

        cout<<" "<<candidate[i];

        i++;

    }

    int state,vote;

   ofstream voter;

   voter.open("file.txt");

   int v;

   for(int k=0;k<5;k++)

   {

       v=rand() % 6 + 0;

       cout<<" "<<candidate[v];

       state=rand() % 4 + 1;

       cout<<" "<<state;

       vote=rand() % 50 + 1;

       cout<<" "<<vote;

       voter<<candidate[v]<<" "<<state<<" "<<vote<<endl;

   }

   candidates can;

   cout<<" total :"<<i;

   cout<<" update to which candidate having state 2:";

   can.update(2,50);

for(int m=0;m<i;m++)

{

        can.countvote(candidate[m]);

      

    }

    voter.close();

   myCourse.close();

  

   return 0;

}

Explanation / Answer

Answer:

import java.util.*;

public class candidates
{
   public final void countvote(String name)
   {
       ifstream up = new ifstream();
       int a = 0;
       int[] sum = new int[10];
       up.open("file.txt");
       while (!up.eof())
       {

           up >> GlobalMembers.candidate[a].compareTo() > 0 > > GlobalMembers.state1[a] >> GlobalMembers.vote1[a];
           if (GlobalMembers.candidate[a].equals(name))
           {
           sum[a] = sum[a] + GlobalMembers.vote1[a];
           }

       }
       System.out.print(" candidate ");
       System.out.print(name);
       System.out.print(" get");
       System.out.print(" ");
       System.out.print(sum[0]);
       up.close();
   }
   public final void update(int s, int v)
   {
       ifstream up = new ifstream();
       String c;
       int a = 0;
       System.out.print(" enter the candidate name:");
       c = ConsoleInput.readToWhiteSpace(true);
       up.open("file.txt");
       while (!up.eof())
       {

       up >> GlobalMembers.candidate[a].compareTo() > 0 > > GlobalMembers.state1[a] >> GlobalMembers.vote1[a];
       if ((GlobalMembers.candidate[a].equals(c)) && (GlobalMembers.state1[a] == s))
       {
           GlobalMembers.vote1[a] = v;
           System.out.print(GlobalMembers.candidate[a]);
           System.out.print(" ");
           System.out.print(GlobalMembers.state1[a]);
           System.out.print(" ");
           System.out.print(GlobalMembers.vote1[a]);
       }
       a++;

       }

       up.close();
   }

}

public class GlobalMembers
{
   public static String[] candidate = new String[6];
   public static int[] state1 = new int[10];
   public static int[] vote1 = new int[10];
   public static int Main()
   {
       ifstream myCourse = new ifstream();

       int i = 0;
       tangible.RandomNumbers.seed(time(null));
       myCourse.open("myfile.txt");
       System.out.print(" Candidates name:");
       while (!myCourse.eof())
       {

           myCourse >> candidate[i];
           System.out.print(" ");
           System.out.print(candidate[i]);
           i++;
       }
       int state;
       int vote;
   ofstream voter = new ofstream();
   voter.open("file.txt");
   int v;
   for (int k = 0;k < 5;k++)
   {
       v = tangible.RandomNumbers.nextNumber() % 6 + 0;
       System.out.print(" ");
       System.out.print(candidate[v]);
       state = tangible.RandomNumbers.nextNumber() % 4 + 1;
       System.out.print(" ");
       System.out.print(state);
       vote = tangible.RandomNumbers.nextNumber() % 50 + 1;
       System.out.print(" ");
       System.out.print(vote);
       voter << candidate[v].compareTo() < 0 < <" " << state << " " << vote << " ";
   }
   candidates can = new candidates();
   System.out.print(" total :");
   System.out.print(i);
   System.out.print(" update to which candidate having state 2:");
   can.update(2, 50);
   for (int m = 0;m < i;m++)
   {
           can.countvote(candidate[m]);

   }
       voter.close();
   myCourse.close();

   return 0;
   }
}

public final class ConsoleInput
{
   private static boolean goodLastRead = false;
   public static boolean lastReadWasGood()
   {
       return goodLastRead;
   }

   public static String readToWhiteSpace(boolean skipLeadingWhiteSpace)
   {
       String input = "";
       char nextChar;
       while (Character.isWhitespace(nextChar = (char)System.in.read()))
       {
  
           if (!skipLeadingWhiteSpace)
           {
               input += nextChar;
           }
       }
  
       input += nextChar;

  
       while (!Character.isWhitespace(nextChar = (char)System.in.read()))
       {
           input += nextChar;
       }

       goodLastRead = input.length() > 0;
       return input;
   }

   public static String scanfRead()
   {
       return scanfRead(null, -1);
   }

   public static String scanfRead(String unwantedSequence)
   {
       return scanfRead(unwantedSequence, -1);
   }

   public static String scanfRead(String unwantedSequence, int maxFieldLength)
   {
       String input = "";

       char nextChar;
       if (unwantedSequence != null)
       {
           nextChar = '';
           for (int charIndex = 0; charIndex < unwantedSequence.length(); charIndex++)
           {
               if (Character.isWhitespace(unwantedSequence.charAt(charIndex)))
               {
  
                   while (Character.isWhitespace(nextChar = (char)System.in.read()))
                   {
                   }
               }
               else
               {
  
                   nextChar = (char)System.in.read();
                   if (nextChar != unwantedSequence.charAt(charIndex))
                       return null;
               }
           }

           input = (new Character(nextChar)).toString();
           if (maxFieldLength == 1)
               return input;
       }

       while (!Character.isWhitespace(nextChar = (char)System.in.read()))
       {
           input += nextChar;
           if (maxFieldLength == input.length())
               return input;
       }

       return input;
   }
}


public final class RandomNumbers
{
   private static Random r;

   public static int nextNumber()
   {
       if (r == null)
           Seed();

       return r.nextInt();
   }

   public static int nextNumber(int ceiling)
   {
       if (r == null)
           Seed();

       return r.nextInt(ceiling);
   }

   public static void seed()
   {
       r = new Random();
   }

   public static void seed(int seed)
   {
       r = new Random(seed);
   }
}