Question

I have a little problem about my code. when i'm working on "toString method" in "Course"...

I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it:

Student class:


public class Student
{
   private String firstName;
   private String lastName;
   private String id;
   private boolean tuitionPaid;
  
   public Student(String firstName, String lastName, String id, boolean tuitionPaid)
   {
       this.firstName=firstName;
       this.lastName=lastName;
       this.id=id;
       this.tuitionPaid=tuitionPaid;
   }
  
   public String getFirstName()
   {
       return firstName;
   }
  
   public String getLastName()
   {
       return lastName;
   }
  
   public String getId()
   {
       return id;
   }
  
   public boolean getTuitionPaid()
   {
       return tuitionPaid;
   }
  
   public void setFirstName(String firstName)
   {
       this.firstName=firstName;
   }
  
   public void setLastName(String lastName)
   {
       this.lastName=lastName;
   }
  
   public void setId(String id)
   {
       this.id=id;
   }
  
   public void setPaid(boolean tuitionPaid)
   {
       this.tuitionPaid=tuitionPaid;
   }
     
   public void setTuitionPaid(boolean tuitionPaid)
   {
       this.tuitionPaid=tuitionPaid;
   }
  
   public String toString(String firstName, String lastName, String id, boolean tuitionPaid)
   {
       String s="Student First Name:"+firstName+"\nStudent Last Name:"+lastName+"\nStudent ID:"
               +id+"\nIs that true that this student have paid the tuition?"+tuitionPaid;
       return s;
   }

}

Course class:

import java.util.ArrayList;


public class Course
{
   private boolean getIn;
   private String name;
   private int maximum;
   private int member=0;
   private Student[] roster;
   private String[] studentInformations;
  
   public Course(String name, int maximum)
   {
       this.name=name;
       this.maximum=maximum;
       roster=new Student[maximum];
   }
  
   public void setRoster(Student[] roster)
   {
       roster=new Student[maximum];
      
       for (int i=0;i<maximum;i++)
       {
           this.roster[i]=roster[i];
           member++;
       }
      
   }

   public int getMaximum()
   {
       return maximum;
   }
  
   public Student[] getStudentRoster()
   {
       return roster;
   }
  
   public String getName()
   {
return name;
   }
  

   public void setMaximum(int maximum)
   {
   if(maximum > 0)
   {
       this.maximum = maximum;
   }
   }
  
       public void setName(String name)
       {
   this.name = name;
       }
      
   public boolean addStudent(Student student)
   {
       boolean add=false;
       if (maximum>member&&student.getTuitionPaid())
       {
           getIn=true;  
           roster[member]=student;
           member++;
           add=true;
       }else
       {
           add=false;
       }
       return add;
   }
     
   public boolean dropStudent(Student student)
   {
       boolean drop=false;
       ArrayList<String> Student=new ArrayList<String>();
      
       String information=student.toString();
      
       if(Student.contains(information))
       {
           Student.remove(information);
       }
       return drop;
   }
  
   public String toString(){
       String classInformation="Class Name: "+name+"\nSpace availiable: "+maximum;
       String register;
       if(getIn==false){
           register="\nThere is NO student in this class";
       }else{
           register="\n\tRegistered: "+member;
       }
       for(int i=0; i<maximum; i++)
       {
           studentInformations[i]=roster[i].toString();
       }
       return classInformation+getIn+studentInformations;
   }
  
}

Run CourseDriver class which test Student class and Course class:

public class CourseDriver {

public static void main(String[] args) {
      
       Student[] studentsInSchool = new Student[7];
       studentsInSchool[0] = new Student("Adam", "Ant", "S925", true);
       studentsInSchool[1] = new Student("Bob", "Barker", "S713", false);
       studentsInSchool[2] = new Student("Chevy", "Chase", "S512", true);
       studentsInSchool[3] = new Student("Doris", "Day", "S513", true);
       studentsInSchool[4] = new Student("Emilio", "Estevez", "S516", false);
       studentsInSchool[5] = new Student("Farrah", "Fawcet", "S956", true);
       studentsInSchool[6] = new Student("Greta", "Garbo", "S419", true);
      
      
       Course course = new Course("Media Studies", 5);
      
       /* note: to test the extra credit, replace the line above with the line below.
       * the rest of the program should run exactly the same.
       *
       * CourseAL course = new CourseAL("Media Studies", 5);
       *
       */
      
       System.out.println("******Testing toString: should print name, max enrollment, " +
               "and a message that the roster is empty.*****");
       System.out.println(course);


       System.out.println("******Testing add method: should add Adam, Chevy, Doris, Farrah, and Greta. " +
               "Should not add Bob or Emilio.*****");
       for(int i=0; i<studentsInSchool.length; i++) {
           Student currentStudent = studentsInSchool[i];
           if(course.addStudent(currentStudent)) {
               System.out.println(currentStudent + " added.");
           } else {
               System.out.println(currentStudent + " was not added.");
           }
       }
       System.out.println();

      
       System.out.println("******Testing toString and add methods: should print name, max enrollment, " +
               "number enrolled, and the roster.*****");
       System.out.println(course);

       System.out.println("******Testing drop and toString methods: no changes should be made when " +
               " trying to drop a student not in the course");
       course.dropStudent(studentsInSchool[4]);
       System.out.println(course);

       System.out.println("******Testing drop and toString methods: Chevy should be dropped");
       course.dropStudent(studentsInSchool[2]);
       System.out.println(course);

       System.out.println("******Testing add, drop, and toString methods: Bob should now be added " +
               " because he paid tuition and now there is room");
       studentsInSchool[1].setTuitionPaid(true);
       course.addStudent(studentsInSchool[1]);
       System.out.println(course);      
      
      

      
   }

}

Please run CourseDriver.java and check my code. Thank you very much!

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
I have a little problem about my code. when i'm working on "toString method" in "Course"...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are four parts for one code, all are small code segments for one question. public class StudentTest { public static void main(String[] args){ // Part Time Student Test Student ft1 = new PartTimeStudent("George", "Bush", "123-12-5678", 1, 2 ); System.out.println(ft1); Student ft2 = new PartTimeStudent("Abraham", "Lincoln", "001-90-5323", 6, 22 ); System.out.println(ft2); // Full Time Student Test Student pt1 = new FullTimeStudent("Nikola", "Tesla", "442-00-0998", 8, 26...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • In this exercise, we will be refactoring a working program. Code refactoring is a process to...

    In this exercise, we will be refactoring a working program. Code refactoring is a process to improve an existing code in term of its internal structure without changing its external behavior. In this particular example, we have three classes Course, Student, Instructor in addition to Main. All classes are well documented. Read through them to understand their structure. In brief, Course models a course that has a title, max capacity an instructor and students. Instructor models a course instructor who...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT