Thread: Code error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Code error


    Hello: I am having some problems with figuring out the code for this problem. Here is the problem:

    2. Write a class (and a client class to test it) that encapsulates the evolution of the sales tax rates in the 50 U.S states over the last 10 years. Your only instance variable should be a two-dimensional array of values representing the sales tax rates. Dimension 1 represents the state and dimension2 represents the year. Your constructor can simply be a default constructor, randomly generating the sales tax rates, which should be between 0 and 0.06. You should include the following methods:
    a. A method returning the index of the state that has the biggest average tax rate over the years.
    b. A method returning an array of indexes of the states that have had at least one year with a tax rate less than 0.001
    c. A method returning the highest sales tax rate over the years for a given state (which will be a parameter)


    Here is what I have so far. I think most of it is correct. Except the bottom section of code that starts public double StateSalesTax (int k) is generating an illegal start of expression error. Thanks for the help.

    Code:
    class StateSalesTax
    {
    double [][]sales;
    public StateSalesTax()
    {
    sales=new double[10][10];
    }
    
    public int Index()
    {
    int id,max=0;
    for(int i=0;i<10;i++)
    {
    double s=0;
    for(int j=0;j<10;j++)s+=sales[i][j];
    if(s>max){max=s;id=i;}
    }
    return id;
    }
    
    public double[] AA()
    {
    double []A=new double[10];
    for(inti=0;i<10;i++)
    {
    for(int j=0;j<10;j++)
    {
    A[i]=-1;
    if(sales[i][j]<0.001)
    {
    A[i]=j;
    break;
    }
    }
    return A;
    }
    
    public double StateSalesTax(int k)
    {
    int max=0;
    for(int i=0;i<10;i++)
    if(sales[k][i]>max)max=sales[k][i];
    return max;
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,706
    Rep Power
    1959
    You should try keep the indention of code. It makes the code easier to read.

    Can you post (copy/paste) the exact error message you get?

    If you have issue with code, you should try to "clean" it out, for example encapsulate your loops and if blocks with { }.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    Hi there. I found a number of compilation errors in your code. Like MrFujin said, I think "cleaning it out" will help you. I didn't actually check to make sure your methods were doing their intended jobs, just cleaned up your compilation errors. The following code will compile for me, but it is still all your own code which may or may not work.

    Code:
    class StateSalesTax
    {
    	double [][]sales;
    	public StateSalesTax()
    	{
    		sales=new double[10][10];
    	}
    
    	public int Index()
    	{
    		int id = 0;
    		double max=0;
    		
    		for(int i=0;i<10;i++)
    		{
    			double s=0;
    			
    			for(int j=0;j<10;j++)
    			{
    				s+=sales[i][j];
    			}
    			if(s>max)
    			{
    				max=s;
    				id=i;
    			}
    		}
    		return id;
    	}
    
    	public double[] AA()
    	{
    		double[] A = new double[10];
    		for(int i=0;i<10;i++)
    		{
    			for(int j=0;j<10;j++)
    			{
    				A[i]=-1;
    				
    				if(sales[i][j]<0.001)
    				{
    					A[i]=j;
    					break;
    				}
    			}
    			
    		}
    		return A;
    		}
    
    		public double getStateSalesTax(int k)
    		{
    			double max=0;
    			for(int i=0;i<10;i++)
    				if(sales[k][i]>max)
    					max=sales[k][i];
    			return max;
    		}
    }

IMN logo majestic logo threadwatch logo seochat tools logo