0% found this document useful (0 votes)
9 views2 pages

Practice 4.2

Uploaded by

selviselvi41167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Practice 4.2

Uploaded by

selviselvi41167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

v.

sripurushothaman 192324114

Practice 4.2

Solutions

3)

Soln:

import java.util.regex.*;

import java.io.*;

public class AnswerKeyProblem {

public static void main(String args[]) throws IOException {

// Read in the file provided by your teacher

BufferedReader codedAnswers = new BufferedReader(new FileReader("CodedAnswerKey"));

// Initialize String to store the answer key

String answers = "";

// Regular expression to match valid answer characters

Pattern pattern = Pattern.compile("[aAbBcCdDeEfF]");

// Read each line of the file

String line = codedAnswers.readLine();

// Keep reading each line and adding valid answers to the string answers

while (line != null) {

// Check if the line matches the valid characters pattern

if (pattern.matcher(line).matches()) {

answers += line;

// Read the next line

line = codedAnswers.readLine();

// Close the file reader

codedAnswers.close();

// Print out the answers

System.out.println("Deciphered Answer Key: " + answers);

}
4)

Soln:

public class AnswerProcessor {

public static String finalAnswers(String answers) {

// Replace according to the given rules

String result = answers.replace('e', 'b')

.replace('E', 'A')

.replace('f', 'c')

.replace('F', 'D');

// Convert the result to lowercase

return result.toLowerCase();

public static void main(String[] args) {

// Example usage

String exampleAnswers = "EeFf";

String processedAnswers = finalAnswers(exampleAnswers);

System.out.println(processedAnswers); // Output: "abcc"

You might also like