5 Replies - 2606 Views - Last Post: 03 May 2011 - 04:29 AM Rate Topic: -----

#1 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-February 11

Prime Numbers up to 120 in decreasing order

Posted 02 May 2011 - 09:50 PM

I am trying to find all the prime numbers under 120 and print them out in decreasing order. Here is the code I have now. The error message I am getting is from the line
StackOfIntegers stack = new StackOfIntegers();

The compiler says it cannot find symbol

public class Prime {


  public static void main(String[] args) {


    final int LIMIT = 120;

     int count = 0;


    StackOfIntegers stack = new StackOfIntegers();



    // Find prime numbers and keep repeating to 120

    for (int number = 2; number < LIMIT; number++)

      if (isPrime(number)) {
        stack.push(number);


	// Increase the count
        count++; 
      
}


    // Print the prime numbers in decreasing order

    System.out.println("The prime numbers less than 120 are \n");


    final int NUMBER_PER_LINE = 10;



	    while (!stack.empty()) {


      System.out.print(stack.pop() + " ");



      if (stack.getSize() % NUMBER_PER_LINE == 0)


	// advance to the new line
        System.out.println(); 
    }

  }


 
	 public static boolean isPrime(int number) {


    // Assume the number is prime

    boolean isPrime = true;

    

	

    for (int divisor = 2; divisor <= number / 2; divisor++) {


      //If true, the number is not prime
  
    if (number % divisor == 0) {


        // Set isPrime to false, if the number is not prime

        isPrime = false;


        break; // Exit the for loop

      }
    
}

   



Is This A Good Question/Topic? 0
  • +

Replies To: Prime Numbers up to 120 in decreasing order

#2 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Prime Numbers up to 120 in decreasing order

Posted 02 May 2011 - 10:00 PM

Just check whether you have defined class StackOfIntegers

you can do it without Stack as well.
//in your main() method
for(int i=120;i>=2;i++)
{
    if(isPrime(i))
    {
        System.out.println(i);
    }
}


This post has been edited by sakshamkum: 02 May 2011 - 10:03 PM

Was This Post Helpful? 1
  • +
  • -

#3 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-February 11

Re: Prime Numbers up to 120 in decreasing order

Posted 02 May 2011 - 10:15 PM

Thank you, but unfortunately I have to do it with StackOfIntegers and I am not sure how to define the class. I thought what I was doing was defining the class. Sorry for being so dumb about this, I am not very good at this.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Prime Numbers up to 120 in decreasing order

Posted 03 May 2011 - 02:57 AM

Depends how much your teacher wants you to re-invent the wheel.

The easiest and laziest way would be

public class StackOfIntegers extends Stack<Integer>{
  ...	
}


Now may be you are not allowed to take that shortcut and you will need


public class StackOfIntegers {

	private Stack<Integer> stack = new Stack<Integer>();
	
}



or he wants you to implement yurself the stack in an array
public class StackOfIntegers {

	private final static int SIZE = 100;
	int[] array = new int[SIZE];
	int index = 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Prime Numbers up to 120 in decreasing order

Posted 03 May 2011 - 04:26 AM

If you have defined the StackofIntegers then kindly post the code up

This post has been edited by sakshamkum: 03 May 2011 - 04:27 AM

Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Prime Numbers up to 120 in decreasing order

Posted 03 May 2011 - 04:29 AM

Ya, sakshamkum is God dammed right. Why speculating on the where to go until you tell us.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1