Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to create a for loop for the second half to print out the students name i

ID: 3780321 • Letter: I

Question

I need to create a for loop for the second half to print out the students name in JAVA?

1 public class Course {
2 private String courseName;
3 private String[] students = new String[4];
4 private int numberOfStudents;
5
6 public Course(String courseName) {
7 this.courseName = courseName;
8 }
9
10 public void addStudent(String student) {
11 students[numberOfStudents] = student;
12 numberOfStudents++;
13 }
14
15 public String[] getStudents() {
16 return students;
17 }
18
19 public int getNumberOfStudents() {
20 return numberOfStudents;
21 }
22
23 public String getCourseName() {
24 return courseName;
25 }
26
27 public void dropStudent(String student) {
28 // Left as an exercise in Exercise 10.9
29 }
30 }

1 public class TestCourse {
2 public static void main(String[] args) {
3 Course course1 = new Course("Data Structures");
4 Course course2 = new Course("Database Systems");
5
6 course1.addStudent("Peter Jones");
7 course1.addStudent("Brian Smith");
8 course1.addStudent("Anne Kennedy");
9
10 course2.addStudent("Peter Jones");
11 course2.addStudent("Steve Smith");
12
13 System.out.println("Number of students in course1: "
14 + course1.getNumberOfStudents());
15 String[] students = course1.getStudents();
16 for (int i = 0; i < course1.getNumberOfStudents(); i++)
17 System.out.print(students[i] + ", ");
18
19 System.out.println();
20 System.out.print("Number of students in course2: "
21 + course2.getNumberOfStudents());
22 }
23 }

Explanation / Answer

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Course {
private String courseName;
private String[] students = new String[4];
private int numberOfStudents;

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

public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents++;
}
  
public String[] getStudents() {
return students;
}

public int getNumberOfStudents() {
return numberOfStudents;
}

public String getCourseName() {
return courseName;
}
  
public void dropStudent(String student) {
// Left as an exercise in Exercise 10.9
}
}
public class TestCourse {
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());
String[] 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());
String[] students2 = course1.getStudents();
/* for loop for printing students 2 */
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students2[i] + ", ");
}
}

/* Note:in case of any doubt,Please ask,Thanks,God bless you!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote