Question

I asked this a little bit ago but didn't clarify exactly what I needed, so I'm...

I asked this a little bit ago but didn't clarify exactly what I needed, so I'm asking again...I've attached my code below the question. Thanks!

Do not change the original contract of the Course class. Instead, ONLY change the implementation of property students, from an array to and ArrayList, and update any methods in class Course that access students to reflect ArrayList. The methods are overridden. That means that you do not need to change the header for methods, only the body of the method. The method signature remains public String[] getStudents() but the body of the method will remove the strings from the ArrayList put them in an Array of String and return the Array of String. A testfile- Exercise11_05.java has been provided for your use on Blackboard. DO NOT alter the testfile Exercise11_05.java. Contract – all method names, method parameters, and method return types must stay the same. This problem is an exercise in abstraction and encapsulation

import java.util.*;

public class Course {
private String courseName;
private ArrayList<String> students = new ArrayList<String>();
private int numberOfStudents;

public Course(String courseName) {
this.courseName = courseName;
numberOfStudents = 0;
}

public void addStudent(String student) {
students.add(student);
numberOfStudents++;
}

public String[] getStudents() {
return students;
}

public int getNumberOfStudents() {
return numberOfStudents;
}

public String getCourseName() {
return courseName;
}

}

//Course Test code...should not be altered.

import java.util.*;

public class CourseTest {
public static void main(String[] args) {
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");

course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");

course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");

System.out.println("Number of students in course1: "
+ course1.getNumberOfStudents());
ArrayList students = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");

System.out.println();
System.out.print("Number of students in course2: "
+ course2.getNumberOfStudents());
}
}

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

import java.util.*;

public class Course {
private String courseName;
private ArrayList<String> students = new ArrayList<String>();
private int numberOfStudents;

public Course(String courseName) {
this.courseName = courseName;
numberOfStudents = 0;
}

public void addStudent(String student) {
students.add(student);
numberOfStudents++;
}

public ArrayList<String> getStudents() {
return students;
}

public int getNumberOfStudents() {
return numberOfStudents;
}

public String getCourseName() {
return courseName;
}

}

__________________________________________

//Course Test code...should not be altered.

import java.util.*;

public class CourseTest {
public static void main(String[] args) {
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");

course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");

course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");

System.out.println("Number of students in course1: "
+ course1.getNumberOfStudents());
ArrayList students = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students.get(i) + ", ");

System.out.println();
System.out.print("Number of students in course2: "
+ course2.getNumberOfStudents());
}
}

------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
I asked this a little bit ago but didn't clarify exactly what I needed, so I'm...
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
  • 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...

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

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

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

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

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

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

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
Active Questions
ADVERTISEMENT