473,468 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Object oriented design for more than one class . Java

4 New Member
Hi .l am real new to programming . l have a problem with my code .l have tried to translate the following programming problem into java language but the problem l have is that l cannot create the subclass that is Cube class.Here is the programming problem:Design a class called Square with a value for the side of the square and validate that value and calculate the area of the square .The child class Cube will use the existing methods of the parent class to validate and receive the side of the cube and create a new method to calculate and display the volume of the cube.Below are the code for two relational classes.Hoping for assistance.


Expand|Select|Wrap|Line Numbers
  1. /**
  2.  *
  3.  * @author Abraham Nkomo
  4.  */
  5. public class Square
  6. {
  7.   public double sideLength;
  8.  
  9.     public Square(double inSideLength )
  10. {
  11.     sideLength  = inSideLength;
  12. }
  13.     double Area;
  14.      public void calculateSquare()
  15.      {
  16.          if (validInput)
  17.  
  18.     Area = sideLength * sideLength;
  19.  
  20.     else
  21.          System.out.println("Invalid input");
  22.  
  23.      }
  24.      boolean validInput = true;
  25.      {
  26.  
  27.          if (sideLength < 0)
  28.          validInput =  false;
  29.  
  30.          else
  31.              validInput = true;
  32.     }
  33.     public double displayArea()
  34. {
  35.     return Area;
  36. }
  37. }
  38.  
  39.  

Below is the code for the child class

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  *
  3.  * @author Abraham Nkomo
  4.  */
  5.  
  6.    public class Cube extends Square
  7.  
  8.   {
  9.  
  10.  
  11.  
  12.  
  13.     double cubeVolume ;
  14.     public  void  calculateCubeVolume()
  15.     {
  16.        validInput = true;
  17.         if(validInput)
  18.             cubeVolume =  sideLength * sideLength * sideLength;
  19.  
  20.         else
  21.             System.out.println("Invalid input");
  22.  
  23.      }
  24.     public double getCubeVolume()
  25.  
  26.     {
  27.             return cubeVolume;
  28.  
  29.     }
  30.  
  31.  
  32. }
  33.  

Please help me to fix my code.
Thanks in advance

KKHULUZA NKOMO
Sep 3 '10 #1
2 1348
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
I am not clear with your question.

But following may be the OOP thing you are expecting.
No need to calculate the volume again. directly return the volume like this.

Expand|Select|Wrap|Line Numbers
  1. Square parentobj = new Square(sideLength);
  2. return (parentobj.calculateSquare())*sideLength;
  3.  
Regards
Dheeraj Joshi
Sep 6 '10 #2
Nepomuk
3,112 Recognized Expert Specialist
Hi!

It's been a while since I did anything like that, but I think you'll need a constructor of the form
Expand|Select|Wrap|Line Numbers
  1. public Cube (double inSideLength )
  2. {
  3.   super(inSideLength);
  4.   // Do other stuff, that is different from what you did in the Square class
  5. }
Greetings,
Nepomuk
Sep 8 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Christian Decker | last post by:
I wanted to know if is a good idea to create classes that mirror exactly the structure of a database. This might sound a bit fuzzy but I'l explain myself with an example: CREATE TABLE...
2
by: Eric Nilsson via .NET 247 | last post by:
I need a good design (pattern?) for working with persistantobjects. This is what has been bothering me, I'm on 3'rd year inuniversity learning computer science, and I am now working on...
4
by: rick walsh | last post by:
I am trying to write an application that displays news articles on a web page. I have recently moved to C# and currently I am trying to learn how to use good Object Oriented design. I assume...
1
by: Patrick | last post by:
Hi all, New to .NET, I'm working on an Winforms client application using VS 2005 beta2. My needs considering data storage are the followings: (1) Small files (0 < length < 10 mb), containing...
4
by: scottrm | last post by:
Is anyone aware of any book/website/tutorial that has a decent object oriented design model for an asp.net e-commerce site (or even a generic e-commerce site). The ibuyspy.com sample site does not...
4
by: scottrm | last post by:
I am fairly new to oo design and I am looking at developing an object oriented asp.net application which will be built on top of a relational database. I have read quite a bit of the theory but...
1
by: scottrm | last post by:
Any recommendations for a good book/article/sample on object oriented design particularly with regards to building an asp.net/sql server application. I don't mean a book explaining concepts like...
6
by: B. Chernick | last post by:
(I'm not too comfortable with inheritance in VB and I don't think I've ever read about a situation like this.) Let's assume Dot Net 1.1 and VB.Net. Let's assume a main Winforms program and a...
1
by: indiarocks | last post by:
I have been developing a application in Python which instantiates objects on the fly. I want to adopt a Object Oriented approach in what I am developing. So far what I have been doing is Script 1...
5
by: virtualadepts | last post by:
I have code here that explains my object oriented design model. I've been reading about other design models from what is documented on wikipedia about the key book on the subject:...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.