Question: Question 1 Rome wasnt built in a day, and Facebook wasnt coded in the main method. We have been coding in the main method all
Question 1
Rome wasnt built in a day, and Facebook wasnt coded in the main method. We have been coding in the main method all this while because we did not want to inundate you with too many new things to deal with. Now its time to remove the safety wheels, and start doing things with methods. In this exercise you have to write a method called greet and return the String Hello Rome from it. What we expect: When we create an instance of Rome and invoke the greet method, we expect to get back the String Hello Rome. Learning outcomes: After completing this exercise, you should have learned how to write a method, and how to return an object from the method.
------------------------------
public class Rome { //Note for programr - You need to remove this method entirely, since //the student needs to implement the method and it's contents ///{ //write your code here //start
//end ///} public static void main(String args[]) { Rome rome = new Rome(); String greeting = rome.greet(); System.out.println("Output: "); System.out.println(greeting); } }
-----------
Question 2
You are stranded in the Amazon rain forests with a robot who calls himself FizzBuzz. The robot is pretty cool because it does all the cleaning for you, but it has an annoying habit. It gives you random numbers and expects you to say Fizz if the number is divisible by 3, or Buzz if the number is divisible by 5, or Fizz Buzz if the number is divisible by both 3 and 5. You humor the robot for a while, but its getting stressful, and things are coming to a point where your relationship with the robot may suffer if you do not automate these interactions with him. In the program below, the robot will give you a number in the variable num. You have to print the appropriate value (Fizz, Buzz, Fizz Buzz, ) to the system console. What we expect: If the value in num is 3 we expect your program to print Fizz, ditto for 6, and 9. If the value in num is 5, we expect your program to print Buzz, ditto for 10 and 20. However, if the value in num is 15, we expect you to print Fizz Buzz, ditto for 30. Finally, if the value in num is 7, we expect you to print an empty string, and ditto for 8, 11, 16, etc. Hint: You can use the % operator to get the remainder from a division operation Learning outcome: After doing this exercise, you should have learned how to: Use the % (modulus operator) Compare two values using one of the comparison operators in Java Make a decision in your code using an if...else if... else branch statement
import java.util.Scanner; public class FizzBuzz {
public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("Enter the number:"); int num = scanner.nextInt(); ///{ //write your code here //start
//end ///} }
}
----
Question 3
Complete the following empty recursive void method, reverse, that accepts an integer array, a starting index and an ending index, and reverses the array. Reversing an array involves: Nothing if the array has 0 or 1 elements. swapping the first and last elements of the array and then reversing the remainder of the array (2nd through next-to-last elements). ------
import java.util.Scanner; class arrayreverse{ static void reverse(int[] arr,int start,int end){ ///{ //write your code here //start
//end ///} } public static void main(String arg[]){ Scanner scanner=new Scanner(System.in); System.out.println("Enter the size of an array:"); int size=scanner.nextInt(); int[] a=new int[size]; for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
