Java question...please read carefully! For this assignment you will write a simp
ID: 667276 • Letter: J
Question
Java question...please read carefully!
For this assignment you will write a simple Java program. Write a class called Print. This class has only a main method. The main method has a single for loop with an index that increments from 0 to 19. At each iteration of the loop, if the current value of the index is multiple of 3, the loop should print:
“How now
brown cow” using a single print statement.
If the loop index is not divisible by 3, the loop should print:
“How now brown cow” using two print statements, one for the first two words and one for the last two words.
Your output should start out like this:
How now
brown cow
How now brown cow
How now brown cow
How now
brown cow
How now brown cow
How now brown cow
How now
brown cow
How now brown cow
How now brown cow
.
.
Examination of different methods to do printing would be helpful as well as a review of escape sequences and a review of the remainder operator should be helpful in this assignment.
If your program compiles, but you get a runtime error stating that main can’t be found, check the signature of your main method to be sure it’s correct.
Submissions that don’t compile will get a grade of 0.
Explanation / Answer
I used System.out.print() function for printing text.
for new line ' ' used in above funtion
Program starts form next line.
public class Print{
public static void main(String []args){
for(int i=0;i<=19;i++){
// logic for multiple of 3
if(i%3==0){
// if yes
System.out.print("How now brown cow ");
}
else{
// if not
System.out.print("How now ");
System.out.print("brown cow ");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.