0

I am still pretty new to programming and need a little help with a program I am writing. It consists of two classes, a generic OrderedPair class and an ObjectPairTest class. This so far is my OrderedPair class: I used some of the suggestions from the comments below and have changed my ObjectPair class to as follows:

    package lab2;

    public class OrderedPair<F, S>{ 


public F getFirst;
public S getSecond;

private F first;
private S second;

public OrderedPair(F first, S second ){
    this.first = first;
    this.second = second;
    }
    public F getFirst() { return first; }
    public S getSecond() {return second;}
    public String toString() {return first.toString() + " | "+second.toString();}
public static final OrderedPair<String, Integer> c1 = new OrderedPair<String, Integer>("Pi", 3);
public static final OrderedPair<String,String> c2 = new OrderedPair<String, String>("Great", "Super");
public static final OrderedPair<Integer, Integer> c3 = new OrderedPair<Integer, Integer>(1, 2);
public static final OrderedPair<Float, String> c4 = new OrderedPair<Float, String>( 5f, "Maximum");
public static final OrderedPair<Integer, Float> c5 = new OrderedPair<Integer, Float>(69, 69f);

    }

And my ObjectPairTest class is as follows:

package lab2;

    public class OrderedPairTest{

public static void main(String[] args){
    System.out.println(c1.toString());
    System.out.println(c2.toString());
    System.out.println(c3.toString());
    System.out.println(c4.toString());
    System.out.println(c5.toString());
}//End class Main
}//End class OrderedPairTest

However when I try and run, it gives me the error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
c1 cannot be resolved
c2 cannot be resolved
c3 cannot be resolved
c4 cannot be resolved
c5 cannot be resolved
at lab2.OrderedPairTest.main(OrderedPairTest.java:6)

Thank you all for your help so far.

2 Answers2

0
public class OrderedPair<F, S>{ 

    private F first;
    private S second;

    public OrderedPair(F first, S second ){
        this.first = first;
        this.second = second;
    }

    public F getFirst() { return first; }
    public S getSecond() {return second;}
    public String toString() {return first.toString() + " | "+second.toString();}

    public static void test(){
        //Five pairs of data 
        OrderedPair<String, Integer> c1 = new OrderedPair<String, Integer>("Pi", 3);
        OrderedPair<String,String> c2 = new OrderedPair<String, String>("Great", "Super");
        OrderedPair<Integer, Integer> c3 = new OrderedPair<Integer, Integer>(1, 2);
        OrderedPair<Float, String> c4 = new OrderedPair<Float, String>( 5f, "Maximum");
        OrderedPair<Integer, Float> c5 = new OrderedPair<Integer, Float>(69, 69f);
        System.out.println(c1.toString());
    }
}
maskacovnik
  • 3,080
  • 5
  • 20
  • 26
-1

Add "OrderedPair." in front of all these variables public static void main(String[] args){ System.out.println(OrderedPair.c1.toString()); System.out.println(OrderedPair.c2.toString()); System.out.println(OrderedPair.c3.toString()); System.out.println(OrderedPair.c4.toString()); System.out.println(OrderedPair.c5.toString()); }

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • 1
    Typically, test class is located in the same package as the class under test. So default visibility is quite enough. – ponomandr Sep 08 '14 at 18:50
  • I tried some of your guys suggestions however I seem to have ran into more problems when trying to put the Test in its own class. I re posted the code with the error message. – Jack Benson Sep 08 '14 at 19:30