Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Rational program calling up from different file but in same package

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Rational program calling up from different file but in same package

    I'm having trouble calling up the methods that are in the constructor file from the main method.
    Here's the code in the main method:
     public static void main(String[] args) {
            // Create a new rational number and set it to 1/3
    		Rational rational1 = new Rational();
    		rational1.numerator = -1;
    		rational1.denominator = 3;
    		printRational(rational1); // -1/3
     
    		// Create a rational number using the second constructor
    		Rational rational2 = new Rational(3, 5);
    		printRational(rational2); // 3/5
     
    		// Negate the rational number
    		negate(rational1);
    		printRational(rational1); // 1/3
     
    		// Invert the numerator and denominator
    		invert(rational1);
    		printRational(rational1); // 3/1
     
    		// Convert the rational number to a double
    		double decimal = toDouble(rational1);
    		System.out.println(decimal); // 3.0
     
    		// Reduce a rational number to its lowest terms
    		Rational rational3 = new Rational(3, 21);
    		printRational(rational3); // 3/21
    		reduce(rational3);
    		printRational(rational3); // 1/7
     
    		// Sum two rationals
    		printRational(add(rational2, rational3)); 
    	}
     
    }
    All the methods are errors.
    Here's my code in the rational file
    public class Rational {
        int numerator, denominator;
     
     
    	public Rational() {
    		this.numerator = 0;
    		this.denominator = 0;
    	}
     
    	public Rational(int numerator, int denominator) {
    		this.numerator = numerator;
    		this.denominator = denominator;
    	}
     
    	public static void printRational(Rational rational) {
    		System.out.println(rational.numerator + "/" + rational.denominator);
    	}
     
    	public static void negate(Rational rational) {
    		int numerator = rational.numerator;
    		int negatedNumerator = -1 * numerator;
    		rational.numerator = negatedNumerator;
    	}
     
     
    	public static void invert(Rational rational) {
    		int oldNumerator = rational.numerator;
    		int oldDenominator = rational.denominator;
    		rational.numerator = oldDenominator;
    		rational.denominator = oldNumerator;
    	}
     
     
    	public static double toDouble(Rational rational) {
    		double numerator = rational.numerator;
    		double denominator = rational.denominator;
    		return numerator / denominator;
    	}
     
     
    	public static int gcd(int a, int b) {
    		if (b == 0) {
    			return a;
    		}
    		int r = a % b;
    		return gcd(b, r);
    	}
     
     
    	public static void reduce(Rational rational) {
    		int gcd = gcd(rational.numerator, rational.denominator);
    		rational.numerator = rational.numerator / gcd;
    		rational.denominator = rational.denominator / gcd;
    	}
     
    	public static Rational add(Rational rational1, Rational rational2) {
    		int newNumerator = rational1.numerator * rational2.denominator
    				+ rational1.denominator * rational2.numerator;
    		int newDenominator = rational1.denominator * rational2.denominator;
    		Rational newRational = new Rational(newNumerator, newDenominator);
    		reduce(newRational);
    		return newRational;
    	}
    }
    Any help would be great!


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Rational program calling up from different file but in same package

    To call a static method you have to use the classes name before it.

    Rational.negate();

    If that doesnt fix your problem, could you repost the class your main method is in? It is hard to test this out without all the code available.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    my21 (April 11th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Rational program calling up from different file but in same package

    Got it thanks!!

Similar Threads

  1. Replies: 6
    Last Post: October 26th, 2012, 11:52 AM
  2. CALLING ANOTHER PROGRAM
    By jeboi in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 6th, 2012, 08:33 PM
  3. [SOLVED] Calling a non-static method outside the package without using extends
    By iamhealed in forum Java Theory & Questions
    Replies: 6
    Last Post: September 9th, 2012, 12:04 AM
  4. Calling method from .class file
    By alexx_88 in forum Java Theory & Questions
    Replies: 6
    Last Post: April 24th, 2012, 03:14 AM
  5. The Rational Class Program [HOMEWORK HELP]
    By FCarlton24 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 18th, 2011, 01:31 PM