CMPE 261 - Exercises

Java basics

  1. Complete the following code:

        class IntegerArray {
            int[] data;
            IntegerArray(int[] data) { 
                this.data=data; 
            }
            /**Find how many times the value appears in the array */
            int countValue(int value) {
    
    
    
    
    
    
    
            }
        }

Class design

  1. Implement a Person class to represent a person with his/her name, gender ("male" or "female"), and age.
  2. Implement a Group class which can store arbitrary number of Person instances (e.g. using java.util.ArrayList), and implements the following interface:

        interface GroupInterface {
            /** Adds given person to group, throws exception if the name already exists in the group */
            void addPerson(Person p) throws Exception;
            /** Returns list of all people in the group*/
            String[] getNames();
            /** Returns the person having the given name, throws exception if no such person in the group*/
            Person getPersonWithName(String name) throws Exception;
            /** Removes the person from the group, throws exception if the person is not in group*/
            void removePerson(String name) throws Exception;
            /**Returns average age of the group. Throws exception if the group is empty*/
            double averageAge() throws Exception;
        }
  3. We want to keep track of people's interests, which are expressed simply as strings (e.g. "computers", "cars", etc.). Modify your Person class so that it supports the following interface:

        interface InterestInterface {
            void addInterest(String i);
            /** Removes the interest from person's interests. Throws Exception if it is not among person's interests*/
            void deleteInterest(String i) throws Exception;
            /** Returns a list of interests*/
            String[] getInterests();
            /** Returns whether the given interest is among the person's interests
            boolean isInterested(String i);
        }
  4. Modify your Group class so that it supports the following interface:

        interface GroupInterestInterface {
            String[] interests();
            int interestedCount(String i);
            Person[] interestedIn(String i);
        }
  5. What are now the type compatibilities of: