This homework involves the development of some relatively simple Java programs and classes. The main reason for this homework is to make sure that everyone is able to get a Java compiler running and to write some simple programs. Please contact us at javanetprog@cs.rpi.edu if you have problems!
Question 1: Simple Java Program
Write a program (a Java class with a public static void
main method) named "repeater" which takes which takes a number and a word on the
command line, and then prints out the given word that number of times.
Here is an example of what it should look like when we run your program:
$> java repeater 5 hi
hi
hi
hi
hi
hi
$> java repeater hi 5
Error: Usage: java repeater number word
|
Question 2: Simple Java Classes and Interfaces
Create a class Person. Each Person object should have a name associated with it.
Override Object's toString method to return it's name. This means that if
System.out.println is called on a Person object, the name attribute will
be printed.
Create an interface RPIPerson that has a method getIDNumber()
Create a class Student that extends your Person class and implements the
RPIPerson interface.
The Student should have a year, that should be printed out when the object is printed using
toString().
Create a class Professor that extends Person and implements RPIPerson.
The Professor should have a department,
represented by a String. This class should also
provide a toString() method.
Using the main file available here, add the students and professors to a
java.util.Vector. Then, using
java.util.Enumeration, go through each element of the
Vector and print out information about the students.
Also using Enumeration, print out the id
numbers by casting the return of .nextElement() to
RPIPerson.
Have your Person class implement java.lang.Comparable, and order people based on ID Numbers.
Use java.util.Arrays and the Vector you created in Part 2 to print the sorted list.
Put your Person, RPIPerson, Student, and Professor
classes into a package. Make the program compile and run again. Make sure to remove your old .class files for those classes.