Write a class TestStudent that does the following: 1. Creates a student with the
ID: 3686019 • Letter: W
Question
Write a class TestStudent that does the following:
1. Creates a student with the name Alice
2. Assigns some courses to Alice
3. Writes the information about Alice to the console window
4. Creates a PartTimeStudent with the name Bob
5. Assigns courses to Bob
6. Writes the information about Bob to the console
Here is my code so far:
class CourseListException extends Exception
{
CourseListException()
{
System.out.println("Too many courses ");
}
CourseListException(String str)
{
System.out.println(str);
}
}
class Student
{
public String name;
public String[5] courses;
Student()
{
name = "NoName";
}
Student(String[] names)
{
int i =0;
for(i = 0; i<5; i++)
{
courses[i] = names[i];
}
}
public void assignCources()
{
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter the Courses Names: ");
while(str!=null)
{
str = sc.hasNext();
courses[i] = str;
}
}
public void writeStudent()
{
for(int i=0; i<5; i++)
System.out.println(courses[i]);
}
}
class extends PartTimeStudent extends Student
{
PartTimeStudent()
{
Student();
}
PartTimeStudent(String names[])
{
Student(names);
}
public void assignCources()
{
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter the Courses Names: ");
while(str!=null)
{
str = sc.hasNext();
courses[i] = str;
}
}
}
Explanation / Answer
import java.util.Scanner;
// Student Class
class Student {
public String name;
public String[] courses;
Student(String name) {
this.courses = new String[5];
this.name = name;
}
Student(String name, String[] courses) {
this.courses = new String[5];
for (int i = 0; i < 5; i++) {
this.courses[i] = courses[i];
}
}
public void assignCources() {
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter the Courses Names for "+name+": ");
int i = 0;
while (i < 5) {
str = sc.nextLine();
courses[i] = str;
i++;
}
}
public void writeStudent() {
for (int i = 0; i < 5; i++)
System.out.println(courses[i]);
}
}
//PartTimeStudent Class
class PartTimeStudent extends Student {
PartTimeStudent(String name) {
super(name);
}
PartTimeStudent(String name, String courses[]) {
super(name, courses);
}
}
// Test Class
class TestStudent{
public static void main(String[] args) {
//1
Student Alice = new Student("Alice");
//2
Alice.assignCources();
//3
System.out.println("Alice courses: ");
Alice.writeStudent();
//4
PartTimeStudent Bob = new PartTimeStudent("Bob");
//5
Bob.assignCources();
//6
System.out.println("Bob courses: ");
Bob.writeStudent();
}
}
/*
Output:
Enter the Courses Names for Alice:
English
Math
Phy
Che
History
Alice courses:
English
Math
Phy
Che
History
Enter the Courses Names for Bob:
CSE
EE
ECE
ME
CE
Bob courses:
CSE
EE
ECE
ME
CE
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.