Csc 185-eigth

  • Uploaded by: pavanil
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Csc 185-eigth as PDF for free.

More details

  • Words: 372
  • Pages: 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/ClassesInterfaces.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.

Related Documents

Csc
May 2020 19
Csc
October 2019 31
Csc
November 2019 34
Csc Bpo
November 2019 24
Firmenvorstellung Csc
June 2020 9
Audio Csc
November 2019 22

More Documents from ""

Csc 201-lecture 14
July 2020 3
Csc 201-lecture 7
June 2020 1
Csc 185-eigth
June 2020 2
Csc 201 Lecture 4
June 2020 2
Csc 185 Lab - 7
June 2020 1