user3066571
user3066571

Reputation: 1471

How to use one java class file to test another

I'm using Netbeans, and for my assignment I have to create a Function class with a bunch of predefined methods. What I need to do is create a separate class file that will be able to refer to the Function one for testing. Does anyone know how to do that? Normally I'd just have the other class further down in the file, but my instructor wants us to create another file entirely and have it somehow refer to the Function class I built. I don't know if that makes sense, can anyone help?

I guess I just don't really understand the relationship between the two, or even how to code it really. I have constructors and the whole bit in the Function class, but I don't even know what the calls would look like to grab specific methods.

Upvotes: 0

Views: 2104

Answers (2)

Justin
Justin

Reputation: 25277

Instead of having another class further down the file, move it into a new file. For example:

public class Fraction {
    //some code
}
class Tester {
    //some code
}

Is a perfectly valid source file, but you could split into two source files (and it is often clearer to do so):

(in Fraction.java):

public class Fraction {
    //some code
}

(in Tester.java):

public class Tester {
    //some code
}

Remember to include the package declaration in both files (put package whateverYourPackageNameIs; at the top of both files)

You can then test it by creating a fraction in the Tester class (via Fraction f = new Fraction(/*constructor args*/);) and call the relevant methods.

Note: this works in any IDE, not just Netbeans. You could even do this with text file programming

Upvotes: 1

Martin Dinov
Martin Dinov

Reputation: 8815

Put them in the same package by adding :

package myPackage

at the top of each source file.

Upvotes: 1

Related Questions