Solved

Need Help (again)- GETTER METHOD: Need to identify all integers divisible by the Prime, removed from one list and added to another list.   ADVISE on where to begin....

Posted on 2007-11-25
11
337 Views
Last Modified: 2010-04-21
Hi again, to those who helped me earlier.  

I have a getter method began, but I'm not sure how to go about the code.  Honestly, I'm so new to Java... I'm not very familiar with getter methods as well as incorporating the usage of modulus operators... %  

Please help......  at least where to begin...

   public static void getComposites(ListInterface<Integer> candidates, ListInterface<Integer> composites, Integer prime)
          {
            //if prime is divisible by candidates    
               //candidates.remove
            // and add to composites list....
           
       }  

0
Comment
Question by:ahowe01
  • 7
  • 4
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20348332
try something like:

for (int i=candidates.getLength()-1; i>=0; i--)
{
   Integer value = candidates.getEntry(i);
   if (prime.intValue() % value.intValue()==0)
   {
        // prime divisible by value
        candidates.remove(i);
        composites.add(value);
   }
}
0
 

Author Comment

by:ahowe01
ID: 20348400
What would cause my program to not be able to identify the get method when I call it?

       //  Displays Composites List
       compositesList.getComposites();
       System.out.println("Composites List:   ");
       System.out.println(compositesList);

then the error says;  cannot find symbol method getComposites()

Maybe I should have started the call first..... instead of attempting to finish the getter method? ?  
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20348424
>        compositesList.getComposites();

u need to pass the listed arguments

       compositesList.getComposites(candidates, composites, prime);
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:ahowe01
ID: 20349507
see I new there was something simple....  

but why does it say:  cannot find symbol variable candidates
                                   cannot find symbol variable composites
                                   cannot find symbol variable prime
 
do I need to declare them, even though the getter method declares them as an integer?
0
 
LVL 92

Expert Comment

by:objects
ID: 20349831
use the name of whatever variables you are using for the 2 lists, and the prime number
0
 

Author Comment

by:ahowe01
ID: 20389483
???   Hi again... Will you look at the remaining of my code and give more guidance, please.    I'm reading up now on getter and setter method.     I'm working on this program in baby steps, piece by piece.  

thank you.  
import java.io.*;
import java.util.*;
 
 
public class Primes {
 
    public static void main( String args[] )   
     {
        int max; 
 
        	       
        System.out.println( "Please enter the maximum value to test for primality" );
        max = getInt("   It should be an integer value greater than or equal to 2.");
        
       // Creates Prime List 	
       ListInterface primeList = new AList();
      
       //  Creates Composites List
       ListInterface compositesList = new AList();      
 
       // Creates Candidate List 
        ListInterface candidateList = new AList();
        
        
        candidateList.add(2);
        candidateList.add(3);
        candidateList.add(4);
        candidateList.add(5);
        candidateList.add(6);
        candidateList.add(7);
        candidateList.add(11);
        candidateList.add(13);
        candidateList.add(17);            	
         
    //  System.out.print(max);    
   for (int position = candidateList.getLength(); position>=0; position--)
   {
        Integer index = (Integer)candidateList.getEntry(position);
        if (index > max)
      {
       candidateList.remove(position);
      }
      else
      {
      	break;
      }
   }
     
       System.out.println("   ");      
       System.out.println(candidateList);                        
                                                             
       //Remove first Prime number from candidateList   
       Object o = candidateList.remove(1);
       if (o != null) {
       	primeList.add(o);
       }
 
        // Displays New Candidate List
       System.out.println("   ");  
       System.out.println("New Candidate List: ");
       System.out.println(candidateList);
    
       //  Displays Prime List
       System.out.println("   "); 
       System.out.println("Prime List:");	
       System.out.println(primeList); 
       	
       //  Displays Composites List
       compositesList.getComposites(candidate, composites, prime);
       System.out.println("Composites List: ");
       System.out.println(compositesList);	 
     }   		   
    
    /**
     * getComposites - remove the composite values from possibles list and
     * put them in the composites list
     *
     * @param  possibles   a list of integers holding the possible values
     * @param  composites   a list of integers holding the composite values
     * @param  prime   an Integer that is prime
     */
    
   public static void getComposites(ListInterface<Integer> candidates, ListInterface<Integer> composites, Integer prime) 
    	{
            //if prime is divisible by candidates     
         	//candidates.remove
            // and add to composites list....
 
            	for (int i=candidates.getLength()-1; i>=0; i--)
                {
                Integer value = candidates.getEntry(i);
                if (prime.intValue() % value.intValue()==0)
                   {
                 // prime divisible by value
                 candidates.remove(i);
                 composites.add(value);
                   }
                }
       }   	       
   
   	
    /**
     * Get an integer value
     *
     * @return     an integer 
     */
     
    private static int getInt(String rangePrompt)
    {
        Scanner input;
        int result = 10;        //default value is 10
                
        try
        
        {
            input = new Scanner(System.in);
            System.out.println(rangePrompt);
            result = input.nextInt();
            
        }
        
        catch(NumberFormatException e)        
        {
            System.out.println("Could not convert input to an integer");
            System.out.println(e.getMessage());
            System.out.println("Will use 10 as the default value");
        }        
        
        catch(Exception e)
        
        {
            System.out.println("There was an error with System.in");
            System.out.println(e.getMessage());
            System.out.println("Will use 10 as the default value");
        }
        
        return result;
                                 
    }    
    
}

Open in new window

0
 

Author Comment

by:ahowe01
ID: 20389604
Do I need to set up a set method... is that something that should always be with a get method?  
0
 
LVL 92

Expert Comment

by:objects
ID: 20389685
not necessarily.
and your get methods are not really getters, typically a getter method simply returns the value of a property of an object.
0
 

Author Comment

by:ahowe01
ID: 20391093
ok, so I'm still stuck...  (And having nightmares... I wish I could learn java like you experts....)

You say I need to pass the arguments listed with the name of my viarables....

I have initialized in my main-

 int prime;
ListInterface compositesList = new AList();  
 ListInterface candidateList = new AList();

so in the main to call my getcomposite i've tried to called them as;

compositesList.getComposites(candidateList, compositesList, prime);

But I still error out....

other advise.... or website that is a good tool to understanding better.  I have the deitel book so that's helping a little bit.

0
 

Author Comment

by:ahowe01
ID: 20391286
nevermind... I see what I was doing wrong....  

thanks... until next time. :)
0
 

Author Closing Comment

by:ahowe01
ID: 31410927
Thanks....
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
java jdbc batch example 8 32
hibernate example issues from command prompt 10 40
How to execute a Python program and gather return output in Java 2 26
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question