csc 185-eigth
TRANSCRIPT
-
8/14/2019 CSC 185-Eigth
1/8
CSC 185
Lab - 8
-
8/14/2019 CSC 185-Eigth
2/8
Lab Practice QuestionsEncryption and Decryption
1) Write a Java program to implement Caesar Cipher.This is a method to encrypt a given text and decrypt theencrypted text. Please read information on Caesar cipher at wikipedia.org. Example:Encryption: Key -3, For the input stringWelcome to the lab encrypted text iszhofrph wr wkh oae
Decryption: You could use Key = 3The above encrypted result should printWelcome to the lab as the decrypted result.
-
8/14/2019 CSC 185-Eigth
3/8
Your program should consist of 2 methodsEncryptMessage() andDecryptMessage().
2) Write a Java program to implement simple bankoperations withdraw and deposit using Javaclasses. The withdraw operation withdraws amount
from the bank account. The deposit operationdeposits the amount user enters from the console.
-
8/14/2019 CSC 185-Eigth
4/8
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 athttp://www.roseindia.net/java/example/java/io/Classes-
Interfaces.shtml Ex: BufferedInputStream, BufferedOutputStream,
FileReader, FileWrite, FileInputStream,FileOutputStream.
-
8/14/2019 CSC 185-Eigth
5/8
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.
-
8/14/2019 CSC 185-Eigth
6/8
Files
File is a sequence of characters whichresides on disk.
We next see how to read from and write toa file.
-
8/14/2019 CSC 185-Eigth
7/8
Reading from a Filepublic 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");}
}}
-
8/14/2019 CSC 185-Eigth
8/8
Write to a Filepublic class Main
{
public static void main(String[] args){
File myfile = new File("C:\\Documents and Settings\\RaghavendraAchanta\\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.