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:
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!
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.
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!
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.
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!
That's a pleasure
Kevin Ramer wrote:Thank you both . . .
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.
The default constructor . . .
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.
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.
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());
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!
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.
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.
Kevin Ramer wrote:I'm guessing both... . . . .
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.
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.
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.
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.
Kevin Ramer wrote:. . . . get it to split and keep a bit of b and create a c..
But you have now; you have just written it on your paper.
I have no clue how to do that. . . .
That's a pleasure
Thank you both very much for your time
Pass the assessment, then think about changing.
but I'm beginning to think I should change my major.