Make a private method part of another method and class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 1680 Views - Last Post: 12 September 2013 - 06:55 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Make a private method part of another method and class

Posted 06 September 2013 - 10:42 AM

I was wondering how to make a private method available to another method, as it is part of it. Then, after that, how could I call that class, without making an object of it? Is it still possible to call is somehow even tho the "inner" or private class is private? Changing isPrime to public is not an option.

class Utilities{

public static int nextDoublePrime( int num ){ //WORK ON
   	
		int notSureYet;
   	//include isPRIME AND dblP for the conditions
      
      return notSureYet;;
      
   }

 private static boolean isPrime(int n){ //MAKE THIS WORK WITH NEXTDOUBLEPRIME AND DOUBLEPRIME change back to PRIVATE
      
      boolean isPrime = true; //assume the number is not a prime	
      int divisor = 2;      
      
      // rules that make it prime
   	// loop
      // check if numbers less that X goes into it evenly X % div == 0 then isnt prime
      
      if(n != 1){ //1 is not a prime number
      
         while(isPrime && divisor < n){ //make sure the divisor is neever greater than n
         
            if(n % divisor == 0){ //if it does not have a remainder, its not prime
            
               isPrime = false;
            } 
         
            divisor++;     
         } 
      }
      else{ //if n is 1, return false, cuz 1 is NOT prime
      
         isPrime = false;
      }
      
      return isPrime;
   }


}




public class Test{
   
   public static void main(String[] args){
      
		
      //for isPrime, but its private
      int X = 53;
      System.out.println("The number " + X + " is prime: " + Utilities.isPrime(X));  
      
       
   }

}


This post has been edited by streek405: 06 September 2013 - 10:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Make a private method part of another method and class

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,570
  • Joined: 12-December 12

Re: Make a private method part of another method and class

Posted 06 September 2013 - 10:46 AM

Did you try your code? Does it work? If not, what errors do you receive?

BTW
boolean isPrime = true;

I wouldn't give this local variable the same name as the method.
Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Make a private method part of another method and class

Posted 06 September 2013 - 10:52 AM

You can just call isPrime(x) just like you would any other method. You can even be explicit and call Utilities.isPrime() in nextDoublePrime.

Remember private methods are available to any other methods in the same class. You can call them as much as you like as long as the caller method is defined in the same class.


Quote

even tho the "inner" or private class is private?


I think you may have some concepts muddled up a little.


Edit - Oops just realised what you want to do. No there is no way to call isPrime in another class. If its private and has to be private then thats it.

This post has been edited by Ryano121: 06 September 2013 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 894
  • Joined: 31-July 13

Re: Make a private method part of another method and class

Posted 06 September 2013 - 11:23 AM

There is no need your isPrime() method to be private , i dont see any reason.
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Make a private method part of another method and class

Posted 06 September 2013 - 11:28 AM

It exposes the implementation of the nextDoublePrime method which is a good reason if thats the main purpose of the class.

Edit - Scrap that just realised what the OP wants to do. I think the idea is to use nextDoublePrime in main and then use isPrime in nextDoublePrime which all works out keeping isPrime private.

If you have to call isPrime in the Test class then you will need to make it public.

This post has been edited by Ryano121: 06 September 2013 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 86
  • View blog
  • Posts: 169
  • Joined: 27-May 13

Re: Make a private method part of another method and class

Posted 06 September 2013 - 11:33 AM

the definition of the keyword private states that you can't use it outside the class.
What you could do is basically a getter method to access the isPrime();

You use a public function outside of your class which then calls the private function.
But that's pointless.(unless the public function has another use than just calling the private method).

Why does it need to be private?

This post has been edited by axel1994: 06 September 2013 - 11:35 AM

Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: Make a private method part of another method and class

Posted 06 September 2013 - 11:39 AM

Quote

I was wondering how to make a private method available to another method, as it is part of it. Then, after that, how could I call that class, without making an object of it?

Ryano121's answers here are correct. A method is always visible to other methods of its class.
(one exception: you can't call a private instance method from a static method. see if you can work out why this is)

Some minor issues in your code that you might like to think about:

boolean isPrime = true; //assume the number is not a prime    

You seem to have some disagreement here. :)

 while(isPrime && divisor < n)

You can use a lower bound than n for this loop

divisor++;

You can change this line to cut your work in half - more efficiency is possible, but half can be achieved with a simple modification here, further improvements require more changes to the code.

This post has been edited by andrewsw: 09 September 2013 - 01:58 PM
Reason for edit:: Whitespace

Was This Post Helpful? 1
  • +
  • -

#8 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 01:49 PM

The reason is has to be private is because my professor said so.
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Make a private method part of another method and class

Posted 09 September 2013 - 01:53 PM

Well if it has to be private then make it private but you will not be able to use the method outside of of the Utilities class.

Instead you will have to use the other public method in Utilities .nextDoublePrime() which you can call in other classes and which itself can call the .isPrime() method as it is inside the same class.
Was This Post Helpful? 2
  • +
  • -

#10 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 03:53 PM

View Postjon.kiparsky, on 06 September 2013 - 11:39 AM, said:

divisor++;

You can change this line to cut your work in half - more efficiency is possible, but half can be achieved with a simple modification here, further improvements require more changes to the code.


But dont I need this to make my program run? When I remove it, it does not finish running.
Was This Post Helpful? 0
  • +
  • -

#11 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 04:05 PM

View PostRyano121, on 09 September 2013 - 01:53 PM, said:

Well if it has to be private then make it private but you will not be able to use the method outside of of the Utilities class.

Instead you will have to use the other public method in Utilities .nextDoublePrime() which you can call in other classes and which itself can call the .isPrime() method as it is inside the same class.

So far I've tried this, but it doesnt work


   public static int nextDoublePrime( int num ){ //WORK on. you can call the private methods because its in the same class 
	  	//create variables
		int sum = 0;
		Utilities.dblP(num);
   	//include isPRIME AND dblP for the conditions
      //check if the numbers are prime
		if(dblP(num) && isDoublePrime == true){ //if the number is double prime check to see if its results are prime
		//then find the next double prime after that
			if(isPrime(num) && isPrime == true){
				sum += num;
			}
		}
      return sum;
      
   }



Neither line 4 nor 7 work by themselves or together
Was This Post Helpful? 0
  • +
  • -

#12 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 894
  • Joined: 31-July 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 04:13 PM

What is dbLP?

This post has been edited by ChrisNt: 09 September 2013 - 04:13 PM

Was This Post Helpful? 0
  • +
  • -

#13 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 04:31 PM

View PostChrisNt, on 09 September 2013 - 04:13 PM, said:

What is dbLP?

Sorry, its another method.

Here is the entire code, minus the main method

//Find the first integer >= num that is a “double prime”, that is, a product of exactly
   // two primes
   public static int nextDoublePrime( int num ){ //WORK on. you can call the private methods because its in the same class 
	
	  	//create variables
		int sum = 0;
		Utilities.dblP(num);
   	//include isPRIME AND dblP for the conditions
      //check if the numbers are prime
		if(dblP(num) && isDoublePrime == true){ //if the number is double prime check to see if its results are prime
		//then find the next double prime after that
			if(isPrime(num) && isPrime == true){
				sum += num;
			}
		}
      return sum;
      
   }





//PRIVATE METHODS for prime and double prime
   
   private static boolean isPrime(int n){ //MAKE THIS WORK WITH NEXTDOUBLEPRIME AND DOUBLEPRIME change back to PRIVATE
      
      boolean isPrime = true; //assume the number is a prime	
      int divisor = 2;      
      
      // rules that make it prime
   	// loop
      // check if numbers less that X goes into it evenly X % div == 0 then isnt prime
      
      if(n != 1){ //1 is not a prime number
      
         while(isPrime && divisor < n){ //make sure the divisor is never greater than n
         
            if(n % divisor == 0){ //if it does not have a remainder, its not prime
            
               isPrime = false;
            } 
         
            divisor++;     
         } 
      }
      else{ //if n = 1, it is NOT prime, so return false
      
         isPrime = false;
      }
      
      return isPrime;
   }
   
   private static boolean dblP(int n){ 
      
      boolean isDoublePrime = true;
      int divisor = 2;
      
      //a double prime should only have two factors, exluding 1 and itself
      //if the number is divided and it goes into evenly then check AGAIN. if the 2nd time around there is NO remainder, its NOT prime
      //if there is a remainder the second time around, then it is prime
      
      while(isDoublePrime){ //20 is NOT a double prime, 106 IS a double prime
         
         if(n % divisor == 0){ //so far it could be a double prime
         
            
            
         }
         else {
            isDoublePrime = false;
         }
         divisor++;
      }
   		     
      return isDoublePrime;
   }    
}






Im not done yet, I just want to know how to use the private methods in the my nextDoublePrime()
Was This Post Helpful? 0
  • +
  • -

#14 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 894
  • Joined: 31-July 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 04:40 PM

Just the name of your private method, you dont need to have an object to call it.

public void callPrivateMethod(String message) {
       printMessage(message);
}

private void printMessage(String message) {
        System.out.println(message);
}



Was This Post Helpful? 1
  • +
  • -

#15 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 721
  • Joined: 10-March 13

Re: Make a private method part of another method and class

Posted 09 September 2013 - 04:50 PM

View PostChrisNt, on 09 September 2013 - 04:40 PM, said:

Just the name of your private method, you dont need to have an object to call it.

public void callPrivateMethod(String message) {
       printMessage(message);
}

private void printMessage(String message) {
        System.out.println(message);
}



Ok thanks. Ill try that
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3