I guess I'm having a lot of problems tonight with this program I'm trying to accomplish for myself... I'm trying to write an application called TestNumFactors which defines a static method with header "public static int numFactors(int n)". When numFactors is called, it should return the number of factors of n as its result. I'm trying to get the program to call numFactors 3 times, passing the arguments 6, 28, and 100, and print out the returned values for each.

Next, I'm trying to get the second program MostFactors to read a positive number from the client, and print out the number and the # of factors it has. So for example, I'm trying to get it to output:

6 - 3
8 - 3
10 - 3


I've never used numFactors before, so I'm pulling my hair out. I know countFactors can be called countFactos(<number>) to get the result, but I don't think that applies to numFactors, does it? All I have so far (even though I know it's very misguided and incorrect) is:

public class TestNumFactors
{

	public static void main (String[] args)
	{
	}

	public static int numFactors(int n)
	{

	       numFactors(6);
	       numFactors(8);
	       numFactors(10);

	return();

	}

}

Most times, I get things down right away without issue, but sometimes, I just hit a brick wall I can't get around no matter how hard I try.

As always, thanks to everyone for the help!

First, move the calls to numFactors to your main method. Second you need to implement the numFactors method which you can do by

for (int i=1; i<=n/2; i++) // since any num > n/2 is not a factor
{
    if (n % i == 0) {
         // i is a factor of n!
    }
}
return countOfFactors;

The % operator is modulo arithmetic. A factor is any number that the number is divisible by (ie mod = 0).

Hope this helps,
darkagn :)

I received this help from a kind soul on a different site....
However I'm having the output look differently at the moment I'm listing 6-1,2,3 \n with the rest of my numbers read in... Instead of counting the factors of each... If I'm doing it wrong hopefully I still get credit for something. I'm working on another application that reads an integer, find the factor of the int with the most factors itself and then prints that factor. hope this pseudo code helps you.

//1st loop: for the first smallest factor:
outer: for loop, d less than inputInt {
inner: if your inputInt is divisible by d, print d and pass the value of the result to be used in your next loop; exit the outer loop
}
}
//2nd loop: for succeeding factors:
keep valueFrom1stLoop more than 1, or exit {
outer: for loop, d less than valueFrom1stLoop {
inner: if valueFrom1stLoop is divisible by d, print d, assign the value of the result as the new valueFrom1stLoop,
d-- to catch if you can still use another d as a factor
}
}
}

Yup, I did that, and the code still wont compile. You'd think it would be easy to just print out the number of factors for 6, 8 and 10. I just can't get it to work out though.

hmmm sorry I couldn't help more, like I said I'm just going to list the number I'm passing through followed by the factors it has i.e. "10 has factors 1 2 5"

It always helps to write down each step you're doing.... then following each variable through the code talking through what each loop is doing. Also throwing random System.out.prints around the loops can reveal where something is happening that you don't want to happen.... if you're looking to count the factors of each # I'd try applying a counting method I dunno you'd have to have the loop store each factor and then output how many factors it stored... sorry I can't help more I've got plenty of my own problems to worry about :) good luck!

Anyone else shed some light on outputting the number of factors 6, 8, and 10 have - by using numFactors (calling it 3 times)?

Have you tried to work with the code darkagn gave you? It pretty much solves your problem for I guess about 90-95% ... Try working with his code and then show us again what you have so we can work from there.

Black Box

Ok, I hope I'm following what darkagn said - here's what I've got:

class TestNumFactors
{

	public static int numFactors(int n)
	{

		for (int i = 1; i <= n/2; i++)
		{
			if (n % i == 0)
			{
			}
		}
			return countOfFactors;
	}

    public static void main (String[] args)
    {

		numFactors(6);
		numFactors(8);
		numFactors(10);
	}
}

Obviously, it still isn't working, but how close am I? He said to move the calls to numFactors to the main method... Do I need to move anything around, add something, etc?

Your structure is good.

Now look at it logically. You call your method numFactors(int) and as you figured out, it should return you a value, i.e. return countOfFactors;. Assuming numFactors is in fact returning a value, you maybe want to store it somewhere so you can use it: variableName = numFactors(int);.
As for your numFactors() method, you return the value of a variable (countOfFactors, but you have neither declared nor initialized it. Obviously it should contain the number of factors, which you calculated using those loops. In order to do so, you should make it assign something to this variable within that if statement.
Think about what you know when n % i == 0.

This should get you back on your way.

Black Box

commented: I like the way you've explained this without giving the answer away. +1

Hi newbieGirl,

Also in another of your threads you output "yes" to the console, but your program here doesn't output anything. This makes it difficult to see what is happening in your program ;)

I know, I don't know what's going on. I'm doing the work, I'm not a slacker! lol It's just not completely clicking with me, even though I've got the basic idea. I'm just getting things all turned around for some reason.

Practice, practice, but I don't know if I can practice anymore when I feel like I've had a labotomy from looking at this for so long. I guess I can say I can definitely read Java code far better than writing it out manually, at the moment. :)

How long did it take for you guys to become fluent?

That depends on what you call fluent... And it also depends on what you think is included in knowing a language. To me (and I'm willing to correct myself if a more experienced developer says otherwise) there are 3 parts of learning a language, actually learning any language.

First thing to learn is basic stuff. Getting to know the types of the language (i.e. integers, chars, floating point variables,...) and knowing how to work with them. By working with them I mean declaration, initialisation and modification. Also the basic set of ways for adjusting the program flow, mostly if, for and while loops. At the end, some less simple datastructures like an array.

Ok, without knowing that, you can't program.

The two other parts don't have a specific order in the learning curve.

As second (because I think this part is more important) there's possibly the hardest part of programming. This includes algorithms and (advanced) datastructures. Algorithms tell you how to solve a given problem (sorting and other things). Datastructures are ways of storing data (instead of normal arrays there are trees, heaps and so on).
Why is this the hardest part? It's endless, algorithms always have to be faster (more efficient) and datastructures always have to organize data using less memory. There's a tradeoff between these two aspects, faster code can mostly be reached by use of more memory, and less memory usage can be achieved by working with an algorithm which you allow to work slower.

The third part contains all the extra's. In my oppinion, this is basically knowing how to search for answers when you don't know something. Knowing about libraries that have great optimized code you can use for this or that, knowing how to use documentation on any language or language module.

Here programming stops. Mind you, this isn't meant to be a complete list of aspects on learning one or more languages, its only to show that 'becoming fluent' has it's nuances.

From here on, you can go further into software development, which is more that 'just the code'; it's also documenting, analysing, designing, maintaining...

I hope this gave you some more insight in how you can plan and optimise your learning curve for programming in general.

Black Box

Don't be too hard on yourself - you're doing just fine. In fact you've almost solved it. There are only a couple of things missing from your program. I don't want to give it all away, so I will repeat Black Box's question - what should happen when n % i = 0? ie what should you put in the if-statement? Start by initialising the variable countOfFacors immediately before the if-statement (should it be a double, String, int or something else and what value should it start with if any?)

The main method is where you should output your results but again I'm not going to tell you exactly how to do this because I think you know about output from another thread...

Ok, I'm sorry for that blob of text, I got carried away...

If your code isn't working yet, post it so we can take a look and guide you further.

Well said, Black Box.

I will just add to what you've said by saying too that the hardest programming language to learn is the first. A problem solved in the first language will be similar in another, although syntax might differ slightly. I personally believe that Java is an excellent starting point for the would-be programmer, because it introduces concepts such as classes and objects and the descriptive compiler error messages help to show you exactly where your errors are in your code.

I would only really call myself an intermediate-level programmer having only graduated last year and with only a small amount of commercial experience, but I am always leaning new things. That's part of the fun and intrigue of the industry!

Anyway I'm going to sleep now as it is late here in Australia. I hope I have pointed you in the right direction newbieGirl :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.