CSC 120

Week 9


To do:

Finish Programming Assignment I.
Read Section 6.6 - 6.9

Sample Programs:

Here are some of the sample programs, and more, we looked at in class:

Counter1.java. and Counter1Test.java
Counter2.java. and Counter2Test.java
Counter3.java. and CounterTest3.java
Counter4.java. and CounterTest4.java

Lab/Homework Assignments:


0. Please look at the previous week's page for resources on graphics. Create a personal web page. Make sure to have fun with it.

1. Recall that Math.random() returns a random number between 0.0 and 1.0. Divide this interval into four equal subintervals. Generate 1,000 random numbers and report how many lands in each subinterval. Your program should include a Counter class like the one we discussed. In the main program you should instantiate four copies of your Counter to count the numbers in subintervals. Here are the programs Counter1.java. and Counter1Test.java you can modify.

Do you get about the same number of random numbers in each subinterval?

2. Here is a class Student.java.


        public class Student {
        
           private String name;                 // Student's name.
           private double test1, test2, test3;   // Grades on three tests.
             
           public void setName(String theName) {
                // Mutator method for setting value of private name
                name = theName;
           }
           
           public String getName() {
                // Accessor method for reading value of private name
                // instance variable, name.
              return name;
           }
           
           public void setTest1(double theTest1) {
                // Mutator method for setting value of private test1
                test1 = theTest1;
           }
           
           public double getTest1() {
                // Accessor method for reading value of private 
                // instance variable, test1
              return test1;
           }
           public double getAverage() { 
                // Compute average test grade.
              return (test1 + test2 + test3) / 3;
           }

        }  // end of class Student

First, add to this class new instance methods setTest2(), getTest2(), setTest3(), getTest3(). Next, write a driver program which creates two Student objects, sets names, three test scores. Then use the get methods to print out the name, the test scores, and the average of of each student.