Google Answers Logo
View Question
 
Q: Java Programming ( Answered,   0 Comments )
Question  
Subject: Java Programming
Category: Computers > Programming
Asked by: math01-ga
List Price: $30.00
Posted: 23 Feb 2004 10:13 PST
Expires: 24 Mar 2004 10:13 PST
Question ID: 309901
A student is a person, and so is an employee. Create a class Person
that has the data attributes common to both students and employees
(name, social security number, age, gender, address, and telephone
number), and appropriate method definitions, including a constructor.
In addition to the features of a Person, a Student has a grade-point
average (GPA), major, and year of graduation. An Employee has a
department, job title, and year of hire. In addition, Employees can be
divided into hourly employees (who have an hourly rate) and nonhourly
employees (who have an annual pay level).

Define a class hierarchy for these abstractions. Create a method named
information(). This method will display in the standard output
information about the receiving object. This method must be overridden
in each of the various child classes. Write an application class that
will create a number of each of these abstractions, store them in an
array of type Person, and then display the information stored in the
array.

Request for Question Clarification by answerguru-ga on 23 Feb 2004 12:44 PST
Hi math01...nice to see you again :)

You may want to consider reevaluating your list price as this task,
while not extremely difficult for a programmer type, is definitely
worth more than $20.

Thanks,
answerguru-ga
Answer  
Subject: Re: Java Programming
Answered By: majortom-ga on 24 Feb 2004 08:49 PST
 
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();
                }
        }
}

Request for Answer Clarification by math01-ga on 24 Feb 2004 11:19 PST
Hi majortom-ga,

Tried compiling and running the program but keep getting these error messages:

Error: class or interface declaration expected.
school.java line 25      void information()

Error: '}' expected.
school.java line 95      }

Error: statement expected.
school.java  line 96   void information()

Error: method information() not found in class person.
school.java  line 98 super.information();

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();
		}
	}
}

Request for Answer Clarification by math01-ga on 24 Feb 2004 12:44 PST
Hi again majortom-ga,

I am getting this time exception in thread "main"
java.long.NoClassDefFoundError. school

Request for Answer Clarification by math01-ga on 24 Feb 2004 12:55 PST
It is now ok.

Clarification of Answer by majortom-ga on 24 Feb 2004 12:57 PST
Great, from your third posting it sounds like everything is working
correctly. Do let me know if there are further issues. Thanks for the
chance to tackle this question.
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy