Given the variables
String stars = "*****";
String stripes = =====;
what do these loops print?
- a. int i = 0;
while (i < 5)
{
System.out.println(stars.substring(0, i));
i++;
}
- b. int i = 0;
while (i < 5)
{
System.out.print(stars.substrings (0, i));
System.out.println(stripes.substrings(i, 5));
i++;
}
- c. int i = 0;
while (i < 10)
{
if (i % 2 == 0) { System.out.println(stars); }
else { System.out.println(stripes); }
}
Explanation of Solution
a.
//Definition of class Test
class Test
{
//Definition of method main
public static void main(String[] args)
{
//Initialize the variables
String stars = "*****";
String stripes = "=====";
//Initialize the value of "i" to 0
int i = 0;
//Check the condition using while loop
while (i < 5)
{
//Print the output
System.out.println(stars.substring(0, i));
//Increment the value of "i"
i++;
}
}
}
Explanation:
The above code snippet initializes the variable “i” to “0”. The condition “i<=5” is checked using “while” loop. Print the output and increment the value of “i”.
Output:
*
**
***
****
Explanation of Solution
b.
//Definition of class Test
class Test
{
//Definition of method main
public static void main(String[] args)
{
//Initialize the variables
String stars = "*****";
String stripes = "=====";
//Initialize the value of "i" to 0
int i = 0;
//Check the condition using while loop
while (i < 5)
{
//Print the output
System.out.println(stars.substring(0, i));
//Print the output
System.out.println(stars.substring(i, 5));
//Increment the value of "i"
i++;
}
}
}
Explanation:
The above code snippet initializes the variable “i” to “0”. The condition “i<=5” is checked using “while” loop. Print the output by calling the two sub string function and increment the value of “i”.
Output:
*****
*
****
**
***
***
**
****
*
Explanation of Solution
c.
//Definition of class Main
class Main
{
//Definition of method main
public static void main(String[] args)
{
//Initialize the variables
String stars = "*****";
String stripes = "=====";
//Initialize the value of "i" to 0
int i = 0;
//Check the condition using while loop
while (i < 10)
{
//Check the condition for "i%2" equals to 0
if (i % 2 == 0)
{
//Print the output
System.out.println(stars);
}
else
{
//Print the output
System.out.println(stripes);
}
}
}
}
Explanation:
The above code snippet initializes the variable “i” to “0”. The condition “i<=10” is checked using “while” loop. Inside the while loop, check the condition of “i%2” using the “if” statement.
Output:
*****
*****
*****
*****
*****
*****
*****
.
.
.
.
Infinite loop
Want to see more full solutions like this?
Chapter 4 Solutions
Big Java Late Objects
Additional Engineering Textbook Solutions
HEAT+MASS TRANSFER:FUND.+APPL.
Problem Solving with C++ (10th Edition)
SURVEY OF OPERATING SYSTEMS
Degarmo's Materials And Processes In Manufacturing
Starting Out With Visual Basic (8th Edition)
Elementary Surveying: An Introduction To Geomatics (15th Edition)
- 3. What does this code print? int i = 0; while (i < 12) { int j; for (j = 0; j < i; j++) { if ((i+j)%3 Ø) == j += 2; } i += j+1; } System.out.println("i = "+i);arrow_forwardWrite EXACTLY what is printed by the codearrow_forward// DebugFive4.java // Outputs highest of four numbers import java.util.*; public class DebugFive4 { publicstaticvoidmain (Stringargs[]) { Scanner input =newScanner(System.in); int one, two, three, four; String str, output; System.out.println("Enter an integer"); str = input.next(); one = Integer.parseInt(str); System.out.println("Enter an integer"); str = input.next(); two = Integer.parseInt(str); System.out.println("Enter an integer"); str = input.next(); three = Integer.parseInt(str); System.out.println("Enter an integer"); str = input.next(); four = Integer.parseInt(str); if(one > two > one > three > one > four) output = "Highest is " + four; else if(two > one && two > three !! two > four) output = "Highest is " + three; else if(three > one && three > two && three > four) output = "Highest is " + three; else output = "Highest is " + four; System.out.println(output); } }arrow_forward
- #include #include #include int main() { int i, j; printf("Columns | JIn"); for (i = 1; i< 4; ++i) { printf("Outer %6d\n", i); for (j = 0; j< i; ++j) { printf(" Inner%10d\n", j); } } /* heading of outer for loop */ /* heading of inner loop */ %3D return (0); } Create a new code modifying it to use "while loop" instead of the "for loop"arrow_forward#includecstdio.h> int x=10; void func1() 7 printf("x=%d\n", x); return; 8 10 int func2(int a, int b) 15 return a+b; 16 18 19 ... func3(...) 20 21 .. return ... int main() 27 { int x-1, y=2; func1(); 28 29 30 int z=func2(x, y); printf("z-%d\n", z); 31 34 Figure 2. C Program for Q2 What is the output after execution of line 29 and 32? a. b. func3() in line 19 to 23 is incomplete. Write the function, func30 to compute and print the volume of cuboid with length x, width y and height z. c. Complete line 33 to call func3() that you created in part (b).arrow_forwardQuestion #4: Re-write the program of Question #3 using data files. The input data numbers.txt contains (positive) integer numbers. Each time the program reads a number, check whether it is a prime and stores the result in the output file Results.txt. 373 373 is Prime!! 11 11 is Prime!! 2552 is NOT prime!! 5 is Prime!! 3561 is NOT prime!! 2552 3561 numbers.txt Results.txtarrow_forward
- int a = 10, b =7; System.out.println(a>b?a:b); Will always print what?arrow_forwardJAva Input. class increment { public static void main(String args[]) { double var1 = 1 + 5; double var2 = var1 / 4; int var3 = 1 + 5; int var4 = var3 / 4; System.out.print(var2 + " " + var4); } } Find output.arrow_forwardmain.cpp 1 @include 2 using namespace std; int maxResult() 4-( int maxVal = 0; 6. for (int i = 0; i <= n; i += a) { for (int j = e; j <= n i;j += b) !! 10 (float)(n - (i + j)) / (float)(c); 11 float z = 12 if (floor (z) { int x = i / a; int y 13 ceil(z)) 14 15 16 j/ b; max (maxVal, x + y + (int)z); 17 maxVal = 18 19 20 21 22 return maxVal; 23 } 24 int main() 26 { 25 27 28 cout << maxResult( ); 29 30 return 0; 31 } Input Compilation failed due to following erors) main.cpp:7:23: error: 'n was not declared in this scope 7 for (int i = 0; i <= n; i t a) main.cpp:7:31: error: 'a' was not declared in this scope 7 for (int i 0; i <= n; i t a) main.cpp:9:36: error: 'b' was not declared in this scope for (int j = 0; j <- n i; j b) %3D main.cpp:11:45: error: was not declared in this scopearrow_forward
- Urgent Answer Pleasearrow_forwardExplain the following code? const int analogPin = A0; const int ledCount = 10; int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; void setup() { for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT); } } void loop() { int sensorReading = analogRead(analogPin); // map the result to a range from 0 to the number of LEDs: int ledLevel = map(sensorReading, 0, 1023, 0, ledCount); for (int thisLed = 0; thisLedarrow_forwardPalindromes - “A palindrome” is a string that reads the same from both directions. For example: the word "mom" is a palindrome. Also, the string "Murder for a jar of red rum" is a palindrome. - So, you need to implement a Boolean function that takes as input a string and its return is true (1) in case the string is a palindrome and false (0) otherwise. - There are many ways to detect if a phrase is a palindrome. The method that you will implement in this task is by using two stacks. This works as follows. Push the left half of the characters to one stack (from left to right) and push the second half of the characters (from right to left) to another stack. Pop from both stacks and return false if at any time the two popped characters are different. Otherwise, you return true after comparing all the elements. Phrases of odd length have to be treated by skipping the middle element like the word "mom", your halves are "m" and "m". - Hint: (without using STL)arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,