Constructor Pair cannot be applied to given types; Required: Integer, String; found: no argument

Viewed 349

This is the assignment:

Write a generic class Pair which has two type parameters—F and S—each representing the type of the first and second element of the pair, respectively. Add set and get methods for the first and second elements of the pair and be sure to demonstrate your setters/getters actually work. (Hint: The class header should be public class Pair<F, S> and the best way to demonstrate your setters is to call them from your constructor(s).)

Write a separate PairTest class to test class Pair. Create 2 Pair types and test your get and set methods on the following pair types: • Pair<Integer, String> p1 takes Integer and String types as a pair • Pair<String, Integer> p2 takes String and Integer

PairTest should output enough information to the terminal to show that your generic class Pair is able to set() and get() different types.

This is my code so far:

public class Pair <F, S> {
    private F First;
    private S Second;  

    public Pair(F First, S Second)
    {
        this.First = First;
        this.Second = Second;
    }
    public void setFirst(F First){
        this.First = First;
    }
    public F getFirst(){
        return First;
    }
    public void setSecond(S Second){
        this.Second = Second;
    }
    public S getSecond(){
        return Second;
    }
}

//test

public class PairTest {
    public static void main (String [] args)    {
        Pair<Integer,String> p1 = new Pair<Integer,String>();//here is where I get the error
        p1.setFirst(1);
        p1.setSecond("String");

        if (p1.getFirst() !=1 || !p1.getSecond().equals("String")){
            System.out.println("Error");
        } 
        else {
            System.out.println("First: " + p1.getFirst());
            System.out.println("Second: " + p1.getSecond());
        }
        System.out.println();
    }

    Pair<String, Integer> p2 = new Pair<String, Integer>();//Same error occurs here
    p2.setFirst(1);//"Package p2 does not exist"
    p2.setSecond("String");

    if (p2.getFirst() !=1 || !p2.getSecond().equals("String")){//error "Package p2 does not exist"
        System.out.println("Error");
    }
    else{//error "illegal start of type"
        System.out.println("First: " + p2.getFirst());//no errors here or below
        System.out.println("Second: " + p2.getSecond());
    }
}

Error given:

Constructor Pair in class Pair <F,S> cannot be applied to given types;

Required: Integer, String

found: no arguments

reason: actual and formal argument lists differ in length where F,S are type-variables:

F extends Object declared in class Pair

S extends Object declared in class Pair

My question:

What am I missing? It says that I need Integer and String.... isn't that what I have?The latter half of PairTest is just a copy of the top half. I'm still relatively new to java and really struggling to grasp what it is exactly that I'm doing and how to do it.

Bonus problem "Package p2 does not exist". What do I need to differently than p1 in order for p2 to "exist"? (please let me know if I should move this to a different question, it was originally part of this one because I thought it was connected to the error)

1 Answers

In Java if you declare a class with a construct with parameters, then the "empty constructor" is not avaiable by default and you have to declare it by yourself.

So for example:

public class A {

    public static void main(String[] args) {
        A first = new A();
    }

}

If you compile it works, but with this class:

public class B {

    public B(int i) {
        
    }
    
    public static void main(String[] args) {
        B second = new B();
    }
    
}

you get a compile error

Related