• Mothod to find Volume of Box declare an array of boxes java programming

    Abhey Member

    Define Class Box and a method to find Volume of Box declare an array of boxes
    and find out which one is the smallest one ??

    Write program in java ,not to take input from user assign some default values to it .???
    Can any one solves this java program ?

  • SapnaVishwas Member

    This sounds quite alot like some homework Wink I will help you get started:

    public class Box{
    
    private int height, width, depth;
    
    public Box(int height, int width, int depth){
    } 
  • Fierro Member

    The above should help you programming wise and considering this is homework you should try to do it yourself. Below I will explain how to do it without providing code unless I get extra time.

    Basically it wants you to randomly generate 3 numbers those being the height, width and depth of the box. You may then want to generate this 3 times, so you would want 3 values for each of the 3 boxes. You could generate these numbers using the maths random function. After you’ve done this you would probably want to times these 3 values together to get the volume of each of the 3 boxes. Once you have the volumes all you have to do is loop through them to see which is the smallest…

  • GloriaFine Member

    Friends i have tried it …. but i think its wrong plz post the write code

    import java.lang.*;
    class Box{
    int s; // s = side
    Box(int x){s=x;}
    void vol()
    {
    System.out.println("The Volume of Box is :"+s*s*s);
    }
    }//End Of class Box
    class Box49{
    public static void main(String args[]){
    int i,s;
    int arr[]=new int[10];
    int vol(s)
    {
    return(s*s*s);
    }
    for(i=1;i<=10;i++)
    {
    arr[i].vol(i*5+10);
    }
    boolean swapped = true;
    int j = 0;
    int tmp;
    while (swapped) {
    swapped = false;
    j++;
    for ( i = 0; i < arr.length - j; i++) {
    if (arr[i] > arr[i + 1]) {
    tmp = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = tmp;
    swapped = true;
    }
    }
    }
    System.out.println("Smallest Box"+arr[0]);
    }//End OF main
    }//End Of class Box49
    
  • Adan Member

    Here is the start of your code, you should be able to figure it out from here:

    import java.util.Arrays;
    class Box {
       public static void main(String[] agrs) {
          int[] boxes = { 244, 483, 895 };
          Arrays.sort(boxes);
          display(boxes);
       }
       public static void display(int[] elems) {
          System.out.println("\nBoxes in order of size (smallest - largest):");
          for (int i = 0; i < elems.length; i++) {
             System.out.println("Box " + i + " is " + elems[i]);
          }
          System.out.println("\nTherefore, we can see that " + elems[0] + " is the smallest box volume");
       }
    }
    
  • SapnaVishwas Member

    I have written only one small Java application before, but since I know some other languages, it is not difficult to adapt. Here is what I made:

    public class BoxTest {
        public static void main(String[] args) {
            int nLowestVolumeId=-1;
            int nLowestVolume=-1;
            int nVolume=0;
            Box aBoxList[]=new Box[3];
           
            aBoxList[0]=new Box(5,6,1);
            aBoxList[1]=new Box(7,9,2);
            aBoxList[2]=new Box(5,4,3);
           
            for(int i=0;i nVolume){
                    nLowestVolumeId=i;
                    nLowestVolume=nVolume;
                }
            }
           
            System.out.println("Box "+nLowestVolumeId+" is the smallest one with volume "+nLowestVolume+".");
        }
    }
    
    Code: Select all
    class Box {
        private int nHeight,nWidth,nDepth;
       
        public Box(int nSetHeight,int nSetWidth,int nSetDepth){
            nHeight=nSetHeight;
            nWidth=nSetWidth;
            nDepth=nSetDepth;
        }
       
        public int getVolume(){
            return nHeight*nWidth*nDepth;
        }
    }
    

    It does not set box width, height and depth to random values but that was not asked for anyway.

  • Fierro Member
    /*Define Class Box and a method to find Volume of Box. Declare an array of boxes
    and find out which one is the smallest one */
    import java.util.Random;
    
    public class Box
    {
    int length;
    int width;
    int height;
    int boxNum;
    
    public Box(int l, int w, int h)
    {
       length = l;
       width = w;
       height = h;
    }
    
    public int getVolume()
    {
       return (length*width*height);
    }
    
    //generate a bunch of box objects with random specs
    //iterate through them and find out the lowest one... somehow
    
    public static void main(String[] args)
    {
       Box[] boxesAr = new Box[10];
       Random rn = new Random();
       
       int lowestBoxNum = 0;
       int lowestVol = 10000000;
       
       for (int i = 0; i < boxesAr.length; ++i)
       {
          boxesAr[i] = new Box(rn.nextInt(15)+1,rn.nextInt(15)+1,rn.nextInt(15)+1);
          boxesAr[i].boxNum = i + 1; //boxNum is ONE greater than the Box Object index in the boxesAr
          //System.out.println("Box " + boxesAr[i].boxNum + " has a volume of " + boxesAr[i].getVolume());
          if (boxesAr[i].getVolume() < lowestVol)
          {
             lowestVol = boxesAr[i].getVolume();
             lowestBoxNum = boxesAr[i].boxNum;
          }
          
       }
       
       System.out.println("The box with the smallest volume is " + lowestBoxNum + " and it has a volume of " + lowestVol + ".");
       
    }
       
    }
    
Viewing 6 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish