For each of the following, suppose that isWalking is true and isTalking is false (first draw a flowchart for each statement and then determine what would be printed by each statement):
Write a boolean method—a method that returns a boolean—that takes an int parameter and converts the integers 0 and 1 into false and true, respectively.
Define a void method named hello that takes a single boolean parameter. The method should print “Hello” if its parameter is true; otherwise, it should print “Goodbye”.
Define a method named hello that takes a single boolean parameter. The method should return “Hello” if its parameter is true; otherwise it should return “Goodbye”. Note the difference between this method and the one in the previous exercise. This one returns a String. That one was a void method.
Write a method named hello that takes a single String parameter. The method should return a String that consists of the word “Hello” concatenated with the value of its parameter. For example, if you call this method with the expression hello("dolly"), it should return “hello dolly”. If you call it with hello("young lovers wherever you are"), it should return “hello young lovers wherever you are”.
Write a Java application program called TwelveDays that prints the Christmas carol “Twelve Days of Christmas.” For this version, write a void method named intro() that takes a single String parameter that gives the day of the verse and prints the intro to the song. For example, intro("first") should print, “On the first day of Christmas my true love gave to me”. Then write methods day1(), day2(), and so on, each of which prints its version of the verse. Then write a main() method that calls the other methods to print the whole song.
Define a void method named verse that takes two String parameters and returns a verse of the Christmas carol “Twelve Days of Christmas.” For example, if you call this method with verse("first", "a partridge in a pear tree"), it should return, “On the first day of Christmas my true love gave to me, a partridge in a pear tree”.
Define a void method named permute, which takes three String parameters and prints out all possible arrangements of the three strings. For example, if you called permute("a", "b", "c"), it would produce the following output: abc, acb, bac, bca, cab, cba, with each permutation on a separate line.
Design a method that can produce limericks given a bunch of rhyming words. That is, create a limerick template that will take any five words or phrases and produce a limerick. For example, if you call
There once a person named Jones
Who had a great liking for stones,
But whenever it rained,
Jones' expression was pained,
Because stones weren't good for the bones.
Define a class named Donor that has two instance variables, the donor’s name and rating, both of which are String s. The name can be any string, but the rating should be one of the following values: “high,” “medium,” or “none.” Write the following methods for this class: a constructor, Donor(String,String), that allows you to set both the donor’s name and rating; and access methods to get both the name and rating of a donor. Write a complete Java application program.
Challenge. Define a CopyMonitor class that solves the following problem. A company needs a monitor program to keep track of when a particular copy machine needs service. The device has two important (boolean) variables: its toner level(too low or not)and whether it has printed more than 100,000 pages since its last servicing (it either has or has not). The servicing rule that the company uses is that service is needed when either 100,000 pages have been printed or the toner is too low. Your program should contain a method that reports either “service needed” or “service not needed” based on the machine’s state. (Pretend that the machine has other methods that keep track of toner level and page count.) Write a complete Java application program.
Challenge. Design and write an OldMacdonald class that sings several verses of “Old MacDonald Had a Farm.” Use methods to generalize the verses. For example, write a method named eieio() to “sing” the “E I E I O” part of the verse. Write another method with the signature hadAnX(String s), which sings the “had a duck” part of the verse, and a method withA(String sound) to sing the “with a quack quack here” part of the verse. Test your class by writing a main() method. Write a complete Java application program.
Suppose you have an Object A, with public methods a(), b(), and private method c(). And suppose you have a subclass of A named B with methods named b(), c() and d(). Draw a UML diagram showing the relationship between these two classes. Explain the inheritance relationships between them and identify those methods that would be considered polymorphic.
Consider the definition of the class C. Define a subclass of C named B that overrides method m1() so that it returns the difference between m and n instead of their sum.
public class C {
private int m;
private int n;
public C(int mIn, int nIn) {
m = mIn;
n = nIn;
}
public int m1() {
return m+n;
}
}