1. Consider the following code Scanner se - new Scanner(System.in); System.out.println("Do you think exams are fun7") st
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
1. Consider the following code Scanner se - new Scanner(System.in); System.out.println("Do you think exams are fun7") st
1. Consider the following code: Scanner sc - new Scanner (System.in); System.out.println("Do you think exams are fun?"); String s - sc.nextLine(); if (s.equals("yes") { System.out.print("Really?"); } else if(s.equals("no")) { String s2 - new String ("me neither"); System.out.print(s); } How many Classes, Objects, Methods does the code contain? 2. For each of the following codes, write the output or state the error is the code would not run. Code A: for (int i = 1; i < 8; i +3) for(int j = 0; j > i; j -- 2) System.out.print() + " "); Code B: char c-'A'; int i = 10; String str - "myString": str += c + i; System.out.println (str); Code C: int sum = 0; for(int i=0; i<100; i+=2) ( sum += i;) System.out.println( i ); 3. The Fibonacci sequence is defined such that a number in the sequence is the sum of the previous two number in the sequence where the first two numbers are 0, 1. The Fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... Write a method, fib_java that takes an integer as argument and outputs the Fibonacci sequence to the first value greater than the input. Example: fib_java(6) would display 0,1,1,2,3,5,8 4. Write some code that will declare, initialize, and fill in an array of type int. After your code executes, the array should look as follows: 0 2 4 6 8 10 12 14 16 18
5. Consider a rectangular grid of size 100x100. Each square in the grid has a unique row and column number (x, y). The square (0, 0) is at the middle; x values increase to the right and decrease to the left; y values increase going down and decrease going up. We want to create a Robot class. Each Robot object represents one robot. Each robot has a location (x, y), and a direction in which it is facing (up. right, down, or left). A robot can move one square forward in the direction it is facing. It can also turn 90 degrees to the right. In addition, we shall be able to know a robot's current x and y position and direction. Write the Robot class that includes the data and behavior needed as described.