Question

Write a class called Course_xxx that represents a course taken at a school. Represent each student...

Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class School_xxx with a main method that creates a course, adds several students, and prints a roll.

Submit School_xxx.java and Course_xxx.java where xxx are your initials.

Student.java

//********************************************************************
// Student.java Author: Lewis/Loftus
//
// Represents a college student.
//********************************************************************

public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;

//-----------------------------------------------------------------
// Constructor: Sets up this student with the specified values.
//-----------------------------------------------------------------
public Student(String first, String last, Address home,
Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}

//-----------------------------------------------------------------
// Returns a string description of this Student object.
//-----------------------------------------------------------------
public String toString()
{
String result;

result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress + "\n";
result += "School Address:\n" + schoolAddress;

return result;
}
}

Please include commentary/documentation for me to better understand the code. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

COURSE.JAVA

package abc;

import java.util.ArrayList;

import java.util.List;

public class Course {

private String courseName;

private List<Student> students= new ArrayList();

//constructor of the Course class that accepts only course name

public Course(String courseName) {

super();

this.courseName = courseName;

}

//addStudent method accepts one Student parameter

public void addStudent(Student student) {

students.add(student);

}

//roll method that prints all students in the course

public void roll(Course course) {

System.out.println("Course name: "+course.courseName);

System.out.println("Students registered for the course "+course.courseName+" :");

System.out.println("======================================");

for (Student student : course.students) { //looping through all the students enrolled for that course

System.out.println(student);// calls to string method of student class

System.out.println("======================================");

}

}

}

================================

STUDENT.JAVA

package abc;

public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;

//-----------------------------------------------------------------
// Constructor: Sets up this student with the specified values.
//-----------------------------------------------------------------
public Student(String first, String last, Address home,
Address school)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}

//-----------------------------------------------------------------
// Returns a string description of this Student object.
//-----------------------------------------------------------------
public String toString()
{
String result;

result = firstName + " " + lastName + "\n";
result += "Home Address:\n" + homeAddress.getAddress() + "\n";
result += "School Address:\n" + schoolAddress.getAddress();

return result;
}
}

=======================================================

ADRESS.JAVA

package abc;

public class Address {

private String address ;

// setters and getters for address

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}

===============================================================

SCHOOL.JAVA:

package abc;

public class School {

public static void main(String[] args) {

Course course1 = new Course("Java"); // Creating course java

// Details of student1

Address homeAddress = new Address();

Address schoolAddress = new Address();

homeAddress.setAddress("3-33, SR nagar,hyderabad, Telangana");

schoolAddress.setAddress("3-4, 5th phase KPHB, Hyderabad, Telangana");

Student s = new Student("Robert", "D", homeAddress, schoolAddress);

course1.addStudent(s); // Robert is added to java course

// Details of student2

Address homeAddress1 = new Address();

Address schoolAddress1 = new Address();

homeAddress1.setAddress("4-33, chanda nagar,hyderabad, Telangana");

schoolAddress1.setAddress("3-4/3, 6th phase KPHB, Hyderabad, Telangana");

Student s1 = new Student("Andrew", "K", homeAddress1, schoolAddress1);

course1.addStudent(s1); //andrew is added to java course

//calling roll method of course to print the students enrolled for the course

course1.roll(course1);

}

}

Add a comment
Know the answer?
Add Answer to:
Write a class called Course_xxx that represents a course taken at a school. Represent each student...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

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

  • using basic c++ please!!! 6. (21%) Consider the following definitions class Student { class Section public:...

    using basic c++ please!!! 6. (21%) Consider the following definitions class Student { class Section public: string getFirst (); string getLast (); intgetGrade () void setFirst (string s); void setLast (string s); void setGrade(int g); numStudents (); in Student getNthStudent (int n); string getCourse (); string getInstructor (); private: private: string firstName; string lastName; int Student students [SIZE]; string course; string instructor; grade ; Write a function, honorRoll that returns a dynamic array containing only those students in section sec...

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

  • please use java do what u can 1. Modify the Student class presented to you as...

    please use java do what u can 1. Modify the Student class presented to you as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor such that each test score is assumed to be initially zero. Provide a method called set Test Score that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called get...

  • CSCI 135 Test 2 Name: 6, (21%) Consider the following definitions: class Student class Section public:...

    CSCI 135 Test 2 Name: 6, (21%) Consider the following definitions: class Student class Section public: string getFirst ) string getLast () int getGrade) void set First (string s) void setLast (string ) void setGrade (nt g): numStudents () Student getNthStudent (int n) string getCourse ) string getInstructor ); private: private: string firstName string lastName Student students [SIZE: string course; string structor; grade; Write a function, honorRoll that returns a dynamic array containing only those students in section sec with...

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

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

  • Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The...

    Rewrite the Course class in Listing 10.6 to implement the comparable and the cloneable interfaces. The clone method must allow a deep copy on the students field. In addition, add the equals(Object o) and the toString() methods. Write a test program to invoke the compareTo, clone, equals, and toString methods in a meaningful way. Below is the Listing from the book that needs to be rewritten public class Course {    private String courseName;    private String[] students = new...

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

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