-2

So we have to create a Rational class that does the following. Create a project in Java that includes a Rational class. The class represents Rational numbers. It should have only 2 fields num and denom. It should have the following public methods (and any other private methods needed):

  • A constructor that takes a num and denom (in that order) as initial field values. If denom is 0, set the number to 0/1.
  • An accessor for num (getNum)
  • An accessor for denom (getDenom)
  • A toString method that returns String of the form "num/denom" (no spaces) where num and denom have the stored values
  • An add method that takes a Rational number r and returns a Rational number that is the result of adding r to this Rational number. It should not change this rational number.
  • (Extra credit - 2 points) A reduce method with no arguments that reduces this Rational number to lowest form.

I have code that I have done in Intellij IDEA but it will not let me run the program. I know that I am getting errors for most things but I believe it is just because I am not putting something in the correct place or leaving something out. This is what I have so far.

import java.util.Scanner;

public class Rational {

    private int num;
    private int den; //fields

    static Scanner userInput = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("")

        public Rational( int n, int d){

            num = n;
            den = d;

            if (d == 0) {
                num = 0;
                den = 1;

                System.out.println("Denominator is 0. Enter a number other than 0 next time.");

            }//close if

            int g = gcd(num, den);
            num = n / g;
            den = d / g;

        }//close Rational

    public String toString() {

        if (den == 1) {
            return num + "";

        } else {

            return num + "/" + den;

        }

    }//close toString

    private Rational add(Rational r) {

        int newNum = (this.num * r.den) + (r.num * this.den);
        int newDen = r.den * this.den;
        return new Rational(newNum, newDen);

    }//close add

    private static int gcd(int m, int n) {

        if (0 == n) {
            return m;

        } else {

            return gcd(n, m % n);

        }//close else

    }//close gcd

}//close main

}//close class
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • What does 'will not let me run the program' mean? What is happening when you run it? – Debosmit Ray Mar 30 '16 at 03:59
  • Why can't you just read the error message? `file.java:12: error: ';' expected System.out.println ("")` Pretty obvious why it fails. – Sorawee Porncharoenwase Mar 30 '16 at 04:01
  • @SoraweePorncharoenwase Where did you find the error message? I don't see anything in the post. Either way, thanks for the info. – Debosmit Ray Mar 30 '16 at 04:05
  • @SoraweePorncharoenwase I fixed the semi colon issue with System.out.println ("") and still no luck. Not sure where to begin. – mrphotochalupas Mar 30 '16 at 04:12
  • For one, why is your rational class inside your main method? – Natecat Mar 30 '16 at 04:14
  • @Natecat Didn't realize that. I have made a new Main class with public class Main { public static void main (String [] args){ } } And I have gotten rid of public static void main (String [] args) { } in the rational class. But I still cannot run it. – mrphotochalupas Mar 30 '16 at 04:20
  • Oh I meant why is the constructor for your rational class inside your main method, there's no reason to make a new class – Natecat Mar 30 '16 at 04:21
  • @Natecat My class has been using BlueJ but I was asking around and they told me about IntelliJ IDEA so I got it. In BlueJ everything had a template but here it's not that way. I have not dealt with public static void main (String [] args) before so I looked what I could up online about it and just placed it there in my code. When I took out the main method it got rid of all of the errors in the rational class, I just still can't run it. – mrphotochalupas Mar 30 '16 at 04:24
  • BlueJ is a crutch. I recommend you relearn Java basics. Can you be more detailed about what happens when you try to run it and update your OP – Natecat Mar 30 '16 at 04:27
  • I edited this question for a reason - so that the syntax highlighting would at least work, and to remove the code snippets since that does not work with Java. Also, I made your requirements section a bit more readable. – Makoto Mar 30 '16 at 04:48
  • @Natecat Can you recommend a good place for me to relearn the basics? BlueJ has been hindering me for sure. – mrphotochalupas Mar 30 '16 at 14:59

1 Answers1

0
      public Rational(int n, int d){ // put Your Constructor Out of the main First
         num = n;
         den = d;

         if (d == 0) {
            num = 0;
            den = 1;
            System.out.println("Denominator is 0. Enter a number other than 0 next time.");
        }//close if
         int g = gcd(num, den);
         num = n / g;
         den = d / g; 
       }//close Rational
    }

  public String toString() {  //put methods Outside of Main
                if (den == 1) {
                    return num + "";
                } else {

                    return num + "/" + den;

                }

            }//close toString

     private Rational add(Rational r) {   //put methods Outside of Main

          int newNum = (this.num * r.den) + (r.num * this.den);
          int newDen = r.den * this.den;
          return new Rational(newNum, newDen);
     }//close add

    private static int gcd(int m, int n) {  //put methods Outside of Main
                if (0 == n)
                    return m;
                else
                    return gcd(n, m % n);        
            }//close gcd

    public static void main (String [] args) { //here is your Main Method.

            System.out.println("");

            // call Your Methods Accordingly. 

        } // main Closed
    } //class Closed
  1. You have put Constructor into the main Method. It should be out of the main box.
  2. Each method that you Defined is exists within the main Method. Again a Compilation Error .
  3. You can call these methods From inside of main either using any class object (If Instance method) or using Class Name directly (If Static Method).
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • ***Explain*** the problem. Don't just leave a bunch of code for someone to pick through. – Makoto Mar 30 '16 at 04:46
  • I have also provide comments to explain the Problem @Makoto Thanks. I have Added the Problem as a `text` Too. – Vikrant Kashyap Mar 30 '16 at 04:52
  • @VikrantKashyap Thank you. I am, as you can tell, new to Java, and new to programming as a whole and my class has not taught us how to use a real compiler such as IntelliJ IDEA as we have been using BlueJ but I wanted to learn real programming so I could learn more on my own. Thanks! – mrphotochalupas Mar 30 '16 at 15:23