CS508 Assignment 3 Solution and Discussion Fall 2019
-
Assignment No. 03
Semester Fall 2019
CS508 - Modern Programming Languages
Total Marks: 20Due Date:16 January 2020
Instructions
Please read the following instructions carefully before solving & submitting assignment:
It should be clear that your assignment will not get any credit if:
• The assignment is submitted after due date.
• The submitted assignment does not open or file is corrupt.
• The assignment is completely or partially copied from (other student, handouts or internet).
• Student ID is not mentioned in the assignment File or name of file is other than student ID.
• The assignment is not submitted in .rar format.Submission details
Following Files Must be submitted in a single .zip or .rar file.
• Code file(s) .java file only
• A .gif file which shows working of your Application (For Recording .gif a software named Screentogif is uploaded on LMS, or you can use any other gif recording tool as well)
You are not required to submit the complete project, only copy these files from project folder. Please note if you submit doc or txt file you will be awarded 0 marks. Make sure to write your own VU ID in the assignment file(s) otherwise assignment will not be accepted.
If you do not submit any of the above mentioned file 50% marks per file will be deducted.
Objective
The objective of this assignment is to give hands on practice on Java programming language.Lectures Covered: This assignment covers Lecture # 27 – 31
Problem Statement:
You are required to create a multithreaded java console application which will read a text file and write processed output in another text file.
• The program will be able to read the text file provided named input.txt using a separate thread.
• Calculate the number of characters in the file as per your student id.
• Last number of your student id is the character your program will calculate from the given list below.Last Number in ID Character to calculate Last Number in ID Character to calculate 0 ! 5 & 1 @ 6 ( 2 # 7 ) 3 $ 8 _(Underscore) 4 % 9 = • Output the result on the screen.
• Create a new thread which will create an output file and store the result in that file.Output.gif has been attached with this Assignment file please observe this file carefully your program’s output must be like this output file.
-
-
-
Error: Could not find or load main class Cs508bs0000000.Cs508bs0000000
-
launcher.java Code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package launcher; /** * * @author Accounts */ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class Launcher { public static void main(String[] args) { BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1024); ReaderThread reader = new ReaderThread(queue); WriterThread writer = new WriterThread(queue); System.out.println("My ID is BS00000000 and special character to read is 'B'"); new Thread(reader).start(); new Thread(writer).start(); } }
ReaderThread.java Class code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package launcher; /** * * @author Accounts */ import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.concurrent.BlockingQueue; public class ReaderThread implements Runnable{ protected BlockingQueue<Integer> blockingQueue ; public ReaderThread(BlockingQueue<Integer> blockingQueue){ this.blockingQueue = blockingQueue; } @Override public void run() { System.out.println("Reader Thread is Started"); BufferedReader br = null; try { br = new BufferedReader(new FileReader(new File("./inputFile.txt"))); int buffer =0; char match = 'B'; while((buffer=br.read())!= -1){ if ((char)buffer == match){ blockingQueue.put(buffer); } } blockingQueue.put(0); //When end of file has been reached System.out.println("Reader Thread ended"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch(InterruptedException e){ }finally{ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
WriterThread.java class code
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package launcher; /** * * @author Accounts */ import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.concurrent.BlockingQueue; public class WriterThread implements Runnable{ protected BlockingQueue<Integer> blockingQueue; public WriterThread(BlockingQueue<Integer> blockingQueue){ this.blockingQueue = blockingQueue; } @Override public void run() { System.out.println("Writer Thread is Started"); PrintWriter writer = null; char match; int no = 0; try { writer = new PrintWriter(new File("outputFile.txt")); while(true){ int buffer = blockingQueue.take(); match = (char)buffer; no++; if(buffer == (0)){ break; } writer.println(no); System.out.println("'"+match+"'" + " Found so Far :" + no); } System.out.println("Writer Thread ended"); System.out.println("Total number of Special character "+"'Blue'"+" in the file are :"+ (no-1)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch(InterruptedException e){ }finally{ writer.close(); } } }
-