• Post Reply Bookmark Topic Watch Topic
  • New Topic

More methods  RSS feed

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't understand methods/objects, apparently.

Problem:
Complete the class method 'analyze' that takes a SimplePublicPair object as an argument
and returns a new SimplePublicTriple object.
The SimplePublicTriple needs to set up as follows:
x = the minimum value of 'a' and 'b'
y = the maximum value of 'a' and 'b'
description:a*b=M
where a,b, and M are replaced with the numerical values of a, b and the multiplication of a and b.
Your code will create a SimplePublicTriple, initializes the three fields and return a reference to the SimplePublicTriple object.

Given:


I just don't even know how to start this. I'm absolutely without any hope. I'd normally ask for a TA's help to get started but it is spring break and no one is available. Usually when I'm here I have an idea how to approach my problem but lack something simple. Here I just have no clue.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We could help you better if we knew something more about the internals of SimplePublicPair and SimplePublicTriple.
But normally, you shoud just take a and b from the SimplePublicPair object, multiply them, and store the result somewhere. Afterwards, you pass this three variables into the constructor for SimplePublicTriple. If it has only the default constructor, or none that takes three arguments, perhaps there are setters.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose your object SimplePublicTriple having three fields a , b and M and SimplePublicPair have 2 fields a and b.

public static SimplePublicTriple analyze(SimplePublicPair in) {



x = in.a > in.b ? in.b : in.a ;
y = in.a < in.b ? in.a : in.b;

M = s.x * s.y ;

SimplePublicTriple s = new SimplePublicTriple(x, y, M);
}
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree with both of you. The triple class should know nothing about the internals of pair, nor about its fields. It should use the externals of pair, i.e. is public interface. That way you are preserving encapsulation, that triple knows nothing about the private fields of pair, and single responsibility, that pair takes care of all its own business rather than letting classes using it do such work.

You say you want the smaller and larger of the pair. Right: the pair class should have a smaller method and a larger method. You could call them min and max which appear to be the programming tradition for such methods, as you will see in the Math class.
The description of the static method which returns a triple object looks like s factory method. It should be a public static method in the triple class, and return a triple object. You have already been told you require three fields in the triple object and what to call them, though it should really be m not M. Start by creating a triple class with a constructor and add that factory method later. Give the triple class a toString() method so you can try it out.Add lots more examples to the testTriples method and you can see whether it works.

There is an ambiguity in the assignment. Does it mean that every triple will have (m == x * y) or does that equality only apply in the factory method?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Campbell I am sorry but i still dont understand. Would you mind to explain in simpler terms?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:I really don't understand methods/objects, apparently.

Actually, I suspect that you're trying to do too much at once.

Start with the SimplePublicTriple class, and forget about everything else.

What do you think it should look like? Just write a skeleton (ie, its field(s) and constructor) for the moment and show it to us.

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Josh Galeigh wrote:I really don't understand methods/objects, apparently.

Actually, I suspect that you're trying to do too much at once.

Start with the SimplePublicTriple class, and forget about everything else.

What do you think it should look like? Just write a skeleton (ie, its field(s) and constructor) for the moment and show it to us.

Winston




I know I set it up as a method. As I read it I'm getting 2 values a and b and setting them as x,y and a*b=M.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:I know I set it up as a method. As I read it I'm getting 2 values a and b and setting them as x,y and a*b=M.

Looks basically OK to me, except that any class in your package can UPDATE those fields and your constructor returns void. How about:
It's not ideal (private fields and "getters" are better), but it'll probably do for what you want.

Indeed, if you created a SimplePublicPair class with getters like this:
you could simply extend it to create your "triple" class so:I'm not saying it's the best way to do it, but making fields publicly visible is generally NOT a good thing.

A few other things:
1. Don't scrunch all your code up. It's not a competition to see how small you can make it; and it's VERY hard to read.
2. Don't declare multiple variables on a single line.
3. Don't use cryptic names. How is any reader supposed to know what 'M' (or 'm' - which is what it should be) means?

HIH

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't make a second class, though. If I try to put it inside my UsingPublicFieldsIsEasy I have to set x,y,xTimesy=0 or I get a compiler error. But then And I can't modify x or y at all anymore since it is set to 0.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:I can't make a second class, though. If I try to put it inside my UsingPublicFieldsIsEasy I have to set x,y,xTimesy=0 or I get a compiler error. But then And I can't modify x or y at all anymore since it is set to 0.

First: Why are you trying to modify them outside a constructor? Is this a requirement you haven't told us about?

Second: Your "constructor" is NOT a constructor because it returns a value (void); and I suspect that is where you're getting the error.

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of the information I have for the problem is at the top. I get the cannot change x,y.. error if I don't have 'int' in the analyze method line:



The auto-test makes the simplepublicpiar, I think. Here is what it uses to check my answers (we're given this code as help):



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:All of the information I have for the problem is at the top.

Josh, you need to read the responses you've been given.

Where did I say that you should add '= 0' to a final field? That will ALWAYS cause problems, unless 0 really is the value you want (which seems unlikely).

My advice: I think you're a bit too close to this problem at the moment. Chill out. If you're old enough, go and have a pint; sleep on it; and come back to it tomorrow; because simply hammering code at it ain't going to get it fixed. You need to think.

However, if the "given" in your opening post is ALL you were given, then you definitely need SimplePublicTriple and SimplePublicPair classes - and I've seen no sign of them so far.

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I added =0 while trying to figure out why I was getting errors. I just forgot to take it back out. Think I'll switch over to another problem for a bit and work on that instead. Perhaps going out and purchasing some beer my let me relax a bit. I'm pretty stressed over this particular problem because I know it represents all the things I don't understand about objects and methods. And the fact that the instructor thought to put the word "easy" in there... man... drives me nuts.

I don't even know if I'm putting the classes in the right spot. I mean I *think* that triple and pair classes should just be subclasses of UsingPublicFieldsIsEasy but I don't even know, yet, what should be in here:
public static SimplePublicTriple analyze(SimplePublicPair in) {...}





 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ahve two public classes, you should put each into its own XXX.java file.

I am getting confused here. I though the factory method would take the smaller and the larger values from the pair object, and create a new triplet object from them. It would appear to me there is no need to sort them in the constructor. Or is there a new requirement that
∀ₚ •x ≤ y
which I have missed?

If there is no such requirement, I would write the pair class like thisGet that working and test it as I showed earlier, then consider adding the factory method. Note that my class will permit a pair (123, 456) and a pair (987, 654).
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was positive that I would not have to make two new java files because of the way the autograder works. So I checked SVN again and found them ALREADY THERE.

Triple:



Pair



But, if I'm honest I'm still not sure how to solve this problem.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:So I checked SVN again and found them ALREADY THERE...

Aha! So I suspect that the solution also involves updating the "triple" class (or creating a subclass of it that does what you want).

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Aha! So I suspect that the solution also involves updating the "triple" class (or creating a subclass of it that does what you want).

Winston


He specifically says not to update those classes in the notes.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:He specifically says not to update those classes in the notes.

So: is creating a subclass "updating"?

Winston
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I create a subclass of triple inside the publicfields?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that is an advanced feature, which you should not use at this stage in your career.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I can't make a subclass. He intends that I not modify those classes at all.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically what I need to do is:

But I can't just cast it like that. I don't know how to handle the public pair 'in.' It gives me an object that I understand has 2 values. Somehow I have to extract their values identify which is larger and cast them into triple.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make a subclass in a different file. That is easy enough.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can make a subclass in a different file. That is easy enough.


For what purpose? Also, I really, really don't think the idea is to create a new file. This is suppose to be easy but I have no clue on how to do it.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote:He specifically says not to update those classes in the notes.


So... does that mean you the programmer aren't supposed to modify the source code of those classes, or does it mean that your code isn't supposed to modify instances of the classes? I'm guessing it's the former, because the latter would be implemented via declaring its variables final. But you might want to hunt down the TA and clarify that.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the top of both classes (pair and triple) it says:

/** A pair of public integers - the simplest kind of object
* Normally we encapsulate the data and make it private.
* Do not modify this file.
*/

and

* A very simple data object where the data fields are public.
* (Usually, we encapsulate the data fields and make them private.)
* Do not modify this file.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So you can write code which modifies instances of those classes. In earlier posts you didn't seem to think that was okay.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Okay. So you can write code which modifies instances of those classes. In earlier posts you didn't seem to think that was okay.


That's probably because I am completely lost with this.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is sort of where I'm stuck at now.

I know I need to create a new triple. And given it certain values from pair. But, again, I'm at a loss on how to do this.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you remove that static field. If you are returning it from a factory method, that should be a local variable to that method.
As for x, y, m in that method, you need to get or calculate them from the pair object and insert them into the triplet object.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As for x, y, m in that method, you need to get or calculate them from the pair object and insert them into the triplet object.


I do not know how to do this, which is my problem.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do what I told you earlier. Forget about calculating them. Get a triplet class working. Then go back and calculate them. I seem to remember there was some code earlier in this thread which would have calculated them from the values in the pair class.

I think the design of a class which has public non‑final fields breaches some of the principles of object‑oriented programming. Fields should be private and accessed via getXXX methods. I still think that min and max methods, as I said earlier, would be a better way to get those values, but you appear not to be able to create them.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already have the triple class. It was given to me. It just holds 2 int values and a string. I wish I had examples of things like this. I can't find anything online to guide me through this at all. I don't even know what other new questions I can ask to figure this out.

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
triple.x = 42


the 42 is the part I really dont understand. So, 'in' carries the value of both a and b. I know triple.x needs to be the smaller of the two values. And the pair class has a boolean method that can tell me if a or b is larger.

So

obviously I don't know what www,bbb,mmm,nnn are suppose to be.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally got it. All I needed to figure this out was to know I could call pair values with "in.a" and "in.b"... I've been working on this since last night...

At least I know now.

Thanks for help!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Galeigh wrote: . . . It just holds 2 int values and a string. . . .
A String??? You never said anything about Strings earlier. You said m = x × y, which is a number, not a String.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Please show us what you have got, if you are permitted to show it.
 
Josh Galeigh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't mention the string value explicitly but the triple class description variable is a string. So I had to change x*y into a string using valueOf. I probably shouldn't put the whole answer up but I'll say that I was really having a problem trying to figure out how I was suppose to get the values from Pair. I simply didn't know I could say "in.a" or "in.b". I didn't realize that I'd be able to use a or b since they weren't declared in the class I was in. I knew they were what the pair class used but I didn't know I could use them like that.

A piece:
It ended up being about 1000000% simpler than I ever realized.

There also ended up being formatting issues for my output so I had to add an if statement for the string output. As the string always had the smaller number written first.

Thanks for the help.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!