Results 1 to 3 of 3

Thread: need help with code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    27

    need help with code

    hello i wrote Statistician class. i am getting error:

    A:\Statistician.java:19: cannot find symbol
    symbol : class Random
    location: class Statistician
    Random r = new Random();
    ^
    A:\Statistician.java:19: cannot find symbol
    symbol : class Random
    location: class Statistician
    Random r = new Random();
    ^
    2 errors

    Process completed.

    can some please tell me how to fix this error

    here is my class:
    import java.lang.*;
    //import java.text.*;

    public class Statistician
    {

    public static void main(String[] args)
    {
    Statistician s1 = new Statistician();
    Statistician s2 = new Statistician();
    Random r = new Random();
    for (int i = 0; i < 5; i++)
    {
    s1.next(r.nextDouble()*100);
    s2.next(r.nextDouble()*100);
    }
    Statistician newstat = Statistician.union(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(newstat);

    }


    /**
    * Initialize a new Statistician.
    * @param - none
    * <dt><b>Postcondition:</b><dd>
    * This Statistician is newly initialized and has not yet been given any
    * numbers.
    **/

    private int NumCount;
    private double sum;
    private double max;
    private double min;

    public Statistician()
    {
    NumCount = 0;
    }

    /**
    * Add the numbers of another Statistician to this Statistician.
    * @param <CODE>addend</CODE>
    * a Statistician whose numbers will be added to this Statistician
    * <dt><b>Precondition:</b><dd>
    * The parameter, <CODE>addend</CODE>, is not null.
    * <dt><b>Postcondition:</b><dd>
    * The numbers from <CODE>addend</CODE> have been added to this
    * Statistician. After the operation, this Statistician acts as if
    * if was given all of its numbers and also given all of the numbers
    * from the addend.
    * @exception NullPointerException
    * Indicates that <CODE>addend</CODE> is null.
    **/

    public void addAll(Statistician addend)
    {
    if (addend.NumCount != 0)
    {
    if (NumCount == 0)
    {
    NumCount = addend.NumCount;
    sum = addend.sum;
    max = addend.max;
    min = addend.min;

    }
    else
    {
    NumCount += addend.NumCount;
    sum += addend.sum;
    //sum+=addend.NumCount;
    if (addend.max > max)
    max = addend.max;
    if (addend.min < min)
    min = addend.min;
    }
    }

    }

    /**
    * Clear this Statistician.
    * @param - none
    * <dt><b>Postcondition:</b><dd>
    * This Statistician is reinitialized as if it has never been given any
    * numbers.
    **/

    public void clear()
    {
    NumCount = 0;
    }

    /**
    * Compare this <CODE>Statistician</CODE> to another object for equality.
    * @param <CODE>obj</CODE>
    * an object with which this <CODE>Statistician</CODE> will be compared
    * @return
    * A return value of <CODE>true</CODE> indicates that
    * <CODE>obj</CODE> refers to a
    * <CODE>Statistican</CODE> object with the same length, sum, mean,
    * minimum and maximum as this
    * <CODE>Statistician</CODE>. Otherwise the return value is
    * <CODE>false</CODE>.
    * <dt><b>Note:</b><dt>
    * If <CODE>obj</CODE> is null or does not refer to a
    * <CODE>Statistician</CODE> object, then the answer is
    * <CODE>false</CODE>.
    **/
    public boolean equals(Object obj)
    {
    if (obj instanceof Statistician)
    {
    Statistician stat = (Statistician) obj;
    return (stat.NumCount == NumCount && stat.sum == sum &&
    stat.max == max && stat.min == min);
    }
    return false;
    }

    /**
    * Determine how many numbers have been given to this Statistician.
    * @param - none
    * @return
    * the count of how many numbers have been given to this Statistician
    * since it was initialized or reinitialized.
    * <dt><b>Note:</b><dd>
    * Giving a Statistician more than
    * <CODE>Integer.MAX_VALUE</CODE> numbers, will
    * cause failure with an arithmetic overflow.
    **/

    public int length()
    {
    return NumCount;
    }

    /**
    * Determine the largest number that has been given
    * to this Statistician.
    * @param - none
    * @return
    * the largest number that has been given to this
    * Statistician
    * since it was initialized or reinitialized.
    * <dt><b>Note:</b><dd>
    * If <CODE>length()</CODE> is zero, then the answer from this method
    * is <CODE>Double.NaN</CODE>.
    **/

    public double maximum()
    {
    if (NumCount == 0)
    return Double.NaN;
    return max;
    //return maximum;
    }

    /**
    * Determine the arithmetic average of all the numbers that have been given
    * to this Statistician.
    * @param - none
    * @return
    * the arithmetic mean of all the number that have been given to this
    * Statistician
    * since it was initialized or reinitialized.
    * <dt><b>Note:</b><dd>
    * If this Statistician has been given more than
    * <CODE>Integer.MAX_VALUE</CODE> numbers, then this method fails
    * because of arithmetic overflow.
    * If <CODE>length()</CODE> is zero, then the answer from this method
    * is <CODE>Double.NaN</CODE>.
    * If <CODE>sum()</CODE> exceeds the bounds of double numbers, then the
    * answer from this method may be
    * <CODE>Double.POSITIVE_INFINITY</CODE> or
    * <CODE>Double.NEGATIVE_INFINITY</CODE>.
    **/

    public double mean()
    {
    if (NumCount == 0)
    return Double.NaN;
    return sum / NumCount;
    }


    /**
    * Determine the smallest number that has been given
    * to this Statistician.
    * @param - none
    * @return
    * the smallest number that has been given to this
    * Statistician
    * since it was initialized or reinitialized.
    * <dt><b>Note:</b><dd>
    * If <CODE>length()</CODE> is zero, then the answer from this method
    * is <CODE>Double.NaN</CODE>.
    **/
    public double minimum()
    {
    if (NumCount == 0)
    return Double.NaN;
    return min;
    }

    /**
    * Give a new number to this Statistician.
    * @param <CODE>number</CODE>
    * the new number that is being given the this Statistician
    * <dt><b>Postcondition:</b><dd>
    * The specified number has been given to this Statistician and
    * it will be included in any subsequent statistics.
    **/

    public void next(double number)
    {
    if (NumCount == 0)
    {
    sum = number;
    max = number;
    //min = number;
    }
    else
    {
    sum += number;
    if (number > max)
    max = number;
    if (number < min)
    min = number;
    }
    NumCount++;
    }

    /**
    * Determine the sum of all the numbers that have been given to this
    * Statistician.
    * @param - none
    * @return
    * the sum of all the number that have been given to this
    * Statistician
    * since it was initialized or reinitialized.
    * <dt><b>Note:</b><dd>
    * If the sum exceeds the bounds of double numbers, then the answer
    * from this method may be
    * <CODE>Double.POSITIVE_INFINITY</CODE> or
    * <CODE>Double.NEGATIVE_INFINITY</CODE>.
    **/
    public double sum()
    {
    if (NumCount == 0)
    return 0;
    return sum;
    }

    /**
    * Create a new Statistician that behaves as if it was given all the
    * numbers from two other bags.
    * @param <CODE>s1</CODE>
    * the first of two Statisticians
    * @param <CODE>s2</CODE>
    * the second of two Statisticians
    * <dt><b>Precondition:</b><dd>
    * Neither s1 nor s2 is null.
    * @return
    * a new Statistician that acts as if it was given all the numbers from
    * s1 and s2.
    * @exception NullPointerException.
    * Indicates that one of the arguments is null.
    **/

    public static Statistician union(Statistician s1, Statistician s2)
    {
    if (s1 == null || s2 == null)
    throw new RuntimeException("..:: null pointer exception ::..");

    Statistician result = new Statistician();
    result.addAll(s1);
    result.addAll(s2);
    return result;
    }
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: need help with code

    1) Use [code][/code] tags when posting here!

    2) Remove the import java.lang.* line. It's implicit.

    3) Add a line containing "import java.util.Random;". Random isn't part of the core library.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: need help with code

    this is your code, working:
    Code:
    import java.util.*;
    
    public class Statistician {
      public static void main(String[] args) {
        Statistician s1 = new Statistician();
        Statistician s2 = new Statistician();
        Random r = new Random();
        for (int i = 0; i < 5; i++) {
          s1.next(r.nextDouble() * 100);
          s2.next(r.nextDouble() * 100);
        }
        Statistician newstat = Statistician.union(s1, s2);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(newstat);
      }
    
      /**
       * Initialize a new Statistician.
       * @param - none
       * <dt><b>Postcondition:</b><dd>
       * This Statistician is newly initialized and has not yet been given any
       * numbers.
       **/
      private int NumCount;
      private double sum;
      private double max;
      private double min;
      public Statistician() {
        NumCount = 0;
      }
    
      /**
       * Add the numbers of another Statistician to this Statistician.
       * @param <CODE>addend</CODE>
       * a Statistician whose numbers will be added to this Statistician
       * <dt><b>Precondition:</b><dd>
       * The parameter, <CODE>addend</CODE>, is not null.
       * <dt><b>Postcondition:</b><dd>
       * The numbers from <CODE>addend</CODE> have been added to this
       * Statistician. After the operation, this Statistician acts as if
       * if was given all of its numbers and also given all of the numbers
       * from the addend.
       * @exception NullPointerException
       * Indicates that <CODE>addend</CODE> is null.
       **/
      public void addAll(Statistician addend) {
        if (addend.NumCount != 0) {
          if (NumCount == 0) {
            NumCount = addend.NumCount;
            sum = addend.sum;
            max = addend.max;
            min = addend.min;
          }
          else {
            NumCount += addend.NumCount;
            sum += addend.sum;
            //sum+=addend.NumCount;
            if (addend.max > max) {
              max = addend.max;
            }
            if (addend.min < min) {
              min = addend.min;
            }
          }
        }
      }
    
      /**
       * Clear this Statistician.
       * @param - none
       * <dt><b>Postcondition:</b><dd>
       * This Statistician is reinitialized as if it has never been given any
       * numbers.
       **/
      public void clear() {
        NumCount = 0;
      }
    
      /**
       * Compare this <CODE>Statistician</CODE> to another object for equality.
       * @param <CODE>obj</CODE>
       * an object with which this <CODE>Statistician</CODE> will be compared
       * @return
       * A return value of <CODE>true</CODE> indicates that
       * <CODE>obj</CODE> refers to a
       * <CODE>Statistican</CODE> object with the same length, sum, mean,
       * minimum and maximum as this
       * <CODE>Statistician</CODE>. Otherwise the return value is
       * <CODE>false</CODE>.
       * <dt><b>Note:</b><dt>
       * If <CODE>obj</CODE> is null or does not refer to a
       * <CODE>Statistician</CODE> object, then the answer is
       * <CODE>false</CODE>.
       **/
      public boolean equals(Object obj) {
        if (obj instanceof Statistician) {
          Statistician stat = (Statistician) obj;
          return (stat.NumCount == NumCount && stat.sum == sum &&
                  stat.max == max && stat.min == min);
        }
        return false;
      }
    
      /**
       * Determine how many numbers have been given to this Statistician.
       * @param - none
       * @return
       * the count of how many numbers have been given to this Statistician
       * since it was initialized or reinitialized.
       * <dt><b>Note:</b><dd>
       * Giving a Statistician more than
       * <CODE>Integer.MAX_VALUE</CODE> numbers, will
       * cause failure with an arithmetic overflow.
       **/
      public int length() {
        return NumCount;
      }
    
      /**
       * Determine the largest number that has been given
       * to this Statistician.
       * @param - none
       * @return
       * the largest number that has been given to this
       * Statistician
       * since it was initialized or reinitialized.
       * <dt><b>Note:</b><dd>
       * If <CODE>length()</CODE> is zero, then the answer from this method
       * is <CODE>Double.NaN</CODE>.
       **/
      public double maximum() {
        if (NumCount == 0) {
          return Double.NaN;
        }
        return max;
      }
    
      /**
       * Determine the arithmetic average of all the numbers that have been given
       * to this Statistician.
       * @param - none
       * @return
       * the arithmetic mean of all the number that have been given to this
       * Statistician
       * since it was initialized or reinitialized.
       * <dt><b>Note:</b><dd>
       * If this Statistician has been given more than
       * <CODE>Integer.MAX_VALUE</CODE> numbers, then this method fails
       * because of arithmetic overflow.
       * If <CODE>length()</CODE> is zero, then the answer from this method
       * is <CODE>Double.NaN</CODE>.
       * If <CODE>sum()</CODE> exceeds the bounds of double numbers, then the
       * answer from this method may be
       * <CODE>Double.POSITIVE_INFINITY</CODE> or
       * <CODE>Double.NEGATIVE_INFINITY</CODE>.
       **/
      public double mean() {
        if (NumCount == 0) {
          return Double.NaN;
        }
        return sum / NumCount;
      }
    
      /**
       * Determine the smallest number that has been given
       * to this Statistician.
       * @param - none
       * @return
       * the smallest number that has been given to this
       * Statistician
       * since it was initialized or reinitialized.
       * <dt><b>Note:</b><dd>
       * If <CODE>length()</CODE> is zero, then the answer from this method
       * is <CODE>Double.NaN</CODE>.
       **/
      public double minimum() {
        if (NumCount == 0) {
          return Double.NaN;
        }
        return min;
      }
    
      /**
       * Give a new number to this Statistician.
       * @param <CODE>number</CODE>
       * the new number that is being given the this Statistician
       * <dt><b>Postcondition:</b><dd>
       * The specified number has been given to this Statistician and
       * it will be included in any subsequent statistics.
       **/
      public void next(double number) {
        if (NumCount == 0) {
          sum = number;
          max = number;
        }
        else {
          sum += number;
          if (number > max) {
            max = number;
          }
          if (number < min) {
            min = number;
          }
        }
        NumCount++;
      }
    
      /**
       * Determine the sum of all the numbers that have been given to this
       * Statistician.
       * @param - none
       * @return
       * the sum of all the number that have been given to this
       * Statistician
       * since it was initialized or reinitialized.
       * <dt><b>Note:</b><dd>
       * If the sum exceeds the bounds of double numbers, then the answer
       * from this method may be
       * <CODE>Double.POSITIVE_INFINITY</CODE> or
       * <CODE>Double.NEGATIVE_INFINITY</CODE>.
       **/
      public double sum() {
        if (NumCount == 0) {
          return 0;
        }
        return sum;
      }
    
      /**
       * Create a new Statistician that behaves as if it was given all the
       * numbers from two other bags.
       * @param <CODE>s1</CODE>
       * the first of two Statisticians
       * @param <CODE>s2</CODE>
       * the second of two Statisticians
       * <dt><b>Precondition:</b><dd>
       * Neither s1 nor s2 is null.
       * @return
       * a new Statistician that acts as if it was given all the numbers from
       * s1 and s2.
       * @exception NullPointerException.
       * Indicates that one of the arguments is null.
       **/
      public static Statistician union(Statistician s1, Statistician s2) {
        if (s1 == null || s2 == null) {
          throw new RuntimeException("..:: null pointer exception ::..");
        }
        Statistician result = new Statistician();
        result.addAll(s1);
        result.addAll(s2);
        return result;
      }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width