Array and Arraylist Mid-Term 2006


Name ______________________________

public class employee
{ private String first,last;
private double salary; // these
private int age; // are written
public employee(String f,String l, double s, int a){} // for you
public String getFirst(){} // already
public String getLast(){}
public double getSalary(){}
public int getAge(){}
}

public class store
{private employee ar = new employee[50];
private int ct; 1. public boolean addAnEmployee(String f, String l, double s, int a) {} // if there is room
2. public void printAllStudentsNames(){} // first and last
3. public String getHighestPaidEmployee(){}
4. public void printNamesInThirties(){} // print people whose ages are in 30's
5. public int getAge(String last) // return -1 if not there
6. public String toString(){} // returns names of all Employees
7. public void delete (String last){}

Now try it with ArrayList

public class Store
{private ar = new ArrayList <employee >(); // holding employee class


8. public boolean addAnEmployee(String f, String l , double s, int a)

9. public void printAllEmployees(){}

10. public boolean delete(String last){}

11. public void fireEmployees(double d){} // remove employees making less than d

}

12. Write a method that will return the smallest value in an ArrayList of Integers. Note the value being returned is an int.

int GetSmallest(ArrayList<Integer> a)

13. Write a method that will delete the first occurence of a value from an ArrayList of Integers. The Arraylist is holding Integers. Return true or false from function.

boolean delete(ArrayList<Integer> a, int n)

14. Write a method that will shift everyone 2 spots in the ArrayList.
If ArrayList was 3 4 6 8 9 it would become 8 9 3 4 6

void shift2(ArrayList<Integer> a)

15. Write a method that will change any odd numbers to the next even number. arrayList is holding Integers.

void changeOdd(ArrayList<Integer> a)

2 7 4 6 9 would become 2 8 4 6 10

16. Write a function that will return the location of the biggest number in an ArrayList of Integer objects.

public int locationOfBiggest(ArrayList<Integer> a)

17. Write a function that will delete the biggest number in an ArrayList of Integer objects. You must call the function written in number 16 to get credit.
public void deleteBiggest(ArrayList<Integer> a)

18. There is an ArrayList set up in the following way. There is a name in one location followed by a phone number for that name in the next location.

Ex.
0 Joe Smith
1 336-4533
2 Tom Jones
3 456-5644

Write a function that will receive a name and then print out the phone number for that person. If the name is not in the list print an appropriate message.

public void printPhone(ArrayList<String> ar, String name)


19. Write a function that will return the average of all the even numbers that are in an ArrayList of Integer

public double getEvenAverage(ArrayList<Integer> ar)
// precondition : Arraylist is not empty and contains Integer objects