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

Write a program named Reverser that reads in a sentence and displays the words o

ID: 3714852 • Letter: W

Question

Write a program named Reverser that reads in a sentence and displays the words of the sentence in reverse order. Use the split method to extract the words from the sentence. If there is punctuation at the end of the sentence you must remove it and place it at the end of the reversed sentence. The program must keep running until the user enters “quit” in any case. Use the command prompt window (terminal) for the program.

Sample program run:

5. Create a program SSNValidator.java. Your program must have the following method:

        public static boolean isValidSSN(String ssn)

The method will determine if a social security number is valid or not. The method returns true if valid, otherwise false. A valid social security number can contain ONLY nine digits or can contain nine digits AND two hyphens in the format:
      XXXXXXXXX
OR
     XXX-XX-XXXX

Use the Character class in the method to determine if there are digits. Do NOT use regular expressions or you will receive a grade of zero.

The program must keep prompting for a social security number and validating it until the user enters Quit (in any case). You must inform the user if the social security number s/he entered is valid or invalid

NO INPUT OR OUTPUT SHOULD BE DONE ANYWHERE EXCEPT main.

Your full name must appear in the output of both programs or 1 point will be deducted each time it is missing. You must follow the Programming Guidelines handout. All code must be correctly indented and commented. Your programs must compile in order to be graded. Do NOT use Eclipse, NetBeans, or IntelliJ for any labs or assignments. Do NOT create packages or projects; otherwise I will not be able to grade your assignment.

Explanation / Answer

First Code:

import java.util.Scanner;

public class Reverser {

void reverse(String s) {
char punctuation = ' ';
if (s.endsWith("?") || s.endsWith("!") || s.endsWith(".")){ //Keeps punctuation as it is
punctuation = s.charAt(s.length()-1);   
s = s.replace(punctuation,' ');
}
String[] split = s.split(" "); //Splitting the sentence from space
String result = "";
for (int i = split.length - 1; i >= 0; i--) { //Adding every split string in reverse order
result += (split[i] + " ");
}
result += punctuation;
System.out.println(result);
}
  
public static void main(String[] args) {

int i = 1;
do{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence(Type quit to exit)");
String sentence = sc.nextLine();
if(sentence.equals("quit")){ //logic to quit
i = 0;
System.out.println("Programmed by Your Full Name"); //Printing your full name at the ending of the output
}
else{
Reverser r = new Reverser();
r.reverse(sentence);
}
}while(i == 1);
}
  
}

Second Code:

import java.util.Scanner;

public class SSNValidator {

public static boolean isValidSSN(String ssn) {
boolean b = false;
boolean c = true;
int count = 0;
for(int i = 0; i < ssn.length(); i++){
  
if((Character.isDigit(ssn.charAt(i)) && ssn.length() == 9) || (Character.isDigit(ssn.charAt(i)) || ssn.charAt(i) == '-' && ssn.length() == 11) ){ //using charactor class for validating SSN
if(ssn.charAt(i) == '-')
{
count++;
}
if(count > 2){
c = false;
}
if(ssn.length() < 9){
c = false;
}
b = true;
}
else{
c = false;
}
}
return c;
}
public static void main(String[] args) {
int i = 1;
do{
Scanner sc = new Scanner(System.in);
System.out.println("Enter SSN(Type quit to exit)");
String ssn = sc.nextLine();
if(ssn.equals("quit")){ //logic to quit
i = 0;
System.out.println("Programmed by Your Full Name"); //Printing your full name at the ending of the output
}
else{
if(SSNValidator.isValidSSN(ssn) == true){
System.out.println("Valid SSN");
}else
System.out.println("Invalid SSN");
}
}while(i == 1);
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote