java - Removing items from an array -
just started taking java 2, , has been on 6-7 months since i've taken java 1 , done programming @ all, please kind if did dumb
the majority of 3 classes below previous assignment took listing book , had add method called getmax() returns value of highest key in array, or -1 if array empty.
this part worked fine.
for next part(the part having trouble with) had modify assignment item highest key not returned method, removed array.
to tried:
public long removemax(printwriter pw) { long maxindex; maxindex = getmax(); delete(maxindex); return maxindex; }
but maxindex = getmax(); , delete(maxindex); giving me errors , i'm not quite sure why. although i'm convinced i'm making small mistake due being rusty @ programming.
the error giving me actual , formal argument lists differ in length. tried changing , switching things around no matter did nothing seemed work.
below employee, higharrayobject , project21rev(class getmax , removemax are) classes in full.
public class employee { protected int empno; // employee number protected string ssno; // social security number protected string lastname; // last name protected string firstname; // first name //this constructor initializes variables public employee(int eno, string ssn, string lname, string fname) // aliases used in function header { empno = eno; // alias assigned actual value ssno = ssn; lastname = lname; firstname = fname; } // make no argument constructor public employee() { empno = 0; ssno = ""; lastname = ""; firstname = ""; } /** copy constructor initializes object copy of employee object @param object2 object copy */ public employee(employee object2) { empno = object2.empno; ssno = object2.ssno; lastname = object2.lastname; firstname = object2.firstname; } // set method sets value each field public void set(int eno, string ssn, string lname, string fname) // aliases used in function header { empno = eno; // alias assigned actual value ssno = ssn; lastname = lname; firstname = fname; } // getkey method returns employee number public int getkey() { return empno; } // setkey method sets employee number public void setkey(int id) { empno = id; } // tostring method // returns string containing instructor information public string tostring() { // create string representing object. string str = "employee number: " + empno + "\nsocial security number: " + ssno + "\nlast name: " + lastname + "\nfirst name: " + firstname; // return string; return str; } }
start of next class
import java.io.*; class higharrayobject { protected employee[] emp; protected int nelems; public higharrayobject(int max) // constructor { emp = new employee[max]; nelems = 0; } // createemployees method creates employee object // each element of array public static void createemployees(employee[] emp, int maxsize) { int empno; string ssno; string lastname; string firstname; // create employees for(int index = 0; index < emp.length; index++) { // employee data emp[index] = new employee(); } } public boolean find(long searchkey, printwriter pw) { system.out.println("trying find item employee number " + searchkey); pw.println("trying find item employee number " + searchkey); int j; for(j=0; j<nelems; j++) if(emp[j].empno == searchkey) // == ok since empno primative break; // exit loop before end if(j == nelems) // gone end? return false; else return true; // no, found } // end find() public void insert(int eno, string sno, string lname, string fname, printwriter pw) { system.out.println("inserting employee employee number " + eno); pw.println("inserting employee employee number " + eno); employee temp = new employee(); temp.empno = eno; temp.ssno = sno; temp.lastname = lname; temp.firstname = fname; emp[nelems] = temp; nelems++; } public boolean delete(long value, printwriter pw) { system.out.println("deleting employee number " + value); pw.println("deleting employee number " + value); int j; for(j=0; j < nelems; j++) // if(value == emp[j].empno) break; // can't find if(j==nelems) return false; else // found { for(int k=j; k<nelems; k++) // move higher ones down { emp[k]=emp[k+1]; } nelems--; // decrement size return true; } } // end delete public void display(printwriter pw) { system.out.println("the array of employees is: "); pw.println("the array of employees is: "); for(int j=0; j<nelems; j++) { system.out.println(emp[j].empno + " " + emp[j].ssno + " " + emp[j].lastname + " " + emp[j].firstname); pw.println(emp[j].empno + " " + emp[j].ssno + " " + emp[j].lastname + " " + emp[j].firstname); } // end } // end delete } // end higharrayobject
start of next class
import java.io.*; public class project21rev extends higharrayobject //reference gaddis p.658 { public project21rev(int max) // subclass constructor { super(max); // call superclass constructor } public void getmax(printwriter pw) // new functionality required assignment { int maxindex = -1; // not found yet if(nelems == 0) system.out.println("number of elements 0"); else { int max = emp[0].empno; // assume first value largest maxindex = 0; (int = 1; < nelems; i++) //now check rest of values largest { if(emp[i].empno > max) { maxindex = i; } } system.out.println("the largest value " + emp[maxindex].empno + " " + emp[maxindex].ssno + " " + emp[maxindex].lastname + " " + emp[maxindex].firstname); pw.println("the largest value " + emp[maxindex].empno + " " + emp[maxindex].ssno + " " + emp[maxindex].lastname + " " + emp[maxindex].firstname); system.out.println("at location " + maxindex); pw.println("at location " + maxindex); } } public long removemax(printwriter pw) { long maxindex; maxindex = getmax(); delete(maxindex); return maxindex; } // modified find method follows public void find( int searchkey, printwriter pw) { system.out.println("trying find item employee number " + searchkey); pw.println("trying find item employee number " + searchkey); int j; boolean found = false; for(j=0; j < nelems; j++) if(emp[j].empno == searchkey) { found = true; system.out.println("found " + emp[j].empno + " " + emp[j].ssno + " " + emp[j].lastname + " " + emp[j].firstname); pw.println("found " + emp[j].empno + " " + emp[j].ssno + " " + emp[j].lastname + " " + emp[j].firstname); system.out.println("at location " + j); pw.println("at location " + j); } if(found == false) { system.out.println(searchkey + " not found"); pw.println(searchkey + " not found"); } } } class project21revapp { public static void main(string[] args) throws ioexception { // set printer output file printwriter pw = new printwriter(new bufferedwriter (new filewriter("output21.dat"))); int maxsize = 100; // array size project21rev arr; // reference array arr = new project21rev(maxsize); // create array arr.insert(77,"a","b","c",pw); arr.insert(99,"d","e","f",pw); arr.insert(44,"g","h","i",pw); arr.insert(55,"j","k","l",pw); arr.insert(22,"m","n","o",pw); arr.insert(88,"p","q","r",pw); arr.insert(11,"s","t","u",pw); arr.insert(00,"v","w","x",pw); arr.insert(66,"y","z","aa",pw); arr.insert(33,"bb","cc","dd",pw); arr.display(pw); // display items int searchkey = 35; // search item arr.find(searchkey, pw); searchkey = 22; // search item arr.find(searchkey, pw); arr.delete(00, pw); // delete 3 items arr.delete(55, pw); arr.delete(99, pw); arr.display(pw); // display items again // new functionality follows arr.getmax(pw); pw.close(); } // end main() } // end class project21revapp
well, error message tells what's wrong.
your method delete()
accepts 2 parameters:
public boolean delete(long value, printwriter pw) { // ... }
you trying call one:
delete(maxindex);
to fix error, pass right amount , types of parameters.
Comments
Post a Comment