(Java) Use the interface \"ArrayBag\" implementation and do the following: 1) Ad
ID: 3795994 • Letter: #
Question
(Java) Use the interface "ArrayBag" implementation and do the following:
1) Add a new class that models a Thing.
2) Thing class should define at least the following:
a. Data members: alphanumeric id, int value, color as a string and material as a string.
b. Methods: default constructor, rand, print, toString and list. These methods are explained as follows:
- default constructor: sets all data members to values of your choice but need to be justified.
- rand: randomly generates an instance of Thing by setting all data members to random values. (more details/demo in class).
- print: displays all data members of a Thing object in one line
- toString: returns a string containing exactly the same info displayed by print().
- list: creates and returns a “ArrayList” of Thing objects.
3) Add a new method to the given ArrayBag “print” that prints all objects contained in the bag.
4) Add a Driver/Demo/Test class to test the methods of Thing and ArrayBag.
Note: There's no need for any explanation, just use //to show which method is which. Need this ASAP, thanks!
EDIT: Color and material can be anything, for example color can be blue and material can be leather. As for the ArrayBag interface definition the ArrayBag should have the following things: alphanumeric id, int value, color as a string and material as a string. Originally, color, material, alphanumeric id, and value should only be created, and then the constructor will give them set values of your choosing. (eg. id: Bag001. Color: Blue. Material: Leather. Value: $50). the method rand should just generate random values for each of the data members (color, material, value, etc.)
Explanation / Answer
PROGRAM CODE:
ArrayBag.java
package arrays;
public interface ArrayBag {
String COLOR[] = {"Blue", "Red", "Yellow", "Green", "White"};
String MATERIAL[] = {"Leather", "Wood", "Polyester", "Plastic", "Iron"};
String alphabetID[]= {"A", "B", "C", "D", "E"};
void print();
}
Thing.java
package arrays;
import java.util.ArrayList;
import java.util.Random;
public class Thing implements ArrayBag{
private int value;
private String id, color, material;
//constructor
public Thing( String id, int value, String color, String material) {
this.id = id;
this.value = value;
this.color = color;
this.material = material;
}
//default constructor
Thing()
{
value = -1;
id = "";
color = "";
material = "";
}
//rand() - generates random number and selects values from provided list in ArrayBag
public void rand()
{
Random rand = new Random();
this.id = alphabetID[rand.nextInt(5)] + (rand.nextInt(1000) + 1000);
this.value = rand.nextInt(1000);
this.color = COLOR[rand.nextInt(5)];
this.material = MATERIAL[rand.nextInt(5)];
}
@Override
public void print() {
System.out.println(id + " " + value + " " + color + " " + material);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return id + " " + value + " " + color + " " + material;
}
//created list and returns it
public ArrayList list()
{
ArrayList list = new ArrayList();
for(int i=0; i<5; i++)
{
Thing thing = new Thing();
thing.rand();
list.add(thing);
}
return list;
}
}
ArrayBagTester.java
package arrays;
public class ArrayBagTester {
public static void main(String[] args) {
Thing thing = new Thing();
thing.rand();
System.out.println(thing);
System.out.println(thing.list());
}
}
OUTPUT:
A1987 555 Red Iron
[B1847 696 Yellow Plastic, E1636 471 Green Polyester, C1374 734 Red Plastic, A1814 505 White Wood, B1830 27 White Leather]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.