By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

please help me (object modelling)

P: 4
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...
Nov 15 '06 #1
Share this Question
Share on Google+
8 Replies


Ganon11
Expert 2.5K+
P: 3,652
What have you done so far?
Nov 16 '06 #2

P: 4
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=gender;
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(boolean 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(givenHumanTable[i]);
}

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

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

// Selectors
int getnumberOfHumans() { 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.", getnumberOfHumans());
}
}

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

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

// Write statistics
void writeReport() {
System.out.print(town1);
}
void writeAllHumansToTerminalWindow() {
Human[] HumanTable = town1.getHumanTable();
int numberOfHumans = town1.getnumberOfHumans();
System.out.println("Number of Humans: " + numberOfHumans);
for (int i=0; i<numberOfHumans; i++)
System.out.println(HumanTable[i]);
}

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

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

// Write alle information about Humans to terminal window.
Paris.writeAllHumansToTerminalWindow();
Paris.writeReport();
}
}
Nov 17 '06 #3

Expert 100+
P: 1,510
a couple of things
(1) in class Human you missed the {} off the constructor
Expand|Select|Wrap|Line Numbers
  1. Human() {}     //** missing {}
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. new Human("Alex",Human.FEMALE,2,1),  // ** missing Human.
  2. new Human("John",Human.MALE,3,2)
  3.  
Nov 17 '06 #4

P: 4
a couple of things
(1) in class Human you missed the {} off the constructor
Expand|Select|Wrap|Line Numbers
  1. Human() {}     //** missing {}
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. new Human("Alex",Human.FEMALE,2,1),  // ** missing Human.
  2. new Human("John",Human.MALE,3,2)
  3.  

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",Human.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];
Nov 17 '06 #5

Expert 100+
P: 1,510
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",Human.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.
Nov 18 '06 #6

P: 4
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?
Nov 20 '06 #7

Expert 100+
P: 1,510
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
Nov 20 '06 #8

Ganon11
Expert 2.5K+
P: 3,652
...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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. string input;
  2. int x;
  3. double y;
  4. x = IN.nextInt(); // Looks for an integer type variable in the stream and stores into x
  5. y = IN.nextDouble(); // See nextInt, but used for doubles
  6. input = IN.nextLine(); // Gets an entire line of string input
Nov 20 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.