Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Add a loop to the overall program that asks the user if they would like to

ID: 3719540 • Letter: J

Question

JAVA

Add a loop to the overall program that asks the user if they would like to repeat the program. If the user enters something other than a yes or no response the program should continue asking the user for a correct response until a correct response is given.

SHOW OUTPUT LOOP REPEATED 2 TIMES; PLEASE FIX ANY ERRORS THAT occur.

import java.util.Scanner;

public class LoopsMS

{

public static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)

{

String str;

int catCount = 0, dogCount = 0, llamaCount = 0;

  

String choice= "";

String degree;

double temperature;

double ansF;

double ansC;

int noGrades = 0;

int grade;

double noA =0;

double noB =0;

double noC =0;

double noD = 0;

double noF =0;

int highest = 0;

int lowest = 0;

double sumOfGrades = 0;

int totalNumbers = 0;

int diamondSize = 0;

int min= 0;

int z = 0;

System.out.print("Please enter sentence and I will check it for profane words: ");

str = keyboard.nextLine();

str = str.replaceAll("[<>{}()#$@^*.,?!'"]","");

for (String word : str.split(" "))

{

if(word.equalsIgnoreCase("cat"))

catCount++;

else if(word.equalsIgnoreCase("dog"))

dogCount++;

else if(word.equalsIgnoreCase("llama"))

llamaCount++;

}

System.out.println("Your sentence contained the profane word "cat" " + catCount + " times.");

System.out.println("Your sentence contained the profane word "dog" " + dogCount + " times.");

System.out.println("Your sentence contained the profane word "llama" " + llamaCount + " times.");

System.out.println();

do

{

System.out.println("Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.");

temperature = keyboard.nextDouble();

degree = keyboard.next();

while(!degree.equalsIgnoreCase("C") && !degree.equalsIgnoreCase("F"))

{

System.out.println("You did not enter a valid temperature scale, Please enter a valid scale:");

degree = keyboard.next();

}

if (degree.equalsIgnoreCase("F"))

{

ansF = (temperature-32)*5/9;

System.out.printf(temperature +" degrees Fahrenheit is equal to %.2f Celsius", ansF);

}

else if (degree.equalsIgnoreCase("C"))

{

ansC = (temperature*9/5)+32;

System.out.printf(temperature + " degrees Celsius is equal to %.2f Fahrenheit ", ansC );

}

System.out.println(" Would you like to repeat the temperature project?(Enter Q or q for quit): ");

choice = keyboard.next();

System.out.println();

}

while(!choice.equalsIgnoreCase("Q"));

System.out.println();

System.out.println("Enter a grade from 0 to 100 or enter negative number to quit");

while (true)

{

grade =keyboard.nextInt();

if (grade<0)

break;

sumOfGrades=sumOfGrades+grade;

if(grade>=90 && grade<=100)

noA++;

else if (grade>=80 && grade<90)

noB++;

else if(grade>=70 && grade<80)

noC++;

else if(grade>=60 && grade<70)

noD++;

else if (grade>=0 && grade<60)

noF++;

if(noGrades==0)

highest=lowest=grade;

if(highest<grade)

highest=grade;

if(lowest>grade)

lowest=grade;

noGrades++;

}

totalNumbers =noGrades;

System.out.println("Total number of grades are: "+noGrades);

System.out.println("Highest Score: "+highest);

System.out.println("Lowest Grade: "+lowest);

System.out.printf("Total Average: %.2f",(sumOfGrades/noGrades));

System.out.println();

System.out.println((int)noF + "-"+ "Fs"+ "- " + (noF/totalNumbers)*100 + "%");

System.out.println((int)noD + "-"+ "Ds"+ "- " + (noD/totalNumbers)*100 + "%");

System.out.println((int)noC + "-"+ "Cs"+ "- " + (noC/totalNumbers)*100 + "%");

System.out.println((int)noB + "-"+ "Bs"+ "- " + (noB/totalNumbers)*100 + "%");

System.out.println((int)noA+ "-"+ "As"+ "- " + (noA/totalNumbers)*100 + "%");

System.out.println();

System.out.println("Please enter the size of the diamond you would like to build. (3 to 50)");

diamondSize=keyboard.nextInt();

min=1;

for(int i=0;i<diamondSize;i++)

{

for(int j=diamondSize-1;j>i;j--)

{

System.out.print(" ");

}

for(int k=0;k<min;k++)

{

if(k%2==0)

{

System.out.print("*");

}else

{

System.out.print(" ");

}

}

min+=2;

System.out.println();

}

z=diamondSize+diamondSize-3;

for(int i=1;i<diamondSize;i++)

{

for(int j=0;j<i;j++)

{

System.out.print(" ");

}

for(int k=0;k<z;k++)

{

if(k%2==0)

{

System.out.print("*");

}else

{

System.out.print(" ");

}

}

z-=2;

System.out.println();

}   

}

  

}

Explanation / Answer

package org.students;

import java.util.Scanner;

public class LoopsMS

{

public static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)

{

String str,yesOrNo;

int catCount = 0, dogCount = 0, llamaCount = 0;

String choice = "";

String degree;

double temperature;

double ansF;

double ansC;

int noGrades = 0;

int grade;

double noA = 0;

double noB = 0;

double noC = 0;

double noD = 0;

double noF = 0;

int highest = 0;

int lowest = 0;

double sumOfGrades = 0;

int totalNumbers = 0;

int diamondSize = 0;

int min = 0;

int z = 0;

do

{

System.out.print("Please enter sentence and I will check it for profane words: ");

str = keyboard.nextLine();

str = str.replaceAll("[<>{}()#$@^*.,?!'"]", "");

for (String word : str.split(" "))

{

if (word.equalsIgnoreCase("cat"))

catCount++;

else if (word.equalsIgnoreCase("dog"))

dogCount++;

else if (word.equalsIgnoreCase("llama"))

llamaCount++;

}

System.out.println("Your sentence contained the profane word "cat" "

+ catCount + " times.");

System.out.println("Your sentence contained the profane word "dog" "

+ dogCount + " times.");

System.out

.println("Your sentence contained the profane word "llama" "

+ llamaCount + " times.");

System.out.println();

do

{

System.out

.println("Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.");

temperature = keyboard.nextDouble();

degree = keyboard.next();

while (!degree.equalsIgnoreCase("C")

&& !degree.equalsIgnoreCase("F"))

{

System.out

.println("You did not enter a valid temperature scale, Please enter a valid scale:");

degree = keyboard.next();

}

if (degree.equalsIgnoreCase("F"))

{

ansF = (temperature - 32) * 5 / 9;

System.out.printf(temperature

+ " degrees Fahrenheit is equal to %.2f Celsius", ansF);

}

else if (degree.equalsIgnoreCase("C"))

{

ansC = (temperature * 9 / 5) + 32;

System.out

.printf(temperature

+ " degrees Celsius is equal to %.2f Fahrenheit ",

ansC);

}

System.out

.println(" Would you like to repeat the temperature project?(Enter Q or q for quit): ");

choice = keyboard.next();

System.out.println();

}while (!choice.equalsIgnoreCase("Q"));

System.out.println();

System.out

.println("Enter a grade from 0 to 100 or enter negative number to quit");

while (true)

{

grade = keyboard.nextInt();

if (grade < 0)

break;

sumOfGrades = sumOfGrades + grade;

if (grade >= 90 && grade <= 100)

noA++;

else if (grade >= 80 && grade < 90)

noB++;

else if (grade >= 70 && grade < 80)

noC++;

else if (grade >= 60 && grade < 70)

noD++;

else if (grade >= 0 && grade < 60)

noF++;

if (noGrades == 0)

highest = lowest = grade;

if (highest < grade)

highest = grade;

if (lowest > grade)

lowest = grade;

noGrades++;

}

totalNumbers = noGrades;

System.out.println("Total number of grades are: " + noGrades);

System.out.println("Highest Score: " + highest);

System.out.println("Lowest Grade: " + lowest);

System.out.printf("Total Average: %.2f", (sumOfGrades / noGrades));

System.out.println();

System.out.println((int) noF + "-" + "Fs" + "- " + (noF / totalNumbers)

* 100 + "%");

System.out.println((int) noD + "-" + "Ds" + "- " + (noD / totalNumbers)

* 100 + "%");

System.out.println((int) noC + "-" + "Cs" + "- " + (noC / totalNumbers)

* 100 + "%");

System.out.println((int) noB + "-" + "Bs" + "- " + (noB / totalNumbers)

* 100 + "%");

System.out.println((int) noA + "-" + "As" + "- " + (noA / totalNumbers)

* 100 + "%");

System.out.println();

System.out

.println("Please enter the size of the diamond you would like to build. (3 to 50)");

diamondSize = keyboard.nextInt();

min = 1;

for (int i = 0; i < diamondSize; i++)

{

for (int j = diamondSize - 1; j > i; j--)

{

System.out.print(" ");

}

for (int k = 0; k < min; k++)

{

if (k % 2 == 0)

{

System.out.print("*");

} else

{

System.out.print(" ");

}

}

min += 2;

System.out.println();

}

z = diamondSize + diamondSize - 3;

for (int i = 1; i < diamondSize; i++)

{

for (int j = 0; j < i; j++)

{

System.out.print(" ");

}

for (int k = 0; k < z; k++)

{

if (k % 2 == 0)

{

System.out.print("*");

} else

{

System.out.print(" ");

}

}

z -= 2;

System.out.println();

}

do

{

System.out.print("Do you want to run the program again (yes/no)? ");

yesOrNo=keyboard.next();

if(!yesOrNo.equalsIgnoreCase("yes") && !yesOrNo.equalsIgnoreCase("no"))

{

System.out.println("** Invalid.Must be either Yer/No **");

}

else

{

keyboard.nextLine();

catCount=0;

dogCount=0;

llamaCount=0;

}

}while(!yesOrNo.equalsIgnoreCase("yes") && !yesOrNo.equalsIgnoreCase("no"));

}while(!yesOrNo.equalsIgnoreCase("no"));

}

}

___________________

Output:

Please enter sentence and I will check it for profane words: Cat has four kittens. Dog has two puppies
Your sentence contained the profane word "cat" 1 times.
Your sentence contained the profane word "dog" 1 times.
Your sentence contained the profane word "llama" 0 times.

Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.
56 f
56.0 degrees Fahrenheit is equal to 13.33 Celsius
Would you like to repeat the temperature project?(Enter Q or q for quit):
n

Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.
54 c
54.0 degrees Celsius is equal to 129.20 Fahrenheit
Would you like to repeat the temperature project?(Enter Q or q for quit):
q


Enter a grade from 0 to 100 or enter negative number to quit
78
87
76
65
54
43
89
-90
Total number of grades are: 7
Highest Score: 89
Lowest Grade: 43
Total Average: 70.29
2-Fs- 28.57142857142857%
1-Ds- 14.285714285714285%
2-Cs- 28.57142857142857%
2-Bs- 28.57142857142857%
0-As- 0.0%

Please enter the size of the diamond you would like to build. (3 to 50)
8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Do you want to run the program again (yes/no)? bye
** Invalid.Must be either Yer/No **
Do you want to run the program again (yes/no)? hello
** Invalid.Must be either Yer/No **
Do you want to run the program again (yes/no)? yes
Please enter sentence and I will check it for profane words: Cat has four kittens
Your sentence contained the profane word "cat" 1 times.
Your sentence contained the profane word "dog" 0 times.
Your sentence contained the profane word "llama" 0 times.

Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.
56
f
56.0 degrees Fahrenheit is equal to 13.33 Celsius
Would you like to repeat the temperature project?(Enter Q or q for quit):
q


Enter a grade from 0 to 100 or enter negative number to quit
78
87
76
65
-60
Total number of grades are: 11
Highest Score: 89
Lowest Grade: 43
Total Average: 72.55
2-Fs- 18.181818181818183%
2-Ds- 18.181818181818183%
4-Cs- 36.36363636363637%
3-Bs- 27.27272727272727%
0-As- 0.0%

Please enter the size of the diamond you would like to build. (3 to 50)
8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Do you want to run the program again (yes/no)? yes
Please enter sentence and I will check it for profane words: hello how are you?
Your sentence contained the profane word "cat" 0 times.
Your sentence contained the profane word "dog" 0 times.
Your sentence contained the profane word "llama" 0 times.

Please enter your temperature, Real number, followed by a space and the scale c or C for Celsius and f or F for Fahrenheit.
56 c
56.0 degrees Celsius is equal to 132.80 Fahrenheit
Would you like to repeat the temperature project?(Enter Q or q for quit):
Q


Enter a grade from 0 to 100 or enter negative number to quit
78
-70
Total number of grades are: 12
Highest Score: 89
Lowest Grade: 43
Total Average: 73.00
2-Fs- 16.666666666666664%
2-Ds- 16.666666666666664%
5-Cs- 41.66666666666667%
3-Bs- 25.0%
0-As- 0.0%

Please enter the size of the diamond you would like to build. (3 to 50)
3
*
* *
* * *
* *
*
Do you want to run the program again (yes/no)? no

_____________Thank You