Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Source Code: Box.java public class Box > implements Comparable > { private T thing; private int thingCount; private T[] oldThings; public Box(T firstThing) { this.thing

image text in transcribed

Source Code:

Box.java

public class Box> implements Comparable> {

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 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));

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

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

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

numbersBox1.replaceContents(new Integer(5));

Box wordBox = new Box("hello");

wordBox.replaceContents("bye");

Box wordBox2 = new Box("bye");

wordBox2.replaceContents("bye");

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

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

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

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

numbersBox3.replaceContents(5);

numbersBox3.replaceContents(2);

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

numbersBox4.replaceContents(12);

numbersBox4.replaceContents(5);

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

ArrayList> listOfNumberBoxes = new ArrayList>();

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 nameList = new ArrayList();

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 numberList = new ArrayList();

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

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

for(int i=0; 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

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions