Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing this java code.(No python and no C++ Just Java, the one used on textpad) A Student has a: Name - the

I need help writing this java code.(No python and no C++ Just Java, the one used on textpad)

A Student has a:

Name - the name consists of the First and Last name separated by a space.

Student Id a whole number automatically assigned in the student class Student id numbers start at 100. The numbers are assigned using a static variable in the Student class

Write the xxx_Student class

Include all instance variables

Getters and setters for instance variables

A static variable used to assign the student id starting at 100

A toString method which returns a String containing the student name and id in the format below:

Student: John Jones ID: 101

xxx_Course class

A Course has the following information (modify your Course class):

A name

An Array of Students which contains an entry for each Student enrolled in the course (allow for up to 10 students)

An integer variable which indicates the number of students currently enrolled in the course.

The Course class

Course -constructor

-courseName: String -Name of the course

-students: Student [] -An Array to store the students for the course

-numberOfStudents: int -The number of students: initialize to 0

+Course(courseName : String) +Creates a course with the specified name

+getCourseName() : String +Returns the course name

+addStudent (student : Student) : void +Adds a Student object to the array of students

+addStudent (studentName: String): void +Creates a Student object using studentName and adds it to the array

+getStudents() : Student [] +Returns the array of students

+getNumberOfStudents() : int +Returns the number of students in the array

In Eclipse: code the Course class

Create the class: Course

Write the 3 instance variables:

courseName: String

students: Student []

numberOfStudents: int

Compile after each step

In Eclipse: code the constructor

Write the constructor below which does the following:

Course (String name)

Sets courseName to name

Creates the students array of size 10

Sets number of Students to 0

Compile after each step

In Eclipse: write the 3 getters

Write the 3 getters:

+getCourseName() : String

+getStudents() : Student []

+getNumberOfStudents() : int

In Eclipse: write the 2 setters

Write the 2 setters:

addStudent (student : Student) : void

public void addStudent (Student student)

addStudent (studentName: String): void

public void addStudent (String studentName)

In Eclipse: Write toString (In bth Student and course class as well)

Return a String that contains the following information concatenated so that the information prints on separate lines as shown in the sample output:

Course: xxxxx Number Of Students: xx

The students in the class are:

Each of the Student objects in the students array followed by a so they print on separate lines

Write a class TestCourse which will

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 the addStudent method of the Course class

At the end of the loop, print the Course object. Its toString method will format the output as shown on the next slide

Sample Output

Enter the course name

CPS2231 -04

Enter the name of a student or Q to quit

Jon

Enter the name of a student or Q to quit

Mary

Enter the name of a student or Q to quit

Tom

Enter the name of a student or Q to quit

q

Course: CPS2231 -04 Number Of Students: 3

The students in the class are:

Student: Jon ID: 100

Student: Mary ID: 101

Student: Tom ID: 102

Recap: I tried to use the code from the other answer but I keep getting errors.

Here is what I have so far.

public class Student {

private String name;

private int studentID;

private static int studentIDSeq = 100;

private Date dateCreated;

public Student()

{

name = "";

studentID = studentIDSeq;

studentIDSeq++;

dateCreated = new Date();

}

public Student(String fName)

{

fullName = fName;

studentID = studentIDSeq;

studentIDSeq++;

dateCreated = new Date();

}

public String getName()

{

return name;

}

public void setlName(String fName)

{

name = fName;

}

public int getStudentID()

{

return studentID;

}

public String toString()

{

return "Student: " + name + "; ID: " + id;

}

}

------------------------------

import java.util.*;

public class Course {

// Instance variables

String courseName;

Student [] students;

int numberOfStudents;

// default constructor

Course()

{

courseName = "";

students = new Student [10];

numberOfStudents = 0;

}

// Parameter constructor

Course(String cN)

{

courseName = cN;

students = new Student [10];

numberOfStudents = 0;

}

public String getCourseName() // accessor for courseName

{

return courseName;

}

public void addStudent(Student [] student)

{

if(numberOfStudents == student.length)

{

Student newstudents [] = new Student(students.length * 2);

for(int i = 0; i < students.length; ++i)

{

newstudents[i] = students[i];

}

students = newstudents;

}

students[numberOfStudents++] = student;

}

public void addStudent (String studentName)

{

addStudent(new \Student(studentName));

}

public Student[] getStudents()

{

return students;

}

public int getNumberOfStudents()

{

return numberOfStudents;

}

public String toString()

{

String ret = "Course: " + courseName + "; Number Of Students: " + numberOfStudents + " ";

ret += "The students in the class are: ";

for(int i = 0; i < numberOfStudents; ++i) {

ret += students[i].toString() + " ";

}

return ret;

}

}

---------------------------------------

import java.util.*;

public class TestCourse {

public static void main(String[] args)

{

Scanner kb = new Scanner(System.in);

System.out.println("Enter the course name");

String name = kb.nextLine();

\Course course = new \Course(name);

while (true)

{

System.out.println("Enter the name of a student or Q to quit");

name = kb.nextLine();

if(name.equalsIgnoreCase("Q"))

{

break;

}

course.addStudent(name);

}

System.out.println(course);

}

}

--------------------------------------

As an output, I tried to type in

CPS2231 -04

And then Mary Smith

Suddenly I got an error.

How do I fix it?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions