Results 1 to 2 of 2
Thread: StackOverFlow
- 02-17-2011, 07:48 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 42
- Rep Power
- 0
StackOverFlow
I'm not sure if this is beginner but i am. i keep getting an error with my recursive binary search method.
this is my code
Java Code:import java.io.*; import java.util.*; public class Crunchy { public static void main(String sArg[]) throws IOException { Scanner oCount = new Scanner(new File("Employee.dat")); Scanner oScan = new Scanner(new File("Employee.dat")); int iCountCom = 0; int iCountHou = 0; int iCountSal = 0; while(oCount.hasNext()) { String s = oCount.nextLine(); if(s.charAt(0) == 'C')iCountCom++; if(s.charAt(0) == 'H')iCountHou++; if(s.charAt(0) == 'S')iCountSal++; } char cType = 'Z'; int iID = 0; String sFirst = ""; String sLast = ""; String sTitle = ""; float fComRate = 0.0F; float fThreshold = 0.0F; float fSalary = 0.0F; float fHourlyRate = 0.0F; boolean bOverEligible = false; Commission[] comAry = new Commission[iCountCom]; Salaried[] salAry = new Salaried[iCountSal]; Hourly[] houAry = new Hourly[iCountHou]; int iCountComB = 0; int iCountSalB = 0; int iCountHouB = 0; while(oScan.hasNext()) { cType = oScan.next().charAt(0); if(cType == 'C') { iID = oScan.nextInt(); sFirst = oScan.next(); sLast = oScan.next(); sTitle = oScan.next(); fSalary = oScan.nextFloat(); fComRate = oScan.nextFloat(); fThreshold = oScan.nextFloat(); } if(cType == 'S') { iID = oScan.nextInt(); sFirst = oScan.next(); sLast = oScan.next(); sTitle = oScan.next(); fSalary = oScan.nextFloat(); } if(cType == 'H') { iID = oScan.nextInt(); sFirst = oScan.next(); sLast = oScan.next(); sTitle = oScan.next(); fHourlyRate = oScan.nextFloat(); bOverEligible = oScan.next().compareToIgnoreCase("Y") == 0 ? true : false; } if(cType == 'C'){comAry[iCountComB++] = new Commission(sFirst, sLast, sTitle, cType, iID, fThreshold, fComRate); comAry[iCountComB-1].setSalary(fSalary);} if(cType == 'S'){salAry[iCountSalB++] = new Salaried(sFirst, sLast, sTitle, cType, iID, fSalary); salAry[iCountSalB-1].setSalary(fSalary);} if(cType == 'H'){houAry[iCountHouB++] = new Hourly(sFirst, sLast, sTitle, cType, iID, fHourlyRate, bOverEligible); houAry[iCountHouB-1].setSalary(fSalary);} }//end of loop for(int i = 0; i < comAry.length; i++) { comAry[i].calculatePay(); salAry[i].calculatePay(); houAry[i].calculatePay(); } comAry = mergeSortCom(comAry, comAry.length); houAry = mergeSortHou(houAry, houAry.length); salAry = mergeSortSal(salAry, salAry.length); Scanner oScanner = new Scanner(new File("Hourly.dat")); while(oScanner.hasNext()) { int iIDB = oScanner.nextInt(); int iIndex = recBinarySearch(houAry, 0, houAry.length, iIDB); float fHoursWorked = oScanner.nextFloat(); houAry[iIndex].setHoursWorked(fHoursWorked); } Scanner oScannerB = new Scanner(new File("Commission.dat")); while(oScannerB.hasNext()) { int iIDB = oScannerB.nextInt(); int iIndex = recBinarySearch(comAry, 0, comAry.length, iIDB); float comSales = oScannerB.nextFloat(); comAry[iIndex].setComSales(comSales); } for(int i = 0; i < comAry.length; i++) { comAry[i].calculatePay(); salAry[i].calculatePay(); houAry[i].calculatePay(); } float fTotal = 0; System.out.println("Commission:"); System.out.printf("%-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s \n", "First Name", "Last Name", "Title" , "Employee ID", "Commission Rate", "Current Sales", "Salary", "Total Pay"); System.out.println("___________________________________________________________________________________________________________________________________"); for(Commission A : comAry) { A.print(); fTotal += A.getTotalPay(); } System.out.println("Total Pay: " + fTotal); fTotal = 0; System.out.println("\nHourly:"); System.out.printf("%-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s \n", "First Name", "Last Name", "Title" , "Employee ID", "Hours Worked", "Hourly Rate", "Overtime Eligible", "Total Pay"); System.out.println("___________________________________________________________________________________________________________________________________"); for(Hourly A : houAry) { A.print(); fTotal += A.getTotalPay(); } System.out.println("Total Pay: " + fTotal); fTotal = 0; System.out.println("\nSalary:"); System.out.printf("%-15s %-15s %-15s %-15s %-15s %-15s \n", "First Name", "Last Name", "Title" , "Employee ID", "Salary", "Total Pay"); System.out.println("________________________________________________________________________________________________"); for(Salaried A : salAry) { A.print(); fTotal += A.getTotalPay(); } System.out.println("Total Pay: " + fTotal); } public static int recBinarySearch(Commission[] com,int low, int high, int iD) { int mid = (high + low) / 2; if(high - low == 0)return low; if(low > high)return -1; if(mid == high)return mid; if(com[mid].getID() == iD)return mid; if(com[mid].getID() > 0)return recBinarySearch(com, mid, high, iD); if(com[mid].getID() < 0)return recBinarySearch(com, 0, mid, iD); return -1; } public static int recBinarySearch(Hourly[] hou,int low, int high, int iD) { int mid = (high + low) / 2; if(low > high)return -1; if(mid == high)return mid; if(hou[mid].getID() == iD)return mid; if(hou[mid].getID() > 0)return recBinarySearch(hou, mid, high, iD); if(hou[mid].getID() < 0)return recBinarySearch(hou, 0, mid, iD); return -1; } public static Commission[] mergeSortCom(Commission[] comA, int length) { for(int i = 0; i < length; i++) { int j = i; Commission b = comA[i]; while((j > 0) && (comA[j-1].compareTo(b) > 0)) { comA[j] = comA[j-1]; j--; } comA[j] = b; } return comA; } public static Salaried[] mergeSortSal(Salaried[] comA, int length) { for(int i = 0; i < length; i++) { int j = i; Salaried b = comA[i]; while((j > 0) && (comA[j-1].compareTo(b) > 0)) { comA[j] = comA[j-1]; j--; } comA[j] = b; } return comA; } public static Hourly[] mergeSortHou(Hourly[] comA, int length) { for(int i = 0; i < length; i++) { int j = i; Hourly b = comA[i]; while((j > 0) && (comA[j-1].compareTo(b) > 0)) { comA[j] = comA[j-1]; j--; } comA[j] = b; } return comA; } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// abstract class Employee implements Comparable <Employee> { private int iID; private String sFirst, sLast, sTitle; private float fTotalPay, fSalary; private char cType; public Employee(int i, String A, String B, String C, char c) { iID = i; sFirst = A; sLast = B; sTitle = C; cType = c; } abstract void calculatePay(); abstract void print(); public int getID() { return iID; } public String getFirst() { return sFirst; } public String getLast() { return sLast; } public String getTitle() { return sTitle; } public float getTotalPay() { return fTotalPay; } public char getType() { return cType; } public void setSalary(float fSal) { fSalary = fSal; } public void setTotalPay(float f) { fTotalPay = f; } public int compareTo(Employee eEmp) { if(this.cType == eEmp.cType) return this.iID - eEmp.iID; return 4; } } ////////////////////////////////////////////////////////////////////////////// class Commission extends Employee { float fSalary, fComRate, fComSales, fThreshold; public Commission(String A, String B,String C,char c, int i, float thresh, float rate) { super(i, A, B, C, c); fThreshold = thresh; fComRate = rate; } public void setComSales(float comsales) { fComSales = comsales; } public void setSalary(float fSal) { fSalary = fSal; } public void calculatePay() { if(fComSales > fThreshold) { super.setTotalPay( (fSalary / 24) + ( (fComSales - fThreshold) * fComRate) ); } else { super.setTotalPay(fSalary / 24); } } public void print() { System.out.printf("%-15s %-15s %-15s %-15d %-15f %-15f %-15f %-15f \n", getFirst(), getLast(), getTitle(), getID(), fComRate, fComSales, fSalary, getTotalPay()); } public String getFirst() { return super.getFirst(); } public String getLast() { return super.getLast(); } public int getID() { return super.getID(); } public String getTitle() { return super.getTitle(); } public float getTotalPay() { return super.getTotalPay(); } public char getType() { return super.getType(); } } //////////////////////////////////////////////////////////// class Salaried extends Employee { float fAnnSalary; public Salaried(String A, String B, String C, char c, int i, float f) { super(i,A,B,C,c); fAnnSalary = f; } public void setSalary(float f) { fAnnSalary = f; super.setSalary(fAnnSalary); } public String getFirst() { return super.getFirst(); } public String getLast() { return super.getLast(); } public int getID() { return super.getID(); } public String getTitle() { return super.getTitle(); } public float getTotalPay() { return super.getTotalPay(); } public char getType() { return super.getType(); } public void calculatePay() { super.setTotalPay(fAnnSalary / 24); } public void print() { System.out.printf("%-15s %-15s %-15s %-15d %-15f %-15f \n", getFirst(), getLast(), getTitle(), getID(), fAnnSalary, getTotalPay()); } } //////////////////////////////////////////////////////////// class Hourly extends Employee { float fHoursWorked,fHourlyRate; boolean bOverEligible; public Hourly(String A, String B, String C, char c, int i, float hour, boolean over) { super(i,A,B,C,c); fHourlyRate = hour; bOverEligible = over; } public void setHoursWorked(float f) { fHoursWorked = f; } public String getFirst() { return super.getFirst(); } public String getLast() { return super.getLast(); } public String getTitle() { return super.getTitle(); } public int getID() { return super.getID(); } public char getType() { return super.getType(); } public void print() { System.out.printf("%-15s %-15s %-15s %-15d %-15f %-15f %-15b %-15f \n", getFirst(), getLast(), getTitle() , getID(), fHoursWorked, fHourlyRate, bOverEligible, getTotalPay()); } public void calculatePay() { if(bOverEligible) { float f = fHoursWorked - 80F; super.setTotalPay((80F * fHourlyRate) + (f * (fHourlyRate * 1.5F))); } else { super.setTotalPay(fHoursWorked * fHourlyRate); } } }
- 02-17-2011, 07:50 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 42
- Rep Power
- 0
Similar Threads
-
StackOverflow Exception with Regexes
By masterrs.mind in forum Advanced JavaReplies: 6Last Post: 03-12-2010, 05:32 PM
Bookmarks