tags:

views:

251

answers:

2

If i was to make a new program and wanted to have a fraction class and what my book calls a driver class wich 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 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? I am taking a Java class and this is part the part of my home work I dont 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.

// begining 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 recieve the fraction 
    // from the driver class for both c and d?

}

can any one help me with this one

+1  A: 

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.

Pete Kirkham
Constructors don't return anything? Not even an instance of the type defined by the class?
George Jempty
No. The instance is allocated, then dup'd on the stack then the constructor ( renamed to <init> ) is called. Have you ever seen a constructor which ends in a return statement.
Pete Kirkham
I am very new to Java and learned a little of vb and c++ back in the day. Like in 98 but I dont remeber that stuff and I am a little confused on this project i am doing.
daddycardona
What would happened if I needed to add those two fractions together and then print it.
daddycardona
You would need to create an plus() method which returned a new fraction representing the sum of the two fractions. You also would want a toString() method which converted the fraction to a string, and then you could call System.out.println( c.plus(d) )
Pete Kirkham
Pete what you said on the first comments helped me understood again that the c and d are new objects holding its place and even though my book says that as well I am still a little confused. I just want to say thanks alot I needed that.
daddycardona
How would you add the numerators and denominators in a plus statement, what I tried to do was put add() int x = this.numerator(c) + this.numerator(d) I dont understand.
daddycardona
Ask a new question and post the code you're working on rather than using comments on this one.
Pete Kirkham
A: 

Hi

I am working on a problem like this. What should the class contain to receive and manipulate the fraction values. Should new instance variables be set up in this class or manipulate the objects already initialized?

gaspar