You are on page 1of 8

CSC 185

Lab - 8
Lab Practice Questions
Encryption and Decryption
1) Write a Java program to implement “Caesar Cipher”.
This is a method to encrypt a given text and decrypt the
‘encrypted’ text. Please read information on ‘Caesar
cipher’ at wikipedia.org.
– Example:
Encryption: Key -3, For the input string
“Welcome to the lab” encrypted text is
“zhofrph wr wkh oae”

Decryption: You could use Key = 3


The above encrypted result should print
“Welcome to the lab” as the decrypted result.
Your program should consist of 2 methods
“EncryptMessage()” and
“DecryptMessage()”.

2) Write a Java program to implement simple bank


operations ‘withdraw’ and ‘deposit’ using Java
classes. The withdraw operation withdraws amount
from the bank account. The deposit operation
deposits the amount user enters from the console.
Java I/O
• Means Java Input and Output.
• It is provided by the java.io package. This package has
InputStream and OutputStream.
• InputStream is defined for reading the stream, byte
stream and array of byte stream.
• Many list of classes provided by java.io package. Please
look into other sample classes at
http://www.roseindia.net/java/example/java/io/Classes-
Interfaces.shtml
• Ex: BufferedInputStream, BufferedOutputStream,
FileReader, FileWrite, FileInputStream,
FileOutputStream.
Standard streams
• Java supports 3 standard streams:
– Standard Input: System.in to read input from
keyboard.
– Standard Output: System.out to write output
to be displayed.
– Standard Error: System.err to write error
output to be displayed.
Files
• File is a sequence of characters which
resides on disk.
• We next see how to read from and write to
a file.
Reading from a File
public class Main
{

public static void main(String[] args)


{
File myfile = new File("C:\\Documents and Settings\\Raghavendra
Achanta\\Desktop\\bankacct.txt");

try{
Scanner s = new Scanner(myfile);
while(s.hasNextLine())
{
String data = s.nextLine();
System.out.println(data);
}
}catch(FileNotFoundException e){ System.out.println("No file specified is found");}
}
}
Write to a File
public class Main
{

public static void main(String[] args)


{
File myfile = new File("C:\\Documents and Settings\\Raghavendra
Achanta\\Desktop\\bankacct.txt");

try {
Scanner s = new Scanner(myfile);
FileWriter f = new FileWriter(myfile);
System.out.println("I am trying to write");
f.write("hello csc 185");
f.close();
System.out.println("Wrote!");

} catch (IOException ex) { System.out.println("IO exception here!");}


}
}
You have to close the file after writing ! It is not necessary to close the file after reading.

You might also like