1

I'm having an extremely difficult time getting a private method with arguments to be usable in my toString method but have no idea how to get the two methods to cooperate.

main class:

import static java.lang.System.*;

public class Triples
{
 private int number;

public Triples()
{
    //this(0);
}

public Triples(int num)
{
    number = num;
}

public void setNum(int num)
{
    number = num;
}

private int greatestCommonFactor(int a, int b, int c)
{
    int max = number;

    for(int n = 1; n <= max; n++)
    {

    for(a = n; a <= max; a++)
    {
        a = n;
        for(b = a +1; b <= max; b++)
        {
            b =n;
            for(c = b + 1; c <= max; c++)
            {
                c = n;
                if(Math.pow(a, 2)+ Math.pow(b, 2)== Math.pow(c, 2))
                {
                    if((a%2==1 && b%2==0)|| (a%2==0 && b%2==1))
                    {
                        if(a%2<=1 && b%2<=1 && c%2<=1)
                        {

                            String last = a + "" + b + c;
                        }
                    }
                }

            }

        }

    }
    }

    return 1;
}

public String toString()
{
    String output="";
    output = output + this.greatestCommonFactor( ) + " \n";


    return output;
}
}

and for cross-referencing my runner class:

import static java.lang.System.*;

import java.util.Scanner;

public class Lab11j
{
 public static void main(String args[])
  {
       Scanner keyboard = new Scanner(System.in);
        String choice="";
            do{
                out.print("Enter the max number to use : ");
                int big = keyboard.nextInt();


                    //instantiate a TriangleThree object
             Triples triple = new Triples(big);
                //call the toString method to print the triangle
                out.println( triple );

                System.out.print("Do you want to enter more data? ");
                choice=keyboard.next();
            }while(choice.equals("Y")||choice.equals("y"));
    }
}

if you find you need clarification of this lab, here's a Google docs of the labsheet: https://docs.google.com/open?id=0B_ifaCiEZgtcX08tbW1jNThZZmM

tech_geek23
  • 229
  • 2
  • 10
  • 23
  • I find it interesting that your private method is named "greatestCommonFactor()" yet it does not appear to compute the GCF of the three numbers given. Are you sure you are doing this assignment correctly? – Code-Apprentice Nov 27 '12 at 01:07
  • thats a good point, the "algorithm help" included on the lab sheet has this: >loop a >>loop b >>>loop c >>>>check that a,b,c fulfill Pythagorean triple criteria – tech_geek23 Nov 27 '12 at 01:11
  • I think you are trying to implement the whole algorithm in the "greatestCommonFactor()" method. I also think that isn't the best place for it, nor the place intended by the lab. Where in your code does the implementation of that algorithm belong? – Code-Apprentice Nov 27 '12 at 01:12
  • so if the whole algorithm doesn't necessarily belong in greatestCommonFactor(), where might the various parts need to be implemented? – tech_geek23 Nov 27 '12 at 01:24
  • If I were doing this assignment, I would implement the algorithm another static method in `Lab11` which main can calls. – Code-Apprentice Nov 27 '12 at 03:20

3 Answers3

3

The variables a, b & c can be used as local variables here. This would allow you to remove them from the argument list of greatestCommonFactor:

private int greatestCommonFactor() {

   int a = 0;
   int b = 0;
   int c = 0; 
   ...

as they are only required within the scope of the method.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • for whatever reason, the way the lab is initially set up, it has the ints as arguments instead – tech_geek23 Nov 27 '12 at 01:03
  • In fact, they *should* be declared as local variables since you don't need an initial value for these from an outside source. – Code-Apprentice Nov 27 '12 at 01:04
  • @tech_geek23 Did you implement the body of the greatestCommonFactor() method or was this given to you? Perhaps your implementation is not what was intended. – Code-Apprentice Nov 27 '12 at 01:05
  • The way it initially was `private int greatestCommonFactor(int a, int b, int c) { int max = number; return 1;}` properly formatted, of course – tech_geek23 Nov 27 '12 at 01:07
  • So when it determines that the GCF of the 3 numbers in fact is 1, how would I carry the indivdual values for a, b, c to the toString method? – tech_geek23 Nov 27 '12 at 01:33
  • Have a look at this [small example](http://answers.yahoo.com/question/index?qid=20080211175959AApMTW5). – Reimeus Nov 27 '12 at 01:44
  • If I were to modify the code in the link you gave, it might just work.... but I'm still confused on how to call the method that gets a, b, and c when I want to print the string `output` – tech_geek23 Nov 27 '12 at 20:23
1

Well, yeah. You're not passing anything to greatestCommonFactor. I'm not sure what you expected to happen in your toString() method when you didn't pass enough arguments to a method.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

you need to pass them like

output = output + this.greatestCommonFactor(1,2,3) + " \n";

the thing is, unless you are passing parameters to toString, without this, this code seems very limited. Alternatively you need to set some fields on the class with what will be passed into your function.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156