Reputation: 1112
If I was to make a new program and wanted to have a fraction class and what my book calls a driver class which is a main class and I have code like this
// Driver class
import java.util.Scanner;
public class DriverClass{
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
Fraction c, d;
System.out.println(" Enter a numerator then a denominator:");
c = new Fraction( stdIn.nextInt(), stdIn.nextInt());
c.print();
System.out.println(" Enter a numerator then a denominator:");
d = new Fraction( stdIn.nextInt(), stdIn.nextInt());
d.print();
}
}
...
in my fraction class I have a method called public fraction. How will that set both numbers from fraction c in the driver class which is coming in from the scanner util and also would this, would the c values be replaced by the values that are coming in from fraction d? I am taking a Java class and this is part the part of my home work I don't understand. I am trying to get these valued passed to the fraction class because in the end I have to add these two fractions together and also multiply them.
// beginning of class
public class Fraction{
private int numerator;
private int denominator;
// well this is what my problem is, how do I call for c
// twice in the Fraction class
public int Fraction(int num, int denom){
this.numerator = num;
this.denominator = denom;
}
// is this the right way to receive the fraction
// from the driver class for both c and d?
}
can any one help me with this one?
Upvotes: 0
Views: 3320
Reputation: 49311
Your Fraction
method is a method which is defined to return an int, but you're calling it as though it were a constructor.
Constructors don't return anything, so don't declare any return type. ( they don't even have a void
type so the compiler knows they are constructors rather than methods, and need to be called with new
. It's a minor bug that Java lets you declare methods with the same name as the class, IIRC one of the puzzles in Java Puzzlers does that ).
Remove the "int
" return type from the definition:
public class Fraction{
private int numerator;
private int denominator;
public Fraction(int num, int denom) {
//...
How will that set both numbers from fraction c in the driver class wich is coming in from the scanner util and also would this, would the c values be replaced by the values that are coming in from fraction d?
You're making two calls to new Fraction(..,..)
. Each time you use new
, it creates a new object of the requested class and then calls the constructor on that object with the values you give it. So c
and d
will hold references to difference instances of Fraction
. As the numerator
and denominator
fields of Fraction
are not marked as static
, then each instance of Fraction
will have its own copy of those fields, so the values passed to the constructor of the object whose reference will stored in the variable c
are stored in the first new object, and those of d
in the second new object. As they are different objects, the values won't replace each other.
Upvotes: 1