Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Difficulty with connecting 2 classes through the Switch Statement

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Difficulty with connecting 2 classes through the Switch Statement

    Hello,

    I'm currently learning Java through a course that is using the learning program BlueJ. However, I'm stuck and am hoping that someone here can help me in the right direction.

    For one of the assignments I need to create two classes, Workgroup and Student. The workgroup has 4 roles, which the student can fulfill. I've added those roles as variables in class Workgroup, as private Student writer.

    For every of the roles, I used below code.
    Where: getRole() is a String from the class Student, where I assign Writer to the object Student.
    getAverageGrade() is a Int from the class Student, where I assign the average grade of the object Student.
    previouslyParticipated() is a Boolean from the class Student. If true, the object cannot be added.

    public void addStudent(Student writer)
        {
            if (writer.getRole() == ("Writer") && (writer.getAverageGrade()) >= averageGrade && writer.previouslyParticipated() == false)
            {
                this.writer= writer;
            } 
        }

    My teacher told me that I need to get these 4 different methods into one method, using the Switch Statement. This is where I get stuck, and I hope that someone can help me out with this. I'm getting stuck on the switch (expression), as if I want to fill out the role of the student it give an error stating 'incompatible types: Student cannot be converted to int.

    Thank you for helping me out, if more information / code is required to explain this. Let me know.

    KR, Frank

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    'incompatible types: Student cannot be converted to int.
    Did the code mean to call a method in the Student class that returns an int value?

    writer.getRole() == ("Writer")
    Use the equals method to compare the contents of two String objects, not the == operator.

    get these 4 different methods into one method, using the Switch Statement.
    What 4 methods are those? What does each one of them do?
    What determines how and when any of them are called?
    What value is the switch statement supposed to select on?

    You need to design the program before determining what types of statements to use in that program. Given the steps the program needs to take to solve the problem, then decide on what types of statements to use.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    F_VRBRG (January 30th, 2022)

  4. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    Hi Norm,

    Thanks for you message.

    Did the code mean to call a method in the Student class that returns an int value?
    No in below code it should put the variable back to null, so that the role in the workgroup is 'vacant' again. So it doesn't do anything with the int value, if I'm correct.

    public void deleteStudent()
        {
            switch (role)
            {
                case "Writer":
                    writer = null;
                    break;
                case "Programmer":
                    programmer= null;
                    break;
                case "Debugger":
                    debugger = null;
                    break;
                case "Theorist":
                    theorist = null;
            }
        }

    What 4 methods are those? What does each one of them do?
    Those 4 methods are all the same as the one in the first post, except instead of writer, it is one of the other three roles. I hope that makes sense?

    What determines how and when any of them are called?
    You can call them anytime, only when the role is still on null, the object can be added. It needs to meet 3 requirements, the role of the student object needs to be equal to one of the workgroup variables, it should have a higher average grade then what is required and the object should not have previously participated in a workgroup.

    What value is the switch statement supposed to select on?
    If I want to add a student to the workgroup, I want to do that by only one method, not four. So I would create an instance of the Student class, and determine its role. When processing it through the switch statement in the Workgroup class, it should execute the case of the role. So for example writer.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    In the posted code, where is the value given to the variable role? It looks like it should be passed as an arg to the deleteStudent method.
    What does it mean to delete a Student. Normally that would mean removing an instance of a Student class from some collection like a list or array.

    add a student to the workgroup, I want to do that by only one method, not four.
    Yes that sounds right. One method to add a student. I don't know how 4 methods could do that.

    I think that the design could be with a Student base class and 4 sub classes, one for each of the roles.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    Hi Norm,

    I haven't learned anything about subclasses (yet). And I have to fix it through the Switch statement, or through the if-else-if statement. Maybe it is easier if I add the entire code I create of the Workgroup and the Student?
    I've tried to add a student through the below code, but it doesn't do anything. I don't know how to fix that.

    Also when trying to delete/remove a student from the workgroup through the Switch Statement, I get the Student cannot be converted to int error. Here I do not know how to fix that, and change Student to int.

    Thanks for the help!

    public void addStudentWriter (Student role)
        {
            if (writer.getRole() == ("Writer") && (writer.getAverageGrade()) >= averageGrade && writer.previouslyParticipated() == false)
            {
                this.writer = writer;
            } 
            else if (programmer.getRole() == ("Programmer") && (programmer.getAverageGrade()) >= averageGrade && programmer.previouslyParticipated() == false)
            {
                this.programmer = programmer;
            } 
            else if (debugger.getRole() == ("Debugger") && (debugger.getAverageGrade()) >= averageGrade && debugger.previouslyParticipated() == false)
            {
                this.debugger = debugger;
            } 
            else if (theorist.getRole() == ("Theorist") && (theorist.getAverageGrade()) >= averageGrade && theorist.previouslyParticipated() == false)
            {
                this.theorist = theorist;
            }
        }

    This is all the code in the class Workgroup
    public class Workgroup
    {
     
        private String name; //de name of the group.
        private double averageGrade; //average Grade for the student to join the workgroup. 
        private Student writer; //the role writer that the student will fulfill.
        private Student programmer; //the role programmer that the student will fulfill.
        private Student debugger; //the role debugger that the student will fulfill.
        private Student theorist; //the role theorist that the student will fulfill.
        private boolean isAttached;//states if a workgroup is attached to another workgroup. 
     
        /**
         * The constructor of the Workgroup
         * 
         * @param   name            The name of the Workgroup
         * @param   averageGrade    The average Grade for the student to join the workgroup.
         */
        public Workgroup(String name, double averageGrade)
        {
            this.name = name;
            this.averageGrade = averageGrade;
            isAttached = false;
            writer = null;
            programmer = null;
            debugger = null;
            theorist = null;
        }
     
        /**
         * Below 4 methods are to add students to the workgroup
         */
        public void addStudentWriter (Student writer)
        {
            if (writer.getRole() == ("Writer") && (writer.getAverageGrade()) >= averageGrade && writer.previouslyParticipated() == false)
            {
                this.writer = writer;
            } 
        }  
        public void addStudentProgrammer (Student programmer)
        {
            if (programmer.getRole() == ("Programmer") && (programmer.getAverageGrade()) >= averageGrade && programmer.previouslyParticipated() == false)
            {
                this.programmer = programmer;
            } 
        }  
        public void addStudentDebugger (Student debugger)
        {
            if (debugger.getRole() == ("Debugger") && (debugger.getAverageGrade()) >= averageGrade && debugger.previouslyParticipated() == false)
            {
                this.debugger = debugger;
            } 
        } 
        public void addStudentTheorist (Student theorist)
        {
            if (theorist.getRole() == ("Theorist") && (theorist.getAverageGrade()) >= averageGrade && theorist.previouslyParticipated() == false)
            {
                this.theorist = theorist;
            } 
        }
     
        /**
         * Below 4 methods is to remove a Student from the workgroup.
         */
        public void deleteWriter()
        {
            writer = null;
        }
        public void deleteProgrammer()
        {
            programmer = null;
        }
        public void deleteDebugger()
        {
            debugger = null;
        }
        public void deleteTheorist()
        {
            theorist = null;
        }
     
        /**
         * Methode - delete Student from workgroup through the Switch Statement.
         */
        public void deleteStudent()
        {
            switch (role)
            {
                case "Writer":
                    writer = null;
                    break;
                case "Programmer":
                    programmer= null;
                    break;
                case "Debugger":
                    debugger = null;
                    break;
                case "Theorist":
                    theorist = null;
            }
        }
     
    }

    This is all the code in the class Student
    public class Student
    {
        private int studentNumber; //The unique number of the student. 
        private String firstName; //First name of the student.
        private String lastName; //Last name of the student.
        private double averageGrade; //Average grade of the student.
        private String role; //The role assigned to the student.
        private boolean previouslyParticipated; //False = not previously participated in a workgroup, true = the opposite.
     
        /**
         * The constructor of the class Student. 
         * 
         * @param   studentNumber       The unique number of the student. 
         * @param   firstName           First name of the student.
         * @param   lastName            Last name of the student. 
         * @param   rol                 The role assigned to the student. This can be "Writer", "Programmer", "Debugger" or "Theorist". 
         */
        public Student(int studentNumber, String firstName, String lastName, String role)
        {
            this.studentNumber = studentNumber;
            this.firstName = firstName;
            this.lastName = lastName;
            this.role = role;
            previouslyParticipated = false;
        }
     
        /**
         * Set the Average Grade of the student. 
         * @param   averageGrade   averageGrade of the student. 
         */
        public void setAverageGrade (double averageGrade)
        {
            this.averageGrade = averageGrade;
        }
     
        /**
         * @param   previouslyParticipated   true = previously Participated, false = not previously Participated. 
         */
        public void setPreviouslyParticipated (boolean previouslyParticipated)
        {
            this.previouslyParticipated = previouslyParticipated;
        }
     
        /**
         * @return  true = previously Participated, false = not previously Participated.
         */
        public boolean previouslyParticipated ()
        {
            return previouslyParticipated;
        }
     
        /**
         * @return The average Grade of the student.
         */
        public double getAverageGrade()
        {
            return averageGrade;
        }
     
        /**
         * @return The assigned role of the student.
         */
        public String getRole()
        {
            return role;
        }
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    The posted code continues to use the == operator instead of the equals method to compare Strings. That needs to be changed.

    I get the Student cannot be converted to int error.
    Please copy the full text of the error message and paste it here. It should show what statement that error happens on.

    Where is the variable: role defined for the deleteStudent method?

    Other than the missing declaration for role, the posted classes compile without any errors.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    The posted code continues to use the == operator instead of the equals method to compare Strings. That needs to be changed.
    Yes, I still need to change that to the correct code.

    Where is the variable: role defined for the deleteStudent method? Please copy the full text of the error message and paste it here. It should show what statement that error happens on.
    I didn't define it in the class Workgroup. But if I do it, and add it as Private Student role; the error I get in the Switch Statement is incompatible types: Student cannot be converted to int. What I do not get is what type of expression I need to create/define in order for the Swith statement to work and set the variables Student writer, Student programmer, Student debugger and Student theorist back to null.

    Other than the missing declaration for role, the posted classes compile without any errors.
    I'm getting the following error in the below code --> java.lang.NullPointerException at Workgroup.addStudentWriter(Workgroup.java35). I know this has something to do with the parameter Student role, but I don't know which parameter to use, in order for it to work.

    public void addStudentWriter (Student role)
        {
            if (writer.getRole().equals("Writer") && (writer.getAverageGrade()) >= averageGrade && writer.previouslyParticipated() == false)
            {
                this.writer = writer;
            } 
            else if (programmer.getRole().equals("Programmer") && (programmer.getAverageGrade()) >= averageGrade && programmer.previouslyParticipated() == false)
            {
                this.programmer = programmer;
            } 
            else if (debugger.getRole().equals("Debugger") && (debugger.getAverageGrade()) >= averageGrade && debugger.previouslyParticipated() == false)
            {
                this.debugger = debugger;
            } 
            else if (theorist.getRole().equals("Theorist") && (theorist.getAverageGrade()) >= averageGrade && theorist.previouslyParticipated() == false)
            {
                this.theorist = theorist;
            }
        }

    Thanks for the help

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    the error I get in the Switch Statement is incompatible types: Student cannot be converted to int.
    What statement is that on? I do not get that error when I compile the posted code.

    I can not compile the addStudentWriter method outside of any class.
    That method's name doesn't make sense as the method changes more that writer variable.
    Also why is the Student object that is passed to the method named role? A Student has a role.
    A Student is not a role.

    getting the following error in the below code -->
    java.lang.NullPointerException at Workgroup.addStudentWriter(Workgroup.java35).
    That is not a compiler error. A NPE happens when the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    What statement is that on? I do not get that error when I compile the posted code.
    In the Switch Statement. I've added the variable Private Student role to the class Workgroup. And when I use role as an expression it give the error 'incompatible types: Student cannot be converted to int'.

    /**
         * Methode - delete Student from workgroup through the Switch Statement.
         */
        public void deleteStudent()
        {
            switch (role)
            {
                case "Writer":
                    writer = null;
                    break;
                case "Programmer":
                    programmer= null;
                    break;
                case "Debugger":
                    debugger = null;
                    break;
                case "Theorist":
                    theorist = null;
            }
        }

    I can not compile the addStudentWriter method outside of any class.
    That method's name doesn't make sense as the method changes more that writer variable.
    Also why is the Student object that is passed to the method named role? A Student has a role.
    A Student is not a role.
    How do I need to write that then, in order for it to work?


    That is not a compiler error. A NPE happens when the code is executed.
    How can I solve this, so that the cases are executed?

    Thanks for the help!

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    added the variable Private Student role to the class Workgroup
    That does not make any sense. A Student is not a role. A Student has a role.
    Why have a Student named role?
    What is the definition of a WorkGroup? It looks like it can have up to 4 Student members, one for each role.


    give the error 'incompatible types: Student cannot be converted to int'.
    Which statement has that error? Add a comment to the code to say where it happens:
    //<<<<< Here is the incompatible types error
    for it to work?
    Please describe what class that method is supposed to be in
    and what it is supposed to do to work.

    You have not posted all of the current code so that it is hard to see that is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    That does not make any sense. A Student is not a role. A Student has a role.
    Why have a Student named role? What is the definition of a WorkGroup? It looks like it can have up to 4 Student members, one for each role.
    Please describe what class that method is supposed to be in
    and what it is supposed to do to work.
    I think that is where my problem is. The Workgroup can have 4 Students, one for each role. For each role I create a variable. For the assignment I need to create a method in the class Workgroup where I use a Student parameter that will fill one of the roles in the workgroup. I know how it works for just a single member, that would work with the below code. However, I need this to work where it checks the assigned role in the class Student, based on that role (e.g. writer; programmer; debugger or theorist), it should assign that object to the variable. So that it is not null anymore. I hope my explanation makes sense in English
    public void addStudentWriter (Student writer)
        {
            if (writer.getRole().equals("Writer") && (writer.getAverageGrade()) >= averageGrade && writer.previouslyParticipated() == false)
            {
                this.writer = writer;
            }


    You have not posted all of the current code so that it is hard to see that is happening.
    Below is the full current code of both the class Workgroup and the class Student.

    public class Workgroup
    {
     
        private String name; //de name of the group.
        private double averageGrade; //average Grade for the student to join the workgroup. 
        private Student writer; //the role writer that the student will fulfill.
        private Student programmer; //the role programmer that the student will fulfill.
        private Student debugger; //the role debugger that the student will fulfill.
        private Student theorist; //the role theorist that the student will fulfill.
        private Student role; // I created this role, to add a student through one method. That method should check if the object of a Student can be added to the workgroup. But if I do that, it doesn't work. 
        private boolean isAttached;//states if a workgroup is attached to another workgroup. 
     
        /**
         * The constructor of the Workgroup
         * 
         * @param   name            The name of the Workgroup
         * @param   averageGrade    The average Grade for the student to join the workgroup.
         */
        public Workgroup(String name, double averageGrade)
        {
            this.name = name;
            this.averageGrade = averageGrade;
            isAttached = false;
            writer = null;
            programmer = null;
            debugger = null;
            theorist = null;
        }
     
        /**
         * Below method is to add students to the workgroup
         */
        public void addStudentWriter (Student role) // <<<<<<<< Here I get the NPE, and I don't know what I need to change in that parameter in order to execute the below code properly.
        {
            if (writer.getRole().equals("Writer") && (writer.getAverageGrade()) >= averageGrade && writer.previouslyParticipated() == false)
            {
                this.writer = writer;
            } 
            else if (programmer.getRole().equals("Programmer") && (programmer.getAverageGrade()) >= averageGrade && programmer.previouslyParticipated() == false)
            {
                this.programmer = programmer;
            } 
            else if (debugger.getRole().equals("Debugger") && (debugger.getAverageGrade()) >= averageGrade && debugger.previouslyParticipated() == false)
            {
                this.debugger = debugger;
            } 
            else if (theorist.getRole().equals("Theorist") && (theorist.getAverageGrade()) >= averageGrade && theorist.previouslyParticipated() == false)
            {
                this.theorist = theorist;
            }
        }
     
        /**
         * Below 4 methods is to remove a Student from the workgroup. This works, but for the assignment I need these 4 methods to be in one single method, ending in the same result. 
         */
        public void deleteWriter() // This method works, however I need this, and the ones below to be in a single method
        {
            writer = null;
        }
        public void deleteProgrammer()
        {
            programmer = null;
        }
        public void deleteDebugger()
        {
            debugger = null;
        }
        public void deleteTheorist()
        {
            theorist = null;
        }
     
        /**
         * Methode - delete Student from workgroup through the Switch Statement. 
         */
        public void deleteStudent()
        {
            switch (role) // <<<<<<<<<< Here is the incompatible types error 
            {
                case "Writer":
                    writer = null;
                    break;
                case "Programmer":
                    programmer= null;
                    break;
                case "Debugger":
                    debugger = null;
                    break;
                case "Theorist":
                    theorist = null;
            }
        }
     
    }
    public class Student
    {
        private int studentNumber; //The unique number of the student. 
        private String firstName; //First name of the student.
        private String lastName; //Last name of the student.
        private double averageGrade; //Average grade of the student.
        private String role; //The role assigned to the student.
        private boolean previouslyParticipated; //False = not previously participated in a workgroup, true = the opposite.
     
        /**
         * The constructor of the class Student. 
         * 
         * @param   studentNumber       The unique number of the student. 
         * @param   firstName           First name of the student.
         * @param   lastName            Last name of the student. 
         * @param   rol                 The role assigned to the student. This can be "Writer", "Programmer", "Debugger" or "Theorist". 
         */
        public Student(int studentNumber, String firstName, String lastName, String role)
        {
            this.studentNumber = studentNumber;
            this.firstName = firstName;
            this.lastName = lastName;
            this.role = role;
            previouslyParticipated = false;
        }
     
        /**
         * Set the Average Grade of the student. 
         * @param   averageGrade   averageGrade of the student. 
         */
        public void setAverageGrade (double averageGrade)
        {
            this.averageGrade = averageGrade;
        }
     
        /**
         * @param   previouslyParticipated   true = previously Participated, false = not previously Participated. 
         */
        public void setPreviouslyParticipated (boolean previouslyParticipated)
        {
            this.previouslyParticipated = previouslyParticipated;
        }
     
        /**
         * @return  true = previously Participated, false = not previously Participated.
         */
        public boolean previouslyParticipated ()
        {
            return previouslyParticipated;
        }
     
        /**
         * @return The average Grade of the student.
         */
        public double getAverageGrade()
        {
            return averageGrade;
        }
     
        /**
         * @return The assigned role of the student.
         */
        public String getRole()
        {
            return role;
        }
    }

  13. #12
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    Your addStudent method should look something like this:
    	public void addStudent(Student student){
    	    switch(student.getRole()){
    	        case "Writer":
    	            // do writer stuff 
    	            break; 
    	        case "Programmer":
    	            // do programmer stuff
    	            break;
    	        // and add all the cases for the student roles 
    	    }
    	}

  14. The Following User Says Thank You to LeslieS For This Useful Post:

    F_VRBRG (February 1st, 2022)

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    The WorkGroup does not need a Student variable named role. Remove that.
    See LeslieS's code for how to get a role for the switch statement in the addStudent method.

    Also the addStudentWriter method is wrong. It should be removed or rewritten to only add a Writer.

    Where is the main method for executing the program?
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    F_VRBRG (February 1st, 2022)

  17. #14
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    Quote Originally Posted by LeslieS View Post
    Your addStudent method should look something like this:
    	public void addStudent(Student student){
    	    switch(student.getRole()){
    	        case "Writer":
    	            // do writer stuff 
    	            break; 
    	        case "Programmer":
    	            // do programmer stuff
    	            break;
    	        // and add all the cases for the student roles 
    	    }
    	}
    Thanks for helping me out. I've been trying to get this code correct, but I'm still struggling. I can add a student, but it doens't add the student to the variable (for example Writer) in the workgroup. All variables stay at NULL. What am I missing/doing wrong in the code?
    I've also tried student.getAverageGrade for every code, but that doesn't work either.

    public void addStudent (Student student)
        {
            switch(student.getRole())
            {
                case "Writer":
                    if (writer.getAverageGrade() >= averageGrade && writer.previouselyParticipated() == false)
                    {
                        this.writer= writer;
                    }
                    break;
                case "Programmer":
                    if (programmer.getAverageGrade() >= averageGrade && writer.previouselyParticipated() == false)
                    {
                        this.programmer = programmer;
                    }
                    break;
                case "Debugger":
                    if (debugger.getAverageGrade() >= averageGrade && debugger.previouselyParticipated() == false)
                    {
                        this.practicus = practicus;
                    }
                    break;
                case "Theorist":
                    if (theorist.getAverageGrade() >= averageGrade && theorist.previouselyParticipated() == false)
                    {
                        this.theorist= theorist;
                    }
                    break;
            }
     
     
        }

    Quote Originally Posted by Norm View Post
    The WorkGroup does not need a Student variable named role. Remove that.
    See LeslieS's code for how to get a role for the switch statement in the addStudent method.

    Also the addStudentWriter method is wrong. It should be removed or rewritten to only add a Writer.

    Where is the main method for executing the program?
    I've deleted the variable Role, and removed the method addStudentWriter. Thank you.

    Where is the main method for executing the program?
    What do you mean with this?

  18. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    What is the addStudent method supposed to do? Where does it get the values to be tested before saving the student variable's contents to one of the 4 role variables in the WorkGroup class?
    The code is testing the contents of the one of the 4 saved Student variables, not the values of the passed argument in the student variable.
    All the tests being made should be against the student variable, not against any the the 4 saved Student variables.

    The code should save student in one of the 4 Student variables. The current code assigns the contents of one of the 4 variable to itself: this.writer = writer; // assigns value to itself

    What do you mean with this?
    A program needs a main method to for it to be executed by the java command. There is no class posted with a main method.
    How do you test the program?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #16
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    What is the addStudent method supposed to do? Where does it get the values to be tested before saving the student variable's contents to one of the 4 role variables in the WorkGroup class?
    The addStudent method will add an object of the class Student to the workgroup, in one of the 4 variables. It checks if the object of the class Student has an average Grade higher than the average Grade set in the workgroup. Also it checks if the student participated in a workgroup before with a boolean.
    The values should be checked in the class Student.

    The code is testing the contents of the one of the 4 saved Student variables, not the values of the passed argument in the student variable.
    All the tests being made should be against the student variable, not against any the the 4 saved Student variables.

    The code should save student in one of the 4 Student variables. The current code assigns the contents of one of the 4 variable to itself: this.writer = writer; // assigns value to itself
    So how would I need to write that in correct code, so that it assigns correctly?

    A program needs a main method to for it to be executed by the java command. There is no class posted with a main method.
    How do you test the program?
    I'm using BlueJ as a program, it's a learning program where you can inspect all your objects and test them.

  20. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    checks if the object of the class Student has an average Grade higher than the average Grade set in the workgroup. Also it checks if the student participated in a workgroup before with a boolean.
    That statement describes what the code needs to do. Write code that uses the argument: student in the tests instead of using one of the 4 Student objects that are saved in the WorkGroup class.
    The only use of the 4 Student variables in the class would be when it is assigned the value in student. For example when a Student's role is to be a Writer:
       writer = student;  //  save this student in its role variable

    What should the addStudent method do if the passed Student object does not have one of the 4 roles?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #18
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    What should the addStudent method do if the passed Student object does not have one of the 4 roles?
    The method works now. If a student doesn't meet the requirements, then it won't be added if one of the 4 roles is still NULL. Also, if a role is already fulfilled by a student, it should leave it out as well.
    I did a test with below code. If writer is already 'fulfilled', and I run the code again with an object that is 'writer', it will overrule the current data. How can you add that in the statement?

    public void addStudent (Student student)
        {
            switch(student.getRole())
            {
                case "Writer":
                    if (student.getAverageGrade() >= averageGrade && student.previouselyParticipated() == false)
                    {
                        writer= student;
                    }
                    break;
                case "Programmer":
                    if (student.getAverageGrade() >= averageGrade && student.previouselyParticipated() == false)
                    {
                        programmer = student;
                    }
                    break;
                case "Debugger":
                    if (student.getAverageGrade() >= averageGrade && student.previouselyParticipated() == false)
                    {
                        practicus = student;
                    }
                    break;
                case "Theorist":
                    if (student.getAverageGrade() >= averageGrade && student.previouselyParticipated() == false)
                    {
                        theorist= student;
                    }
                    break;
            }
     
        }

  22. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    There are 4 copies of the same code in the switch statement. That code to test for an acceptable Student should be done one time before the switch statement. All the switch statement would do is assign the Student to one of the 4 role variables, except for the following.

    if a role is already fulfilled by a student, it should leave it out as well.
    In the switch case statement for a specific role, add code to test if the role is already assigned and not replace it.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #20
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    Quote Originally Posted by Norm View Post
    There are 4 copies of the same code in the switch statement. That code to test for an acceptable Student should be done one time before the switch statement. All the switch statement would do is assign the Student to one of the 4 role variables, except for the following.


    In the switch case statement for a specific role, add code to test if the role is already assigned and not replace it.
    Thank you very much for the help, I just tested it and it works how I want it to work.

  24. #21
    Member
    Join Date
    Jan 2024
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty with connecting 2 classes through the Switch Statement

    Certainly, Frank. It seems like you're on the right track, but let's refine your approach to use a switch statement effectively. Here's how you can achieve that:

    ```java
    public void addStudent(Student student) {
    switch(student.getRole()) {
    case "Writer":
    if (student.getAverageGrade() >= averageGrade && !student.previouslyParticipated()) {
    this.writer = student;
    }
    break;
    // Add cases for other roles here
    default:
    // Handle cases for roles other than "Writer"
    break;
    }
    }
    ```

    In this code:

    1. We're using the `switch` statement to check the role of the student.
    2. Inside each `case`, we're checking the conditions specific to each role. For "Writer", we check if the average grade is greater than or equal to the required grade and if the student has not previously participated.
    3. The `default` case is for handling roles other than "Writer". You can add cases for other roles similarly.
    4. We're using `!student.previouslyParticipated()` to check if the student has not previously participated, which is equivalent to `student.previouslyParticipated() == false`.

    Make sure to adjust the conditions and add cases for the other roles as per your assignment requirements. If you help with Java assignment and any further issues or need more clarification, feel free to ask There are resources online where you can find guidance for programming tasks, such as the one you're facing. You might consider exploring various programming forums or seeking help from online assignment help platforms like ProgrammingHomeworkHelp.com to navigate through your challenges effectively.

Similar Threads

  1. Replies: 2
    Last Post: December 10th, 2019, 06:17 AM
  2. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Difficulty with understanding interaction between Classes
    By JBRPG in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2012, 07:16 AM
  5. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM