Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,257 Java Programmers. There are 2,041 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

problem

 
Reply to this topicStart new topic

problem, i am not sure what to do

webbywebb
post 7 May, 2008 - 08:57 AM
Post #1


New D.I.C Head

*
Joined: 30 Jan, 2007
Posts: 43



here is my code but i am not sure wha to do part of the code is missing the part that is miss is the problem


CODE

import java.io.*;
import java.util.Scanner;
import becker.util.DateTime;
import becker.util.Test;


/** A list of the "bigs" and "littles" associated with a Big Brother/Big Sister
* program.  "Bigs" are the Big Brothers and Big Sisters;  "littles" are
* the Little Brothers and Sisters they are (potentially) paired with.
* @author Byron Weber Becker */
public class BigBroBigSis extends Object
{    
   private Person[] persons;        // the list of bigs and littles
    
   /** Construct a new object by reading all the bigs and littles from a file.
    * @param fileName the name of the file storing the information for bigs and littles */
   public BigBroBigSis(String fileName)
   {  super();
    
      // count the number of Persons in the file
      int count = 0;
      Scanner in = this.openFile(fileName);
      while (in.hasNextLine())
      {  Person p = new Person(in);
         count++;
      }
      in.close();
        
      // allocate an array to hold each object we read
      this.persons = new Person[count];
        
      // Read the data, storing a reference to each object in the array
      in = this.openFile(fileName);
      for (int i=0; i<count; i++)
      {  this.persons[i] = new Person(in);    
      }
      in.close();

   }
    
   /** Swap the elements at indices a and b. */
   private void swap(int a, int b)
   {  Person temp = this.persons[a];
      this.persons[a] = this.persons[b];
      this.persons[b] = temp;
   }

   /** Calculate the average age of persons in the array. */
   public double calcAverageAge()
   {  int sumAges = 0;            
      for (int i=0; i<this.persons.length; i++)
      {  Person p = this.persons[i];
         sumAges = sumAges + p.getAge();
      }
      return (double) sumAges/this.persons.length;
   }

   /** Find the average age of the "littles". */
   public double getAverageLittleAge()
   {  int sumAges = 0;                
      int numLittles = 0;            
      for (Person p : this.persons)
      {  if (p.getRole() == Role.LITTLE)
         {  sumAges = sumAges + p.getAge();
            numLittles = numLittles + 1;
         }
      }
      return (double) sumAges/numLittles;
   }

   /** Search for the first person object matching the given name.
    * @param name The name of the person to find (the key).
    * @returns The first matching person object;  null if there is none. */
   public Person search(String name)
   {  for (int i=0; i<this.persons.length; i++)
      {  Person p = this.persons[i];
          if (p.getName().equals(name))
         {  return p;            // success!  Exit the loop
         }
      }
      return null;                // failure
   }
    
    /** Search for the first person object matching the given name
    * @param name The name of the person to find (the key) */
    public Person searchAlt(String name)
    {    int i = 0;                
        while (i < this.persons.length &&
                    !this.persons[i].getName().equalsIgnoreCase(name))
        {    i++;
        }
    
        if (i == this.persons.length)
        {    return null;            // failure:  got to the end without finding it
        } else
        {    return this.persons[i];    // success
        }
    }
    
   /** Find the oldest person in the list.
    * (Assumes there is at least one person in the array. */
   public Person findOldestPerson()
   {  Person oldestSoFar = this.persons[0];    
        
      for (Person currentPerson : this.persons)
      {  if (currentPerson.getAge() > oldestSoFar.getAge())
         {  oldestSoFar = currentPerson;
         }
      }
      return oldestSoFar;
   }

    
   /** Sort the list of persons in alphabetical order by name. */
   public void sort()
   {  for (int firstUnsorted=0; firstUnsorted<this.persons.length-1; firstUnsorted++)
      {  int extremeIndex = this.findExtreme(firstUnsorted);
         this.swap(firstUnsorted, extremeIndex);
      }
   }
    
   /** Find the extreme element in the unsorted portion of the array.
    * @param indexToStart the smallest index in the unsorted portion of the array
    * @return the index of the extreme element. */
   private int findExtreme(int indexToStart)
   {  int indexBestSoFar = indexToStart;
      String nameBestSoFar = this.persons[indexBestSoFar].getName();
      for (int i=indexToStart+1; i<this.persons.length; i++)
      {  String currPersonName = this.persons[i].getName();
         if (currPersonName.compareTo(nameBestSoFar) < 0)
         {  indexBestSoFar = i;
            nameBestSoFar = this.persons[i].getName();
         }
      }
      return indexBestSoFar;
   }
    
   /** Sort the persons array in increasing order by age */
   public void sortByAge()
   {  for (int firstUnsorted=0; firstUnsorted<this.persons.length-1; firstUnsorted++)
      {  // find the youngest unsorted person
         int extremeIndex = firstUnsorted;
         for (int i=firstUnsorted + 1; i<this.persons.length; i++)
         {  if (this.persons[i].getAge() < this.persons[extremeIndex].getAge())
            {  extremeIndex = i;
            }
         }
            
         // swap the youngest unsorted person with the person at firstUnsorted
         Person temp = this.persons[extremeIndex];
         this.persons[extremeIndex] = this.persons[firstUnsorted];
         this.persons[firstUnsorted] = temp;
      }
   }

    
   /** Extract a subset of all the persons who have the given gender and role.
    * @param g The gender of all members of the subset
    * @param r The role of all members of the subset */
   public Person[] extractSubset(Gender g, Role r)
   {  int ssSize = this.countSubset(g, r);
      Person[] subset = new Person[ssSize];
      this.fillSubset(subset, g, r);
      return subset;
   }
    
  [b] /** Count the number of persons matching the given gender and role.
    *  @param g The gender of persons to be included in the subset
    *  @param r The role of the persons to be included in the subset.  */
   private int countSubset(Gender g, Role r)
   {  // To be completed as an exercise.
      return 0;
   }[/b]  
   /** Fill the subset array with Person objects matching the given gender and role.
    * @param subset  The array to fill with elements belonging to the subset.
    *  @param g The gender of persons to be included in the subset
    *  @param r The role of the persons to be included in the subset.  */
   private void fillSubset(Person[] subset, Gender g, Role r)
   {  int ssPos = 0;    // position within the subset
      int arrPos = 0;   // position within the array
      while (ssPos < ss.length)
      {  Person p = this.persons[arrPos];
         if (p.getGender() == g && p.getRole() == r)
         {  subset[ssPos] = p;
            ssPos++;
         }
         arrPos++;
      }
   }
  
  
  
    
    
   /** Find the most compatible person for p.  
    * @param p The person to find a match for
    * @returns The most compatible person for p; null if there isn't one
    * (e.g. everyone else is already matched) */
   public Person findMostCompatible(Person p)
   {  Person mostCompatibleSoFar = null;
      double compatScore = -1.0;        // less than any valid compatibility score
      for (int i=1; i<this.persons.length; i++)
      {  if (this.persons[i].getCompatibility(p) > compatScore)
         {  mostCompatibleSoFar = this.persons[i];
            compatScore = mostCompatibleSoFar.getCompatibility(p);
         }
      }
      return mostCompatibleSoFar;
   }
    
   /** Find the oldest person in the list.
    * (Assumes there is at least one person in the array. */
   public Person findFirstBirthday()
   {  Person oldestSoFar = this.persons[0];    // role: most-wanted holder
      DateTime oldestBirthdate = oldestSoFar.getBirthdate();    // role: follower
        
      for (int i=1; i<this.persons.length; i++)
      {  Person currentPerson = this.persons[i];    // role: temporary
         DateTime currentBirthdate = currentPerson.getBirthdate();    // role: follower
         if (currentBirthdate.isBefore(oldestBirthdate))
         {  oldestSoFar = currentPerson;
            oldestBirthdate = currentBirthdate;
         }
      }
      return oldestSoFar;
   }
        
   /** List all the bigs and littles on System.out. */
   public void listAll()
   {  for (int i=0; i<this.persons.length; i++)
      {  System.out.println(this.persons[i].toString());
      }    
   }
    
   /** List, on System.out, all the compatible persons.
    * @param p The person to be compatible with
    * @param minCompatibility The minimum acceptable compatibility score */
   public void listCompatible(Person p, double minCompatibility)
   {  for (int i = 0; i<this.persons.length; i++)
      {  Person other = this.persons[i];
         if (other.getCompatibility(p) > minCompatibility)
         {  System.out.println(other.getCompatibility(p) + "  " + other);
         }
      }
   }
    
   /** Write the entire list of bigs and littles to a file.
    * @param out The already open output file. */
   public void write(PrintWriter out)
   {  for (int i = 0; i<this.persons.length; i++)
      {  this.persons[i].write(out);
      }
   }
    
   /** Count the number of people of the given characteristics. */
   public int count(Gender g)
   {  int count = 0;
      for (int i=0; i<this.persons.length; i++)
      {  if (this.persons[i].getGender() == g)
         {  count++;
         }
      }
      return count;
   }
    
   /** Sort the list of persons in alphabetical order by name. */
  
   /*
    public void sort()
    {    for(int firstUnsorted=0; firstUnsorted<this.persons.length-1; firstUnsorted++)
    {    // find min in unsorted part of the list
    int min = firstUnsorted;
    for(int i=firstUnsorted+1; i<this.persons.length; i++)
    {    if (this.persons[i].getName().compareTo(this.persons[min].getName()) < 0)
    {    min = i;
    }
    }
    
    // swap
    Person temp = this.persons[firstUnsorted];
    this.persons[firstUnsorted] = this.persons[min];
    this.persons[min] = temp;
    }
    }
    */
    
   /** Sort the list of persons according to a caller-specified criteria.
    * @param comparator An object implementing the Comparator interface
    *    that is used to define the ordering of the person objects */
    /*
   public void sort(Comparator<Person> comparator)
   {  for (int firstUnsorted=0; firstUnsorted<this.persons.length-1; firstUnsorted++)
      {  // find min in unsorted part of the list
         int min = firstUnsorted;
         for (int i=firstUnsorted+1; i<this.persons.length; i++)
         {  if (comparator.compare(this.persons[i], this.persons[min]) < 0)
            {  min = i;
            }
         }
            
         // swap
         Person temp = this.persons[firstUnsorted];
         this.persons[firstUnsorted] = this.persons[min];
         this.persons[min] = temp;
      }
   }
    */
    
    /** Open the named data file. */
    private Scanner openFile(String fileName)
    {  try
      {  File f = new File(fileName);
         return new Scanner(f);
      }
      catch (FileNotFoundException ex)
      {  System.out.println(ex.getMessage());
         System.out.println("working directory = " + System.getProperty("user.dir"));
         System.exit(1);
         return null;
      }
    }

    
   public static void main(String[] args)
   {  System.out.println("Testing BigBroBigSis");
      BigBroBigSis bbbs = new BigBroBigSis("test.txt");
      Test.ckIsNotNull("persons not null", bbbs.persons);
      Test.ckEquals("numPersons", 6, bbbs.persons.length);
      Test.ckEquals("name of 1st person", "Kenneth A Parsons",
            bbbs.persons[0].getName());
            
      Person[] maleLittles = bbbs.extractSubset(Gender.MALE, Role.LITTLE);
      Test.ckEquals("size of subset", 2, maleLittles.length);
      Test.ckEquals("name 1", "Kenneth A Parsons", maleLittles[0].getName());
      Test.ckEquals("name 2", "Roydyn A. Clayton", maleLittles[1].getName());
   }
}



here is the missing part:

CODE


   /** Count the number of persons matching the given gender and role.
    *  @param g The gender of persons to be included in the subset
    *  @param r The role of the persons to be included in the subset.  */
   private int countSubset(Gender g, Role r)
   {  //not sure what to do here
      return 0;
   }


This post has been edited by webbywebb: 7 May, 2008 - 08:58 AM


Attached File(s)
Attached File  becker.zip ( 350.77k ) Number of downloads: 5
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


jjsaw5
post 7 May, 2008 - 12:34 PM
Post #2


D.I.C Addict

Group Icon
Joined: 4 Jan, 2008
Posts: 536

so your missing part of your code, which some how you have....shouldn't you just put the missing piece into the code?

Or don't you know where to put it?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

webbywebb
post 7 May, 2008 - 09:03 PM
Post #3


New D.I.C Head

*
Joined: 30 Jan, 2007
Posts: 43

i am not sure what to put as the missing code
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pbl
post 7 May, 2008 - 09:12 PM
Post #4


D.I.C Addict

Group Icon
Joined: 6 Mar, 2008
Posts: 843

QUOTE(webbywebb @ 7 May, 2008 - 09:03 PM) *

i am not sure what to put as the missing code

so jjsaw5 it is the blind leading the blind
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

webbywebb
post 8 May, 2008 - 03:22 PM
Post #5


New D.I.C Head

*
Joined: 30 Jan, 2007
Posts: 43

can someone help me
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 09:44AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month