Results 1 to 4 of 4
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Problem calling another class

    I'm trying to create a program that will prompt the user for 10 values between 0 and 99. Once I have the values I want to create a little output bar chart using the asterik sign to show how many of the values fell into each category of 10, ie 0-9, 10-19, etc.. I'm able to prompt the user for the values and store them into an array however I'm having trouble calling to the class I developed for determining the frequency and outputting the chart. Any advice would be helpful. Also if I'm making it harder than I have to let me know that as well. Here is the code I have:


    Java Code:
    /*
     * Histogram.java
     *
     * Created on October 24, 2007, 3:52 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Bill
     */
    
    import java.io.*;
    import javax.swing.*;
    
    public class Histogram {
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            // creates
            int inputtedValue[] = new int[ 10 ];  // array to hold inputted data
            
            for( int inputCounter = 0; inputCounter < inputtedValue.length; inputCounter++ ) {
                String input = JOptionPane.showInputDialog
                        ("Please enter a number between 0 and 99\n" +
                        "Stop entering values at any time by entering -1.\n");
                
                int numberProvided = Integer.parseInt(input);
                inputtedValue[ inputCounter ] = numberProvided;
            }
            
            HistogramFreq freqCalc = new HistogramFreq();
            freqCalc.processValues();
        } // end main
    } // end class Histogram
    Java Code:
    public class HistogramFreq
    {
        private int valuesProvided[];
            
         public HistogramFreq(int inputtedValue[])
        {
            valuesProvided = inputtedValue;
        }
        
        public void processValues()
        {
         determineFrequency();  
        }
            
          public void determineFrequency()
          {
            int frequency[] = new int [10];
                    
                   for( int valueEntered : valuesProvided)
                        ++frequency[valueEntered/10];
                   
                   for(int count = 0; count < frequency.length; count++)
                   {
                       System.out.printf("%02d-%02d: ", count * 10, count * 10 + 9);
                       
                       for(int stars = 0; stars < frequency[count]; stars++)
                           System.out.print("*");
                       
                       System.out.println();
                   }
        } // end method determineFrequency()
        
    } // end class HistogramFreq

  2. #2
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Also forgot to include in previous post that I'm getting the following error message while trying to run from Netbeans:

    symbol : constructor HistogramFreq()
    location: class HistogramFreq
    HistogramFreq freqCalc = new HistogramFreq();

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    About the error:
    Java Code:
        // You are using a no-argument constructor.
        HistogramFreq freqCalc = new HistogramFreq();
        ...
    // The HistorgamFreq class does not have a no-args
    // constructor, only a one-argument constructor.
    public HistogramFreq(int[] inputtedValue)
    The rule is that if you do not specify a constructor for a class the vm (virtual machine) gives you one (a no-args/default constructor) for free. If you do specify a constructor with one or more arguments the vm does not provide a default no-args constructor. So if you want a no-args constructor in this case you must provide it for your class.
    Java Code:
    import javax.swing.JOptionPane;
    
    public class H
    {
        public static void main(String[] args)
        {
            int[] inputtedValue = //new int[ 10 ];  // array to hold inputted data
    
            for( int inputCounter = 0; inputCounter < inputtedValue.length; inputCounter++ )
            {
                String input = JOptionPane.showInputDialog
                        ("Please enter a number between 0 and 99\n" +
                        "Stop entering values at any time by entering -1.\n");
                
                int numberProvided = Integer.parseInt(input);
                inputtedValue[ inputCounter ] = numberProvided;
            }
    
            HistogramFreq freqCalc = new HistogramFreq(inputtedValue);
            freqCalc.processValues();
        }
    }
    
    class HistogramFreq
    {
        private int[] valuesProvided;
    
        public HistogramFreq(int[] inputtedValue)
        {
            valuesProvided = inputtedValue;
        }
    
        public void processValues()
        {
            determineFrequency();  
        }
    
        public void determineFrequency()
        {
            for(int j = 0; j < valuesProvided.length; j++)
            {
                System.out.printf("%02d-%02d: ", j * 10, j * 10 + 9);
                for(int k = 0; k < valuesProvided[j]/10; k++)
                    System.out.print("*");
                System.out.println();
            }
        }
    }

  4. #4
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Calling displayable from a canvas class
    By Snalk in forum CLDC and MIDP
    Replies: 0
    Last Post: 03-28-2008, 11:58 PM
  3. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM
  4. calling a java class from html
    By Ed in forum Advanced Java
    Replies: 1
    Last Post: 07-08-2007, 12:58 AM

Posting Permissions

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