Kristina Mortimer
Kristina Mortimer

Reputation: 7

How to create a HarnessTest class for java classes?

I have created basic classes for my application. I'm trying to run manual unit test for my two classes under TestHarness constructor but am running into and error: Constructor Person in class cannot be applied to given types required: no argument found: String, String, int reason: actual and formal argument lists differ in lenght

and

cannot find symbol symbol: method setLastName(String) location: variable p1 of type Person

here is my Person.java class:

public class Person {
    private String firstName, lastName;
    private Integer age;



    public Person (String firstName, String lastName, Integer age) {
        this.firstName = firstName;
        if (firstName == "" || firstName == null)
            this.firstName = "No first name given";



        this.lastName = lastName;
        if (lastName == "" || lastName == null)
            this.lastName = "No last name given";


        this.age = age; 


    }

    public String getFirstName () {
        return firstName;
    }

    public void setFirstName (String firstName) {
        if (firstName == "" || firstName == null)
            this.firstName = "No first name given";
        else
            this.firstName = firstName;
    }

    public String getLastName () {
        return lastName;
    }

    public void setLastName (String lastName) {
        if (lastName == "" || lastName == null)
            this.lastName = "No last name given";
        else
            this.lastName = lastName;
    }

    public Integer getAge () {
        return age;
    }

    public void setAge (Integer age) {
            this.age = age;
    }

    @Override
    public String toString() {
    return "First Name: "+firstName + "\nLast Name : " + lastName +
            "\nAge: " + ((age<=0 || age>=120)? "No age given" : age); 
}
}

Here is my Case.java class:

public class Case {
     private String caseID ="" ;


public Case (String newCaseID) {
    this.caseID  = newCaseID; 
    if (caseID == "" || caseID == null)
            this.caseID = "No case ID given";
        else
            this.caseID = caseID;
}

public String getCaseID () {
    return caseID;
}

public void setCaseID (String newCaseID) {
    if (caseID == "" || caseID == null)
            this.caseID = "No case ID given";
        else
            this.caseID = caseID; 
}


@Override
public String toString() {
return "Case ID: "+ caseID;
        }
}

And here is my TestHarness.java class:

package HHHN;
/**
 *
 * @author 18057
 */

public class TestHarness {

    public TestHarness () {
    //Person test cases
    //Test 1 - valid first name
    Person p1 = new Person("","Smith",14);
    System.out.print("Person information: " + p1.toString());
    //change first name
    p1.setFirstName("Laura");
    System.out.println(p1.toString());


    //Test 2 - valid last name
    Person p2 = new Person("Laura","",14);
    System.out.print("Person information: " + p2.toString());
    //change last name
    p2.setLastName("Smith");
    System.out.println(p2.toString());

    //Test 3 - valid age
    Person p3 = new Person("Laura","Smith",0);
    System.out.print("Person information: " + p3.toString());
    //change age
    p3.setAge(14);
    System.out.println(p3.toString());



    //Case test cases
    //Test 1 - valid caseID
    Case c1 = new Case("");
    System.out.println("Missing person case ID: "+c1.toString());
    //change case ID
    c1.setCaseID("V1234");
    System.out.print(c1.toString());

}
}

Any help would be appreciated.

Let me know if any additional information is needed.

Upvotes: 0

Views: 116

Answers (1)

Matt
Matt

Reputation: 111

Your code has a few errors in it. We can start in your 'TestHarness' class:

Person p1 = new Person("", "Smith", 14);

You cant have duplicate variables, so each p1 AFTER the first must either have a different name (p2?) or you can reuse the variable again like so:

p1 = new Person("Laura", "", 14);

Next we have the code in your 'Person' class. It doesnt cause an error but its pointless:

this.firstName = firstName;
    if (firstName == "" || firstName == null)
        this.firstName = "No first name given";
    else
        this.firstName = firstName;

Can be changed to:

this.firstName = firstName;
if (firstName == "" || firstName == null)
    this.firstName = "No first name given";

Since you have already set the variable you dont need to set it again for the else case.

Moving on to the age, you cant set an integer as a String, so you wont be able to make that variable equal to "No age given" in the way that you did. Instead you can change your toString method to include an if statement inside of it.

@Override
public String toString() {
    return "First Name: "+firstName + "\nLast Name : " + lastName +
        "\nAge: " + ((age <=0  || age >= 120 )? "No age given" : age); 
}

Then when you set the age, dont bother checking if its within the bounds, just set it to whatever integer value it is given. Your error is coming from:

this.age = age; 
    if (age <=0  || age >= 120 )
        this.age = "No age given";    //<--- ERROR
    else
        this.age = age;

As you cannot set an integer as a string. Hopefully this was helpful.

Upvotes: 1

Related Questions