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

Source Code: Box.java public class Box<T extends Comparable<? super T>> implemen

ID: 3882800 • Letter: S

Question

Source Code:

Box.java

public class Box<T extends Comparable<? super T>> implements Comparable<Box<T>> {

  

   private T thing;

   private int thingCount;

  

   private T[] oldThings;

  

   public Box(T firstThing) {

       this.thing = firstThing;

       this.thingCount = 1;

      

       //oldThings = new T[5]; // not allowed

       oldThings = (T[]) (new Comparable[5]); // allowed

      

   }

  

   public T getContents() {

       return thing;

   }

   public int getCount() {

       return thingCount;

   }

  

   public void replaceContents(T newThing) {

       this.thing = newThing;

       thingCount++;

   }

  

   @Override

   public String toString() {

       return thing.toString() + " (item " + thingCount + ")";

   }

  

   @Override

   public boolean equals(Object other) {

       if(other instanceof Box<?>) {

           Box<?> otherBox = (Box<?>) other;

          

           boolean sameThing = this.thing.equals(otherBox.thing);

           boolean sameCount = this.thingCount==otherBox.thingCount;

          

           return sameThing && sameCount;

          

       } else {

           return false;

       }

   }

  

   @Override

   public int compareTo(Box<T> otherBox) {

       if(this.thing.compareTo(otherBox.thing)==0) {

           return Integer.compare(this.thingCount, otherBox.thingCount);

       } else {

           return this.thing.compareTo(otherBox.thing);  

       }

  

   }

}

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

BoxR.java

public class BoxR {

  

   private Object thing;

   private int thingCount;

  

   public BoxR(Object firstThing) {

       this.thing = firstThing;

       this.thingCount = 1;

   }

  

   public Object getContents() {

       return thing;

   }

   public int getCount() {

       return thingCount;

   }

  

   public void replaceContents(Object newThing) {

       this.thing = newThing;

       thingCount++;

   }

  

   @Override

   public String toString() {

       return thing.toString() + " (item " + thingCount + ")";

   }

  

   @Override

   public boolean equals(Object other) {

       if(other instanceof BoxR) {

           BoxR otherBoxR = (BoxR) other;

          

           boolean sameThing = this.thing.equals(otherBoxR.thing);

           boolean sameCount = this.thingCount==otherBoxR.thingCount;

          

           return sameThing && sameCount;

          

       } else {

           return false;

       }

   }

}

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

BoxTester.java

import java.util.*;

public class BoxTester {

   public static void main(String[] args) {

       BoxR numbersBoxNotReally = new BoxR(new Integer(4));

       numbersBoxNotReally.replaceContents(new Integer(5));

      

       //numbersBoxNotReally.replaceContents(new String("hello"));

      

       Integer currentContents = (Integer) numbersBoxNotReally.getContents();

      

       Box<Integer> numbersBox1 = new Box<Integer>(new Integer(4));

       numbersBox1.replaceContents(new Integer(5));

       Box<String> wordBox = new Box<String>("hello");

       wordBox.replaceContents("bye");

      

       Box<String> wordBox2 = new Box<String>("bye");

       wordBox2.replaceContents("bye");

      

       System.out.println("same boxes? " + wordBox.equals(wordBox2));

       System.out.println("same boxes? " + wordBox.equals(numbersBox1));

      

       Box<Integer> numbersBox2 = new Box<Integer>(new Integer(10));

       Box<Integer> numbersBox3 = new Box<Integer>(new Integer(2));

       numbersBox3.replaceContents(5);

       numbersBox3.replaceContents(2);

       Box<Integer> numbersBox4 = new Box<Integer>(new Integer(13));

       numbersBox4.replaceContents(12);

       numbersBox4.replaceContents(5);

       Box<Integer> numbersBox5 = new Box<Integer>(new Integer(7));

      

      

       ArrayList<Box<Integer>> listOfNumberBoxes = new ArrayList<Box<Integer>>();

       listOfNumberBoxes.add(numbersBox1);

       listOfNumberBoxes.add(numbersBox2);

       listOfNumberBoxes.add(numbersBox3);

       listOfNumberBoxes.add(numbersBox4);

       listOfNumberBoxes.add(numbersBox5);

      

       Collections.sort(listOfNumberBoxes);

      

       System.out.println(listOfNumberBoxes);

      

      

      

   }

}

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

NameInput.java

import java.util.*;

public class NameInput {

   public static void main(String[] args) {

      

       Scanner scan = new Scanner(System.in);

      

       ArrayList<String> nameList = new ArrayList<String>();

      

       System.out.println("Enter names or "quit" to quit:");

       String name = scan.nextLine();

      

       while(!name.equalsIgnoreCase("quit")) {

           nameList.add(name);

          

           System.out.println("Enter names or "quit" to quit:");

           name = scan.nextLine();

       }

      

       Collections.sort(nameList);

       String firstName = nameList.get(0);

      

       System.out.println(nameList);

   }

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

NumericInput.java

import java.util.*;

public class NumericInput {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

      

       ArrayList<Integer> numberList = new ArrayList<Integer>();

      

       System.out.println("How many numbers?");

       int numberNumbers = Integer.parseInt(scan.nextLine());

      

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

           int userNumber = Integer.parseInt(scan.nextLine());      

           numberList.add(userNumber);

       }

      

       int total = 0;

       for(Integer i : numberList) {

           total += i;

       }

       double average = (total * 1.0) / ( (double) numberList.size() );

      

       System.out.println("The average is " + average);

   }

}

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

Student.java

public class Student implements Comparable {

   private String firstName, lastName;

   private int studentID;

  

   public Student(String firstName, String lastName, int studentID) {

       this.firstName = firstName;

       this.lastName = lastName;

       this.studentID = studentID;

   }

  

   public String getFirstName() {

       return firstName;

   }

   public String getLastName() {

       return lastName;

   }

   public int getStudentID() {

       return studentID;

   }

   public void setFirstName(String firstName) {

       this.firstName = firstName;

   }

   public void setLastName(String lastName) {

       this.lastName = lastName;

   }

   public void setStudentID(int studentID) {

       if(studentID >=0 && studentID <= 9999) {

           this.studentID = studentID;

       }

   }

  

   @Override

   public String toString() {

       return lastName + ", " + firstName + " (" + studentID + ")";

   }

      

   @Override

   public boolean equals(Object other) {

       if(other instanceof Student) {

           Student otherStudent = (Student) other;

          

           return this.studentID == otherStudent.studentID;      

       } else {

           return false;

       }

      

   }

  

   @Override

   public int compareTo(Object other) {

       if(other instanceof Student) {

           Student otherStudent = (Student) other;

           if(this.studentID < otherStudent.studentID) {

               return -1;

           } else if(this.studentID == otherStudent.studentID){

               return 0;

           } else {

               assert this.studentID > otherStudent.studentID;

               return 1;

           }

       } else {

           return -99;

       }

   }

  

   /*

   @Override

   public int compareTo(Object other) {

       if(other instanceof Student) {

           Student otherStudent = (Student) other;

           if(this.lastName.equals(otherStudent.lastName)) {

               return this.firstName.compareTo(otherStudent.firstName);

           } else {

               return this.lastName.compareTo(otherStudent.lastName);

           }

       } else {

           return -99;

       }

   }

   */

  

  

  

  

}

Question 3: Box Modification Part A- Box with History Modify the Box class from the Source Code from the Week 02 Video. Add an ArrayList to keep track of the "box history"- all items that have been previously added to the box. Write the new instance data variable, modified constructor, and any new getters/setters. Writea modified replaceContents method. Post only your new, added code- do not post code that was not changed Write code to go in a driver program to demonstrate the addition of the history.

Explanation / Answer

Please Find the updated Code .

package com;

import java.util.ArrayList;

import java.util.List;

public class Box<T extends Comparable<? super T>> implements Comparable<Box<T>> {

  

private T thing;

private int thingCount;

  

private T[] oldThings;

  

private List list;

  

  

public List getList() {

return list;

}

public void setList(List list) {

this.list = list;

}

public Box(T firstThing) {

this.thing = firstThing;

this.thingCount = 1;

  

//oldThings = new T[5]; // not allowed

oldThings = (T[]) (new Comparable[5]); // allowed

  

}

  

public Box(T thing, int thingCount, T[] oldThings, List list) {

super();

this.thing = thing;

this.thingCount = thingCount;

this.oldThings = oldThings;

this.list = list;

}

public T getContents() {

return thing;

}

public int getCount() {

return thingCount;

}

  

public void replaceContents(T newThing,ArrayList list) {

this.thing = newThing;

this.list=list;

thingCount++;

}

  

@Override

public String toString() {

return thing.toString() + " (item " + thingCount + ")";

}

  

@Override

public boolean equals(Object other) {

if(other instanceof Box<?>) {

Box<?> otherBox = (Box<?>) other;

  

boolean sameThing = this.thing.equals(otherBox.thing);

boolean sameCount = this.thingCount==otherBox.thingCount;

  

return sameThing && sameCount;

  

} else {

return false;

}

}

  

@Override

public int compareTo(Box<T> otherBox) {

if(this.thing.compareTo(otherBox.thing)==0) {

return Integer.compare(this.thingCount, otherBox.thingCount);

} else {

return this.thing.compareTo(otherBox.thing);   

}

  

}

}

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