vendredi 27 janvier 2017

How to call results of a Boolean method in a main method

So, school has moved us on to Java, without really explaining the basics.

Right now I have an issue where I have a class with a method equalsTest that tests whether two names are the same. Then in my main method I have to call the results of equalsTest and print "Same friend" if the names are the same (equalsTest = true) and "not same friend" if the names are different (equalsTest = false)

public class Person {

/* Attribute declarations */
private String lastName;    // last name
private String firstName;   // first name
private String email;       // email address

public Person(String firstName, String lastName, String email) {
    this.firstName = firstName;
    this.lastName = lastName;       
    this.email = email;
}

//there are a few other methods in here but are not necessary for the issue explained

public boolean equalsTest(Person other){
    if (this.firstName.equals(other.firstName)&& this.lastName.equals(other.lastName))
        return true;
    else
        return false;

public static void main (String[] args) {
    // create a friend
    Person friend1 = new Person("Mickey", "Mouse", "");
    friend1.setEmail("mickey@uwo.ca");

    // create another friend
    Person friend3 = new Person ("Mickey", "Mouse", "");

    // create a friend without email
    Person friend2 = new Person("Minnie", "Mouse", "");

//Here I need another method that calls the "true" or "false" boolean result of the equalsTest method above and says "if equalsTest true, print "Same Friend", and if equalsTest false, print "Not Same Friend", etc.

Also, part of the requirement for the lab is that I CANT edit the equalsTest method, I have to call it in the main method. I know it would be easier to just make a section in the main method that would analyze it, but can't do it.

Aucun commentaire:

Enregistrer un commentaire