0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you. i try to add like this: myCollege.addLecturer("David",4,"Banana",1004); but i get error compiler
Please. Take a breath and read the compiler errors. You gave that method four arguments when it only takes one. What should you do? Hint: Create a new Lecturer and pass it to that method.
Still has an error: i create a new Lecturer >> Lecturer[] L4 = new Lecturer[] { new Lecturer("David", 3, "Banana", 1001) }; and >> myCollege.addLecturer(L4); but stiil error
Please see revisions.
I saw, thanks, Works. but i try to print like that >> System.out.println(myCollege); this give my an error >> The problem is in the department print function (inside for loop). how can i fix that?
|
0

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.

1 Comment

Also thank you understand it. I have a problem reading through the main function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.