Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Programming help  (Read 3130 times)

0 Members and 1 Guest are viewing this topic.

NDBAM

  • Guest
Programming help
« on: May 13, 2009, 08:15:39 AM »
im having trouble understanding this programing class i have and how to complete it so if you could please help it would be greatly appreciated. im not sure how to create the constructor and complete the data in exercise 1, and in exercise 2 i dont understand what to complete and how to complete it.
"
Exercise 1 ― Constructor and Print
Complete the constructor in the following program so that it constructs the array data, then copies values from the parameter array into data.

Then complete the print() method. If you want, write it so that it prints seven values per line, except for the last line.

import java.io.* ;

class Weight
{
  int[] data;
 
  // Constructor
  Weight(int[] init)
  {
    // Construct the array the same length
    // as that referenced by init.
    data = new ....
   
    // Copy values from the
    // input data to data.
    for (int j.....)
    {
      data[j] =
    }
  }
 
  //Print
  void print()
  {
    for (int j.....)
    {
      System.out.println();
    }
  }
 
  public static void main ( String[] args )
  {
    int[] values = { 98,  99,  98,  99, 100, 101, 102, 100, 104, 105,
                    105, 106, 105, 103, 104, 103, 105, 106, 107, 106,
                    105, 105, 104, 104, 103, 102, 102, 101, 100, 102};
    Weight june = new Weight( values );
    june.print();
  }
}     
"
"
Exercise 2 ― Average
Now add an average() method to the class. Use integer math.

import java.io.* ;

class Weight
{
  . . .
 
  int average()
  {
    . . .
  }
 
  public static void main ( String[] args )
  {
    int[] values = { 98,  99,  98,  99, 100, 101, 102, 100, 104, 105,
                    105, 106, 105, 103, 104, 103, 105, 106, 107, 106,
                    105, 105, 104, 104, 103, 102, 102, 101, 100, 102};
    Weight june = new Weight( values );
    int avg = june.average();
    System.out.println("average = " + avg );
  }
}     

To check your method, initialize the array to ten values that have an easily computed average