importjava.util.Scanner; public class TruffleDriver { public static void main(St
ID: 3615494 • Letter: I
Question
importjava.util.Scanner;
public class TruffleDriver
{
public static void main(String[] args)
{
int[] count;
count = new int[4];
for (int index = 0; index<4; index++)
{
{
Stringfilling = ""; // truffle fields I'll need from user
Stringcoating = "";
booleananswer;
TruffleMyTruffle;
Scannerscan = new Scanner(System.in); // set up scanner objectcalled scan for user input
// get user input for the properties of aTruffle
System.out.print("Enter Coating: ");
coating =scan.nextLine();
System.out.print("Enter Filling: ");
filling =scan.nextLine();
System.out.print("Storable at Room Temperature? (true orfalse): ");
answer =scan.nextBoolean();
and the Truffle Class to gowith it is:
Explanation / Answer
Ok, it looks like you need some help understanding the differencebetween the reference to an object and the object itself. This isunderstandable. It's a very important distinction but a subtle oneand one that's especially hard to understandbecause the stupid things have the same name! It is one of the moredifficult but fundamental concepts of object oriented programs. Letme see if I can help explain to you exactly what's going onhere. I'm going to start with a simpler example for your edification. Let's use your truffle object. Truffle myTruffle = new Truffle ("Peanut Butter","Chocolate", false); //What you need to understand here is that there are 2 separateparts to this situation. There is the new truffle object you'vecreated, using the new keyword, with Peanut Butterfilling, and then there is the "myTruffle" name which is simply areference to that truffle. Imagine it as a giant finger pointing atthe truffle you've created. Truffle myTruffle2; //What we have here is ONLY a reference, without an object to pointto. Imagine it as a giant finger pointing nowhere. It doesn'tresolve to anything yet, and if you tried to do something likemyTruffle2.getCoating() your system would throw aNullPointerException because it doesn't point anywhere; that is,there is no object that you would be getting the coating valuefor. myTruffle2 = myTruffle; //Now we have made a second reference to the first truffle object!Imagine both myTruffle and myTruffle2 as giant fingers pointing atthat one Peanut Butter filled truffle you made. There is only 1actual truffle here...but you have made 2 names for it! myTruffle2.setFilling("Sawdust"); //Now you see we have set the filling of the truffle toSawdust. myTruffle.getFilling(); //Believe it or not, thiswill return Sawdust! There was only one truffle object and you usedthe myTruffle2 name to reference that object, and now it has a newfilling called sawdust myTruffle = new Truffle("Cotton", "ball bearings",false); //Now there are 2 truffle objects! When we called new Truffle here,we made a second truffle object. We also took the giant pointingfinger called myTruffle and pointed it at a new truffle! Keep inmind, though, that myTruffle2 still points to the OLD truffleobject we created. myTruffle2.getFilling(); //This will return"Sawdust" because it still points to that first truffle we createdand changed the name to sawdust myTruffle.getFilling(); //This will return "ball bearings" becauseit points to that new, second truffle we just created. myTruffle2 = null; //Now we have just taken thepointing finger reference known as myTruffle2 and pointed it atnothing. null is what java uses as the value a reference has whenit points to nothing. If you want to make a reference point tonothing, you set it to null; if a reference hasn't been pointed toanything yet, it will be set to null. Truffle myTruffle3; //Here myTruffle3 == nullbecause nobody's set it to anything There are now no fingers left pointing at our original truffle withthe chocolate coating. It is effectively gone from the world, andjava's internal memory garbage collection will throw away theobject since there's nothing pointing at it anymore (and thus noway we could ever use it again!). Now, when you make an array of truffles, you are actually making anarray of REFERENCES (those big pointing fingers). You still canonly make objects by using the "new" keyword. When you say Truffle[] myTruffle = new Truffle[4];what you are actually doing is creating an array of TruffleREFERENCES. It may look like you've created objects because thenew keyword was used but that was actually beingused to create the array, not truffles. It can be confusing becauseyou use new both to create an array and alsoto create the objects, but here it just made the array. You stillneed to make actual truffle objects for each of those references topoint to. You are correctly creating the truffle in each for loop iteration,but you can't just print out a truffle by printing the myTruffleobject as that is the array -- not a specific truffle. To find eachof the four truffles you need to look at myTruffle[0] (the firsttruffle you created) through myTruffle[3] (the last truffle youcreated). You need to not print anything in that first for loop you made andthen make a second for loop that loops i through the integers 0 - 3and prints like // System.out.println("Your Truffle is:" + MyTruffle[i]); //hintthis isn't going to work now need something different Hope this is helpful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.