Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

Students also viewed these Databases questions