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

Java programming: 1. define another public method called howMany() which just re

ID: 3761306 • Letter: J

Question

Java programming:

1. define another public method called howMany() which just returns how many Courses have been created. Users will ask the Course class how many have been created (see the PolymorphismMinilabDriver), so make it static.

package polymorphismPart1;

//This class describes an academic course, which contains a name and a roster of Students

public class Course
{
   //data
   private String name;
   private int maxCount;
   private int count;
   private Student[] roster;
   private static int numInstances = 0;


   //default constructor - sets the maxCount to 30, initializes name/count, creates array,
   // updates numInstances.
   public Course()
   {
       this.name = "unknown";
       this.maxCount = 30;
       this.count = 0;
       this.roster = new Student[maxCount];
       this.numInstances++;
   }

   //************************************************
   //parameterized constructor - receives name, maxCount, initializes count, creates array,
   // updates numInstances.


   //toString() - returns its representation as a String
   public String toString()
   {
       String outString = "Roster of: " + name;
       for (int i=0; i<count; i++)
           outString += " " + (i+1) + ". " + roster[i];

       return outString;
   }

   //takeAttendance - goes through the roster, printing each Student's answer
   public void takeAttendance()
   {
       System.out.println("Taking attendance...");
       for (int i=0; i<count; i++)
       {
           Student respondant = roster[i];
           System.out.println(" " + respondant.getName() + " (" + respondant.getClass().getName() + ")" +
           " answers " + respondant.answer());
       }
   }

   //addStudent - puts a Student into the next available roster space, updates count
   public void addStudent(Student newStudent)
   {
       if (count >= roster.length)
           System.out.println(" " + name + " is full. Cannot add " + newStudent);
       else
           roster[count++] = newStudent; //note tricky use of ++ here...
   }
}

____________________________________________________________________________

2. add a new method called getID() to the Student class. It should just return the ID. Make sure the method is also visible to everyone.

package polymorphismPart1;

//This class describes a Student

public class Student
{
   //data
   private String name;
   private int ID;

   //default constructor
   public Student()
   {
       name = "unknown";
       ID = 0;
   }

   //parameterized constructor
   public Student(String name, int ID)
   {
       this.name = name;
       this.ID = ID;
   }

   //toString - returns String representation of the instance
   public String toString()
   {
       return name + ": " + ID;
   }

   //answer - returns how the Student instance answers during attendance
   public String answer()
   {
       return ""Here""; //note use of escape character here...
   }

   //getName - returns the name
   public String getName()
   {
       return name;
   }

}

_______________________________________________________________________
3. Copy your Part1 Course and Student class into the polymorphismPart2 package and include a parameterized Course constructor (one that can receive some arguments). This constructor should accept a String and an int and use them to set the name and maxCount. Be sure it does the other things that the default constructor does.

package polymorphismPart2;

//This is the driver class for the program Part2

public class PolymorphismMinilabDriver2
{
   public static void main(String[] args)
   {
       //create a Student
       Student firstStudent = new Student("Joe", 13);
       System.out.println("----- First Student is: ");
       System.out.println(firstStudent);

       //tell it to return its ID - print it.
       System.out.println(" ----- Ask the first Student for its ID: ");
       System.out.println(firstStudent.getID());

       //create a new Course
       Course algebra = new Course("Algebra 1-2", 8);

       //tell it to add some Students
       algebra.addStudent(firstStudent);
       algebra.addStudent(new Student());
       algebra.addStudent(new BrownNose("Peter", 21));
       algebra.addStudent(new Student("Janet", 43));
       algebra.addStudent(new BrownNose("Jim", 243));
       algebra.addStudent(new Student("Al", 3));
       algebra.addStudent(new BrownNose());
       algebra.addStudent(new BrownNose("Jenn", 23));
       algebra.addStudent(new Student("Jose", 43));
       algebra.addStudent(new BrownNose("Grant", 7));

       //print the string representation of the course to see what is there
       System.out.println(" ----- Creating a Course and telling it to add some Students. Printing it:");
       System.out.println(algebra);

       //tell the course to take attendance
       System.out.println(" ----- telling the course to take attendance");
       algebra.takeAttendance();

       //create two more new Courses
       Course history = new Course();
       Course spanish = new Course();

       //ask the Course class how many have been created (static)
       System.out.println(" ----- Asking the Course class how many courses have been created: ");
       System.out.println(Course.howMany());
   }
}

Explanation / Answer

package polymorphismPart2;

//This class describes an academic course, which contains a name and a roster of Students

public class Course

{

//data

private String name;

private int maxCount;

private int count;

private Student[] roster;

private static int numInstances = 0;

//default constructor - sets the maxCount to 30, initializes name/count, creates array,

// updates numInstances.

public Course()

{

this.name = "unknown";

this.maxCount = 30;

this.count = 0;

this.roster = new Student[maxCount];

this.numInstances++;

}

//************************************************

//parameterized constructor - receives name, maxCount, initializes count, creates array,

// updates numInstances.

public Course(String string, int i) {

       name = string;

       maxCount = i;

   }

  

public static int howMany() {

   return numInstances;

}

//toString() - returns its representation as a String

public String toString()

{

String outString = "Roster of: " + name;

for (int i=0; i<count; i++)

outString += " " + (i+1) + ". " + roster[i];

return outString;

}

//takeAttendance - goes through the roster, printing each Student's answer

public void takeAttendance()

{

System.out.println("Taking attendance...");

for (int i=0; i<count; i++)

{

Student respondant = roster[i];

System.out.println(" " + respondant.getName() + " (" + respondant.getClass().getName() + ")" +

   " answers " + respondant.answer());

}

}

//addStudent - puts a Student into the next available roster space, updates count

public void addStudent(Student newStudent)

{

if (count >= roster.length)

System.out.println(" " + name + " is full. Cannot add " + newStudent);

else

roster[count++] = newStudent; //note tricky use of ++ here...

}

}

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

package polymorphismPart2;

//This class describes a Student

public class Student

{

//data

private String name;

private int ID;

//default constructor

public Student()

{

name = "unknown";

ID = 0;

}

//parameterized constructor

public Student(String name, int ID)

{

this.name = name;

this.ID = ID;

}

//toString - returns String representation of the instance

public String toString()

{

return name + ": " + ID;

}

//answer - returns how the Student instance answers during attendance

public String answer()

{

return ""Here""; //note use of escape character here...

}

//getName - returns the name

public String getName()

{

return name;

}

  

public int getID() {

   return ID;

}

}

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

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