please help me (object modelling)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alphaville
    New Member
    • Nov 2006
    • 4

    please help me (object modelling)

    Hi. I hope someone can help me with this-

    First there is a category Human, which has information about name, gender, age and unique ID.

    Human shall be saved in a class Town using a table.

    The constructor Town(int numberhumans) has parameters, which says the number of humans the town can have (maximum). The program shall let the user do as followed:

    - make a Town
    - add a Human
    - change the information to the Human
    - delete the Human
    - get out a list with the context
    - print the context in Human to a text file
    - read the context in Human from a text file
    - print the Humans in correct order of age (1,2,3...).

    I hope someone can help me :) I really need it...
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What have you done so far?

    Comment

    • alphaville
      New Member
      • Nov 2006
      • 4

      #3
      Originally posted by Ganon11
      What have you done so far?
      Well, I done some stuff, but I'm uncertain about the rest. I have to classes and one client. It's working now, but as it should do. Now I've decided the Humans information, but this should also be done by the user. As you can see above, there are several things that ain't working... I hope you can help me a bit!

      class Human {
      // Constants
      final static boolean MALE = false;
      final static boolean FEMALE = true;

      // Field variables
      private String name;
      private boolean gender;
      private int age, ID;


      // Constructors
      Human()
      Human(String name, boolean gender, int age, int ID) {
      this.name=name;
      this.gender=gen der;
      this.age=age;
      this.ID=ID;
      }


      void putName(String HumanName) { name=HumanName; }
      void putAge(int HumanAge) { age=HumanAge; }
      void putID(int HumanID) { ID = HumanID; }
      void putGender(boole an givenGender) { gender= givenGender; }

      String getName() { return name; }
      int getAge() {return age; }
      int getID() {return ID; }
      boolean getGender() {return gender; }

      boolean isMALE() { return (gender == MALE); }
      boolean isFEMALE() { return (gender == FEMALE); }

      public String twoString() {
      String genderInfo;
      if (gender)
      genderInfo = "FEMALE";
      else
      genderInfo = "MALE";

      return String.format(" name: %s ID: %d age: %d" + "gender: %-11s", name, ID, age, genderInfo);
      }
      }

      public class Town {

      // Field declaration
      private Human[] HumanTable;
      private int numberOfHumans;

      // Standard constructor
      Town() {
      HumanTable = new Human[30];
      }

      // Establishes a Town from a Table with Humans
      Town(Human[] givenHumanTable ) {
      HumanTable = new Human[30];
      for(int i=0; i < givenHumanTable .length; i++)
      addHuman(givenH umanTable[i]);
      }

      // Constructor which receives field values as parameters.
      Town(Human[] HumanTable, int numberOfHumans) {
      this.HumanTable = HumanTable;
      this.numberOfHu mans = numberOfHumans;
      }

      // Places a Human in the Human Table.
      void addHuman(Human newHuman) {
      assert numberOfHumans != HumanTable.leng th: "No more room for Humans.";
      HumanTable[numberOfHumans] = newHuman;
      numberOfHumans+ +;
      }

      // Selectors
      int getnumberOfHuma ns() { return numberOfHumans; }
      Human[] getHumanTable() { return HumanTable; }

      // Returns Human which has a given ID.
      Human getHuman(int ID) {
      assert 0 <= ID && ID <= numberOfHumans : "Illegal ID-number";
      return HumanTable[ID];
      }

      // Returns statistics over Humans.
      public String twoString() {
      return String.format(" The Town has %d Humans.", getnumberOfHuma ns());
      }
      }

      import java.util.Scann er;
      public class Client1 {
      // Field
      private Town town1;

      // Constructor
      Client1() {
      town1 = new Town();
      }
      Client1(Town town1) {
      this.town1 = town1;
      }

      // Write statistics
      void writeReport() {
      System.out.prin t(town1);
      }
      void writeAllHumansT oTerminalWindow () {
      Human[] HumanTable = town1.getHumanT able();
      int numberOfHumans = town1.getnumber OfHumans();
      System.out.prin tln("Number of Humans: " + numberOfHumans) ;
      for (int i=0; i<numberOfHuman s; i++)
      System.out.prin tln(HumanTable[i]);
      }

      public static void main(String[] args) {
      // Establish a Table with Humans.
      Human[] HumanInfo = {
      new Human("Alex",FE MALE,2,1),
      new Human("John",MA LE,3,2)
      };

      // Establish a Town.
      Town register = new Town(HumanInfo) ;
      // Establish a Client.
      Client1 Paris = new Client1(registe r);

      // Write alle information about Humans to terminal window.
      Paris.writeAllH umansToTerminal Window();
      Paris.writeRepo rt();
      }
      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        a couple of things
        (1) in class Human you missed the {} off the constructor
        Code:
        Human() {}     //** missing {}
        did you mean to set the instance variables to default values?

        and in class Client1 in main() you missed the Human. off FEMALE and MALE (which are data members of Human) in the statements
        Code:
        new Human("Alex",Human.FEMALE,2,1),  // ** missing Human.
        new Human("John",Human.MALE,3,2)

        Comment

        • alphaville
          New Member
          • Nov 2006
          • 4

          #5
          Originally posted by horace1
          a couple of things
          (1) in class Human you missed the {} off the constructor
          Code:
          Human() {}     //** missing {}
          did you mean to set the instance variables to default values?

          and in class Client1 in main() you missed the Human. off FEMALE and MALE (which are data members of Human) in the statements
          Code:
          new Human("Alex",Human.FEMALE,2,1),  // ** missing Human.
          new Human("John",Human.MALE,3,2)

          Yeah, you're correct. Had actually done that, seems like it got lost... The program worked when I compiled it. Probably forgot to save the last changes.

          And yes, this: new Human("John",Hu man.MALE,3,2). This should be default. Should be read in from the keyboard by the user of the program. The same is this: HumanTable = new Human[30];

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by alphaville
            Yeah, you're correct. Had actually done that, seems like it got lost... The program worked when I compiled it. Probably forgot to save the last changes.

            And yes, this: new Human("John",Hu man.MALE,3,2). This should be default. Should be read in from the keyboard by the user of the program. The same is this: HumanTable = new Human[30];
            Your program appears to have much of the functionality required and you just need to test it.
            A problem you do have is the output from main() is not correct -change the name of function twoString() in class Human and class Town to be called toString(). Fix it and the program output is then
            Number of Humans: 2
            name: Alex ID: 1 age: 2gender: FEMALE
            name: John ID: 2 age: 3gender: MALE
            The Town has 2 Humans.

            Comment

            • alphaville
              New Member
              • Nov 2006
              • 4

              #7
              Originally posted by horace1
              Your program appears to have much of the functionality required and you just need to test it.
              A problem you do have is the output from main() is not correct -change the name of function twoString() in class Human and class Town to be called toString(). Fix it and the program output is then
              Number of Humans: 2
              name: Alex ID: 1 age: 2gender: FEMALE
              name: John ID: 2 age: 3gender: MALE
              The Town has 2 Humans.
              I fixed it, but the problem is that now I've just made some input (Alex, 1, 2, FEMALE), but this should be done by the user of the program. Do you know how to fix that?

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                Originally posted by alphaville
                I fixed it, but the problem is that now I've just made some input (Alex, 1, 2, FEMALE), but this should be done by the user of the program. Do you know how to fix that?
                to read information from an input stream you can overload the >> (get from or extraction) operator, see
                http://www.fredosaurus .com/notes-cpp/oop-friends/overload-io.html

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  ...but not in Java. >> is a C++ operator.

                  In Java, I believe the new standard (as of Java5) is to use a Scanner class as follows:

                  Code:
                  Scanner IN = new Scanner(System.in);
                  You can display an appropriate message to the user prompting input, and then use the different functions of the Scanner class to retrieve that input and process it. The ones I have seen are:

                  Code:
                  string input;
                  int x;
                  double y;
                  x = IN.nextInt(); // Looks for an integer type variable in the stream and stores into x
                  y = IN.nextDouble(); // See nextInt, but used for doubles
                  input = IN.nextLine(); // Gets an entire line of string input

                  Comment

                  Working...