Results 1 to 10 of 10
  1. #1
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Help me with the test review questions please

    Test review questions I'm having problems with:

    Write a class called Box which has a constructor that creates a box or carton whose length width height are passed to the constructor int type.

    ex: Box bx=new Box(3,4,2) creates a box object bx with length of 3, width of 4, and height of 2,

    Also create a default constructor which creates a Box whose length, width and height are 1.

    Write the following methods:

    vol which computes the volume of a Box and returns it as an int

    surace which computes the entire surface area of a Box and returns it as an int. The surface area is the total area of all six sides of the Box.

    bigger which takes a Box object and increases the length, width and height by specified amounts passed as parameters to the method examples

    Box b1= new Box(7,2,3); creates a box 7 x 2 x 3.

    b1.vol() would return the volume of b1

    b1.surface() would return the surface area of box b1.

    b1.bigger(2,0,3) which would increase the length of box b1 by 2 and its height by 3.

    Its width would be unchanged. Its new dimensions would be 9 x 2 x 6.

    Box b2= new Box(); crreates a box 1 x 1 x 1.

    b2.bigger(2,3,4) would change the dimensions of b2 to 3 x 4 x 5.

    Write the code to determine if a box bb will fit inside a box cc. hint: vars cannot be private


    Java Code:
    public class boxes
    {
       public static void main(String[] args)
       {
          box b= new box(2,2,2);
          b1.bigger(1,1,1);
       }
    }
       class box
       {
       public int height;
      public int length;
      public int width;
      public box (int h, int w, int l)
          {height=h;
          width=w;
          length=l;}
       public box()
    {
       height=1;
       width=1;
       length=1;}
       
       public bigger(int h, int w, int l)
       {
         height+=h;
         width+=w;
         length+=l;
         }
    }
    The above code is what I made so far. I am having problems doing bigger()

    More questions:

    2) Which of the following expressions, if any, have the same values for any integers x, y, and z.

    i. (x/y) * z ii. x* z/y iii. x/(y*z) iv. z*x/y

    3) Which of the following give the same result?

    i. 5.0/2 ii 5/2 iii. 12/5 iv 12.0/5.0

    10) Assume an array is declared as int num[5]; write the statements that correctly assigns the values 0, 1, 4, 9, 16 to the successive array elements?

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help me with the test review questions please

    what is b1 in this example
    i can see you creating box b, but what is b1?

  3. #3
    cjtemple is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Help me with the test review questions please

    You also need a return type for bigger, based on what you have the return type would be void.

  4. #4
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me with the test review questions please

    Thanks guys it worked:

    Java Code:
    public class boxes
    {
       public static void main(String[] args)
       {
          box b= new box(2,2,2);
          System.out.println("box b height is "+ b.height+ "box b width is "+ b.width+ "box b length is "+b.length);
          System.out.println("box b volume is "+b.volume(b.height,b.length,b.width));
          System.out.println("box b area is "+ b.area(b.height, b.length, b.width));
          b.bigger(1,1,1);
          System.out.println("box b now height is "+ b.height+ " box b width now is "+ b.width+ " box b length now is "+b.length);
       }
    }
       class box
       {
       public int height;
      public int length;
      public int width;
      public box (int h, int w, int l)
          {height=h;
          width=w;
          length=l;}
       public box()
    {
       height=1;
       width=1;
       length=1;}
       
       public void bigger(int h, int w, int l)
       {
         height+=h;
         width+=w;
         length+=l;
         }
       public double volume(int h, int l, int w)
       {
       return h*l*w;
       }
       public double area(int h, int l, int w)
       {
       return 2*(h*w)*2*(h*l)*2*(w*l);
       }
    }
    Now can you please answer the rest of the questions?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Help me with the test review questions please

    It would be better if you would write your own answers and your reasoning behind them. Then if you get any
    wrong, we could help you understand where you made a mistake.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me with the test review questions please

    My answers:

    2) Which of the following expressions, if any, have the same values for any integers x, y, and z.

    i. (x/y) * z ii. x* z/y iii. x/(y*z) iv. z*x/y

    Answer: NONE

    3) Which of the following give the same result?

    i. 5.0/2 ii 5/2 iii. 12/5 iv 12.0/5.0

    i, ii, iii.

    10) Assume an array is declared as int num[5]; write the statements that correctly assigns the values 0, 1, 4, 9, 16 to the successive array elements?

    int num[5];
    num[0]=0;
    num[1]=1;
    num[2]=4;
    num[3]=9;
    num[4]=15;

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Help me with the test review questions please

    Quote Originally Posted by ssunlimited View Post
    My answers:

    2) Which of the following expressions, if any, have the same values for any integers x, y, and z.

    i. (x/y) * z ii. x* z/y iii. x/(y*z) iv. z*x/y

    Answer: NONE

    3) Which of the following give the same result?

    i. 5.0/2 ii 5/2 iii. 12/5 iv 12.0/5.0

    i, ii, iii.

    10) Assume an array is declared as int num[5]; write the statements that correctly assigns the values 0, 1, 4, 9, 16 to the successive array elements?

    int num[5];
    num[0]=0;
    num[1]=1;
    num[2]=4;
    num[3]=9;
    num[4]=15;

    Item 2. What about ii, and iv? Without parentheses and identical operator precedence, the expression is evaluated from left to right.
    In both cases, x * z is evaluated first before division by y.

    Item 3. ii and iii evaluate to 2 because of integer division. i gives 2.5 because of the 5.0.

    Item 10. That's one way of doing it. (except for the typo - you meant 16 not 15).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me with the test review questions please

    Oh thank you Jim. But item 3 doesn't it mean the answers are i and iv and ii and iii? Because i and iv are 2.5 and ii and iii are 2?

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Help me with the test review questions please

    The question was which gives the same result. Only ii and iii give the same result. I was explaining why i was incorrect. Because it gives 2.5.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help me with the test review questions please

    Thank you all!

Similar Threads

  1. Please Help Me!!! Last minute test questions...
    By justinm231 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 06:42 PM
  2. Replies: 0
    Last Post: 09-15-2011, 12:02 PM
  3. Memory Leak questions, code review
    By mensaFool in forum Advanced Java
    Replies: 9
    Last Post: 03-08-2010, 04:27 PM
  4. 2 test questions on JSP.
    By an24 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-22-2009, 11:14 AM
  5. Some Questions - from old test papers 100 level
    By Julie in forum New To Java
    Replies: 5
    Last Post: 09-03-2008, 06:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •