0% found this document useful (0 votes)
9 views

oop theory assignment BISMA

The document contains an assignment for Object Oriented Programming, consisting of two tasks. The first task involves writing a Java program to implement a Caesar cipher for encrypting a user-provided message based on a shift key. The second task requires creating a Java program that generates a pattern based on the user's full name input.

Uploaded by

64419
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

oop theory assignment BISMA

The document contains an assignment for Object Oriented Programming, consisting of two tasks. The first task involves writing a Java program to implement a Caesar cipher for encrypting a user-provided message based on a shift key. The second task requires creating a Java program that generates a pattern based on the user's full name input.

Uploaded by

64419
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming

Assignment 1

Name: Bisma Amin


Semester: BSCS-2
Sap id: 63288
2

TASK 1
Question:

A Caesar cipher is a simple encryption technique where each letter of a message is shifted forward in the
alphabet by a given number of places.

Write a java program that:

1. Reads a message from the user.


2. Reads a shift key (integer) from the user.
3. Encrypt the message using the Caesar cipher.
4. Displays the encoded message.

Solution:

Code:
public class OoptAss1T1 {

public static void main (String [] args) {

Scanner scans = new Scanner (System.in);

Scanner scant = new Scanner (System.in);

// Read the message from the user

System.out.println("Enter your secret message: ");

String secretmessage = scans.nextLine();

// Read the shift key from the user

System.out.println("Enter a number with whom u want to encoded the message : ");

int num = scant.nextInt();

// Encrypt the message

// Display the encoded message

System.out.println("The encoded message: " + encodedCaesarmessage(secretmessage, num));

public static String encodedCaesarmessage(String line, int n) {


3

// bulitin class

//container that efficiently stores and builds the encrypted message.

StringBuilder newmessage = new StringBuilder ();

// for each loop

for (char c: line.toCharArray()) { // for each loop to covert the line string into the array of character

if (Character.isLetter(c)) { // checks whether the chacracter is uppercase or lowercase, if not it


will skip executiom

char base = Character.isUpperCase(c)? 'A’: 'a’; // will encrypt on the bases of cases (lower or
upper)

char encryptedChar = (char) ((c - base + n) % 26 + base); // formula to conversion

newmessage.append(encryptedChar); // the new encrypted charcter will be stored here

} else {

newmessage.append(c); // if theres any space, number or punctuation, it will not


be changed.

// Keep spaces and punctuation unchanged

return newmessage.toString(); //This returns the final encrypted message as a String.

Output:
4

TASK 2
Question:

Write a java program that prompts the user for their full name and generates a pattern based on the
given input.

Solution:

Code:
public class OopstAss1T2 {

public static void main (String [] args) {

Scanner scanstr = new Scanner (System.in);

System.out.println("Enter ur name = ");

String fullname = scanstr.nextLine();

String [] Split = fullname.split(" ");

//int len = name.length();

String p1 = Split [0];

String p2 = Split [1];

pattern (Split [0]);

pattern (Split [1]);

public static void pattern (String name)

for (int i= name.length(); i>0; i--)

System.out.println(name.substring(0, i));

}
5

for (int i=2; i<=name.length(); i++)

System.out.println(name.substring(0, i));

System.out.println();

Output:

You might also like