0

this is a homework assignmnet but i have tried really hard

a default constructor to create a Vector,

an addBox method that adds a Box object to the Vector,

a printVector method to print the dimensions of each Box object in the Vector.
public class BoxVector {

//Part A: declare a Vector as a private data field
//Part B: Write a constructor to create a Vector
//Part C: Write a addBox method to add a Box object to the Vector
//Part D: Write a method to print the dimensions of each Box object in the Vector
public static void main(String argv[]){
BoxVector v;
v = new BoxVector();
Box b1 = new Box (1,2,3);
Box b2 = new Box (4,5,6);
v.addBox(b1);
v.addBox(b2);
v.printVector();
}

}


The output of the program should be as follows (assuming you have added the required methods in correctly):

Box 0 dimension:
width = 1.0
height = 2.0
length = 3.0
Box 1 dimension:
width = 4.0
height = 5.0
length = 6.0

here is what i have I am so lost
import java.util.*;
class BoxVector{ public static void main (String[] args) {
Vector bv = new Vector(2); //create a vector with the capacity of 2
System.println("Capacity = “ + bv.capacity() + “ size = “ + bv.size());"
BoxVector bv;
bv = BoxVector();
Box b1 = new Box(1.0, 2.0, 3.0);
Box b2 = new Box();
Box b3 = new Box(0.5, 1.5, 2.5);
bv.add(b1); //add b1 to the end of vector
bv.add(b2); //add b2 to the end of vector
bv.add(b3); //add b3 to the end of vector
System.println ("Capacity = “ + bv.capacity() + “ size = “ + bv.size());"
System.println ("Width of the 1st element = “ + ((Box)bv.firstElement()).getWidth());"
System.println ("Width of the 2nd element = “ + ((Box)bv.elementAt(1)).getWidth());"
System.println ("Width of the last element = “ + ((Box)bv.lastElement()).getWidth());"
bv.remove(2); System.println("Capacity = “ + bv.capacity() + “ size = “ + bv.size());" }}

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by rickste_r
0

Ok we have three classes = Box, BoxVector and BoxTest.
The Box class simply creates a Box object:

//create a Box object, and have return methods to return values
class Box
{
  private double width;
  private double height;
  private double length;

  //constructor
  public Box(double widthIn, double heightIn,  double lengthIn)
  {
    width = widthIn;
    height = heightIn;
    length = lengthIn;
  }

  //returns box width
  public double getWidth()
  {
    return width;
  }

  //returns box height
  public double getHeight()
  {
    return height;
  }
  //returns box length
  public double getLength()
  {
    return length;
  }
}//end class

------------------------------------------------------------------------
The BoxVector class sets up a vector with addBox and PrintVector methods

import java.util.*; //for vector

class BoxVector
{
  private Vector boxes;

  //#a default constructor to create a Vector#
  public BoxVector ()
  {
    boxes = new Vector();
  }

  //#addBox method that adds a Box object to the Vector#
  public void addBox(Box b)
  {
    boxes.add(b);
  }

  //#printVector method to print the dimensions of each Box in the Vector#
  //returns the elements in the vector as a string

  public String printVector()
  {
    Enumeration en = boxes.elements();

    //create a stringbuffer to hold the elements
    StringBuffer currentBox = new StringBuffer("");

    //for the for loop to display the position of the element
    int i=0;

    while (en.hasMoreElements()) 
    {

      for (i = 0; i < boxes.size(); i++) 
      {

      //scan through the elements
      Box thisBox = (Box) en.nextElement();
      currentBox.append("\nBox " + (i+1) +
                        " , Width: " + thisBox.getHeight() +
                        " , Length: " + thisBox.getLength() +
                        " , Height: " + thisBox.getHeight());
      }
    }

    return currentBox.toString();
  }

}//end class

--------------------------------------------------------------------------
The final class BoxTest runs the show....

public class BoxTest
{
  public static void main(String[] args)
  {
    //create a Boxvector object to store varios boxes
    BoxVector holder = new BoxVector();

    //create and add boxes here
    Box box1 = new Box(1.0, 1.0, 1.0);
    holder.addBox(box1);

    Box box2 = new Box(3.0, 3.0, 3.0);
    holder.addBox(box2);

    //display boxes using printvector method of boxVector
    System.out.println("Box details: " + holder.printVector());
  }
}

------------------------------------------------------------------------
Hope that helps...... ! rickste_r.

Edited by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.