473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inheritance

I am finding trouble using inheritance in my solution to the following question:

"Write a class called shapes that accepts two variables into its constructor called a and b. Along with the getter and setter methods required, write a method called toPrinter() to output the coordinates of the shape in the following format: [a, b].
Next write a class called orange that is a child class of shapes. This time the constructor accepts in a, b (centre of the orange), and a radius. Override the toPrinter() method in the orange class to include outputting the radius, as well as a and b. Write a method to get the area of the orange. Include any relevant classes to run and test the application."

I can the first part of the question but not the second paragraph. I am finding it difficult using inheritance.

Below is my solution so far.Can anyone spot where I am going wrong with the inheritance?

Expand|Select|Wrap|Line Numbers
  1. public class Shapes_5
  2. {
  3. public static void main(String[] args) 
  4. {
  5.  
  6. Shapes_Values c = new Shapes_Values();
  7.  
  8. c.setValueA(5);
  9. c.setValueB(14);
  10.  
  11. c.goToPrinter();
  12.  
  13. }
  14. }
  15.  
  16.  
  17.  
  18. class Shapes_Values
  19. {
  20.  
  21. int ValueA;
  22. int ValueB;
  23.  
  24.  
  25.     void setValueA(int iValueA)   //setter method
  26.     {
  27.  
  28.         ValueA = iValueA;
  29.  
  30.     }
  31.  
  32.  
  33.     void setValueB(int iValueB)       //setter method
  34.     {
  35.  
  36.         ValueB = iValueB;
  37.  
  38.     }
  39.  
  40.     int getValueA()                //getter method
  41.     {
  42.  
  43.         return ValueA;
  44.  
  45.     }
  46.  
  47.     int getValueB()                    //getter method
  48.     {
  49.  
  50.         return ValueB;
  51.  
  52.     }
  53.  
  54.     void goToPrinter()            //Outputs the coordinates of the shape
  55.     {
  56.  
  57.         System.out.println("[" + ValueA + "," + ValueB + "]");
  58.  
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. class Orange extends Shapes_5
  65. {
  66. Shapes_Values c = new Shapes_Values();
  67.  
  68. c.setValueRadius(2);
  69. }
  70.  
  71. class Shapes_Values
  72. {
  73.  
  74. int ValueRadius;
  75.  
  76.     void setValueRadius(int iValueRadius)   //setter method
  77.     {
  78.  
  79.         ValueRadius = iValueRadius;
  80.  
  81.     }
  82.  
  83.     int getValueRadius()                //getter method
  84.     {
  85.  
  86.         return ValueRadius;
  87.  
  88.     }
  89.  
  90. }

Thanks in advance!!
Dec 2 '07 #1
3 1108
heat84
118 100+
You have not created a constructor for your shape class . Either way , a and b can be instance variables of your class so you need to declare them in your shape class and create public methods that allow for accessing and changing data in the shape class (accessor and mutator methods).
Dec 3 '07 #2
[quote=javatech007]I am finding trouble using inheritance in my solution to the following question:

"Write a class called shapes that accepts two variables into its constructor called a and b. Along with the getter and setter methods required, write a method called toPrinter() to output the coordinates of the shape in the following format: [a, b].
Next write a class called orange that is a child class of shapes. This time the constructor accepts in a, b (centre of the orange), and a radius. Override the toPrinter() method in the orange class to include outputting the radius, as well as a and b. Write a method to get the area of the orange. Include any relevant classes to run and test the application."

I can the first part of the question but not the second paragraph. I am finding it difficult using inheritance.

Below is my solution so far.Can anyone spot where I am going wrong with the inheritance?

Expand|Select|Wrap|Line Numbers
  1. public class Shapes_5
  2. {
  3. public static void main(String[] args) 
  4. {
  5.  
  6. Shapes_Values c = new Shapes_Values();
  7.  
  8. c.setValueA(5);
  9. c.setValueB(14);
  10.  
  11. c.goToPrinter();
  12.  
  13. }
  14. }
  15.  
  16.  
  17.  
  18. class Shapes_Values
  19. {
  20.  
  21. int ValueA;
  22. int ValueB;
  23.  
  24.  
  25.     void setValueA(int iValueA)   //setter method
  26.     {
  27.  
  28.         ValueA = iValueA;
  29.  
  30.     }
  31.  
  32.  
  33.     void setValueB(int iValueB)       //setter method
  34.     {
  35.  
  36.         ValueB = iValueB;
  37.  
  38.     }
  39.  
  40.     int getValueA()                //getter method
  41.     {
  42.  
  43.         return ValueA;
  44.  
  45.     }
  46.  
  47.     int getValueB()                    //getter method
  48.     {
  49.  
  50.         return ValueB;
  51.  
  52.     }
  53.  
  54.     void goToPrinter()            //Outputs the coordinates of the shape
  55.     {
  56.  
  57.         System.out.println("[" + ValueA + "," + ValueB + "]");
  58.  
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. class Orange extends Shapes_5
  65. {
  66. Shapes_Values c = new Shapes_Values();
  67.  
  68. c.setValueRadius(2);
  69. }
  70.  
  71. class Shapes_Values
  72. {
  73.  
  74. int ValueRadius;
  75.  
  76.     void setValueRadius(int iValueRadius)   //setter method
  77.     {
  78.  
  79.         ValueRadius = iValueRadius;
  80.  
  81.     }
  82.  
  83.     int getValueRadius()                //getter method
  84.     {
  85.  
  86.         return ValueRadius;
  87.  
  88.     }
  89.  
  90. }


When I go to compile the code, one error shows up in line 68 saying an identifer is expected!! Any ideas what i did wrong dere!!??
Dec 3 '07 #3
When I go to compile the code, one error shows up in line 68 saying an identifer is expected!! Any ideas what i did wrong dere!!??
Dec 3 '07 #4

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.