Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2007, 06:55 AM
Member
 
Join Date: Nov 2007
Posts: 4
nhlfan is on a distinguished road
help with oop code
Hello guys,

I am doing the following exercise:

a) Create a base class named Rectangle that contains length and width data members. From this class, derive a class named Box having an additional data member named depth. The method members of the base Rectangle class should consist of a constructor and have an area( ) method. The derived Box class should have a constructor and an override method named area( ) that returns the surface area of the box and a volume( ) method.

b) Include the classes constructed for Exercise 8a in a working Java program. Have your program call all of the member methods in each class and explain the result when the area method is called using two Box objects.

I have 3 separate files : Rectangle.java, Box.java, and UsingShapes.java
This is what I have so far:

Rectangle.java

Code:
public class Rectangle { protected double length; protected double width; public Rectangle (double lh, double wh) { length = lh; width = wh; } public double area() { return(length * width); } }


Box.java

Code:
public class Box { protected double depth; public Box (double lh, double wh, double dh) { super(lh); super(wh); depth = dh; } public double area() { return ((2*super.area)+(2*(dh*super(lh))+ (2*(dh*super(wh))))); //surface area of box = 2(h*w) + 2(h*l) + 2(w*l) } public double volume() { return (depth * super.area()); } }


UsingShapes.java (File which runs the other 2 files)

Code:
public class UseShapes { public static void main (String[] args) { Rectangle rect1 = new Rectangle(2,4); Box bv = new Box(5,5,5); Box boxOne = new Box(1, 2, 3); Box boxTwo = new Box(4, 5, 6); System.out.println("The area of the rectangle is " + rect1.area); System.out.println("The volume of the box is " + bv.volume); System.out.println("The surface area of boxOne is " +boxOne.area); System.out.println("The surface area of boxTwo is " +boxTwo.area); } }
Any help? Thanks alot
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007, 09:21 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
From this class, derive a class named Box
This means that Box extends (from) Rectangle.
Code:
public class UseShapesRx { public static void main (String[] args) { RectangleRx rect1 = new RectangleRx(2,4); BoxRx bv = new BoxRx(5,5,5); System.out.println("The area of the rectangle is " + rect1.area()); System.out.println("The volume of the box is " + bv.volume()); BoxRx boxOne = new BoxRx(1, 2, 3); BoxRx boxTwo = new BoxRx(4, 5, 6); System.out.println("The surface area of boxOne is " +boxOne.area()); System.out.println("The surface area of boxTwo is " +boxTwo.area()); } } public class BoxRx extends RectangleRx { protected double depth; public BoxRx(double lh, double wh, double dh) { super(lh, wh); depth = dh; } public double area() { //surface area of box = 2(h*w) + 2(h*l) + 2(w*l) double area = 2*(length*width + width*depth + length*depth); return area; } public double volume() { return width * length * depth; } }
No changes in Rectangle class but name changed to RectangleRx for this.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help fixing my code.. or non code? MrHuggykins New To Java 1 03-19-2008 11:12 PM
Pls some one to help mi wit this code _nik_ New To Java 3 02-10-2008 03:02 AM
tic tac toe code zoe New To Java 1 07-23-2007 05:36 PM
Need help with my code. stormviper New To Java 0 07-12-2007 04:18 PM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 04:52 PM


All times are GMT +3. The time now is 08:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org