0% found this document useful (0 votes)
25 views12 pages

12

The document describes a Java program that encrypts strings by shifting letters forward based on whether they are vowels or consonants. It includes the algorithm, class definitions, and code to accept a string from the user, encrypt it by shifting letters, and display the encrypted string.

Uploaded by

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

12

The document describes a Java program that encrypts strings by shifting letters forward based on whether they are vowels or consonants. It includes the algorithm, class definitions, and code to accept a string from the user, encrypt it by shifting letters, and display the encrypted string.

Uploaded by

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

12) WAP in JAVA to accept a string from the user and print the encrypted

form of the letter. If the letter is a consonant interchange it with the letter
one ahead and if vowel by two ahead.

ALGORITHM

Step 1: Start

Step 2: Declare a class and write the functions with name encrypt().
Step 3: Declare a class with another name call the previous function using
extends.
Step 4: Accept the data from the user and print the values in different lines.
Step 5: Stop.

Program code:

public class Encryptography


{
void Encrypt(String s)
{
int i;String ns=""; // taking the input
for (i=0;i<s.length();i++)
{
// checking if the letter is Z
if (s.charAt(i) == 'Z')
ns = ns+'A';
if ( s.charAt(i) == 'z' )
ns = ns +'a';
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' ||
s.charAt(i) == 'u' || s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' ||
s.charAt(i) == 'O' || s.charAt(i) == 'U' ) // checking vowel
{ int n = s.charAt(i);
ns=ns+(char)(n+2);
}
else
{
int n = s.charAt(i);
ns=ns+(char)(n+1);
}
}
System.out.println(ns);
}
}
import java.util.Scanner;
public class Display extends Encryptography
{
//calling the class Encryptography
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Encryptography ob = new Encryptography ();
// creating object
System.out.print("Enter a word: ");
// accepting the input from the user
String s = sc.nextLine();
// displaying the value
ob.Encrypt(s);
}
}
13) WAP in java to accept a string and print it by modifying.
void accept() = accept the string
void encode() = interchange each letter with the letter next to it and if it,s
y then change it to a and if z then change it to b.

ALGORITHM

Step 1: Start

Step 2: Declare a class and write the functions with name accept() and
encode().
Step 3: Declare a class with another name call the previous function using
extends.
Step 4: Accept the data from the user and print the values in different lines.
Step 5: Stop.

Program code:

public class Encode


{
String str;
Encode()
{
String str = "";
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
str = sc.nextLine();
}
void encode()
{
int i,t;char ch;int l = str.length();
for(i=0;i<l;i++)
{
ch = str.charAt(i);
if (ch == 'Y' || ch == 'y')
{
t = ch - 24;
ch = (char)t;
System.out.println(ch);
}
else if (ch == 'Z' || ch == 'z')
{
t = ch - 24;
ch = (char)t;
System.out.println(ch);
}
else if (Character.isLetter(ch) == true)
{
t = ch;
t+=2;
ch = (char)t;
System.out.println(ch);
}
else
continue;
}
}
}
import java.util*;
public class Display extends Encode {
public static void main(String args[]) {
Encode ob = new Encode();
ob.accept();
ob.encode();
}
}
14) WAP in java to accept from the user his name, basic salary and
experience.
void Accept() = to accept the name, basic salary and experience void
Increment() = to calculate the increment of the salary void Display() = to
display the name, incremented salary.

ALGORITHM

Step 1: Start

Step 2: Declare a class and write the functions with name Accept(), Increment()
and Display().

Step 3: Declare a class with another name call the previous function using
extends.
Step 4: Accept the data from the user and print the values in different lines.
Step 5: Stop.

Program code:

public class Salary


{ // class to accept the salary
String name;int bas,expn;double inc,nbas;
Grade_Revision()
{
expn = 0;
nbas = 0.0;
bas = 0;
}
void Accept()
{ // accepting the values
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.print("Enter your basic salary: ");
bas = sc.nextInt();
System.out.print("Enter your experience: ");
expn = sc.nextInt();
}
void Increment()
{ // calculating the increment
if ( expn < 3 )
inc = 1000 + 10*bas/100;
if ( expn >= 3 && expn < 5 )
inc = 3000 + 12*bas/100;
if ( expn >=5 && expn < 10 )
inc = 5000 + 15*bas/100;
if (expn >= 10)
inc = 8000 + 20*bas/100;
}
void Display()
{ // displaying the values
System.out.println("Name: "+name); System.out.println("Basic salary:"+bas);
System.out.println("Experience: "+expn+" years");
System.out.println("Increment: "+(nbas+inc));
}
}
import java.util.*;
public class Display extends Salary
{ // calling the class
public static void main(String args[])
{
Salary ob = new Salary();
ob.Accept();
ob.Increment();
ob.Display();
}
}
15) WAP in java to accept from the user the name of the book and price
void accept() = to accept the name and price of the book void calculate() =
to calculate the discount on the book void display() = to display the name
and price of the book.

ALGORITHM

Step 1: Start

Step 2: Declare a class and write the functions with name accept(), calculate()
and display().
Step 3: Declare a class with another name call the previous function using
extends.
Step 4: Accept the data from the user and print the values in different lines.
Step 5: Stop.

Program code:

public class BookFair


{ // write the method according to the user
String Bname;double p,d;
BookFair()
{ // default constructor
Bname = "";
d = 0.0;
p = 0.0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of the book: "); Bname = sc.nextLine();
System.out.print("Enter the price: ");
p = sc.nextDouble();
}
void calculate()
{ // calculating the price
if (p > 0 && p <= 1000)
d = 2.0*p/100;
else if (p > 1000 && p <= 3000)
d = 10.0*p/100;
else if (p > 3000)
d = 15.0*p/100;
else
System.out.println("Invalid number");
}
void display ()
{
System.out.println("Name of the book: "+Bname); System.out.println("The
price of the book: "+p); System.out.println("The discounted price: "+(p-d));
}
}
import java.util.*;
public class See extends BookFair
{
public static void main(String args[])
{ // calling the class BookFair
BookFair p = new BookFair(); // creating object p.input(); // accepting the
input
p.calculate();
p.display(); // displayoing the values
}
}

You might also like