3 Replies - 454 Views - Last Post: 01 February 2013 - 04:14 AM Rate Topic: -----

#1 Nubisco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-January 13

error: class, interface, or enum expected?

Posted 01 February 2013 - 02:36 AM

So when I compile my code I keep getting that error message at the end of my class bracket,
I'm sure its something simple or at least I think it is, but I can't figure it out.
If you know what I should do, please let me know. Thank you.

public class TestMethods 
{
/*
 * Returns a String containing the given number of * characters.  
 * <p>
 * If width is 0 or more, the returned String will end with a newline ('\n')
 * character.  If width is negative, returns the empty string "".
 * <p>
 * Examples:
 * <pre>
 * row(3)  -> "***\n"
 * row(5)  -> "*****\n"
 * row(0)  -> "\n"
 * row(-2) -> ""
 * </pre>
 *
 * @param width  How many *s should be in the generated row
 * @return       A String containing width *s and a \n
 */
public static String row(int width)
{
  if(width > 0)
  {
   String result = "";
	
	if(width > 0)
   {
  
	for(int i = 0; i < width; i++)
	{   
	  result += "*";
   }  
	  result += "\n";
	}
	else
   {
     result = "";
   }
   return result;
 
  }

}//method
		  
	  


/**
 * Returns a String of *s that form a right triangle of the given size.
 * Both the width and height will be equal to the given size.  The right
 * angle of the printed triangle will be in the lower-left.
 * <p>
 * If size is 0 or less, returns the empty String "".
 * <p>
 * Examples:
 * <pre>
 * triangle(6)  -> "*\n**\n***\n****\n*****\n******\n"
 * triangle(3)  -> "*\n**\n***\n"
 * triangle(1)  -> "*\n"
 * triangle(0)  -> ""
 * triangle(-2) -> ""
 * </pre>
 * <p>
 * In these examples, '\n' is a single newline characters.  If these 
 * strings were printed, they would result in multiple lines of output.
 *
 * @param size  The width and height of a right triangle of *s
 * @return      A String that contains the complete triangle, including
 *              necessary line breaks ('\n')
 */
public static String triangle(int size)
{ 
  if(size > 0)
  {
   String result = "";
	
   for (int i = 0; i < size; i++)
   {
     for (int j = 0; j < i; j++)
     {
        result += "*";
     }
	     result += "\n";
   }
  
  }
  else
  {
    result = "";
  } return result;
}//method

  
  //HINT: To avoid loops, reuse your row method above to generate each line 
  // of the triangle
  
  


/**
 * Returns a String containing the given String fragment repeated the given
 * number of times with a decreasing count placed between the fragments. 
 * That is, the format of the returned string is frag#frag#frag here # is 
 * the sequences of decreasing integers from (times-1) to 1.  (See examples 
 * for more.)
 * <p>
 * If times is 0 or less, returns an empty string "". 
 * <p>
 * Examples:
 * <pre>
 * interpose"ho", 3)     -> "ho2ho1ho"
 * interpose("yes", 5)    -> "yes4yes3yes2yes1yes"
 * interpose("", 5)       -> 4321
 * interpose("x", 1)      -> "x"
 * interpose("raise", -1) -> ""
 * </pre>
 *
 * @param str    The string to repeat
 * @param times  How many times to repeat it
 * @return       A String containing str x times with a decreasing count
 *               between the repeated strs.
 */
public static String interpose(String str, int times)
{
 String result = "";
	
  if(times > 0)
  {
	if(times > 0)
	{
	 
	 for (int x = times; x >= 1; x--)
    {
        result += str + x;
    }
		  result += str;
	}  
    else
	 {
	     result = "";
    }
		
  }return result;
	  
}//method

  
  

/**
 * Sets the cells within a section of the given array equal to i*i, where i 
 * is the index of the containing cell.  That is, starting at i = startIndex,
 * sets array[i] equal to i*i, and then repeats this for all higher i up to
 * the end of the array.
 * <p> 
 * Changes the given array, but also returns a reference to it.
 * If start index is outside the bounds of the array, this method does 
 * nothing, returning the array unchanged.
 * <p>
 * Examples:
 * <pre>
 * squares(new int[5], 2)     -> [0, 0, 4, 9, 16]
 * int[] nums = {2, 4, 6, 8, 10};
 * squares(nums, 6)           -> [2, 4, 6, 8, 10]
 * squares(nums, 3)           -> [2, 4, 6, 9, 16]
 * </pre>
 *
 * @param array       The array of ints to change
 * @param startIndex  The index within array at which to start writing 
 *                    squares
 * @return            The same array given as a parameter
 */
public static int[] squares(int[] array, int startIndex)
{
  if(startIndex < array.length)
	 { 		  
		for(int i = startIndex; i < array.length; i++)
	   { 
		   System.out.print(i * i);
			System.out.print(" ");
		}
	 }
	  else
	  {
		 for(int j = 0; j < array.length; j++)
		 {
		   System.out.print(array[j]);
			System.out.print(" ");
		 }
	  } return array;

}//squares method

  
/**
 * Uses the Euclidean algorithm to compute the greatest common divisor 
 * of the given two integers.  In other words, assuming |a| >= |b|:
 * <pre>
 * gcd(a, 0) == a, or
 * gcd(a, B)/> == gcd(b, a - (b * (a/B)/>))
 * </pre>
 * <p>
 * When calling this method, the parameter a does not need to be >= b
 * because this method will appropriately reorder the parameters' values 
 * if necessary. The returned value will always be >= 0.
 * <p>
 * Examples:
 * <pre>
 * gcd(48, 32)  -> 16
 * gcd(32, 48)  -> 16
 * gcd(32, 32)  -> 32
 * gcd(0, 32)   -> 32
 * gcd(9, 17)   -> 1
 * gcd(18, -6)  -> 6
 * gcd(-6, -18) -> 6
 * </pre>
 * 
 * @param a  One of the two values to find the GCD of
 * @param b  The other of two values to find the GCD of
 * @return   The positive GCD of a and b
 */
public static int gcd(int a, int B)/>
{
	int rem;
	  
	if(Math.abs(a) < Math.abs(B)/>)
   {
     int temp = a;
     a = b;
     b = temp;
   }
 
   while(b!=0)
   {
     rem = a % b;
     a = b;
     b = rem;
   }
     
	return Math.abs(a);
	  
}//gcd method
   
  // HINT: Use Math.abs and compare the values of a and b and 
  // swap their  values if |a| < |b|.  After that point, you'll know
  // that |a| >= |b|.
  
  
  
  }//Main Method
  
}//Class



Is This A Good Question/Topic? 0
  • +

Replies To: error: class, interface, or enum expected?

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: error: class, interface, or enum expected?

Posted 01 February 2013 - 02:44 AM

Well, you need to have a
public static void main(String[] args) {

//this is where you create objects of your class and consequently called the methods of the classes using objects.

}


Well,you can either create a separate class with a main(), or you can add the main() to the same class.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: error: class, interface, or enum expected?

Posted 01 February 2013 - 04:07 AM

My post disappeared . . . what I tried to say was:

While r.n's point may be valid, the error message you're getting is referring specifically to the fact that your open and close braces, '{' and '}', don't match - you don't have an equal number of each.

This post has been edited by GregBrannon: 01 February 2013 - 04:09 AM

Was This Post Helpful? 1
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: error: class, interface, or enum expected?

Posted 01 February 2013 - 04:14 AM

Yes sir, I believe you are right...and I feel it is to do with the line 250 of the code, as there seems to be a closing braces for the main() but there is no main() method present.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1