PDA

View Full Version : Java Assignment Confusion




zechmann
Nov 30, 2007, 04:26 PM
error



pilotError
Nov 30, 2007, 06:46 PM
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.

dukebound85
Nov 30, 2007, 06:54 PM
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

zechmann
Dec 1, 2007, 06:54 PM
g

pilotError
Dec 1, 2007, 07:28 PM
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.


Create a base class named Rectangle that contains length and width data members. The method members of the base Rectangle class should consist of a constructor and an area() method.


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...

zechmann
Dec 1, 2007, 08:04 PM
c

zechmann
Dec 3, 2007, 12:04 PM
b

ChrisBrightwell
Dec 3, 2007, 01:48 PM
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.

ex: 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.

jeremy.king
Dec 3, 2007, 02:18 PM
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.

ChrisBrightwell
Dec 3, 2007, 02:39 PM
Your assignment doesn't say to write a main() method, so why bother? You don't have to run anything.You can't test w/out a main() method, for starters.

Your Box should probably take in a height and width too.Box extends Rectangle. Height and width are inherited.

Gelfin
Dec 3, 2007, 02:41 PM
if anyone could help me for just a little bit i would really appreciate it... this assignment is due in a couple of hours and i still can't seem to get it to work. It just won't seem to run

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.

pilotError
Dec 3, 2007, 02:42 PM
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));
super.area();
}
}



Tester Class...


public class Tester
{

public static void main( String args[] )
{
Box myBox = new Box(2, 3, 4);
Rectangle myRect = myBox;

myBox.area();

myRect.area();
}
}

jeremy.king
Dec 3, 2007, 02:44 PM
You can't test w/out a main() method, for starters.
I'm just pointing out that its NOT part of his assignment. Many professors/teachers also check that directions can be followed.


Box extends Rectangle. Height and width are inherited.

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:

pilotError
Dec 3, 2007, 02:54 PM
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 :)

zechmann
Dec 3, 2007, 03:50 PM
a