Inheritance Code Question

Question about code

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 8727 Views - Last Post: 01 May 2006 - 08:26 AM Rate Topic: -----

#1 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Inheritance Code Question

Posted 29 April 2006 - 07:09 PM

Hello,

I am new to Java and new to the board. I am working on a made up program that uses inheritance to create seperate classes(in seperate files) and includes intentional errors and error handling to correct the errors. The problem is, the variables that I created are not seeming to transfer to the "Class ExceptionC." I receive errors- "Cannot find variable C." "Cannot find variable "ExceptionB"

Suggestions greatly appreciated!
Thanks a million,
-Sof

public class ExceptionA //extends Exception 
{
        public String A;
        public int B;
        public int C; 
    

    public ExceptionA( String a, int b, int c ) 
    {
     
        A = a;
        B = b;
        C = c;    
    }

        public void setA ( String a )
        {
            A = a;
        }
    
        public String getA ( String a )
        {
            return a;
        }
        
        public void setB ( int b )
        {
            B = b;
        }
    
        public int getB ( int b )
        {
            return b;
        }
        
        public void setC ( int c )
        {
            C = c;
        }
    
        public int getC ( int c )
        {
            return c;
        }

        public String toString()
        {
            return String.fromat( "This is the entered data a: %s b: %s c:%s ", getA(), getB(), getC() );
        }
        
        public abstract int except();
        }

}

 public class ExceptionB extends ExceptionA
    {   
        private int calculatedResult;
        private int result;
        
        public ExceptionB( String a, int b, int c ) 
        {
            super( a, b, c );
            setCalculatedResult( result );
        }
        
        public int setCalculatedResult( int result )
        {
            calculatedResult = result < 0 ? 0 : result;
        }
        
        public int getCalculatedResult()
        {
            return calculatedResult;
        }
        
        public int exceptions()
        {
            return getCalculatedResult();
        }
        
        public String toString()
        {
            return String.format( "This is the calculated Data: %s", super.toString(), getCalculatedResult() );
        }
        public int except();
        {
            
        }
    }


public class ExceptionC 
    {        
        public static void main(String args[])
        {
            ExceptionB exceptionB = new ExceptionB( "Hello", 150, 0 );
            System.out.println( "This is the Data" );
                        
          while ( C == 0  )
            {
               System.out.println( ExceptionB );
                try
                    {
                        System.out.print( "We are going to try to divide %s by %s ", getA(), getB() );
                        int result = quotient( A, B );                    
                    }
                
                catch ( Exception exception )
                {
                    System.out.println( "This division results in error. Characters cannot be used in Arithmetic calculations." );
                }
                
                try
                   {
                        System.out.println( "We are going to try to divide %s by %s .", getB(), getC() );
                        int result = quotient( B, C );
                    }
                
                catch ( ArithmeticException arithmeticexception )
                    {
                        System.out.println( "This division results in error. Integers cannot be divided by zero." );
                    }
                
                finally
                    {
                        System.out.println( "We are going to try to divide %s by %s .", getC(), getB() );
                        int result = quotient( C, B );
                        System.out.println( "This division results is successful. Zero can be divided by another integer. The result is %s", getresult() );
                    }
                
                for ( int j = 0; j < exception.length; j++ )
                    System.out.printf( "Exception %s is %s\n", j, exceptions[ j ].getClass .getName() );
                 }
        }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance Code Question

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Inheritance Code Question

Posted 29 April 2006 - 08:05 PM

Are you getting those errors at runtime, or compile time? I see some itmes that may prevent compilation, but if you're getting it to compile, then let us know...
Was This Post Helpful? 0
  • +
  • -

#3 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 29 April 2006 - 08:21 PM

Sorry about that, Compiling errors. I can't get it to compile. And I am not quite sure why. For some reason, it isn't recognizing the variables A, B, or C. I am new to this, so it may just be something I am missing. I have looked over it and looked over it and I am not sure.

Thanks for the help!
~Sof
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Inheritance Code Question

Posted 29 April 2006 - 08:36 PM

Well, the error concerning not being able to find ExceptionB is being thrown becasue the variable you decalred was named exceptionB...java is a case sensitive language. Similarly, C by itself in the main fucntion has no meaning...it has not been declared in that scope as a stand alone variable...it is part of a class object, and will have to referenced.
Was This Post Helpful? 0
  • +
  • -

#5 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 29 April 2006 - 08:41 PM

I guess I am not quite sure how I would reference variable 'C'

ExceptionB exceptionB = new ExceptionB( "Hello", 150, 0 );


C as a variable is supposed to be the int value o, so I would need to go ahead an refence that in the main?



I guess I am not quite sure where the variables need to be lowerecase either.

Thanks for the Help! I really appreciate it.

This post has been edited by -sofia-: 29 April 2006 - 08:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Inheritance Code Question

Posted 29 April 2006 - 08:53 PM

ExceptionB exceptionB = new ExceptionB( "Hello", 150, 0 );


The above line declares and instantiates avariable of type ExceptionB named exceptionB.
System.out.println( ExceptionB )


This line references a variable named ExceptionB...there is no such variable...only exceptionB.

As for the variable named C, I am assuming you are trying to access the variable named C original declared in class ExceptionA, and inherited by class ExceptionB. If this is the case, then variable C should be accessed through the class structure...namely the variable of type ExceptionB you declared named exceptionB
while ( exceptionB.C == 0  )


Was This Post Helpful? 0
  • +
  • -

#7 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 29 April 2006 - 09:01 PM

Okay. That makes sense.

Thanks for the help. Before compiling, however, it is also bringing up errors from these lines. getA , getB and variables A and B are still not recognized.

System.out.print( "We are going to try to divide %s by %s ", getA(), getB() );
                        int result = quotient( A, B );           



Thanks a million!
Java is actually beginning to make a little more sense! :D
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Inheritance Code Question

Posted 29 April 2006 - 09:05 PM

The problem is the same...those variables and methods are members of class ExceptionA, which has been inherited by class ExceptionB, which has been declared and instantiated in the form of variable excpetionB...they just need to be referenced correctly as members of their class, as we did with variable C.
Was This Post Helpful? 0
  • +
  • -

#9 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 29 April 2006 - 09:10 PM

I am really sorry, but I am not quite sure how I would do that. Would I need to create another line referencing like...

while( ExecptionB.A) ??????

Thanks again!

This post has been edited by -sofia-: 29 April 2006 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#10 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 29 April 2006 - 09:27 PM

-sofia-, on 29 Apr, 2006 - 08:02 PM, said:

I am really sorry, but I am not quite sure how I would do that. Would I need to create another line referencing like...

while( ExecptionB.A)    ??????

Thanks again!

Ooops, I tried that. Didn't work. Still not recognized.

This post has been edited by -sofia-: 29 April 2006 - 09:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Inheritance Code Question

Posted 30 April 2006 - 07:43 AM

Again, the problem is occurring becasue there is no variable named ExceptionB...only exceptionB. Each member or method should be referenced as such. Thsi is the same solution you used to be able to see variable C. From you previous code, it should look like:

System.out.print( "We are going to try to divide %s by %s ", exceptionB.getA(), exceptionB.getB() );
                       int result = quotient( A, B );



As for the call to the function quotient, that does not appear to be defined anywhere.
Was This Post Helpful? 0
  • +
  • -

#12 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 30 April 2006 - 08:03 AM

Hello again!

Thanks for the response. I tried including
System.out.print( "We are going to try to divide %s by %s ", exceptionB.getA(), exceptionB.getB() );
                      int result = quotient( A, B );

but, now I get

" getA( javastring.lang) and "getB(javaint.lang) cannot be applied to () " and it still doesn't seem to recognize the variables. :huh:

I appreciate your advice!
Thanks a million!

This post has been edited by -sofia-: 30 April 2006 - 08:05 AM

Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Inheritance Code Question

Posted 30 April 2006 - 08:33 AM

The new error you are receiving is becasue you are calling the methods like so
exceptionB.getA()


while they have been defined as needing to receive a value as an argument:

public String getA (string A)


therefore, the compiler sees the call to the method as incorrect. In this case, since I doubt very much you would need to supply the very value to the method which you were looking to have returned, I'd suggest that you modify your methods to receive no values in the case of the get methods.
public String getA ()
{
   return a;
}


and so on...
Was This Post Helpful? 0
  • +
  • -

#14 -sofia-  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 06

Re: Inheritance Code Question

Posted 30 April 2006 - 09:18 AM

Okay, thanks a million! Awesome. That took care of that. I removed the values from the get/set methods as you recommended. And after that, I received the error cannot find Symbol
for
 System.out.println( "This division result is successful. Zero can be divided by another integer. The result is %s", getresult() );



so, I replaced .println with .printf. So, that took care of the Symbol error.



However, I still receive an error for variable A and B
int result = quotient( A, B );



I really appreciate it!
~Sof

This post has been edited by -sofia-: 30 April 2006 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#15 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Inheritance Code Question

Posted 30 April 2006 - 10:41 AM

I am going to go out on a limb here and say it has to do with your quotient method (for which the code is not posted, and cannot help correct)

*I'm not saying this implementation it incorrect (and it isn't), but it may have been easier to implement throwing the errors
kudos for learning inheritence though :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2