0% found this document useful (0 votes)
11 views3 pages

Assignment 2

The document contains a Java program for encoding and decoding messages using a simple shift cipher. It includes methods for encoding, decoding, and displaying a menu for user interaction. The main method allows users to choose to encode or decode a message or exit the program.

Uploaded by

ritika sandalwar
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)
11 views3 pages

Assignment 2

The document contains a Java program for encoding and decoding messages using a simple shift cipher. It includes methods for encoding, decoding, and displaying a menu for user interaction. The main method allows users to choose to encode or decode a message or exit the program.

Uploaded by

ritika sandalwar
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/ 3

Assignment 2

import java.util.Scanner;

public class TextEncoderDecoder {

// Method to encode a message


public static String encode(String message, int shift) {
StringBuilder encodedMessage = new StringBuilder();
for (char ch : message.toCharArray()) {
if (Character.isLetter(ch)) {
char base = Character.isUpperCase(ch) ? 'A' : 'a
encodedMessage.append((char) ((ch - base + shift
} else {
encodedMessage.append(ch);
}
}
return encodedMessage.toString();
}

// Method to decode a message


public static String decode(String message, int shift) {
return encode(message, 26 - shift % 26);
}

// Method to display the menu


public static void displayMenu() {
System.out.println("\nMenu:");
System.out.println("1. Encode a message");
System.out.println("2. Decode a message");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
}

Assignment 2 1
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;

do {
displayMenu();
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1: // Encode a message
System.out.print("Enter the message to encod
String messageToEncode = scanner.nextLine();
System.out.print("Enter the shift value: ");
int encodeShift = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.println("Encoded Message: " + enc
break;
case 2: // Decode a message
System.out.print("Enter the message to decod
String messageToDecode = scanner.nextLine();
System.out.print("Enter the shift value: ");
int decodeShift = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.println("Decoded Message: " + dec
break;
case 3: // Exit
System.out.println("Exiting the program. Goo
break;
default:
System.out.println("Invalid choice. Please t
}
} while (choice != 3);

scanner.close();

Assignment 2 2
}
}

Assignment 2 3

You might also like