0

I've made a main method in one class and a lot of other small methods in another class. When I call on them in my main method using their location and making sure that they would outprint if I called on them, they still don't outprint. Only the print two methods show any output. I'm not sure how to go about fixing it so I haven't tried many things yet. Could you look at my code and check why they aren't working?

Update: I've managed to get all the line in the main method except for 28 working with the help I received. Now all that's left is that one output. I've changed the code so it works a bit better and will shut down if it doesn't output, but the output is still missing.package rational;

My Main Method

    package rational;

    /**
     *
     * @author Dominique
     */

    public class Rational {


        public static String number() {
        rational1 number= new rational1(27, 3);
        String r3= number.printRational(number);
        return r3;
    }
        public static void main(String[] args) {
          rational1 number= new rational1(27,3);
            System.out.println(number());  
          String r3=number();
          System.out.println(rational1.toDouble(27,3 ));
          rational1.add(number);
          rational1.invert(r3, number);
          rational1.negate(r3, number);
          rational1.toDouble(27, 3);
        }


    }

My Other Method Class

    package rational;

/**
 *
 * @author Dominique
 */
public class rational1 {
    public int top;
    public int bottom;

public rational1 () {
 this.top = 0;       
 this.bottom = 0;

 } 
 public rational1(int top, int bottom){
        this.top=top;
        this.bottom=bottom;
    }
public String printRational(rational1 r1){
    String r3=("Your fraction is "+String.format(r1.top+" / "+r1.bottom));
    return r3;
}
public static void invert(String r2, rational1 r1) {
    int index = r2.indexOf('s');

    if (index != -1) {
            System.out.print(r2.substring(0, index+1));//works
            System.out.println(" "+r1.bottom + "/" + r1.top);
            index++;
    }
    else {
            System.exit(0);
        }
}
public static void negate(String r2, rational1 r1){
    int index = r2.indexOf('-');  
 if (index != -1) {
  String stringValueOf = String.valueOf(r1.top);
  System.out.println(r2.substring(0, 17));//works
  System.out.println(r1.bottom+"/"+stringValueOf.substring(1));
  index++;
  }
}


public static double toDouble(int one, int two){
    int three= one/two;
    return three;
}

public static double gcd( double a, double b)
{
double r = a % b;
if (r != 0)
{
return gcd(b, r );
}
else
{
return b;
}
}
 public static double reduce(double t, double b){
        double numberone=gcd(t, b);
        double pick=numberone*(b/t);
        return pick;
    }

 public static double add(rational1 r1){
     double pickone=(r1.top);
     double choice= pickone+pickone;
     double choice2=reduce(choice, r1.bottom);
     return choice2;
 }
}

3 Answers3

2

So the problem is in invert method:

public static void invert(String r2, rational1 r1){
       int index = 0;

  while (index < 1) {
    if (r2.charAt(index) == '/') {
    System.out.print(r2.substring(0, 17));
    System.out.print(r1.bottom+"/"+r1.top);
    index++;
    }else{
      System.exit(0); 
   }
 `}
}

This method immediate checks the character at r2.charAt(index) == '/'), but this is never the case. Because the character at index = 0 is 'Y' from the printRational method. Because that's not the case then System.exit(0) gets called which immediately ends the program without running the rest of the program.

I believe that this code will work.

public static void invert(String r2, rational1 r1) {
    int index = r2.indexOf('/');

    if (index != -1) {

            index++;
    }
    else {
                            System.out.print(r2.substring(0, index));//works
            System.out.print(r1.bottom + "/" + r1.top);
        }
}
Brian Thorpe
  • 317
  • 2
  • 13
0

The print method does not necessarily flush the buffer to the screen. Try replacing the print method with the println method.

karakuricoder
  • 1,065
  • 8
  • 8
0

Once this is in rational package., try to change the system.out.print to system.out.println .Basically all your codes are okay. Try look at this link.

Click [here] (http://introcs.cs.princeton.edu/java/92symbolic/Rational.java.html)!