JAVA The Student Class: A Student has a Name-first and last name seperated by a
ID: 3606287 • Letter: J
Question
JAVA
The Student Class:
A Student has a
Name-first and last name seperated by a space
Student ID-id starts at 100, assigned using a static variable
All instance variable and getters and setters for all instance variables. A toString method in the format "Student: John Jones; ID: 101"
The Course Class:
Course
-courseName:String
-students: Student[]
-numberOfStudents: int
+Course(courseName: String)
+getCourseName(): String
+addStudent(student:Student): void
+addStudent(studentName: String): void
+dropStudent(studentId: int): void
+getStudents(): Student[]
+getNumberOfStudents(): int
The array of students allows for up to 10 students. Number of Students in coinstructer sets to 0. Three getters for getCourseName(): String, getStudents(): Student[], and getNumberOfStudents(): int. Two setters for addStudent(student: Student): void and addStudent(studentName: String): void. a toString method in the format of
Course: Calculus; Number Of Students: 4
The Students in the class are:
Student: Mary Smith; ID: 100
Student: John Jones; ID: 101
Student: Susan Thompson; ID: 102
Student: George Johnson; ID: 103
TestCourse
prompt the user for the name of a Course and create a Course object. In a loop, until the user enters a q or a Q; prompt the user to enter student names or Q to end, for each student entered, create a Student object and add it to the Course using addStudent method the Course class has. At the end of the loop, print the Course object using its toString method.
Explanation / Answer
Student.java
public class Student {
//Declaring instance variables
private String studentName;
//Declaring static variable
private static int studentId = 100;
//Parameterized constructor
public Student(String studentName) {
super();
this.studentName = studentName;
}
// getters and setters
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public static int getStudentId() {
return studentId;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Student :" + studentName + "; ID: " + (++studentId);
}
}
___________________
Course.java
import java.util.Arrays;
public class Course {
// Declaring instance variables
private String courseName;
private int noOfStudents;
Student students[] = null;
// Parameterized constructor
public Course(String courseName) {
super();
this.courseName = courseName;
this.noOfStudents = 0;
students = new Student[10];
}
// getters and setters
public Student[] getStudents() {
return students;
}
public String getCourseName() {
return courseName;
}
public int getNoOfStudents() {
return noOfStudents;
}
// This method will add the Student to the Student Array
public void addStudent(Student student) {
if (noOfStudents < 10) {
students[noOfStudents] = student;
noOfStudents++;
} else
System.out.println("** Array is Full **");
}
// This method will add the Student to the Student Array
public void addStudent(String studentName) {
if (noOfStudents < 10) {
students[noOfStudents] = new Student(studentName);
noOfStudents++;
} else
System.out.println("** Array is Full **");
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.println("Course :" + getCourseName() + "; Number of Students:" + noOfStudents);
for (int i = 0; i < noOfStudents; i++) {
System.out.println(students[i]);
}
return " ";
}
}
___________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
String courseName, studName;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the course name entered by the user
System.out.print("Enter Course Name :");
courseName = sc.nextLine();
//Creating An Course class object
Course c = new Course(courseName);
Student st = null;
/* This while loop continues to execute
* until the user enters "Q" or "q"
*/
while (true) {
System.out.print("Enter Student Name :");
studName = sc.nextLine();
if (studName.equals("Q") || studName.equals("q"))
break;
else {
st = new Student(studName);
c.addStudent(st);
continue;
}
}
c.toString();
}
}
____________________
Output:
Enter Course Name :Calculus
Enter Student Name :Mary Smith
Enter Student Name :John Jones
Enter Student Name :Susan Thompson
Enter Student Name :George Johnson
Enter Student Name :Q
Course :Calculus; Number of Students:4
Student :Mary Smith; ID: 101
Student :John Jones; ID: 102
Student :Susan Thompson; ID: 103
Student :George Johnson; ID: 104
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.