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
Starting Out With Visual Basic (7th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Concepts Of Programming Languages
Java: An Introduction to Problem Solving and Programming (7th Edition)
Starting Out with C++: Early Objects (9th Edition)
- Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is quit 0. Ex: If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps the doctor away. Eating 2 shoes a day keeps the doctor away. import java.util.Scanner; public class LabProgram {public static void main(String[] args) {Scanner scnr = new Scanner(System.in);String some_string;int some_int;some_string = scnr.next();some_int = scnr.nextInt();if (some_int != 0){System.out.println("Eating " + some_int + " " + some_string + " a day keeps the doctor away.");}}} My program wil online output the first input but not the next. What am I missing???arrow_forwardWrite a program that inputs a number and checks whether it is a perfect number or not. A perfect number is the number that is numerically equal to the sum of its divisors. For example, 6 is a perfect number because the divisors of 6 are 1, 2, 3 and 1+2+3=6arrow_forwardJAVA Write a java program that takes a string as input from the user and finds out if the string is a palindrome or not. A string is a palindrome if it reads the same from forward as well as from backward. For example, the string “madam” is a palindrome. Use a for loop to solve this problem. Sample inputs and outputs: Input: madam Output: palindrome Input: Dad Output: palindrome Input: hello Output: not a palindromearrow_forward
- A square is divided into four smaller regions as shown in (a). If you throw a dart into the square one million times, what is the probability for the dart to fall into an odd-numbered region? Write a program to simulate the process and display the result.arrow_forwardJAVA: Replace the for loop below with a code using the "While" loop: int s = 0; int c = 0; boolean valid = true; for(int i = 4; i <= 20 || valid; i = i+2) { if(i%4 != 0) { s = s + i; c++; } if(i > 20) { valid = false; } } System.out.println(s / c);arrow_forwardWrite a program that reads an integer and displays all its smallest factors in an increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.arrow_forward
- Write a Java program using a for loop or a while loop that reads a word and prints the word in reverse. For example, if the user provides the input "Harry", the program prints "yrraH".arrow_forwardWrite a program that simulates a simple calculator. It reads two integers and a character. If the character is a ‘+’, the sum is printed; if it is a ‘-’, the difference is printed; if it is a ‘*’, the product is printed; if it is a ‘/’, the quotient is printed; and if it is a ‘%’, the remainder is printed. (Refrance C++)arrow_forwardWrite a Java program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The table should include rows for all temperatures between 0 and 100 degrees Celsius that are multiples of 10 degrees Celsius.arrow_forward
- in java String inputWord is read from input. Write a while loop that iterates until inputWord is equal to "Finale". In each iteration of the loop: Read integer clothingNumber from input. If clothingNumber is less than 30, output the value of inputWord, followed by ": low on stock" and a newline. Otherwise, output the value of inputWord, followed by ": well stocked" and a newline. Read string inputWord from input. Click here for example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.util.Scanner; public class PairedDataProcessing { publicstaticvoidmain(String[] args) { Scannerscnr=newScanner(System.in); StringinputWord; intclothingNumber; inputWord=scnr.next(); /* Your code goes here */ } }arrow_forwardWrite a program using nested for loops that will print the table below exactly as shown: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 You MUST use nested for loops to accomplish this. You MUST store the value printed in a variable. You cannot just print the values as a String like “10 11 12 13 14 ….” Questions What is the primary difference between how a while loop operates and how a do while loop operates? View the code below and indicate where each variable would have scope. (Circle all boxes for each variable in which that variable would be useable. class DoLittle { public static void main(String[] args) { int number =…arrow_forwardUsing the Java programming language write a program that takes an email address as an input from the keyboard and, using a loop, steps through every character looking for an @ sign. If the email address has exactly one @ character, then print a message that the email address is valid; otherwise, print a message that it is invalid.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education