Results 1 to 3 of 3
 04052010, 06:43 PM #1Member
 Join Date
 Apr 2010
 Posts
 2
 Rep Power
 0
How can I solve "void cannot be dereferenced" error?
Hi everbody,
We have a homework.The question is below.
"Define a class Rational, which in addition
to the constructor and the accessor methods Enumerator and Denominator has methods
printRational (which print a rational number) as well as getSum, and getProduct,
which generate the sum and the product of rational numbers and return the corresponding
object of type Rational. For instance, if we generate rational numbers Rational
r1 = new Rational(1,2); and Rational r2 = new Rational(3,7); then r1.print()
should print 1/2. r2.getProduct(r1).print(); should print 3/14 (which corresponds
to the product of r2 and r1), and r2.getSum(r1).print(); should print 13/14 (which
corresponds to the sum of r2 and r1)."
I wrote like this
Java Code:public class Rational{ int enumerator; int denominator; Rational(int e, int d){ enumerator=e; denominator=d; } public void getSum(Rational r){ enumerator=enumerator+r.enumerator; denominator=denominator+r.denominator; } public void getProduct(Rational r){ enumerator=enumerator*r.enumerator; denominator=denominator*r.denominator; } public String printRational(){ String output=enumerator + "/" + denominator; return output; } public static void main (String[] args) { Rational r1=new Rational(1, 2); Rational r2=new Rational(3, 7); System.out.println(r1.printRational()); System.out.println(r2.printRational()); r2.getSum(r1).printRational(); r2.getProduct(r1).printRational(); } }
C:\Users\aCid\Desktop\Java Docs\HomeWorks\Rational.java:35: void cannot be dereferenced
r2.getSum(r1).printRational();
^
C:\Users\aCid\Desktop\Java Docs\HomeWorks\Rational.java:36: void cannot be dereferenced
r2.getProduct(r1).printRational();
^
2 errors
Could you help me, please?
Kind Regards

Don't have the getSum method return void! Have it return the Rational object that it creates. If this were my class, I'd not change the classes numerator nor denominator, but instead calculate new numerator and denominator and with this information create a new Rational object and return it. Same for the product.
 04052010, 08:54 PM #3Member
 Join Date
 Apr 2010
 Posts
 2
 Rep Power
 0
Similar Threads

How to solve this "Fixtures code" error exception?
By makerror in forum New To JavaReplies: 4Last Post: 12022009, 03:45 AM 
How can I prevent "found void but expected java.lang.String" ?
By trueblue in forum New To JavaReplies: 3Last Post: 05212009, 03:48 PM 
Help!! error message "long cannot be dereferenced"
By soc86 in forum New To JavaReplies: 2Last Post: 11302008, 04:24 PM 
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10202008, 07:35 AM 
How to solve "No compiler error"?
By iceman in forum New To JavaReplies: 5Last Post: 04222008, 03:37 AM
Bookmarks