liran123
liran123

Reputation: 15

java - Function in class

I need to write a function to College department :

Add function adds additional lecturer. Action returns false if there is no place to add additional lecturer, and at the same true if the lecturer was successfully added.

What I had written so far:

public boolean newLecturer(Lecturer[] AllLecturer) {
    int MaxLecturer = 0;
    MaxLecturer = this.maxLecturer;
    int sum = 0;
    sum += 1;

    if (sum < MaxLecturer) {
        System.out.println("true");
        return true;
    }

    else {
        System.out.println("false");
        return false;
    }

}

The function does not work properly, It always returns true (because that the Max Lecturer always bigger than sum).

main:

public class main {

public static void main(String[]args){

Lecturer[] L1 = new Lecturer[]{new Lecturer("David",3,"Banana",1001)};  
Lecturer[] L2 = new Lecturer[]{new Lecturer("Yossi",5,"apple",1002)};   
Lecturer[] L3 = new Lecturer[]{new Lecturer("Y",2,"t",1003)};   

    College myCollege = new College("College1",20,L1,3);

    //System.out.println(myCollege);
    //myCollege.allLecturer=L2;
    //System.out.println(myCollege);

    myCollege.newLecturer(L1);
    myCollege.newLecturer(L2);
    myCollege.newLecturer(L3);
}
}

class College (Function here):

public class College {

public String name;
public int numOfLecturer;
public Lecturer[] allLecturer;
public int maxLecturer;

// constructor
public College(String Name, int NumOfLecturer, Lecturer[] AllLecturer,
        int MaxLecturer) {
    this.name = Name;
    this.numOfLecturer = NumOfLecturer;
    this.allLecturer = AllLecturer;
    this.maxLecturer = MaxLecturer;
}

public College(String Name) {
    this.name = Name;
}

public College(Lecturer[] AllLecturer) {
    this.allLecturer = AllLecturer;
}

public boolean newLecturer(Lecturer[] AllLecturer) {
    int MaxLecturer = 0;
    MaxLecturer = this.maxLecturer;
    int sum = 0;
    sum += 1;

    if (sum < MaxLecturer) {
        System.out.println("true");
        return true;
    }

    else {
        System.out.println("false");
        return false;
    }

}

@Override
public String toString() {
    String lecturers = "";

    for (Lecturer lecturer : allLecturer) {
        lecturers += lecturer;

    }

    return "[Name College: " + name + "] " + " [num Of Lecturer: "
            + numOfLecturer + "]" + " [all Lecturer: " + lecturers + "]"
            + " [max Lecturer " + maxLecturer + "]";
}
}

class Lecturer:

public class Lecturer {
public String name;
public int numOfTimesPenFalls;
public String favoriteIceCream;
public int autoNumber;

// constructor
public Lecturer(String Name, int NumOfTimesPenFalls,
        String FavoriteIceCream, int AutoNumber) {
    this.name = Name;
    this.numOfTimesPenFalls = NumOfTimesPenFalls;
    this.favoriteIceCream = FavoriteIceCream;
    this.autoNumber = AutoNumber;
}

public Lecturer(String Name) {
    this.name = Name;

}

@Override
public String toString() {
    return "[name: " + name + "] " + " [num Of Times Pen Falls: "
            + numOfTimesPenFalls + "] " + " [favorite Ice Cream: "
            + favoriteIceCream + "] " + " [auto Number: " + autoNumber
            + "]";
}
}

And finally how can I print it? Like this gives a compiler error:

myCollege.newLecturer("David",2,"Apple",1004);

thank you.

Upvotes: 0

Views: 99

Answers (2)

duffymo
duffymo

Reputation: 308763

You're new; you need a lot of help.

Start by learning and following Java coding standards. Variable names should start with lower case. Classes start with upper. Deviations from that make your code hard to read.

Your method is wrong. You need something like this inside that class:

private static final int MAX_LECTURERS = 3;

private int numLecturers = 0;
private Lecturer [] lecturers = new Lecturer[MAX_LECTURERS];

public boolean addLecturer(Lecturer lecturer) {
    boolean addedLecturer = false;
    if (this.numLecturers < MAX_LECTURERS) {
        this.lecturers[numLecturers++] = lecturer;
        addedLecturer = true;
    }
    return addedLecturer;
}

Here's how you use this method:

Lecturer newLecturer = new Lecturer("foo", 1, "bar", 3);
college.addLecturer(newLecturer);

Please stop with all that array nonsense. The array is inside the College class.

Upvotes: 1

P. Phi
P. Phi

Reputation: 26

The sum variable in your code is a local variable, its scope is only at the function level. This means the sum always get initialized to 0 and increased to 1 every time the function newLecturer() is called. That's why sum always smaller than MAX_LECTURER (1<3).

You need to use class variable numLecturers like in duffymo answer above.

Upvotes: 0

Related Questions