1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Java Assignment Confusion

Discussion in 'Mac Programming' started by zechmann, Nov 30, 2007.

  1. macrumors member

  2. macrumors 68020


    Sounds like the point of the assignment is to show you inheritance.

    It's a pretty straightforward assignment.

    Create the base class Rectangle
    - create an area() method

    Create the Box class by extending rectangle
    - create and area() method

    Create a Box object and show that you can call the base classes methods.

    have you learned the super keyword yet?

    The Box constructor can use the Rectangle constructor for initialization

    super (length, width); // calls the Rectangle constructor
    depth = ???; // initialize the depth variable (only in Box)

    create a couple of objects

    Box myBox = new Box(2,2,2);
    Rectangle myRect = myBox;

    System.out.println("Rectangle Area: " + myRect.area() );
    System.out.println("Box Area: " + myBox.area() );

    Hopefully you'll get the idea.
  3. macrumors P6


    since this sounds like hw, you should post what you have done so pointers can be given to help as opposed to others doing which completely defeats the purpose of the assignment
  4. macrumors member

  5. macrumors 68020


    The why you are doing it is to show that you can extend the functionality of something without changing the core functionality.

    Given that, you need to follow the instructions you've been given.

    public class Rectangle
      int length;
      int width; 
            Rectangle( int len, int wid )  // Constructor with 2 parameters
              length = len; 
              width = wid;
            public void area ()
              System.out.println("Area of the Rectangle is: " + (length * width) );
    Try and complete it from here...
  6. macrumors member

  7. macrumors member

  8. macrumors 68020


    Errors finding main() are almost always due to classpath errors.

    Manually set the classpath in your java command and you should be good to go.

    java -cp ./ Example

    EDIT: I just looked at your code ... there is no main method. You have to write a method (called "main()") that is the entry point for execution and uses all the stuff you put together above.
  9. macrumors 603


    A bunch of thoughts.
    • Your assignment doesn't say to write a main() method, so why bother? You don't have to run anything.
    • Your Box should probably take in a height and width too.
    • Your area() method for Box is actually a volume calculation (don't forget you DO need an volume() method)
    • Surface Area is a sum of the 6 surfaces. Hint - each surface has the same area as the surface on the opposite side.
  10. macrumors 68020


    You can't test w/out a main() method, for starters.

    Box extends Rectangle. Height and width are inherited.
  11. macrumors 68020


    I hate to be the one to break this to you, but somebody needs to: it's looking a lot like you're about to have an important educational experience that has nothing to do with Java.

    I think perhaps you're not getting the level of help you want because it's sounding less like you need answers to specific questions and more like you need somebody to teach you Java. You're already shoveling money into an institute of higher learning to do that, and obviously you're behind. Your situation goes beyond what a web forum can effectively help you with.

    Your class presumably has a textbook. You presumably have notes, and if you've missed some classes due to your injury, it's your responsibility to contact one of your classmates to get what you've missed. Even without those resources, typing "Java tutorial" into Google will point you directly at answers to pretty much all your questions without waiting for strangers in a forum to hand-hold you through an assignment.

    In the time you have remaining, I suggest you look for the answers to your questions using the resources you have at hand and submit as complete a project as you possibly can for partial credit. Then find a way to live with the results. Here are some suggestions for how to recover:

    1. The entire purpose of your class is that you know certain things about Java when you're done, and right now you don't seem to know those things as well as you ought to. Everything you do from here on out has to have the direct effect of fixing that problem.

    2. If your chances of passing the class are in jeopardy as a result of this assignment, talk to your instructor. DO NOT play for sympathy on the basis of your injury and DO NOT ask for a break. Instead acknowledge that you screwed up, take responsibility for it and ask if there is anything you can do to fix things. He is under no obligation to toss you a rope, but a willingness to do the work to make things right is the only hope you've got. You may find your very strict professor is a lot more reasonable if you adopt this attitude. Or not. Instructor roulette is just a fact of life in post-secondary education.

    3. If this situation in any way jeopardizes your attendance at your school generally, you may have to cop the same "how can I fix this" attitude with your advisor or a dean. Remember: they aren't there to save your butt, but they may give you an opportunity to save your own butt if you're willing to work for it.

    4. For pete's sake follow through with anything you agree to as quickly as you possibly can.

    Sorry if this seems harsh. Believe me, practically every student goes through this in some form at some time. Learn from it. It's a good half of what higher education teaches you, and this is how the rest of your life will operate. If it comes to this, you're far from the first student who ever had to re-take a class. That, too, is a recovery strategy.
  12. macrumors 68020


    Hmmm, you need to try a little harder next time.

    public class Box extends Rectangle
            int depth;
            Box (int len, int wid, int dep)
                    super(len, wid);        // Call the Rectangle's constructor
                    depth = dep;
            public void area ()
                    System.out.println ("The area of the Box is: " + (length * width * depth));
    Tester Class...

    public class Tester
     public static void main( String args[] )
      Box myBox = new Box(2, 3, 4);
      Rectangle myRect = myBox;
  13. macrumors 603


    I'm just pointing out that its NOT part of his assignment. Many professors/teachers also check that directions can be followed.

    I meant the constructor. How else are they going to be set appropriately for the calculations?

    Edit...Seems pilotError is too nice, but the area method is still incorrect calculation and a volume() method is missing :eek:
  14. macrumors 68020


    Sometimes folks need a little kick in the... head to get things moving.

    The assignments not complete, but you'll get the idea now. Running this may surprise you if this is the first time you've seen it.

    I have to say, it didn't look like you put forth all that much effort on this. Hopefully you've learned something...

    Good Luck :)
  15. macrumors member


Share This Page