Getting a class to work within a program

Used a for loop in my class to search for perfect numbers

Page 1 of 1

6 Replies - 824 Views - Last Post: 22 May 2009 - 08:22 PM Rate Topic: -----

#1 ninetynineclips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-May 09

Getting a class to work within a program

Post icon  Posted 17 May 2009 - 07:43 PM

I have the calculations correct for doing this assignment, but the actual format for submission is confusing me. Here it goes...

The assignment is to write a class to find the perfect numbers within a given range. (The number 6 is a perfect number because 3 + 2 + 1 = 6). The class should have a method findNumbers which searches for all the perfect numbers within a range. Then it says to use the class in a program that asks the user for the range.

I am able to do this problem if it was all put together into one program. My problem is getting the class AND the program to work together. I know, it may be lame, but is driving me nuts. lol.

Maybe I am designing my class all wrong.
How do I call for findNumbers in my program??




 
import java.util.Scanner;

public class PerfectNumberProg
{
public static void main(String[]args)
{
  int lower;
  int upper;
 
  Scanner keyboard = new Scanner(System.in);
  PerfectNumber num = new PerfectNumber();
  
  System.out.print("This program will find perfect numbers within a given range." +
				   " Please enter the lowest value of the range: ");
  lower = keyboard.nextInt();
  
  System.out.print("Enter the highest value of the range: ");
  upper = keyboard.nextInt();

  num.setLowerLimit(lower);
  num.setUpperLimit(upper);
  
  System.out.println("The perfect numbers are: " + num.findNumbers());
  
}
}





public class PerfectNumber
{

  private int lowerLimit;
  private int upperLimit;

  
  public PerfectNumber()
  {
	lowerLimit = 0;
	upperLimit = 0;
  }

  public void setLowerLimit(int lower)
  {
	lowerLimit = lower;
  }
  
  public void setUpperLimit(int upper)
  {
	upperLimit = upper;
  }
  
  public int getLowerLimit()
  {
	return lowerLimit;
  }
  
  public int getUpperLimit()
  {
	return upperLimit;
  }
  
  public void findNumbers()
  {
	
int sum = 0, x = 0;
for (int num = lowerLimit; num < upperLimit; num++)
{
for (int factor = 1;factor < num; factor++)
 {
x = num % factor;
if (x == 0)
sum = sum + factor;
 }
if (sum == num)
 {
System.out.println(num);
System.out.println("\nThe factors are: ");


for (int factor = 1;factor < num; factor++)
 {
x = num % factor;
if (x == 0)
System.out.println(factor);
 }
}
}
  } 


Is This A Good Question/Topic? 0
  • +

Replies To: Getting a class to work within a program

#2 happycamper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Re: Getting a class to work within a program

Posted 17 May 2009 - 07:57 PM

The problem is you are calling

System.out.println("The perfect numbers are: " + num.findNumbers());


But you have the findNumbers() method as a void type. So it will not return any values and you are asking it to return a value in the above code. Either change the findNumbers method to public int findNumbers() or keep it as void and make sure you're not trying to get a return value from it.

This post has been edited by happycamper: 17 May 2009 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Getting a class to work within a program

Posted 17 May 2009 - 08:43 PM

Considering his findNumbers() method has output in it, perhaps change the main method to:
System.out.println("The perfect numbers are: ");
num.findNumbers();

and then in findNumbers(), output each perfect number when you get it.

Or

Make the return type of findNumbers() 'String', and then everytime you find a perfect number add it to the String that contains the numbers you have found previously, and return that String.
Was This Post Helpful? 0
  • +
  • -

#4 ninetynineclips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-May 09

Re: Getting a class to work within a program

Posted 22 May 2009 - 05:42 PM

Thanks for both responses but I still CANNOT get this to work.
If i take out void and replace it with int, I need to return some value and the code does not execute.

and the 2nd response... it seems like it would work but

System.out.println("The perfect numbers are: ");
num.findNumbers();



how would I output each perfect number when I get it if the user is entering the range? Output it each time where, in the program or the class? Does that screw up my code for calculating a perfect number?


I feel like I am so close with finally finishing this problem which I've been trying to do for the past few weeks, and it is so frustrating that I can't do it.
Was This Post Helpful? 0
  • +
  • -

#5 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Getting a class to work within a program

Posted 22 May 2009 - 07:17 PM

As long as you only need to output the numbers (and not keep track of them for later use), do this
Hopefully you've updated your code since the first post, because that program does not work correctly.
public static void main(String[]args)
{
  int lower;
  int upper;
 
  Scanner keyboard = new Scanner(System.in);
  PerfectNumber num = new PerfectNumber();
 
  System.out.print("This program will find perfect numbers within a given range." +
				   " Please enter the lowest value of the range: ");
  lower = keyboard.nextInt();
 
  System.out.print("Enter the highest value of the range: ");
  upper = keyboard.nextInt();

  num.setLowerLimit(lower);
  num.setUpperLimit(upper);
 
  System.out.println("The perfect numbers are: ");
  num.findNumbers();
 
}

...

public void findNumbers()
  {
	int sum = 0, x = 0;
	for (int num = lowerLimit; num < upperLimit; num++)
	{
		sum = 0;
		for (int factor = 1;factor < num; factor++)
		{
			
			x = num % factor;
			if (x == 0)
			{
				sum = sum + factor;
			}
			if (sum == num)
			{
				System.out.println(num);
				
				...
			}
		}
  }

Was This Post Helpful? 0
  • +
  • -

#6 ninetynineclips  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-May 09

Re: Getting a class to work within a program

Posted 22 May 2009 - 08:10 PM

View PostEd_Bighead, on 22 May, 2009 - 06:17 PM, said:

As long as you only need to output the numbers (and not keep track of them for later use), do this
Hopefully you've updated your code since the first post, because that program does not work correctly.
public static void main(String[]args)
{
  int lower;
  int upper;
 
  Scanner keyboard = new Scanner(System.in);
  PerfectNumber num = new PerfectNumber();
 
  System.out.print("This program will find perfect numbers within a given range." +
				   " Please enter the lowest value of the range: ");
  lower = keyboard.nextInt();
 
  System.out.print("Enter the highest value of the range: ");
  upper = keyboard.nextInt();

  num.setLowerLimit(lower);
  num.setUpperLimit(upper);
 
  System.out.println("The perfect numbers are: ");
  num.findNumbers();
 
}

...

public void findNumbers()
  {
	int sum = 0, x = 0;
	for (int num = lowerLimit; num < upperLimit; num++)
	{
		sum = 0;
		for (int factor = 1;factor < num; factor++)
		{
			
			x = num % factor;
			if (x == 0)
			{
				sum = sum + factor;
			}
			if (sum == num)
			{
				System.out.println(num);
				
				...
			}
		}
  }



Wow, u are amazing. Thanks so much. It actually executes now! The only thing that is not working right is each factor is printing multiple times. Hmmm.
Was This Post Helpful? 0
  • +
  • -

#7 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Getting a class to work within a program

Posted 22 May 2009 - 08:22 PM

Yeah, I noticed that too. I've got some free time so I'll help you out.

EDIT
So here's a quick run down of how your algorithm works.
It iterates 'num' for every integer in the range.
For every 'num' it loops through a for loop going from 1 all the way to 'num'
The all the way is the important part because even when it finds it's last factor, it checks the remaining integers less than 'num'
So for 'num'= 6, it checks 1-5.
When it gets to 3, sum==num, so it prints 6
It then goes to 4, and sum==num again, so it prints 6
And then to 5, and sum==num still, so it prints 6.
This applies to 28 as well, so you'll need to figure out a way to only print it once.

Also! Your algorithm is still incorrect because 24 is not a perfect number, so think about why the program prints it out and why it shouldn't, so you can correct the problem.

This post has been edited by Ed_Bighead: 22 May 2009 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1