views:

303

answers:

3

import java.util.Scanner;

public class Fraction
{
private int numerator;
private int denominator;
    private int product;
    private int sum;

public Fraction(int num, int denom)
{
 this.numerator=num;
 this.denominator=denom;

 if (this.denominator == 0){

 System.out.println("can not divide by zero");
 }// end if
}// end fraction


  public void print()
  {
       System.out.println(this.numerator + "/" + this.denominator);
  }

public void printAsDouble()
{
 System.out.println((double) this.numerator / this.denominator);
}

public mulitply(Fraction)
{
 Fraction(x).product = ((( Fraction(c).numerator * Fraction(d).denominator) + (Fraction(d).numerator * Fraction(c).denominator)) / (Fraction(c).denominator * Fraction(d).denominator));
}
}// end class Fractiont

I keep getting the same errors expected and invalide method declaration

Ok i have a new fraction in my main class called x and that is where i was calling the fraction X from. and what does it mean that it needs to return something?

A: 

multiply has no return type defined. Fraction has no one-argument constructor.

Zed
+2  A: 

This is not legal Java code:

Fraction(x).product =  ((( Fraction(c).numerator * Fraction(d).denominator) + (Fraction(d).numerator * Fraction(c).denominator)) / (Fraction(c).denominator * Fraction(d).denominator));

Objects are created using the "new" keyword:

new Fraction(x).product = ...

Also, you need to specify argument names and return values in prototypes:

public void mulitply(Fraction f)
{
....
JesperE
+5  A: 

Your multiply function is totally messed up. Could you tell us what you want to do?

Here are two examples of how a multiply function could look like, but you either have to set the field numerator and denominator public or implement getter methods:

public void multiply( Fraction b ) {
 this.numerator = this.numerator * b.numerator;
 this.denominator = this.denominator * b.denominator;
}

public static Fraction multiply( Fraction a, Fraction b ) {
 return new Fraction( a.numerator*b.numerator, a.denominator*b.denominator );
}

In your main function you could call this then like this: (See the difference between the static and "normal" method)

Fraction x = new Fraction( 1, 2 );
Fraction m = new Fraction( 1, 3 );

Fraction result = Fraction.multiply( x, m );
result.print();  // 1/6

x.print();   // 1/2
x.multiply( m );
x.print();   // 1/6

If you want to chain calls, do it like this:

public Fraction multiply( Fraction b ) {
 this.numerator = this.numerator * b.numerator;
 this.denominator = this.denominator * b.denominator;
 return this;
}

Then you can do things like

Fraction x = new Fraction(1,2).multiply( new Fraction(1,3) ).multiply( new Fraction(2,5) );
svens
is there any way You could give me a hane on my project that is due tonight at midnight. I dont understand enought to figure out my project due at midnight. I have been very busy and started writing this code on monday and am very confused my email is [email protected]
daddycardona
I'm sorry, but we're not here to help you with your homework.
JesperE
maybe you could help me understand my homework.
daddycardona
This answer looks correct to me. Then you can drop the "product" and "sum" class members.If your assignment requires you have the "product" and "sum" members, then it's a very weird assignment that you'll need to explain further.Otherwise, I suggest you:1. Verify that this answer actually works.2. Make sure you understand how it works.3. Compare it against what you came up with, so you can understand why your version didn't work.Practice at understanding why some things work, and others don't, will help you become a better programmer.
Bob Murphy