Question

In: Computer Science

Using Java, write code as follows: Create an interface called Items. It should define at least...

Using Java, write code as follows:

  1. Create an interface called Items. It should define at least the following methods:
    1. public void add( Object item ) - adds a single object to the collection.
    2. public Object get( int index ) - returns an item at a specific index.
    3. public int size() - returns the number of items stored in the collection.
    4. public void addAll( Object[] items ) - adds all of the elements in the array to the collection.
  2. Write a class called AbstractItems that implements the Items interface. It should provide concrete implementations of at least the following methods:
    1. public void addAll( Object[] items ) - This should simply loop and call the add method for each item in the array.
    2. public String toString() - returns a string that lists all of the items on a single line. For example, if the collection contained the strings “abc”,“123”, and “cat” the toString method should return something like “abc 123 cat”. This method should use a loop and call the get method.
  3. Create a class called ArrayOfItems that extends AbstractItems.
    1. Create a constructor that takes an integer parameter called size and use it to create an Object array of that size to hold your items.
    2. Use the array to implement the remaining abstract methods inherited from the Items class: add, size, and get. Note that size should return the number of items that have been added to the array, not the size of the array. In other words, if your initial array is large enough to hold 10 items, but only 2 items have been added, your size method should return 2, not 10. Once the array is full, the add method should ignore additional values.
  4. You should include a main method to test the addAll and toString methods as well as the add, get, and size methods.
  5. Create a class called Node. A Node has two instance variables.
    1. An Object named item.
    2. A reference to another Node called next.

Create the appropriate accessors and mutators for both variables.

  1. Create a class called ListOfItems that extends AbstractItems. This new class will us the Node created previously. The class will need three instance variables:
    1. A Node called head.
    2. A Node called tail.
    3. An int called size.

When a ListOfItems is created, create a new Node and set both the head and the tail to refer to it. The first time that the “add” method is called, add the Object to the head node and increment size from 0 to 1 Each time add is called after that, follow this algorithm:

  1. Create a new Node.
  2. Add the Object to the new Node. using the setItem method.
  3. Use the setNext mutator on the current tail to point to the new Node.
  4. Change the tail to point to the new Node.
  5. Increment size.

Also provide an implementation for the get method. This will require you to start at the head and use the getNext method to count the appropriate number of nodes in the list before returning the item at the specified index using its getItem method.

  1. Create a class called ItemsTest with two methods:
    1. public static void testItems(Items items) - this method should thoroughly test all methods of the Items interface (add, addAll, get, size, toString). The Items instance should be printed to standard output (using its toString method) each time the contents are changed.
  2. public static void main(String[] args) - Your main method should create at least one instance of each of the implementation classes and call the testItems method. You must show that your ArrayOfItems ignores values after the array is full.

Solutions

Expert Solution

import java.io.*;

import java.util;

public class void main {

interface Items
{
   public void method1();
}

int add()

{

public void add( Object item )  

public Object get( int index )

public int size(add)

public static void main(String[] args)

public void addAll( Object[] items ) // done for looping //

public String toString()

{

int arr[] = new int[] { "abc", 1, 2, 0 };

        Vector<Integer> v = new Vector();

        Hashtable<Integer, String> m = new Hashtable();

}

  

public class MyClass { int x; public MyClass()
{

x = 55;
}

public static void main(String[] args)

{

MyClass myObj = new MyClass(); //constructor //

System.out.println(myObj.x);

}

}

class node

{

object item =new item()

   class Node{

int data;   

Node next;    

sessionItem.setItem(keyname, value)

class Item LinkNode;

}

}

public static void testItems(Items)

{

public class Calculator {
   public static int add(int number8, int number9) {
      return number8 + number9;
   }

 
   public static int sub(int number1, int number2) {
      return number1 - number2;
        }

}

}


Related Solutions

JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface...
JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface below for details. EditableDigraph below: import java.util.NoSuchElementException; /** * Implements an editable graph with sparse vertex support. * * */ public interface EditableDiGraph {    /** * Adds an edge between two vertices, v and w. If vertices do not exist, * adds them first. * * @param v source vertex * @param w destination vertex */ void addEdge(int v, int w); /** *...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Using Java, define a train class and write code to demonstrate its functionality The train should...
Using Java, define a train class and write code to demonstrate its functionality The train should run between Boston and New York, stopping at different, different station along the way. Provide output to indicate the train’s current status.
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT