Please help me to explain these codes

Reply

Join Date: Oct 2007
Posts: 178
Reputation: lordx78 is an unknown quantity at this point 
Solved Threads: 2
lordx78's Avatar
lordx78 lordx78 is offline Offline
Junior Poster

Please help me to explain these codes

 
0
  #1
Oct 24th, 2007
import java.util.*;
import java.io.*;


class Week3Question2
{
public static void main (String args[])
{
TreeSet OldAccount = new TreeSet();
TreeSet CurrentAccount = new TreeSet();
TreeSet PseudoAccount = new TreeSet();

try
{
RetrieveAcc(OldAccount,CurrentAccount);
}
catch(Exception exc)
{
System.out.println("No such file!");
}

try
{
WriteAcc(OldAccount,CurrentAccount,PseudoAccount);
}
catch(Exception exc)
{
System.out.println("Process failed !");
}

System.out.println(CurrentAccount);

System.out.println(CurrentAccount.size()+"\n\n");

System.out.println(OldAccount);

System.out.println(OldAccount.size());
}

public static void RetrieveAcc(TreeSet Old, TreeSet Current)throws Exception
{
String dataStore = "";
Scanner oldAcc = new Scanner(new FileReader("oldAccounts.txt"));
Scanner currAcc = new Scanner(new FileReader("currAccounts.txt"));
oldAcc.skip("ID\tName");
currAcc.skip("ID\tName");

while(oldAcc.hasNext())
{
dataStore = oldAcc.nextLine();
Old.add(dataStore);
}

while(currAcc.hasNext())
{
dataStore = currAcc.nextLine();
Current.add(dataStore);
}

Old.remove("\t");
Current.remove("\t");
oldAcc.close();
currAcc.close();
}

public static void WriteAcc(TreeSet Old, TreeSet Current,TreeSet Pseudo)throws Exception
{
FileWriter gradAcc = new FileWriter("gradAccounts.txt");
FileWriter newAcc = new FileWriter("newAccounts.txt");
Pseudo.addAll(Old);
Pseudo.removeAll(Current);

Object IteData;
Iterator PseudoIte = Pseudo.iterator();
gradAcc.write("ID\tName\r\n");

while(PseudoIte.hasNext())
{
IteData = PseudoIte.next();
gradAcc.write(IteData+"\r\n");
}

gradAcc.close();

newAcc.write("ID\tName\r\n");
Pseudo.clear();
Pseudo.addAll(Current);
Pseudo.removeAll(Old);
PseudoIte = Pseudo.iterator();

while(PseudoIte.hasNext())
{
IteData = PseudoIte.next();
newAcc.write(IteData+"\r\n");
}
newAcc.close();
//System.exit(0);
}
}

&

import java.util.*;
import java.io.*;

class Week3Question3
{
public static void main (String args[])
{
String[] OldAccounts = new String[31];
String[] CurrentAccounts = new String[15];

try
{
loadOldAcc(OldAccounts,CurrentAccounts);
}
catch(Exception e)
{
System.err.println("Error in reading file!");
}

try
{
loadCurrAccounts(OldAccounts,CurrentAccounts);
}
catch(Exception e)
{
System.err.println("Error in reading file!");
}

try
{
writeRecord(OldAccounts,CurrentAccounts);
}
catch(Exception e)
{
System.err.println("Error in reading file!");
}
}

public static void writeRecord(String[] Old,String[] Current) throws Exception
{
BufferedWriter gradAccounts = new BufferedWriter(new FileWriter("gradAccounts.txt"));
gradAccounts.write("ID\tName\r\n");
boolean GradIsAvailable = false;

for(int m=1; m<Old.length; m++)
{
for(int n=1; n<Current.length; n++)
{
if(Old[m].trim().equals(Current[n].trim()))
{
GradIsAvailable = true;
break;
}
}
if(!GradIsAvailable)
{
gradAccounts.write(Old[m] + "\r\n");
}
GradIsAvailable = false;;
}
gradAccounts.close();

BufferedWriter newAccounts = new BufferedWriter(new FileWriter("newAccounts.txt"));
newAccounts.write("ID\tName\r\n");
boolean NewIsAvailable = false;

for(int m=1; m<Current.length; m++)
{
for(int n=1; n<Old.length; n++)
{
if(Current[m].trim().equals(Old[n].trim()))
{
NewIsAvailable = true;
break;
}
}
if(!NewIsAvailable)
{
newAccounts.write(Current[m] + "\r\n");
}
NewIsAvailable = false;;
}
newAccounts.close();
}

public static void loadOldAcc(String[] Old, String[] Current) throws Exception
{
Scanner oldAccounts = new Scanner(new FileReader("oldAccounts.txt"));

for(int m=0; m<Old.length; m++){
Old[m] = oldAccounts.nextLine();
}
}

public static void loadCurrAccounts(String[] Old, String[] Current) throws Exception
{
Scanner currAccounts = new Scanner(new FileReader("currAccounts.txt"));

for(int n=0; n<Current.length; n++){
Current[n] = currAccounts.nextLine();
}
}

}

// Please help me to explain these codes by writing the explanation beside the codes using '//'.thanks
Last edited by lordx78; Oct 24th, 2007 at 1:22 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Please help me to explain these codes

 
0
  #2
Oct 24th, 2007
Um, no. If you can't understand homework code that you have copied, that would be your own problem.

If you have a specific question about some operation that it is performing then post it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC