• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with homework assignment  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my homework this week I am supposed to create a class to use with a class Driver that has already been written and we aren't allowed to mess with it. With that being said... I can't seem to figure out how to write most of the methods that we are supposed to do.  Starting to think I'm not cut out to do this.  Anyways, here is what we are being asked to do:

1. The toString method returns a String that contains all the information on the current object (this) with labels. The following is an example of the string that must be returned by this method (without the double quotes, that are added here just for reference):
        [National Business Machines]
        Stock Ticker: NBM
                 # of Employees: 1000
                 Net Worth: 1000000.0

2. The grow method takes no parameters. It returns the current Business object (this), after modifying its attributes. The header will be:

public Business grow()

Inside this method, the number of employees and the net worth of the current Business will be increased by 10%. This will be done by getting the number of employees and net worth of the current object (using the this.get methods), multiplying them by 1.1, and setting these new values back in the instance variables of the current object using the set methods (this.set). Be mindful that the number of employees is an integer and need to be typecasted to (int) after the multiplication by the decimal 1.1. When all these calculations are done, the method returns the current object with the following sentence:

return this;


I currently have gotten this far:




Basically I have hit a wall and have no idea what to do... I have tried everything I can think of or have learned from taking this online so far to no avail.  In the grow class, its already a String class so doing math with Int and Doubles is just giving me errors.  I have tried to alter them in the only way I know how with Integer.parseInt and Double.parseDouble and it says I can't change it.  If I can figure out how to get the String method to let me math... I think I can figure this out.. but so far no luck.  This is only 2 of the 5 problems as I would like to try the rest on my own.  I think I got the first answer?  Second I am having zero luck.  Thank you in advance!

Kevin
 
Master Rancher
Posts: 2712
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kevin,

welcome to the Ranch and enjoy the stay!

Lets try to get you up and rolling. If you look at what the grow() method is supposed to do (see the instructions) is to increase both the netWorth and the number of Employees by 10%. Well, lets just do that.
So,

It really is that simple!


The same goes for setting a new Name and setting a new Ticker. Try it.

Now look at the takeOver() method.It means that the netWorth will be increased with the netWorth of the Business that is being taken over. To get that netWorth of that other Business, you must use that Business' method getNetWorth() method, just as for the new number of Employees. In Holland a Business that takes over some other Business is legally forced to also take over the other Business'employees (only to get rid of them asap).

You must ask your teacher what should be done in case of forming a joint venture. Usully it means that two businesses form a third one, each bringing in a part of their netWorth. What that means in the light of this exercise is not clear. The same holds for the method branchOut.

Well, I hope you have now some good ideas how to go on. Remember, when in doubt, ask your teacher first for the details and ask the parts that are still unclear in this forum. 'Till then, happy coding!
 
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KR, welcome to the Ranch

You have been told wrongly what a default constructor is; you never write a default constructor. Do you need that no‑arguments constructor? It will set your business with no employees and no worth. I think excess constructors are more trouble than they are worth, and I would suggest you delete it, unless the assessment instructs you to implement such a constructor.
 
Kevin Ramer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both,

Yes it is assessed work and if I was smart enough with java jargon I would have reworded the question but I'm not sure how to do that, also why I didn't ask for all 5 methods I am supposed to do.  Not trying to cheat, just can't get instructor help on the weekends and even if I email it takes 48 hours for response, but I was stuck.  The default constructor is part of the assignment so it has to be there, and I think shortly after posting this, I figured it out for the most part.. although not as tidy as the above code but I used:



any problems with that or just way overboard? Thank you both!

KR
 
Campbell Ritchie
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Ramer wrote:Thank you both . . .

That's a pleasure

The default constructor . . .

You haven't got a default constructor, as I told you earlier. You have a no‑arguments constructor, even though you will occasionally hear it called a nullary constructor. I don't like assessments being too prescriptive; sometimes you may be required to do some iffy coding, but there is no way out of that now.
Lines 3, 4, and 5 in your method don't do anything. The rest looks all right, but, in line 7, what will happen if you have < 5 employees? You will never increase its size.

Even computing teachers are human and need their weekends off.
 
Kevin Ramer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am having another issue that I've wasted hours on now... so in the driver, in the next bit of code it calls on object b that is already created and is working fine, then it calls on object c and creates object c = b.grow().branchOut("Branch of "+b.getName());.   The problem that I am having with this, is that for some reason.. it not only calls b, but changes both b and c and I don't know why???  I need to edit the contents of object c but everytime I do it changes object b to match it as well which, in every way I can think of, shouldn't happen?   B is only being called upon in order to create an object c so why is it changing the original b object?  And if that's the case and how it is supposed to work.. how the heck do I edit c then?  The only editing to b I am supposed to do is grow it again by 10% which I have done with success, but again.. it changes c to match.  I need to find a way in the "branchOut" method I am creating to edit just the numberOfEmployees and netWorth for b, then edit c's stockTicker, numberOfEmployees, and netWorth.   Sorry for asking so many questions but I feel reallly dumb right now.
 
Campbell Ritchie
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the code button, then your code would look like this. . . and be much easier to read.
That doesn't do anything to the object in reference c; au contraire, it replaces the contents of reference c with whatever the branchOut method returns when it is called on the contents of the reference b. Whatever was in reference c before (if anything) has now disappeared unless you kept another reference to it.
 
Kevin Ramer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok hopefully I have you for a second as this is due in a few hours unfortunately.. the driver that I cant change reads:


                    System.out.println("\nBusinesses b and c, after business b grows 10% and branch out to create c");
c = b.grow().branchOut("Branch of "+b.getName());
System.out.println("b:\n"+b);
System.out.println("c:\n"+c);

and it basically should print Business b (because it was already previously made) and create a clone-ish Business c.  I can't seem to get the branchOut method to just edit c, it just changes b completely to what c should look like.  Here's what I have:




Don't know if you can tell by what I have given you here, but I have to create a method called branchOut (title exaclty as it shows from instructor) and it should change some aspects of c and only the numberOfEmployees and netWorth of B.  Cant get it to do anything but change both b and c to what c should look like.  C is showing up correctly but b is now all wrong.  Help!
 
Campbell Ritchie
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the branch out method supposed to act on the current object or create a new object?
 
Kevin Ramer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing both... so it is supposed to take the numberOfEmployees and netWorth from b and change them from what they are now to:
numberOfEmployees = numberOfEmployees-(numberOfEmployees*.25);
netWorth = netWorth-(networth*.25);

but leave the rest of b the same, i.e. name, and stock ticker.

Then with the starting information from b, create object c which will have b's name + "Branch of" + b as well as a new stock ticker which you can see I have built already and new numbers for employees and networth which I have built.  Just now sure how to create the actual object and still have b.
 
Piet Souris
Master Rancher
Posts: 2712
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Piet, this is assessed work; please don't go writing complete methods.


I know, bur when OP is stuck, an example ohten performs better than a thousand words of explanation. I tried to be as minimalist as I could. Did I cross the line here? Well, I hope not.
 
Campbell Ritchie
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Ramer wrote:I'm guessing both... . . . .

Guessing is no good. You have to know. You should have instructions or a specification to tell you what that method ought to do. What you said suggests that “both” is correct. That is another bit of iffy coding: a method should do one thing and do it well, not two things. But you are stuck with that, it appears.
In which case, work out how a method will take ¼ of one company and create a second company object with that ¼.
Once you have worked that out, you will have to work out how to retain the correct number of references, so the old and new companies are both recorded in your code.
 
Piet Souris
Master Rancher
Posts: 2712
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP
This does not clearly tell what this 'brancheOut' is good for. If you want to branchOut of B, creating C on the fly, then I think you must first do B.grow, then let C be a new version of that grown B, and let C branchOut. The way you have it now does a branchOut of B, making a copy of it in C.
 
Kevin Ramer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree, but we were specifically told that we CANNOT edit the main driver class.... so all of that I am stuck with and just have to figure out how to get it to work.   I have been trying since about 2pm my time to get it to split and keep a bit of b and create a c.. I have no clue how to do that.  Honestly, I'm not the only one in my class that is utterly baffled at this assignment and I don't have the slightest on how to get this to work.  Thank you both very much for your time but I'm beginning to think I should change my major. 
 
Piet Souris
Master Rancher
Posts: 2712
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, yes.
Now, given that the meaning of this now famous branchOut is clear as mud:

the methods in the Business class constantly return a reference to its own instance (return this), so if you have c= b.grow().brancheOut(b.getName(); then first b.grow() is performed, returning a reference to the now grown b, then a brancheOut of this grown B is done, changing B and returning B's reference that is stored in C. So both B and C refer to the same grown branchedOut B.

My suggestion was to do just B.grow(); Then create a new Business by calling the constructor with input paramters of B (to be obtained via the getter metods). A reference to thst newly created Business is put in C. Now do a brancheOut of C, leaving B complletely unchanged.
 
Campbell Ritchie
Marshal
Posts: 59438
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Ramer wrote:. . . . get it to split and keep a bit of b and create a c..

Write down with paper and pencil how you would do that. Don't even look at a computer whilst doing that. Don't use words out of your program like b or c.

I have no clue how to do that. . . .

But you have now; you have just written it on your paper.

Thank you both very much for your time

That's a pleasure

but I'm beginning to think I should change my major. 

Pass the assessment, then think about changing.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!