Question

You need to write a class called SingleItemBox (in a file called SingleItemBox.java that you create)....


You need to write a class called SingleItemBox (in a file called SingleItemBox.java that you create). This class has a constructor that takes a single item (of any type) and puts it in the box. You also need a method called getItem() which provides the item back to the user but does not remove it from the box (this is an “accessor”, if you remember, sometimes called a “getter”).

public class BoxUsageDemo {
   /**
   * This is a main method with demo code.
   * @param args command line args (not used)
   */
   public static void main(String[] args) {
       //demo putting an apple in a box
       class Apple { }
      
       //make an apple
       Apple a1 = new Apple();
      
       //put the apple in a box
       SingleItemBox<Apple> appleBox = new SingleItemBox<>(a1);
      
       //check that the apple was put in the box
       if(appleBox.getItem().equals(a1)) {
           System.out.println("yay 1");
       }
      
      
      
       //demo putting a banana in a box
       class Banana { }
      
       //make a banana
       Banana b1 = new Banana();
      
       //put the banana in a box
       SingleItemBox<Banana> bananaBox = new SingleItemBox<>(b1);
      
       //check that the banana was put in the box
       if(bananaBox.getItem().equals(b1)) {
           System.out.println("yay 2");
       }
      
      
      
       //demo putting a banana in a box
       class Cat { }
      
       //make a banana
       Cat c1 = new Cat();
      
       //put the banana in a box
       SingleItemBox<Cat> catPlayBox = new SingleItemBox<>(c1);
      
       //check that the banana was put in the box
       if(catPlayBox.getItem().equals(c1)) {
           System.out.println("yay 3");
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN JAVA:

SingleItemBox.java

//generic class to accept and store an object of any class

class SingleItemBox<T>

{

//instance variable

private T obj;

//parametrized constuctor

public SingleItemBox(T obj)

{

this.obj=obj;

}

//getter method

public T getItem()

{

return obj;

}

}

BoxUsageDemo.java

class Main

{

public static void main(String[] args) {

//demo putting an apple in a box

class Apple { }

//make an apple

Apple a1 = new Apple();

//put the apple in a box

SingleItemBox<Apple> appleBox = new SingleItemBox<>(a1);

//check that the apple was put in the box

if(appleBox.getItem().equals(a1)) {

System.out.println("yay 1");

}

//demo putting a banana in a box

class Banana { }

//make a banana

Banana b1 = new Banana();

//put the banana in a box

SingleItemBox<Banana> bananaBox = new SingleItemBox<>(b1);

//check that the banana was put in the box

if(bananaBox.getItem().equals(b1)) {

System.out.println("yay 2");

}

//demo putting a banana in a box

class Cat { }

//make a banana

Cat c1 = new Cat();

//put the banana in a box

SingleItemBox<Cat> catPlayBox = new SingleItemBox<>(c1);

//check that the banana was put in the box

if(catPlayBox.getItem().equals(c1)) {

System.out.println("yay 3");

}

}

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
You need to write a class called SingleItemBox (in a file called SingleItemBox.java that you create)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() {...

    Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() { System.out.println("A new animal has been created!"); } public void sleep() { System.out.println("An animal sleeps..."); } public void eat() { System.out.println("An animal eats..."); } } public class Cat extends Animal { public Cat() { super(); System.out.println("A new cat has been created!"); } public void sleep() { System.out.println("A cat sleeps..."); } public void purr() { System.out.println("A cat purrs..."); } public void eat() { System.out.println("An cat eats...");...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • please who can help with this question in Java 1.  Write a class called Pair that...

    please who can help with this question in Java 1.  Write a class called Pair that stores a pair of numbers in private fields. Include a two-arg constructor, mutators and accessors. Override the equals method so that a Pair (1,2) would be considered equal to Pair (2,1). Write an appropriate toString method and make the toString method non-overrideable. 2.  Write an interface called Comparable that includes a method isGreater, isLesser, and isSame. These methods should each take a Pair object...

  • Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called...

    Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....

  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this...

    Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this class in future exercises to fully build out a Tic Tac Toe game! The TicTacToe class should have a 2D array as an instance variable and a constructor that initializes the 2D array with the "-" value. Add a getter method that returns the private 2D instance variable. public class TicTacToeTester { //You don't need to alter any of the code in this class!...

  • Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will...

    Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will need to name . class file A4MA5331550 also when done put both files in a zipped folder named A4MA5331550 and email the folder to [email protected] here is the program: import java.util.Scanner; /** * 09/17/2017 * Dakota Mammedaty * MA5331550 * Bubble sorted */ public class MA5331550 { public static void main(String[] args) throws IOException { Sort st =new Sort(); st.getData(); System.out.println("=================Sorting Algorithms================="); System.out.println("1. Bubble...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT