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

Hi I really neeeeeeeeeeeeeeeed helllllllllp on this program that uses the bag to

ID: 3668047 • Letter: H

Question

Hi I really neeeeeeeeeeeeeeeed helllllllllp on this program that uses the bag to add and remove a student from the course and it should have the student name and id and course name here is the description of the program and the projects assorted with the program I also in this as a netbeans project asaap

Lab 1 – Bag Client

A school offers a number of courses which students can enroll in. Additionally, students may take multiple courses.

For example, Tom, Dick and Harry are students. Tom and Dick enroll in History101, Dick and Harry are enrolled in Programming101. Thus, the school’s Registrar has two courses to manage.

One way of tackling this problem is using the BagADT. The Registrar maintains a ‘bag’ of courses and each course maintains a ‘bag’ of students.

Our Registrar needs a program to perform the following:

1.      Create a pool of course offerings.

2.      Add students to specific course.

3.      Drop students from a specific course.

4.      Prepare a report of course offerings.

5.      Prepare a report of student enrollment (by course).

Use the standard problem solving methodology that has been described. Examine the description and determine which classes would be most appropriate. As a start, a prototype Netbeans project has been provided that also contains a fully working implementation of the BagADT as a ‘library’. As you examine the starter main class, take note of the import statement.

This class already creates a pool of course offerings and also serves as a test program – without modification. Examine the code. It can provide insight as to handle the other facets of this lab exercise.

The immediate demands are minimal, therefore, you need not design your classes to contain all the data that a typical system would require. For example, a Course class would only need to identify the course name and section; a Student class need only be identified by name and ID. NOTE: as there may be similar names, a student is uniquely identified by ID.

Start with the Student class. A constructor header is provided, but you need to provide definitions of instance variables and any other methods required to support the program. For example, atoString method. Because you may be comparing one student to another, the equals method need to be implemented.

The Course class is somewhat more complex. Multiple students may be enrolled in the same course. In support of Step 2, it contains a list of all enrolled students. It must also be able to remove a student from that course (Step 3). Additionally, it must be able to return a string of all enrolled students in that course (in support of Step 5). If necessary, import the Bag library.

When tested, your output should appear as follows:

Course List

History101 – Section 1

History101 – Section 2

Math101– Section 1

Programming101– Section 1

Course Enrollment

History101 – Section 1

Tom (123)

Dick (234)

History101 – Section 2

Harry (345)

Alice (456)

Ted (567)

Alice (678)

Math101– Section 1

Tom (123)

Programming101– Section 1

Alice (678)

Course Enrollment

History101 – Section 1

Tom (123)

Dick (234)

History101 – Section 2

Harry (345)

Ted (567)

Alice (678)

Math101– Section 1

Tom (123)

Programming101– Section 1

Alice (678)

A prototype Netbeans project has been provided for your convenience. It contains the three classes already discussed and a jar file containing the Java .class files for the Bag and BagADT classes.

import DSLib.*;

/**
* Lab1 - Bag Client for School Registrar
*
* @author gcohen
*/
public class Lab1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create a bag of Courses
BagADT<Course> courses = new Bag<>();

// Create some courses and add to the bag
Course history101_1 = new Course("History101", 1);
Course history101_2 = new Course("History101", 2);
Course math101_1 = new Course("Math101", 1);
Course programming101_1 = new Course("Programming101", 1);

// Enroll some students
history101_1.enroll(new Student("Tom", 123));
history101_1.enroll(new Student("Dick", 234));
history101_2.enroll(new Student("Harry", 345));
Student alice456 = new Student("Alice", 456); // Soon to be withdrawn
history101_2.enroll(alice456);
history101_2.enroll(new Student("Ted", 567));
history101_2.enroll(new Student("Alice", 678));
math101_1.enroll(new Student("Tom", 123));
programming101_1.enroll(new Student("Alice", 678));

// Create a list of courses
courses.add(history101_1);
courses.add(history101_2);
courses.add(math101_1);
courses.add(programming101_1);

// Print list of courses
System.out.println("Course List");
for (Course course : courses) {
System.out.println(course);
}

// Print list of courses + enrolled students
System.out.println(" Course Enrollment");
for (Course course : courses) {
System.out.println(course); // Name+course section
System.out.println(course.enrollmentList());
}

// Drop a student and reprint enrollment
history101_2.withdraw(alice456);

System.out.println(" Course Enrollment");
for (Course course : courses) {
System.out.println(course); // Name+course section
System.out.println(course.enrollmentList());
}
}

}

/**
*
* @author YOUR NAME(S)
*/
public class Course implements CourseADT {

// TODO: add appropriate instance variables and complete constructor(s).
// Complete each method and add other methods as necessary

Course(String courseName, int section) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
  
@Override
public boolean enroll(Student student) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean withdraw(Student student) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public String enrollmentList() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
  
public String toString() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

/**
*
* @author YOUR NAME(S)
*/
public class Student {

Student(String studentName, int id) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
  
@Override
public String toString() {
throw new UnsupportedOperationException("Not supported yet.");
}
  
// Need to compare this student to another
public boolean equals(Student aStudent) {
throw new UnsupportedOperationException("Not supported yet.");
}
  
}

/**
* Methods for this course
* @author YOUR NAME
*/
public interface CourseADT {
/**
* Enroll a student in this course
* @param student
* @return true if successful
*/
boolean enroll(Student student);
  
/**
* Withdraw the specified student form this course.
* @param student
* @return true if successful
*/
boolean withdraw(Student student);
  
/**
* Return a list of courses plus the students enrolled in each.
* @return
*/
String enrollmentList();
}

package DSLib;

import java.util.ArrayList;
import java.util.Iterator;

public class Bag<T extends Object> implements BagADT<T> {

private ArrayList<T> bag;

public Bag() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: invokespecial java/lang/Object."<init>":()V
* 4: aload_0
* 5: new java/util/ArrayList
* 8: dup
* 9: invokespecial java/util/ArrayList."<init>":()V
* 12: putfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 15: return
* */
// </editor-fold>
}

public int size() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: invokevirtual java/util/ArrayList.size:()I
* 7: ireturn
* */
// </editor-fold>
}

public boolean isFull() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: iconst_0
* 1: ireturn
* */
// </editor-fold>
}

public boolean isEmpty() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: invokevirtual java/util/ArrayList.isEmpty:()Z
* 7: ireturn
* */
// </editor-fold>
}

public boolean add(T newEntry) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: aload_1
* 5: invokevirtual java/util/ArrayList.add:(Ljava/lang/Object;)Z
* 8: ireturn
* */
// </editor-fold>
}

public T remove() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: invokevirtual DSLib/Bag.removeRandom:()Ljava/lang/Object;
* 4: areturn
* */
// </editor-fold>
}

public boolean remove(T anEntry) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: aload_1
* 5: invokevirtual java/util/ArrayList.remove:(Ljava/lang/Object;)Z
* 8: ireturn
* */
// </editor-fold>
}

public T removeRandom() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: invokevirtual java/util/ArrayList.size:()I
* 7: istore_1
* 8: iload_1
* 9: ifne 14
* 12: aconst_null
* 13: areturn
* 14: invokestatic java/lang/Math.random:()D
* 17: iload_1
* 18: i2d
* 19: dmul
* 20: d2i
* 21: istore_2
* 22: aload_0
* 23: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 26: iload_2
* 27: invokevirtual java/util/ArrayList.remove:(I)Ljava/lang/Object;
* 30: areturn
* */
// </editor-fold>
}

public void clear() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: new java/util/ArrayList
* 4: dup
* 5: invokespecial java/util/ArrayList."<init>":()V
* 8: putfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 11: return
* */
// </editor-fold>
}

public int getFrequencyOf(T anEntry) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: iconst_0
* 1: istore_2
* 2: aload_0
* 3: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 6: invokevirtual java/util/ArrayList.iterator:()Ljava/util/Iterator;
* 9: astore_3
* 10: aload_3
* 11: invokeinterface java/util/Iterator.hasNext:()Z
* 16: ifeq 42
* 19: aload_3
* 20: invokeinterface java/util/Iterator.next:()Ljava/lang/Object;
* 25: astore 4
* 27: aload_1
* 28: aload 4
* 30: invokevirtual java/lang/Object.equals:(Ljava/lang/Object;)Z
* 33: ifeq 39
* 36: iinc 2, 1
* 39: goto 10
* 42: iload_2
* 43: ireturn
* */
// </editor-fold>
}

public boolean contains(T anEntry) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: invokevirtual java/util/ArrayList.iterator:()Ljava/util/Iterator;
* 7: astore_2
* 8: aload_2
* 9: invokeinterface java/util/Iterator.hasNext:()Z
* 14: ifeq 37
* 17: aload_2
* 18: invokeinterface java/util/Iterator.next:()Ljava/lang/Object;
* 23: astore_3
* 24: aload_1
* 25: aload_3
* 26: invokevirtual java/lang/Object.equals:(Ljava/lang/Object;)Z
* 29: ifeq 34
* 32: iconst_1
* 33: ireturn
* 34: goto 8
* 37: iconst_0
* 38: ireturn
* */
// </editor-fold>
}

public T[] toArray() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: invokevirtual java/util/ArrayList.toArray:()[Ljava/lang/Object;
* 7: astore_1
* 8: aload_1
* 9: checkcast "[Ljava/lang/Object;"
* 12: areturn
* */
// </editor-fold>
}

public boolean equals(BagADT bag) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_1
* 1: aload_1
* 2: invokeinterface DSLib/BagADT.equals:(LDSLib/BagADT;)Z
* 7: ireturn
* */
// </editor-fold>
}

public Iterator iterator() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: getfield DSLib/Bag.bag:Ljava/util/ArrayList;
* 4: invokevirtual java/util/ArrayList.iterator:()Ljava/util/Iterator;
* 7: areturn
* */
// </editor-fold>
}
}

package DSLib;

import java.util.Iterator;

public interface BagADT<T extends Object> extends Iterable<T> {

public int size();

public boolean isFull();

public boolean isEmpty();

public boolean add(T t);

public T remove();

public boolean remove(T t);

public T removeRandom();

public void clear();

public int getFrequencyOf(T t);

public boolean contains(T t);

public T[] toArray();

public boolean equals(BagADT bagadt);

public Iterator iterator();

public String toString();
}

Explanation / Answer

/*
* ArrayBag.java
*
* A modified version of the ArrayBag class with these additional methods:
* increaseCapacity(), grabItemsFrom(), numItemsContainsFrom(), and
* removeDuplicates(). Also contains additional code in main() to test
* the new methods.
*/

import java.util.*;

/**
* An implementation of a Bag ADT using an array.
*/
public class ArrayBag implements Bag {
/**
* The array used to store the items in the bag.
*/
private Object[] items;
  
/**
* The number of items in the bag.
*/
private int numItems;
  
public static final int DEFAULT_MAX_SIZE = 50;


// **** ADDITIONAL METHODS ADDED IN SECTION:

/**
* increaseCapacity()
*
* Increases the capacity (ie., the number of items the Bag
* can possibly hold) of the Bag it's called on, without
* losing any items already in the Bag.
*/
public void increaseCapacity ( int amount ) {

// Don't allow this method to be used to DECREASE the
// capacity of the Bag:
  
if ( amount < 0 ) {
throw new IllegalArgumentException("amount must be >= 0");
}

// Create a new items array able to hold "amount" more items
// than the current items array:

Object[] newItems = new Object[items.length + amount];
  
// Copy the first numItems things from the current items
// array into the new array:

System.arraycopy(items, 0, newItems, 0, numItems);
  
// Make our "items" instance variable point at the new array
// (the old array will get garbage-collected, since there are
// no longer any references to it):

items = newItems;
}

/**
* grabItemsFrom()
*
* Attempts to "grab" numItemsToGrab random items from the other
* Bag and add them to this Bag. If there isn't enough room to
* add that many items to this Bag, NO items are added and the
* method returns false.
*/
public boolean grabItemsFrom ( Bag other, int numItemsToGrab ) {
  
// If there isn't enough space in this Bag to hold numItemsToGrab
// additional items or other is null, return false:

if ( items.length - numItems < numItemsToGrab || other == null ) {
return false;
}

// Otherwise, "grab" numItemsToGrab random items from other,
// and add them to this Bag:

for ( int i = 0; i < numItemsToGrab; i++ ) {
add(other.grab());
}

// Return true to indicate that we did successfully grab
// and add the items:

return true;
}

/**
* numItemsContainsFrom()
*
* Returns the number of items in other that this Bag contains.
*/
public int numItemsContainsFrom ( Bag other ) {
  
// If other is null we won't be able to call any methods on it,
// so we throw an exception if it is:

if ( other == null ) {
throw new IllegalArgumentException("other can't be null!");
}

// Get an array containing the items from other:

Object[] otherItems = other.toArray();
int count = 0;

// For each Object from other, we check whether this Bag
// contains it. If it does, we increase our counter by one:

for ( Object o : otherItems ) {
if ( contains(o) ) {
count++;
}
}
  
// We return the count of the number of items from other
// that were contained in this Bag:

return count;
}

/**
* removeDuplicates()
*
* Returns a NEW Bag containing all of the items from this Bag,
* but with duplicates removed (ie., each item will appear only
* once in the Bag that's returned).
*/
public Bag removeDuplicates() {
  
// Create a new Bag able to hold at least numItems things --
// numItems is the maximum number of items we could possibly
// put into the new Bag:

Bag noDupes = new ArrayBag(numItems);

// For each of the numItems things in this Bag, we add the
// current item to the noDupes Bag, but only if noDupes
// doesn't already contain it.
//
// NOTICE how we're careful to only examine the first numItems
// positions in the items array -- the remaining positions are
// unused and contain the value null, so we don't want to examine
// them:

for ( int i = 0; i < numItems; i++ ) {
if ( ! noDupes.contains(items[i]) ) {
noDupes.add(items[i]);
}
}
  
// Return the new Bag we created:

return noDupes;
}

/**
* Default, no-arg constructor - creates a new, empty ArrayBag with
* the default maximum size.
*/
public ArrayBag() {
items = new Object[DEFAULT_MAX_SIZE];
numItems = 0;
}
  
/**
* A constructor that creates a new, empty ArrayBag with the specified
* maximum size.
*/
public ArrayBag(int maxSize) {
if (maxSize <= 0)
throw new IllegalArgumentException("maxSize must be > 0");
items = new Object[maxSize];
numItems = 0;
}
  
/**
* add - adds the specified item to the Bag. Returns true on
* success and false if there is no more room in the Bag.
*/
public boolean add(Object item) {
if (item == null)
throw new IllegalArgumentException("item must be non-null");
if (numItems == items.length)
return false; // no more room!
else {
items[numItems] = item;
numItems++;
return true;
}
}
  
/**
* remove - removes one occurrence of the specified item (if any)
* from the Bag. Returns true on success and false if the
* specified item (i.e., an object equal to item) is not in the Bag.
*/
public boolean remove(Object item) {
for (int i = 0; i < numItems; i++) {
if (items[i] != null && items[i].equals(item)) {
// Shift the remaining items left by one.
System.arraycopy(items, i+1, items, i, numItems-i-1);
items[numItems-1] = null;
  
numItems--;
return true;
}
}
  
return false; // item not found
}
  
/**
* contains - returns true if the specified item is in the Bag, and
* false otherwise.
*/
public boolean contains(Object item) {
for (int i = 0; i < numItems; i++) {
if (items[i] != null && items[i].equals(item))
return true;
}
  
return false;
}
  
/**
* containsAll - does this ArrayBag contain all of the items in
* otherBag? Returns false if otherBag is null or empty.
*/
public boolean containsAll(Bag otherBag) {
if (otherBag == null || otherBag.numItems() == 0)
return false;
  
Object[] otherItems = otherBag.toArray();
for (int i = 0; i < otherItems.length; i++) {
if (!contains(otherItems[i]))
return false;
}
  
return true;
}
  
/**
* numItems - returns the number of items in the Bag.
*/
public int numItems() {
return numItems;
}
  
/**
* grab - returns a reference to a randomly chosen in the Bag.
*/
public Object grab() {
if (numItems == 0)
throw new NoSuchElementException("the bag is empty");
int whichOne = (int)(Math.random() * numItems);
return items[whichOne];
}
  
/**
* toArray - return an array containing the current contents of the bag
*/
public Object[] toArray() {
Object[] copy = new Object[numItems];
System.arraycopy(items, 0, copy, 0, numItems);
return copy;
}
  
/**
* toString - converts this ArrayBag into a readable String object.
* Overrides the Object version of this method.
*/
public String toString() {
String str = "{";
  
for (int i = 0; i < numItems; i++)
str = str + " " + items[i];
str = str + " }";
  
return str;
}
  
/* Test the ArrayBag implementation. */
public static void main(String[] args) {
// Create a Scanner object for user input.
Scanner in = new Scanner(System.in);
  
// Create an ArrayBag named bag1.
System.out.print("Size of bag 1: ");
int size = in.nextInt();
Bag bag1 = new ArrayBag(size);
in.nextLine(); // consume the rest of the line
  
// Read in strings, add them to bag1, and print out bag1.
String itemStr;
for (int i = 0; i < size; i++) {
System.out.print("item " + i + ": ");
itemStr = in.nextLine();
bag1.add(itemStr);
}
System.out.println("bag 1 = " + bag1);
System.out.println();
  
// Select a random item and print it.
Object item = bag1.grab();
System.out.println("grabbed " + item);
System.out.println();
  
// Iterate over the objects in bag1, printing them one per
// line.
Object[] items = bag1.toArray();
for (int i = 0; i < items.length; i++)
System.out.println(items[i]);
System.out.println();
  
// Get an item to remove from bag1, remove it, and reprint the bag.
System.out.print("item to remove: ");
itemStr = in.nextLine();
if (bag1.contains(itemStr))
bag1.remove(itemStr);
System.out.println("bag 1 = " + bag1);
System.out.println();

// TEST CODE FOR THE ADDITIONAL METHODS WE WROTE:

Bag bag2 = new ArrayBag(15);
bag2.increaseCapacity(5);
bag2.grabItemsFrom(bag1, 10);
System.out.println("bag2 contains " + bag2.numItemsContainsFrom(bag1)
+ "/" + bag1.numItems() + " items from bag1");
System.out.println("bag2:");
System.out.println(bag2);
System.out.println("bag2 without duplicates:");
System.out.println(bag2.removeDuplicates());
}
}

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