Programming in Java - Homework 1

Instructor: Moorthy
CS-2220-01 Summer 1998
Due: May 28, 11:59:59 PM

Objective

The objective of this homework is to introduce you to programming in Java. Part A asks you to write a simple application; in Part B, you will write an applet; Part C helps you understand differences between some of the primitive types in Java, and helps you learn to use the language constructs and classes of the Java API; in Part D you will define your first class.

Part A: Random Sentences

Write an application program that uses random number generation to create sentences. Use four arrays of strings called article, noun, verb and proposition. Create a sentence by selecting a word at random from each array in the following order article noun, verb, proposition, article and noun. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When a final sentence is output, it should start with a capital word and end with a period. The program should generate 20 sentences and output them one at a time.

The article array should contain "the","a","one","some", and "any"; the 
noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the
verb array should contain the verbs "drove", "jumped","ran","walked", 
and "skipped"; the proposition array should contain the propositions
"to", "from", "over", "under", and "on".

Part B: Applet

Write an applet for the above problem. The text is output in a text area.

Create an HTML file that can be used to run your applet.

Part C: R-Combinations

Write an application that accepts as a command line argument two numbers, n and r, and calculates the number of r-combinations of the n-set, C(n,r). C(n,r) is defined as follows:
C(n,r) = n * (n-1) * (n-2) * ... * (n-r+1)/ (1* 2 * ... * r)

Your calculation should be done six times, using six different variable types to store intermediate values: (1) as a byte; (2) as a short; (3) as a int; (4) as a long; (5) as a java.lang.Integer; and (6) as a java.math.BigInteger. In the comments of your code, explain the differences between each of these data types, and how these differences can affect the behavior of your program.

When you are writing code to perform this calculation, you should use several different techniques and language constructs to perform your computation. At a minimum, your code should demonstrate your knowledge of the following: (1) the while statement (JSS, page 102); (2) the for statement (JSS, page 196); and (3) recursion (JSS, pages 443-461).

Part D: Student Class

In this part, you will use Java to implement a class for maintaining Student information.
     Student
        first_name : STRING;
        last_name  : STRING;
        sex        : STRING;
        Class	   : STRING;
The corresponding Java Implementation is as follows:
public class Student {

public String first_name,last_name,sex,Class;

// Constructor
public Student() { 
first_name="Joe";last_name="Bob";sex="Male";
Class="Freshman";
}

public Student(String a, String b, String c, String d) {
first_name=a;last_name=b;sex=c;
Class=d;
}

public String  FindClass() {
return Class;
}
public void toPrint() { 
...
}
}


Assignment

Your assignment is to define a method toPrint in class Student in Java that prints the students first name, last name, sex and Class. generat
  • You should define a test case that demonstrates your program's correctnesss Make sure you demonstrate how to create new Student objects and fill each of the fields with meaningful data.
  • Your class should be documented. Before submission, you should execute the javadoc tool to generate the documentation, and you should include the documentation with your homework submission.