COP 3337 Section U05 Spring 2017 LIST PROGRAMS ------------- Program 1: ---------- The class Person is shown below. public class Person { // the fields private String name; private int age; // construct a person with name n and age ag public Person(String n, int ag) { name = n; age = ag; } // return the name of the person public String getName() { return name; } // return the age of the person public int getAge() { return age; } // two persons are equal if they have the same name and age public boolean equals(Person p) { if ( p == null) return false; else return name.equals(p.name) && age == p.age; } // override toString public String toString() { return getClass().getSimpleName() + "[ name = " + name + } } Write the method public static void deleteNames( LinkedList list, char ch) that deletes from list all persons whose name begins with character ch. If list is empty or has no items then return. If the item is null or, its name is null or the empty string then do not delete it. For example if list = { new Person("Bill",75), new Person(null, 20), null, new Person("",16), new Person("Mark",55), new Person("Barbara",45)} and ch = 'B', after the method returns, the list must be { new Person(null, 20), null, new Person("",16), new Person("Mark",55)} The Answer: ----------- // delete from list all persons whose name begins with the letter ch public static void deleteNames(LinkedList list, char ch) { // check for null list if (list == null) return ; // nothing to do // create an iterator and traverse the list ListIterator it = list.listIterator(); while ( it.hasNext()) { // get the current person Person p = it.next(); if ( p != null) { // get the name String name = p.getName(); // check if name starts with ch if ( name != null && name.length() > 0 && name.charAt(0) == ch) it.remove(); } // end if } // end while } Problem 2: ---------- Write the method public static void deletePersons(int low, int high, LinkedList list) that traverses list and removes all persons whose age is >= low and <= high. The class Person was listed above. For example if the items of list are Person("John", 25), Person("Maria", 30), Person("Pepe", 15), Person("Jorge", 45), Person("Papa", 60), Person("Paul", 70) and the call is deletePersons(25, 45, list); after the method returns the list must be Person("Pepe", 15), Person("Papa", 60), Person("Paul", 70) If list is null just return. The class Person is the one from Problem 1. The Answer: ----------- // delete all persons aged from low to high (inclusive) public static void deletePersons(int low, int high, LinkedList list) { // check if list is null if (list == null) return; // set an iterator ListIterator it = list.listIterator(); // process the list while(it.hasNext()) { // get the item Person p = it.next(); // do we delete it? if (p != null) { // get the age int age = p.getAge(); // check if we remove the item if ( age >= low && age <= high) it.remove(); } } // end while } Problem 3: ---------- Write the method evaluate of the class Polynomial shown below. import java.util.LinkedList; import java.util.ListIterator; import java.util.Collections; public class Polynomial { // a linked list of Term objects sorted in the decreasing order of // the exponents private LinkedList poly; // the constructor and other methods public double evaluate( double x) { // you must write it } } // end Polynomial The class Term is shown below. The method evaluates the polynomial poly for the value x. For example, if poly consists of Term( -1.0, 3), Term(2.0, 2), Term(500,0) and x = 10, evaluate(10) returns the number -1.0 * (10 ** 3) + 2.0 * (10 ** 2) + 500 * (10 ** 0) = -300 where 10 ** 3, 10 ** 2, 10 ** 0 are 10 to the 3rd, 2nd, and 0 power, in this order. If poly is null or any of the terms in poly is null, throw a null pointer exception. // the class Term describes a term of polynomial a * Math( (double) n) // a is the coeeficient and n the exponent public class Term { // the fields private double coefficient; private int exponent; // construct a term with coefficient c and exponent i public Term( double c, int i) { coefficient = c; exponent = i; } // the get methods public double getCoefficient() { return coefficient; } public int getExponent() { return exponent; } } The Answer: ----------- // evaluate poly at x public double evaluate(double x) { // initialize sum double sum = 0.0; ListIterator pointer = poly.listIterator(); // process all terms while ( pointer.hasNext()) { // get the next term Term t = pointer.next(); // interpret the term sum += t.getCoefficient() * Math.pow(x, t.getExponent()); } return sum; } You can simplify this program by using an enhanced for loop. // evaluate poly at x public double evaluate(double x) { // initialize sum double sum = 0.0; // process all terms for ( Term t : poly) { sum += t.getCoefficient() * Math.pow(x, t.getExponent()); } return sum; }