wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes constructor problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor problems" Watch "constructor problems" New topic
Author

constructor problems

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am building a simple math program that uses constructors and while I was able to take care of a number of my errors I am left with 3 I dont understand fully.





G:\Documents and Settings\ben\Desktop\Desktop\Desktop\Ben\park>javac HultinBenPr
og7.java
HultinBenProg7.java:20: cannot find symbol
symbol : constructor Fraction()
location: class Fraction
x = new Fraction(); // create a fraction for number 0
^
HultinBenProg7.java:23: add() in Fraction cannot be applied to (Fraction)
x.add(c).add(d);
^
HultinBenProg7.java:30: multiply() in Fraction cannot be applied to (Fraction)
x.multiply(c).multiply(d);
^
3 errors





My diver class:




my driven class:




I appreciate any help in the matter
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

There is no default constructor once you make your own.

You should overload the constructor to make one with no arguments (even if it's empty), that will fix at least one of your problems.

Hope that helps,
Janeice


When you do things right, people won't be sure you've done anything at all.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
You don't have a no-arg constructor for your Fraction class. Since you put in your own constructor ( public Fraction(int n, int d) ) you will have to pass in 2 ints to your constructor, or write a no arg constructor, like:

If you don't provide a constructor, when you compile java will create a no-arg constructor for your class for you. When you provide any constructor of any time, however, that no-arg constructor will not be provided, so you must create your own if you want to use one.

In essence, what Janeice said .


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot for the help, I added the empty Fraction constructor which got rid of:


HultinBenProg7.java:20: cannot find symbol
symbol : constructor Fraction()
location: class Fraction
x = new Fraction(); // create a fraction for number 0



so the last two errors:



HultinBenProg7.java:23: add() in Fraction cannot be applied to (Fraction)
x.add(c).add(d);
^
HultinBenProg7.java:30: multiply() in Fraction cannot be applied to (Fraction)
x.multiply(c).multiply(d);


are the ones that remain. Any thoughts?
Thanks a lot
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Your add() and multiply() methods don't have any input parameters, but you are trying to pass something into the method. Since they aren't expecting any inputs, you are getting errors.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
thats the thing I am having trouble with. I am not sure how to tell java to add or multiple variables found in an object that will be specified by the driver, not in the method itself. (besides the math operators + and * I understand that part, or making a formula x = n * d ) I just dont know how to do math with variable names that will be specified elsewhere.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
For your add() method, you would need to give it parameters, like:



Then, inside your method, you can reference i and j just like regular variables.

How much experience do you have with Java? I would recommend working through the Java tutorials, or picking up a book like Head First Java. Just a suggestion based on the questions you have been having, they may help you get a good understanding of the basics of the language.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
well I tried the suggestion you made and it didnt seem to help, I still get the same errors.





HultinBenProg7.java:23: add(int,int) in Fraction cannot be applied to (Fraction)

x.add(c).add(d);
^
HultinBenProg7.java:30: multiply(int,int) in Fraction cannot be applied to (Frac
tion)
x.multiply(c).multiply(d);
^
2 errors

G:\Documents and Settings\ben\Desktop\Desktop\Desktop\Ben\park>





here are the changes that I made to the add() and multiply() methods.



obviously this is what code they are being called by in the driver class, via reference variables.


the compiler is complaining about not being able to use add(int,int), would the add and multiply method need to have string parameters seeing as the values are coming from objects? Just a thought.

Thanks a lot for your help
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Hey Ben, if you declare a method with two parameters as you have done:



Then you have to access them using two parameters, not one!

For example:


And you are receiving an int so you cannot invoke then again add(int, int) for that object as int has no method add, so this:



is not correct, pass both c and d (if you want to sum them) to the method add.

Hope this helps you!
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ben Hultin wrote:well I tried the suggestion you made and it didnt seem to help, I still get the same errors.

the compiler is complaining about not being able to use add(int,int), would the add and multiply method need to have string parameters seeing as the values are coming from objects? Just a thought.

Thanks a lot for your help


That was just an example, not what you needed to put in your code. You would need to change the 2 ints to whatever your input types are. I highly recommend reading this regarding method arguments. It should help you.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
well as for the code:




That was given by the proffessor as to make it work with my driven class and its methods within.

I read the article you mentioned and some other ones as well, that may help, but I culdnt find one that covered this particular problem of referencing an objects parameter, not arguments which is what their examples showed.

what I getting from the code so far, is that the teacher is trying to add first c to x, then add d to x as well.



I am sure this was rather obvious, but I find the long hand version helps to reduce confusion as to the purpose of the code.

c is holding two ints inside of it, so is d. So I figure the add() and multiply() methods would working with two ints found inside of each object individually then assign the result to x, which will be printed.

That is what I believe, I may be very wrong about this.

I appreciate the help in the matter
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ben Hultin wrote:well as for the code:




That was given by the proffessor as to make it work with my driven class and its methods within.

I read the article you mentioned and some other ones as well, that may help, but I culdnt find one that covered this particular problem of referencing an objects parameter, not arguments which is what their examples showed.

what I getting from the code so far, is that the teacher is trying to add first c to x, then add d to x as well.



I am sure this was rather obvious, but I find the long hand version helps to reduce confusion as to the purpose of the code.

c is holding two ints inside of it, so is d. So I figure the add() and multiply() methods would working with two ints found inside of each object individually then assign the result to x, which will be printed.

That is what I believe, I may be very wrong about this.

I appreciate the help in the matter


Those two object might be contain 2 ints each, but you aren't giving the ints to the method. You are passing a reference to the c object and a reference to the d object. You can then access the ints in those objects once they have been passed into the methods, but your method signature would need to be written to accept Fraction objects rather than ints.

Alternatively, you could pass in the ints from those object directly using the dot (.) operator. Something like x.add(c.numberator), although you have your variables set to private so you would probably need setter and getter methods (which are a good idea in general, actually).
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
So to get the method signature to accept objects and not ints that would be something like this?



or would it be something like this?



I dont see why the second would work seeing as the object is holding ints and not Strings.

I appreciate the help
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ben Hultin wrote:So to get the method signature to accept objects and not ints that would be something like this?



or would it be something like this?



I dont see why the second would work seeing as the object is holding ints and not Strings.

I appreciate the help


Ok, I'm going to break this down a little bit:

Method signature: public String (int a);
----public is the access modifier. This means anyone can call this method.
---String - This is the method's return type. If your method had a return statement, it would have to return a string
---(int a) - this is the list of arguments to this particular method. This particular one accepts a single int argument.
Now you can delcare an argument as an object, like: public void (String a), or public void (Car a), or in your case, public void(Fraction c), which would accept a Fraction object. If you declare that a method takes an int argument, you can't pass an object that happens to have an int within it. You have to pass the object reference then use that to get your int within the method, or declare the method to take an int and just pass in the int.

Here is a code example to demonstrate:

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot for your help, I made the changes as you mentioned, and thanks for showing me how to pass an object into a method as a parameter. Nothing I read covered that particular problem. While I am no longer getting errors about those methods I am gettting a problem on the calling end.





HultinBenProg7.java:23: int cannot be dereferenced
x.add(c).add(d);
^
HultinBenProg7.java:30: int cannot be dereferenced
x.multiply(c).multiply(d);
^
2 errors






Here is my updated code:



and the calling objects are the same.

Thanks a lot for your help


W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Ok, I'm not 100% sure what is happening, but I think I have an idea.

When you are calling x.add(c).add(d); the first thing that is happening is x.add(c), which returns an int. from there, you are basically receiving and int value, say 42, and calling an add method on it, so it would look like 42.add(d). You can't call methods on primitives because they don't have methods.

I suggest trying breaking it up, so you have x.add(c); x.add(d);.

Again, there may be a better/different way that I am not aware of, since I am still learning Java myself, but I think that is causing your problem.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot for all your help, I made the changes and java seemed to like it. Now its asking for a return statment on my print methods... I thought the System.out.print() would be the return statement, but apparenlty not so.





.\Fraction.java:37: missing return statement
}
^
.\Fraction.java:47: missing return statement
}
^
2 errors


// second compile

G:\Documents and Settings\ben\Desktop\Desktop\Desktop\Ben\park>javac HultinBenPr
og7.java
.\Fraction.java:35: incompatible types
found : void
required: int
return System.out.println(this.numerator + " / " + this.denomina
tor);
^
.\Fraction.java:43: incompatible types
found : void
required: int
return System.out.println(this.quotient);
^
2 errors

G:\Documents and Settings\ben\Desktop\Desktop\Desktop\Ben\park>





after a couple tries of ideas, I am left with the errors above and the code below.



The odd thing is none of the entire Fraction class has been declared with void. Obviously I am missing something here.

Thanks in advance
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

if you want to return something, you should return just a double..... or a String......

Like this:



Is that making sense? The return type that you declare needs to match what it is that follows the return statement.

Janeice
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot for your help, I got rid of the errors, but now I am stuck on this problem with copying a value from one object to another where both are declared in the driver class but manipulated in the driven class. The teacher has this setup that I need to make work in the driven class.





What he wants is to add the two values in c and copy them to x. Then add the values in d and copy them also to x. Then print out the results of x.

The code I have for my driven methods are:



Thanks a lot for the help
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

You are SO close!

1. Uncomment the first 2 lines of the method
2. Get rid of the statement after the return and the System.out.println statement (the second part is optional... I keep println statements until I'm ready to hand something in... it's a crutch for me)
3. Change the return statement to something that makes sense based on our last few posts about return types. Hint: Think about what it is that you're calculating and what the ANSWER is.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Are you refering to something like this? I made it a double bc quotient is a double type thats being returned, is that correct?





The other question I have is once I have the value assigned to quotient... how would I assign that value to object x in my driver class from this driven class method?

Thanks a lot for the help
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

Ben Hultin wrote:Are you refering to something like this? I made it a double bc quotient is a double type thats being returned, is that correct?


If quotient was declared as a double, you need to return a double. If it was declared as an int and you want to return a double you need to cast it.

Ben Hultin wrote:
The other question I have is once I have the value assigned to quotient... how would I assign that value to object x in my driver class from this driven class method?

Thanks a lot for the help


I'm not understanding what you're doing here.... the value will be accessed like so:



I'm just not sure you're understanding how this is working, nuts and bolts wise. I think maybe that's why you're getting confused.

Next time you reply, it would probably be helpful to post what you have so far (now that it's changed), and what the application is/should be doing (in real people words, not programming words). That may help you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor problems