File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can't understand class concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't understand class concept

Tirthankar Mukherjee
Ranch Hand

Joined: Apr 08, 2006
Posts: 51
Hello, what I was trying to do is that, two complex numbers will be given and when any method like sum, subtract, multiply... will be called the program will work it out, but problem I am facing is that, HOW TO TAKE two input using only one constructer, even if it is done how can I perform the sum on it, what I tried is :

class ComplexNumber
{

float real;
float imaginary;

getval(float a, float b)
{
real=a;
imaginary=b;
}

void sum()
{
float A= ??? //What is needed to be done here ???
float B= ??? // Can the objects defined T & M be used here if yes then how ???
System.out.println("The required sum is :" +A+"i"+B);
}
}

class Complex
{
public static void main(String args[])
{
ComplexNumber T = new getval1(2.3,0.4);
ComplexNumber M = new getval1(2.3,0.5);
sum();
}
}

ThankYou , Tirthankar
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1010
    
    3
Of course, we won't do someone's homework for them. However, you seem to be having trouble with syntax and related issues despite some honest effort, so I do't feel like I'm giving away too much if I just give you an answer or two.

  • Use doubles instead of floats everywhere. Either that or you'll have to typecast your constants to floats.
  • The constructor should have a signature like "public ComplexNumber(double a, double b)"
  • To make a new ComplexNumber, use "new ComplexNumber(r,i)" instead of "new getval()".
  • The sum() method should take a ComplexNumber argument, add it to "this" and return a ComplexNumber
  • Then your test program should make T and M and then a third one which is set to T.sum(M)
  • The ComplexNumber class should provide a way to allow Complex.main() to access the two components. Either make them public (bad idea) or provide accessors (good idea).
  • Make Complex.main() responsible for printing the values in the ComplexNumber returned by T.sum(M).


  • Cool?
    [ April 18, 2006: Message edited by: Ryan McGuire ]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    I'm not sure I understand the question. But from what you're describing, it seems you might want to consider defining your methods to...
  • Take a single instance of ComplexNumber as an argument.
  • Perform the operation using "this" as one operand and the passed instance as the other operand.
  • Return a new instance of ComplexNumber.
  • So using the methods would look something like this...

    ComplexNumber c1 = new ComplexNumber(1.2f, 3.4f);
    ComplexNumber c2 = new ComplexNumber(5.6f, 7.8f);
    ComplexNumber result = c1.sum(c2);

    PS: Please use Code Tags to keep your indentation showing.
    [ April 18, 2006: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Tirthankar Mukherjee
    Ranch Hand

    Joined: Apr 08, 2006
    Posts: 51
    code :

    class ComplexNumber
    {

    double real;
    double imaginary;

    ComplexNumber(double a, double b)
    {
    real=a;
    imaginary=b;
    }

    void sum(ComplexNumber x, ComplexNumber y)
    {
    double A= x.real + y.real;
    double B= x.imaginary+y.imaginary;
    System.out.println("The required sum is :" +A+"i"+B);
    }
    }

    class Complex
    {
    public static void main(String args[])
    {
    ComplexNumber T = new ComplexNumber(2.3,0.4);
    ComplexNumber M = new ComplexNumber(2.3,0.5);
    sum(T,M);
    }
    }

    I modified it to this but still not working, giving error in method sum, and in the line >>> sum(T,M)

    NB : This is not mu homework, I am really a newbie and working hard for my next semister as Java is too big to grasp in 1 sem.; as all my Profs. is busy now in their projects, I am facing problem amd providing the same answer to all(including you) that this is NOT my H.W.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Tirthankar Mukherjee:
    ...giving error in method sum, and in the line >>> sum(T,M)...

    Please use Code Tags to keep your indentation showing.

    The method sum is an instance (non-static) method, so you need an instance of the class in order to call it. For example, if "c1" referenced an instance of ComplexNumber, then you could call the method using c1.sum(c2, c3);

    Alternatively, you could make the method static, in which case it would be associated with the class rather than a particular instance. Then you would typically call it using the class name. For example, ComplexNumber.sum(c2, c3); Alternatively, you could also call it directly from main (another static method in the same class), as you've tried to do.
    Ryan McGuire
    Ranch Hand

    Joined: Feb 18, 2005
    Posts: 1010
        
        3
    Originally posted by Tirthankar Mukherjee:
    I modified it to this but still not working, giving error in method sum, and in the line >>> sum(T,M)

    NB : This is not mu homework, I am really a newbie and working hard for my next semister as Java is too big to grasp in 1 sem.; as all my Profs. is busy now in their projects, I am facing problem amd providing the same answer to all(including you) that this is NOT my H.W.[/QB]


    Instead of making making sum() return nothing (i.e. void), have it return a ComplexNumber. Once you calculate A and B, use them to make a new ComplexNumber to return. Also, I would take marc's second option and make sum() a static method. Then in Complex.main(), once you have M and T, declare ANOTHER ComplexNumber variable and set it to ComplexNumber.sum(T, M).

    Re: Homework: That's cool.

    Re: Using code tags: Please do.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    When you call a method like sum, do you want...
  • One of the ComplexNumber operands to be modified by the operation? For example, c1.sum(c2) would return nothing, but would result in c1's value changing. OR...
  • A new instance of ComplexNumber returned as the result, leaving the original operands unchanged? For example, ComplexOperations.sum(c1, c2) would return a new instance of ComplexNumber, but c1 and c2 would not change.
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Can't understand class concept