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

If you were to get these questions on a test and could not use a computer to ans

ID: 3850250 • Letter: I

Question

If you were to get these questions on a test and could not use a computer to answer this question, only pen and paper.

How would you answer them? Please explain in detail and with step-by-step on how to approach questions like these :)

The class Course represents a university course class Course course number and name private in number private String name public Course (int number String name this number number this name name public String toString this number this name return equals returns true if th is equal to a given course and false otherwise public Course boolean equals Course course return this number course. number & & this name equals (Course .name) The class Courselist represents a list of courses: class CourseList private static final int CAPACITY 5 courses private Course courses; the number of courses private int courseCount; public Course List new Course ICAPACITY Course course count 0; add adds a course to the list public void add (Course course) courses courseCount] J Course course Count toString returns a string representation of the list code is missing here remove removes a given course from the list. If the course is not in the list the list is not updated. code is missing here An instance of class CourseList is created and used like this: Course List list add (new list.add (new list.add (new list.add (new list. add (new systen-SRyt Rript.ln list list new Course List Course 10 Algebra") Course 20 Algorithms Course (30 Electronics") Course (40 Physics") 50 Programming") Course list. remove new Course (40 Physics list. remove 30 Electronics")) new Course When this code fragment is executed, the following printout is generated:

Explanation / Answer

Explanation :

1. Created the Course class with required fields and define the constructor to initialize the fields and also provided the getter method to access the fields.

2. Created CourseList class with required fields and define the default constructor to initialize the array which contains the Course object.a

a. Remove method to remove the object from the array. It finds the index first and copy the elements of the old array to new array by removing the object from the old array.

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

package org.jay.chegg.july;

class Course{
  
   private int number;
   private String name;
   /**
   * @param number
   * @param name
   */
   public Course(int number, String name) {
       this.number = number;
       this.name = name;
   }
  
   public String toString(){
       return "<"+this.number+", "+this.name+">";
   }
  
   public boolean equals(Course course) {
       return this.number == course.number && this.name.equals(course.name);
   }

   /**
   * @return the number
   */
   public int getNumber() {
       return number;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }
  
  
  
  
}

class CourseList{
   private static final int CAPACITY = 5;
   //courses
   private Course[] courses;
   // number of courses
   private int courseCount;
  
   public CourseList() {
       courses = new Course[CAPACITY];
       courseCount = 0;
   }
  
   /**
   *
   * @param course
   */
   public void add(Course course){
       courses[courseCount] = course;
       courseCount++;
      
   }
  
   /**
   *
   * @param course
   * @return
   */
   public Course remove(Course course){
int index = 0;
       if(courseCount > 0){
           // finding the index of the object to remove in the array
           for (int i = 0;i<courses.length;i++) {
               if(courses[i].equals(course)){
                   index = i;
                   break;
               }
           }
           int removeIndex = courseCount - index - 1;
           if (removeIndex > 0){
               System.arraycopy(courses, index+1, courses, index,removeIndex); // copy the contents of array to another array
               courses[--courseCount] = null;
     
   } else {
   throw new ArrayIndexOutOfBoundsException();
   }
       }else {
throw new ArrayIndexOutOfBoundsException();
}
      
       return course;
}
  
   @Override
   public String toString() {
       String output = "";
       if(courseCount>0){
           for (Course course : courses) {
               if(course!=null){
               output += course.toString();
               }
           }
       }
      
       return output;
   }
  
  
}

public class CourseDemo {

   public static void main(String[] args) {
CourseList list = new CourseList();
       //adding course to the list
       list.add(new Course(10, "Algebra"));
       list.add(new Course(20, "Algorithms"));
       list.add(new Course(30, "Electronics"));
       list.add(new Course(40, "Physics"));
       list.add(new Course(50, "Programming"));
       System.out.println("Before removing");
       System.out.println(list);
       //removing course from a list
       list.remove(new Course(40, "Physics"));
       list.remove(new Course(30, "Electronics"));
       //printing the list
       System.out.println("After removing");
       System.out.println(list);
   }

}

------------------------------------------------------output-----------------------------------------------------------------

Before removing
<10, Algebra><20, Algorithms><30, Electronics><40, Physics><50, Programming>
After removing
<10, Algebra><20, Algorithms><50, Programming>