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,...

  • 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...

  • 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...

  • 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...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

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
ADVERTISEMENT