this program me practice creating objects , defining classes. have multiple methods written in rational class , main written in main class. methods calling correctly in main, when call add , reduce methods, i'm getting error methods can't found. have commented on lines below receiving errors.
here rational class:
package main; public class rational { int numerator, denominator; public rational() { this.numerator = 0; this.denominator = 0; } public static void printrational(rational tester) { system.out.println(tester.numerator + "/" + tester.denominator); } public rational(int n, int d) { this.numerator = n; this.denominator = d; } public static void negate(rational tester) { tester.numerator = tester.numerator * -1; if(tester.denominator <= 0) { tester.numerator = tester.numerator * -1; tester.denominator = tester.denominator * -1;} } public static void invert(rational tester) { int tempn = tester.numerator; int tempd = tester.denominator; tester.numerator = tempd; tester.denominator = tempn; } public static double todouble(rational tester) { double retval = (double)tester.numerator / (double)tester.denominator; return retval; } public static void reduce(rational tester) { int remain, num1, num2; num1 = tester.numerator; num2 = tester.denominator; remain = num1 % num2; while(remain != 0) { remain = num1 % num2 ; num1 = num2; num2 = remain; } int gcd = num1; rational ret = new rational(tester.numerator/gcd, tester.denominator/gcd); printrational(ret); } //step 11 write method called add takes 2 rational numbers arguments , returns new rational object. return object should contain sum of arguments. public static rational add(rational tester, rational testadd) { rational retadd = new rational(0, 0); if(tester.denominator == testadd.denominator) { retadd.numerator = tester.numerator + testadd.numerator; retadd.denominator = tester.denominator; return retadd; } else { retadd.numerator = tester.numerator * testadd.denominator; retadd.denominator = tester.denominator * testadd.denominator; testadd.numerator = testadd.numerator * tester.denominator; testadd.denominator = testadd.denominator * tester.denominator; retadd.numerator += testadd.numerator; int remain, num1, num2; num1 = retadd.numerator; num2 = retadd.denominator; remain = num1 % num2; while(remain != 0) { remain = num1 % num2 ; num1 = num2; num2 = remain; } int gcd = num1; retadd.numerator = retadd.numerator/gcd; retadd.denominator = retadd.denominator/gcd; return retadd; } } } }
and here main class in different file:
package main; import static main.rational.todouble; public class main { public static void main(string[] args) { rational tester = new rational(); tester.numerator=4; tester.denominator=3; rational testadd = new rational(5, 7); rational.printrational(tester); rational.negate(tester); rational.printrational(tester); rational.invert(tester); rational.printrational(tester); double doublevalue = todouble(tester); system.out.println(doublevalue); reduce(tester); //error here printrational(add(tester, testadd)); //error here } }
you're statically importing todouble
method rational
(btw, java classes traditionally capitalized, should name class rational
), not others, reduce
, can't invoke them without prefacing them class name.
so rational.reduce(tester);
work, adding import static main.rational.reduce;
top of main
class.
Comments
Post a Comment