Question

In: Computer Science

IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...

IN JAVA ECLIPSE PLEASE

The xxx_Student class

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

• 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

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

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

Write the 3 getters

+getCourseName() : String

+getStudents() : Student []

+getNumberOfStudents() : int

Write the 2 setters

public void addStudent (Student student)

public void addStudent (String studentName)

Write toString

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 \n so they print on

separate lines

Write a class xxx_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

Enhancement 1 (5 extra points)

Change the Course class so that a Course can have an unlimited number of students.

Start out with an Array of 10 Student objects

Once the Array is full,

Create a new Array of Student of twice the size as the original

Copy the elements from the full array to the new array

Re-assign the references

Solutions

Expert Solution

code

Student.java student class


public class Student
{
   private String Name;//instance varibale Name that holds
   private int id;//instace variable id that store the id of the studnt
   static int idStart=100;//static variable whosw scope is limeted to this class
  
//perameterize cunstructor that will assing name to Name and idStart value to id
   public Student(String name)
   {
       Name=name;
       id=idStart;
       idStart++;//it will increament by 1 when every time object of this class is created
   }
   public String getName()//accessor method that will return the name of the student
   {
       return Name;
   }
   public int getId()//accessor method that will return the id of the student
   {
       return id;
   }

@Override
//override the toString method that will return formatted string
public String toString() {
return "Student: " + Name + " Id: " + id ;
}
  
  
}


Course.java course class


public class Course
{
private String courseName;//instance varibale named coueseName that is hold the name of the course
private Student []students;//declare the array of student objects
private int numberOfStudents;//and instance variable numberOfStudents that will hold the current number of studenr in the array

//perameterize constructor that recived the parameter type string
public Course(String courseName) {
this.courseName = courseName;//assing the name of the course to the instance variable courseNmae
this.students=new Student[10];//initialize the students array with size 10
numberOfStudents=0;//and assign numberOfStudents to 0
  
}

public String getCourseName() {//accessor method that will return the course name of the Course
return courseName;
}

public Student[] getStudents() {//accessor method that will return the array of the student object name of the Course
return students;
}

public int getNumberOfStudents() {//accessor method that will return the current number of student in the array
return numberOfStudents;
}
public void addStudent (Student student)//add Student method has perameter type student
{
if(numberOfStudents==students.length)//fisrt check if there is space in array means the number student is same as size of the student
{
increaseArraySize();//if so then it will call the increaseArraySize;
}
students[numberOfStudents]=student;//add the student object to students array at numberOfStudents position in the array
numberOfStudents++;//increamt student count by 1
}
public void addStudent (String studentName)//its another method that will recive the string parameter that is tha name of the student
{
if(numberOfStudents==students.length)//fisrt check if there is space in array means the number student is same as size of the student
{
increaseArraySize();//if so then it will call the increaseArraySize;
}
//it will create the new student object and add to the array
students[numberOfStudents]=new Student(studentName);
numberOfStudents++;//increamt student count by 1
}

@Override
  
//this is override method that will return the formatted string that contains the name of the couese and all the
//student that enrolled in that perticular course
public String toString()
{
String str="";//declare an empty string
  
str+="Course: "+getCourseName()+" Number of studenrs :"+numberOfStudents;
str+="\nThe students in the class are:";
for(int i=0;i<numberOfStudents;i++)
{
str+="\n"+students[i].toString();
}
return str;
}
  
//private method increaseArraySize that will return nothing
private void increaseArraySize() {
Student[] newArray = new Student[students.length * 2];//it will create an array of type of student with doubled the size of the students array
//copy all the student object to the new array one by one
for(int i = 0; i < students.length; i++)
{
newArray[i] = students[i];
}
//assing new array to students instance variable
students=newArray;
}
  

  
}
TestCourse.java with main method


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// this is test class with main method
public class TestCourse {

public static void main(String[] args) throws IOException
{
//crate the object of the BufferedReader for user input from the console
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String courseName;
//ask from the user to enter the name of the course
System.out.println("Enter the course name");
courseName=reader.readLine();//read the name with reader object and assing to courseName varible
Course crcs=new Course(courseName);//creates the object of the course Class with perameter courseName
String sName;
while(true)//infinite while loop untill user will enter q as student name
{
//promot user to enter the name of the student
System.out.println("Enter the name of a student or Q to quit:");
//read then name and assign to variable sName
sName=reader.readLine();
//check if sName is q or Q
if(sName.equalsIgnoreCase("q"))
break;//then it will break the loop
crcs.addStudent(sName);//other wise call addStudent method of couese class with string perameter
}
System.out.println(crcs);//this will print the course object crcs .. means it will call toString method of course class
}
  
}


output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

A Java question. You are given a Student class. A Student has a name and an...
A Java question. You are given a Student class. A Student has a name and an ArrayList of grades (Doubles) as instance variables. Write a class named Classroom which manages Student objects. You will provide the following: 1. public Classroom() a no-argument constructor. 2. public void add(Student s) adds the student to this Classroom (to an ArrayList 3. public String hasAverageGreaterThan(double target) gets the name of the first student in the Classroom who has an average greater than the target...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP a. Create a class named Student that has fields...
PLEASE EXPLAIN ANSWER. USING JAVA via jGRASP a. Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method...
This is Java class working on the eclipse. I include some of the codes just so...
This is Java class working on the eclipse. I include some of the codes just so you know what needed. create and work with interfaces you'll create the DepartmentConstants interface presented. In addition, you'll implement an interface named Displayable that's similar to the Printable interface Create the interfaces 1- Import the project named ch12-ex1_DisplayableTest and review the codes package murach.test; public interface Displayable {     String getDisplayText(); } 2 . Note that this code includes an interface named Displayable that...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a...
Using Java preferably with Eclipse Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple The class Apple DOES NOT HAVE a main method Some of the attributes of Apple are Type: A string that describes the apple.  It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith Weight: A decimal value representing...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
use eclipse give me java codes Task III: Write a classCarInsurancePolicy The CarInsurancePolicy class will...
use eclipse give me java codes Task III: Write a class CarInsurancePolicy The CarInsurancePolicy class will describe an insurance policy for a car. 1. The data members should include all the data members of an Insurance Policy, but also the driver’s license number of the customer, whether or not the driver is considered a “good” driver (as defined by state law) , and the car being insured (this should be a reference to a Car object -- write a separate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT