Question: SO ALL I NEED IS TO WRITE COMMENTS FOR EACH LINE OF CODE, BUT I'M UNSURE WHAT ROLE EACH FUNCTION PLAYS. Can anyone help with

SO ALL I NEED IS TO WRITE COMMENTS FOR EACH LINE OF CODE, BUT I'M UNSURE WHAT ROLE EACH FUNCTION PLAYS. Can anyone help with this? The comments are supposed to be detailed enough so that someone who didn't program could figure out what the program is doing. Thanks.

For this code, it takes user input of a particular character and outputs how many times that character appears:

example input: g Every good person is gladly accepted. output: 2

Code:

import java.util.Scanner;

public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); char numChar = scnr.next().charAt(0); String charLength = scnr.nextLine(); int charCount = 0; for (int i = 0; i < charLength.length(); i++) if (charLength.charAt(i) == numChar) charCount++; System.out.println(charCount); } }

*This next code takes user input and outputs the string in reverse until user enters 'Quit,' 'quit,' or 'q' to stop it.*

Code:

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String s; while (scnr.hasNextLine()) { s = scnr.nextLine(); if (s.equalsIgnoreCase("quit") || s.equalsIgnoreCase("q")) { break; } for (int i = s.length() - 1; i >= 0; --i) { System.out.print(s.charAt(i)); } System.out.println(); } } }

*This last code deals with palindromes, wherein it takes an input of words such as "Anna" or or phrases like "never odd or even" and states whether or not they are palindromes. A palindrome is a word that has the same spelling forward and backward.*

Code:

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String s; s = scnr.nextLine(); boolean palindromeWord = true; int i = 0, j = s.length() - 1; while(i < j){ if(s.charAt(i) == ' ') i++; else if(s.charAt(j) == ' ') j--; else if(s.charAt(i) != s.charAt(j)){ palindromeWord = false; break; } else{ i++; j--; } } if(palindromeWord) System.out.println(s + " is a palindrome"); else System.out.println(s + " is not a palindrome"); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!