Hello, math01,
Below you will find a complete implementation as requested. Place this
code in a file called "School.java" and compile that file with your
java compiler; with the Sun JDK, the command would be "javac
School.jave". This will produce several .class files. Then, run the
main method of School.class. If you are using the Sun JDK, the command
to do that will be "java School".
If you have any questions about the code, feel free to ask. It is very
straightforward. The "School" class, which contains the main() method,
generates random students and employees and has a few helper methods
and arrays of strings to make that task easier. The rest is completely
self-explanatory.
Thank you for the opportunity to answer this question.
* * *
class Person {
static final int male = 0;
static final int female = 1;
String name;
String ssec;
int age;
int gender;
String address;
String phone;
Person(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg)
{
name = nameArg;
ssec = ssecArg;
age = ageArg;
gender = genderArg;
address = addressArg;
phone = phoneArg;
}
}
void information()
{
System.out.println("Name: " + name);
System.out.println("Social Security #: " + ssec);
System.out.println("Age: " + age);
System.out.print("Gender: ");
if (gender == male) {
System.out.println("Male");
} else {
System.out.println("Female");
}
System.out.println("Address: " + address);
System.out.println("Phone: " + phone);
}
}
class Student extends Person
{
double gpa;
String major;
int year;
Student(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
double gpaArg,
String majorArg,
int yearArg)
{
// Invoke Person constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg);
gpa = gpaArg;
major = majorArg;
year = yearArg;
}
void information()
{
System.out.println("Student");
super.information();
System.out.println("GPA: " + gpa);
System.out.println("Major: " + major);
System.out.println("Year of Grad.: " + year);
}
}
class Employee extends Person
{
String department;
String title;
int hired;
Employee(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
String departmentArg,
String titleArg,
int hiredArg)
{
{
// Invoke Person constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg);
department = departmentArg;
title = titleArg;
hired = hiredArg;
}
void information()
{
super.information();
System.out.println("Department: " + department);
System.out.println("Title: " + title);
System.out.println("Year Hired: " + hired);
}
}
class HourlyEmployee extends Employee
{
double rate;
HourlyEmployee(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
String departmentArg,
String titleArg,
int hiredArg,
double rateArg)
{
// Invoke Employee constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg, departmentArg, titleArg, hiredArg);
rate = rateArg;
}
void information()
{
System.out.println("Hourly Employee");
super.information();
System.out.println("Hourly Rate: " + rate);
}
}
class NonhourlyEmployee extends Employee
{
double level;
NonhourlyEmployee(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
String departmentArg,
String titleArg,
int hiredArg,
double levelArg)
{
// Invoke Employee constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg, departmentArg, titleArg, hiredArg);
level = levelArg;
}
void information()
{
System.out.println("Nonhourly Employee");
super.information();
System.out.println("Pay Level: " + level);
}
}
public class School
{
static String randomString(String[] args)
{
return args[(int) (Math.random() * args.length)];
}
static String randomStringOfDigits(int digits, int dash1, int dash2)
{
int i;
String result = "";
for (i = 0; (i < digits); i++) {
char ch = (char) ('0' + ((int) (Math.random() * 10)));
if ((i == dash1) || (i == dash2)) {
result += "-";
}
result += ch;
}
return result;
}
static String maleNames[] = {
"John",
"Joe",
"Larry",
"Moe",
"Curly",
"Dave",
"Stephen",
"Carl",
"Fred",
"Percy",
"Harry",
"Frodo",
"Sam",
"Aragorn",
"Chris"
};
static String femaleNames[] = {
"Jane",
"Jen",
"Jessica",
"Carly",
"Joanna",
"Linda",
"Jamie",
"Michelle",
"Sarah",
"Eowyn",
"Patricia",
"Gloria"
};
static String lastNames[] = {
"Smith",
"Jones",
"Smith-Jones",
"Davis",
"Farnsworth",
"Baggins",
"O'Malley",
"Turnside",
"Bigglestein"
};
static String titles[] = {
"Vice-President",
"Dean",
"Vice-Dean",
"Provost",
"Assistant Provost",
"Janitor",
"Cook",
"Instructor",
"Professor",
"Bottle-Washer"
};
static String departments[] = {
"Computer Science",
"Art",
"English",
"Spanish",
"French",
"German",
"Music",
"Chemical Engineering",
"Animal Husbandry"
};
static String streets[] = {
"1st Av",
"2nd Av",
"3rd Av",
"4th Av",
"5th Av",
"6th Av",
"7th Av",
"8th Av",
"9th Av",
"10th Av"
};
public static void main(String[] args)
{
int i;
Person people[] = new Person[30];
for (i = 0; (i < 10); i++) {
int gender = (int) (Math.random() * 2);
String name;
if (gender == Person.male) {
name = randomString(maleNames);
} else {
name = randomString(femaleNames);
}
people[i] = new Student(
name + " " + randomString(lastNames),
randomStringOfDigits(9, 3, 5),
(int) (Math.random() * 50.0) + 16,
gender,
(int) (Math.random() * 1000) + " " +
randomString(streets),
randomStringOfDigits(10, 3, 6),
((int) (Math.random() * 4 * 100)) / 100.0,
randomString(departments),
2003 + (int) (Math.random() * 6));
}
for (i = 10; (i < 20); i++) {
int gender = (int) (Math.random() * 2);
String name;
if (gender == Person.male) {
name = randomString(maleNames);
} else {
name = randomString(femaleNames);
}
people[i] = new HourlyEmployee(
name + " " + randomString(lastNames),
randomStringOfDigits(9, 3, 5),
(int) (Math.random() * 50.0) + 16,
gender,
(int) (Math.random() * 1000) + " " +
randomString(streets),
randomStringOfDigits(10, 3, 6),
randomString(departments),
randomString(titles),
1920 + (int) (Math.random() * 84),
7.00 + ((int) Math.random() * 50 * 100)
/ 100.0);
}
for (i = 20; (i < 30); i++) {
int gender = (int) (Math.random() * 2);
String name;
if (gender == Person.male) {
name = randomString(maleNames);
} else {
name = randomString(femaleNames);
}
people[i] = new NonhourlyEmployee(
name + " " + randomString(lastNames),
randomStringOfDigits(9, 3, 5),
(int) (Math.random() * 50.0) + 16,
gender,
(int) (Math.random() * 1000) + " " +
randomString(streets),
randomStringOfDigits(10, 3, 6),
randomString(departments),
randomString(titles),
1920 + (int) (Math.random() * 84),
15000 + 1000 * ((int) (Math.random() * 50)));
}
for (i = 0; (i < 30); i++) {
System.out.println("Person #" + (i + 1));
people[i].information();
System.out.println();
}
}
} |
Clarification of Answer by
majortom-ga
on
24 Feb 2004 12:01 PST
My apologies! I am not sure what happened, but the code changed during
the copy-and-paste process. I suspect it was due to the need to copy
and paste separate pages of text. I'm using a different text editor
this time to ensure the copy and paste is one smooth step, and I'm
copying the code *back* to my system from this answer window and
compiling it now to be sure...
... OK, yes, it is definitely the correct code this time! Again, my
apologies for the code-pasting error.
*** Code Follows *** Cut Here ***
class Person {
static final int male = 0;
static final int female = 1;
String name;
String ssec;
int age;
int gender;
String address;
String phone;
Person(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg)
{
name = nameArg;
ssec = ssecArg;
age = ageArg;
gender = genderArg;
address = addressArg;
phone = phoneArg;
}
void information()
{
System.out.println("Name: " + name);
System.out.println("Social Security #: " + ssec);
System.out.println("Age: " + age);
System.out.print("Gender: ");
if (gender == male) {
System.out.println("Male");
} else {
System.out.println("Female");
}
System.out.println("Address: " + address);
System.out.println("Phone: " + phone);
}
}
class Student extends Person
{
double gpa;
String major;
int year;
Student(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
double gpaArg,
String majorArg,
int yearArg)
{
// Invoke Person constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg);
gpa = gpaArg;
major = majorArg;
year = yearArg;
}
void information()
{
System.out.println("Student");
super.information();
System.out.println("GPA: " + gpa);
System.out.println("Major: " + major);
System.out.println("Year of Grad.: " + year);
}
}
class Employee extends Person
{
String department;
String title;
int hired;
Employee(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
String departmentArg,
String titleArg,
int hiredArg)
{
// Invoke Person constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg);
department = departmentArg;
title = titleArg;
hired = hiredArg;
}
void information()
{
super.information();
System.out.println("Department: " + department);
System.out.println("Title: " + title);
System.out.println("Year Hired: " + hired);
}
}
class HourlyEmployee extends Employee
{
double rate;
HourlyEmployee(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
String departmentArg,
String titleArg,
int hiredArg,
double rateArg)
{
// Invoke Employee constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg, departmentArg, titleArg, hiredArg);
rate = rateArg;
}
void information()
{
System.out.println("Hourly Employee");
super.information();
System.out.println("Hourly Rate: " + rate);
}
}
class NonhourlyEmployee extends Employee
{
double level;
NonhourlyEmployee(String nameArg,
String ssecArg,
int ageArg,
int genderArg,
String addressArg,
String phoneArg,
String departmentArg,
String titleArg,
int hiredArg,
double levelArg)
{
// Invoke Employee constructor
super(nameArg, ssecArg, ageArg, genderArg, addressArg,
phoneArg, departmentArg, titleArg, hiredArg);
level = levelArg;
}
void information()
{
System.out.println("Nonhourly Employee");
super.information();
System.out.println("Pay Level: " + level);
}
}
public class School
{
static String randomString(String[] args)
{
return args[(int) (Math.random() * args.length)];
}
static String randomStringOfDigits(int digits, int dash1, int dash2)
{
int i;
String result = "";
for (i = 0; (i < digits); i++) {
char ch = (char) ('0' + ((int) (Math.random() * 10)));
if ((i == dash1) || (i == dash2)) {
result += "-";
}
result += ch;
}
return result;
}
static String maleNames[] = {
"John",
"Joe",
"Larry",
"Moe",
"Curly",
"Dave",
"Stephen",
"Carl",
"Fred",
"Percy",
"Harry",
"Frodo",
"Sam",
"Aragorn",
"Chris"
};
static String femaleNames[] = {
"Jane",
"Jen",
"Jessica",
"Carly",
"Joanna",
"Linda",
"Jamie",
"Michelle",
"Sarah",
"Eowyn",
"Patricia",
"Gloria"
};
static String lastNames[] = {
"Smith",
"Jones",
"Smith-Jones",
"Davis",
"Farnsworth",
"Baggins",
"O'Malley",
"Turnside",
"Bigglestein"
};
static String titles[] = {
"Vice-President",
"Dean",
"Vice-Dean",
"Provost",
"Assistant Provost",
"Janitor",
"Cook",
"Instructor",
"Professor",
"Bottle-Washer"
};
static String departments[] = {
"Computer Science",
"Art",
"English",
"Spanish",
"French",
"German",
"Music",
"Chemical Engineering",
"Animal Husbandry"
};
static String streets[] = {
"1st Av",
"2nd Av",
"3rd Av",
"4th Av",
"5th Av",
"6th Av",
"7th Av",
"8th Av",
"9th Av",
"10th Av"
};
public static void main(String[] args)
{
int i;
Person people[] = new Person[30];
for (i = 0; (i < 10); i++) {
int gender = (int) (Math.random() * 2);
String name;
if (gender == Person.male) {
name = randomString(maleNames);
} else {
name = randomString(femaleNames);
}
people[i] = new Student(
name + " " + randomString(lastNames),
randomStringOfDigits(9, 3, 5),
(int) (Math.random() * 50.0) + 16,
gender,
(int) (Math.random() * 1000) + " " +
randomString(streets),
randomStringOfDigits(10, 3, 6),
((int) (Math.random() * 4 * 100)) / 100.0,
randomString(departments),
2003 + (int) (Math.random() * 6));
}
for (i = 10; (i < 20); i++) {
int gender = (int) (Math.random() * 2);
String name;
if (gender == Person.male) {
name = randomString(maleNames);
} else {
name = randomString(femaleNames);
}
people[i] = new HourlyEmployee(
name + " " + randomString(lastNames),
randomStringOfDigits(9, 3, 5),
(int) (Math.random() * 50.0) + 16,
gender,
(int) (Math.random() * 1000) + " " +
randomString(streets),
randomStringOfDigits(10, 3, 6),
randomString(departments),
randomString(titles),
1920 + (int) (Math.random() * 84),
7.00 + ((int) Math.random() * 50 * 100)
/ 100.0);
}
for (i = 20; (i < 30); i++) {
int gender = (int) (Math.random() * 2);
String name;
if (gender == Person.male) {
name = randomString(maleNames);
} else {
name = randomString(femaleNames);
}
people[i] = new NonhourlyEmployee(
name + " " + randomString(lastNames),
randomStringOfDigits(9, 3, 5),
(int) (Math.random() * 50.0) + 16,
gender,
(int) (Math.random() * 1000) + " " +
randomString(streets),
randomStringOfDigits(10, 3, 6),
randomString(departments),
randomString(titles),
1920 + (int) (Math.random() * 84),
15000 + 1000 * ((int) (Math.random() * 50)));
}
for (i = 0; (i < 30); i++) {
System.out.println("Person #" + (i + 1));
people[i].information();
System.out.println();
}
}
}
|