CS代写 Example sheet 2 – cscodehelp代写

Example sheet 2
1. Consider the following method, which is a simple version of a childrens’ game (“Guess Who”) for animals. It allows you to identify an animal by considering how many legs it has and whether it has fins or feathers, can fly, moos or brays. We assume that the only possible animals are: human, goldfish, horse, duck, snake, cow, ostrich, donkey.
public​ ​static​ String identify(​int​ l​ egs​, ​boolean​ ​fins​, ​boolean feathers​, ​boolean​ ​flies​,b​ oolean​ ​moos​, ​boolean​ ​brays​){
if​(​legs​==0){

Copyright By cscodehelp代写 加微信 cscodehelp

if​(​fins​) ​return​ ​”goldfish”​;
else​ ​return​ ​”snake”​;
if​(​legs​ == 2){ if​(​feathers​){
if​(​flies​) ​return​ ​”duck”​; else​ ​return​ ​”ostrich”​;
else​ ​return​ ​”human”​;
if​(​legs​ == 4){
if​(​moos​) ​return​ ​”cow”​;
if​(​brays​) ​return​ ​”donkey”​;
else​ ​return​ ​”horse”​; else​ ​return​ ​”octopus”​;
(a) ​What would be output if I include the following in my main method? ​String ​s1​ = identify(2,​false​,​true​,​true​,​false​,​false​);
System.​out​.println(​”The animal is a “​ + ​s1​);
String ​s2​ = identify(0, ​false​,​false​,​false​,​false​,​false​); System.​out​.println(​”The animal is a “​ + ​s2​);

​(b) What statements should I add to my main method to produce the output: ​The animal is a human
2. ​This question is based on a paper “fortune teller” (like those often made by small children). Read the following method and then answer the questions below. I’ve added numbers so that I can refer to lines of the code.
18. 19. 20.
21. 22. 23.
public​ ​static​ String fortuneTeller(Scanner m​ yScanner​){ System.​out​.println(“​ Pick a number!”​);
​int​ ​n​ = ​myScanner​.nextInt();
​myScanner​.nextLine(); ​// remember why we do this? ​if​(​n​%2==0){
System.​out​.println(​”Pick one of the colours: red, blue, green, yellow”​);
String ​colour​ = ​myScanner​.nextLine().trim();
​if​(​colour.​ equals(​”red”​)) ​return​ ​”You will be very rich!”​; ​if​(​colour.​ equals(​”blue”​)) ​return​ ​”You will have eternal
happiness!”​;
​if​(​colour.​ equals(​”green”​)) ​return​ ​”Your children will be
strong!”​;
​if​(​colour.​ equals(​”yellow”​)) ​return​ ​”Your children will be
​return​ ​”You made a mistake!”;​
System.​out​.println(​”Pick one of the colours: pink, purple, black, white”​);
String ​colour​ = ​myScanner​.nextLine().trim(); ​if​(​colour.​ equals(​”pink”​)) ​return​ ​”You will have a big
​if​(​colour.​ equals(​”purple”​)) ​return​ ​”You will live to be
very old!”​;
​if​(​colour.​ equals(​”black”​)) ​return​ ​”You will never be
Lonely!”​;
​if​(​colour.​ equals(​”white”​)) ​return​ ​”You will never be
Hungry!”​;
​return​ ​”You made a mistake!”;​

(a) Notice that the input parameter has type ​Scanner​. How would you call the method from the ​main​ method?
(b) Why do we need the extra ​nextLine()​ statement on line 4?
(c) What is the condition on line 5 asking?
(d) What do you think the purpose of “​trim()​” is on lines 7 and 16?
(e) How would you print the output from the method to the screen?
(f) If the method was called and a user input the number 15, followed by the colour “pink”, what would the output string be?
(g) If instead the method was called and a user input the number 16, followed by the colour “pink”, what would the output string be?
(h) Give an example of what the user should input in order to receive the fortune “You will never be lonely”?
(i) What would happen if I replaced the code at line 20 with the following (then input the number 11, followed by “white”)?:
if​(​colour​==​”white”​) ​return​ ​”You will never be hungry!”​;
(j) Write a new​ fortuneTeller ​method that has the same name but different signature. It should take no parameters and create the scanner inside the method before calling the original version (of the method).

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

Leave a Reply

Your email address will not be published. Required fields are marked *