Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Joushva Kamble
855 PointsQuestion about my lab involving java coding
This is what the lab part is asking to do:
Run part4.Driver. This time output IS produced. There are already method calls written into the constructor. This time things are a little more challenging. You might have noticed that in the earlier part each of the check methods could be run independently of the others because new Zoe, Mal and Wash objects were created in these methods and the getQuote() method was called as many times as needed to get the relevant value.
In part 4, however, the calls are dependent on one another. You cannot check the correct functioning of checkSecondQuote without checkFirstQuote having been called first. Likewise you cannot verify that checkThirdQuote is working correctly without both checkFirstQuote and checkSecondQuote having been called, in that order. Edit the part4.Zoe, part4.Mal and part4.Wash classes to make the output correct.
This is what the console has on Eclipse (IDE):
I expected to get: Wash: Psychic, though? That sounds like something out of science fiction. Zoe: We live in a spaceship, dear. What I got was: Sweetie, we're crooks. If everything were right, we'd be in jail. Zoe: I know something ain't right.
I expected to get: Zoe: I know something ain't right. Wash: Sweetie, we're crooks. If everything were right, we'd be in jail. What I got was: Zoe: I know something ain't right. Sweetie, we're crooks. If everything were right, we'd be in jail.
I expected to get: Zoe: Sir, we don't want to deal with Patience again. Mal: Why not? Zoe: She shot you. Mal: Well, yeah, she did a bit. What I got was: Zoe: I know something ain't right. Mal: Why not? Zoe: I know something ain't right. Mal: Why not?
The Lab5.java window has the code:
package part4;
public class Lab5 {
private Zoe _z;
private Mal _m;
private Wash _w;
public Lab5() {
_z = new Zoe();
_m = new Mal();
_w = new Wash();
checkFirstQuote();
System.out.println("\n----\n");
checkSecondQuote();
System.out.println("\n----\n");
checkThirdQuote();
}
public void checkFirstQuote() {
System.out.println("I expected to get:");
System.out.println(" Wash: Psychic, though? That sounds like something out of science fiction.");
System.out.println(" Zoe: We live in a spaceship, dear.");
System.out.println("What I got was: ");
System.out.println(" " + _w.getQuote());
System.out.println(" " + _z.getQuote());
}
public void checkSecondQuote() {
System.out.println("I expected to get:");
System.out.println(" Zoe: I know something ain't right.");
System.out.println(" Wash: Sweetie, we're crooks. If everything were right, we'd be in jail.");
System.out.println("What I got was: ");
System.out.println(" " + _z.getQuote());
System.out.println(" " + _w.getQuote());
}
public void checkThirdQuote() {
System.out.println("I expected to get:");
System.out.println(" Zoe: Sir, we don't want to deal with Patience again.");
System.out.println(" Mal: Why not?");
System.out.println(" Zoe: She shot you.");
System.out.println(" Mal: Well, yeah, she did a bit.");
System.out.println("What I got was: ");
System.out.println(" " + _z.getQuote());
System.out.println(" " + _m.getQuote());
System.out.println(" " + _z.getQuote());
System.out.println(" " + _m.getQuote());
}
public void checkFourthQuote() {
System.out.println("I expected to get:");
System.out.println(" Zoe: Planet's coming up a mite fast.");
System.out.println(" Wash: That's just 'cause - I'm going down too quick. Likely crash and kill us all.");
System.out.println(" Mal: Well, that happens, let me know.");
System.out.println("What I got was: ");
System.out.println(" " + _z.getQuote());
System.out.println(" " + _w.getQuote());
System.out.println(" " + _m.getQuote());
}
}
The code that is given for Zoe.java is:
package part4;
public class Zoe { private String _quote1; private String _quote2; private String _quote3; private String _quote4; private String _quote5;
public Zoe() {
_quote1 = "We live in a spaceship, dear."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote2 = "I know something ain't right."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote3 = "Sir, we don't want to deal with Patience again."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote4 = "She shot you."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote5 = "Planet's coming up a mite fast."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
}
public String getQuote() {
rotateQuotes();
return "Zoe: " + _quote1;
}
private void rotateQuotes() {
_quote1 = _quote2;
_quote2 = _quote1;
}
}
the code that is given for mal is :
package part4;
public class Mal { private String _quote0; private String _quote1; private String _quote2;
public Mal() {
_quote0 = "Why not?"; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote1 = "Well, yeah, she did a bit."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote1 = "Well, that happens, let me know."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
}
public String getQuote() {
rotateQuotes();
return "Mal: " + _quote0;
}
private void rotateQuotes() {
String temp = _quote0;
_quote1 = _quote2;
_quote2 = temp;
}
}
the code that is given for wash is :
package part4;
public class Wash { private String _quote1; private String _quote2; private String _quote3;
public Wash() {
_quote1 = "Psychic, though? That sounds like something out of science fiction."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote2 = "Sweetie, we're crooks. If everything were right, we'd be in jail."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
_quote3 = "That's just 'cause - I'm going down too quick. Likely crash and kill us all."; // https://en.wikiquote.org/wiki/Firefly_(TV_series)
rotateQuotes();
}
public String getQuote() {
return _quote1;
}
private void rotateQuotes() {
String temp = _quote1;
_quote1 = _quote2;
_quote2 = _quote3;
_quote3 = temp;
}
}