Results 1 to 3 of 3
  1. #1
    fervent07 is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default 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();
    		
    	}
    	
    }
    But i get some errors.How can i fix these?

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    fervent07 is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    Thank you. I fixed errors.
    Last edited by fervent07; 04-05-2010 at 09:12 PM.

Similar Threads

  1. How to solve this "Fixtures code" error exception?
    By makerror in forum New To Java
    Replies: 4
    Last Post: 12-02-2009, 02:45 AM
  2. Replies: 3
    Last Post: 05-21-2009, 03:48 PM
  3. Replies: 2
    Last Post: 11-30-2008, 03:24 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. How to solve "No compiler error"?
    By iceman in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •