Pages

Total Pageviews

Sunday, June 16, 2019

Lab 12 Exercise Questions at Java Programming


import java.util.*;
import java.io.*;

class Decryptor
{
   // Decyrpting characters
   public static char decrypt(char c)
   {
      if(c > 'a' && c <= 'z')
         c--;
      else if(c > 'A' && c <= 'Z')
         c--;
      else if(c == 'a')
         c = 'z';
      else if(c == 'A')
         c = 'Z';
      return c;
   } 
   public static void writeAndPrint(PrintWriter outStream, char data)
   {
      outStream.print(data);
      System.out.print(data);
   }

   public static void main(String[] args) throws FileNotFoundException,
IOException
   {
      Scanner keyboard = new Scanner(System.in);

      System.out.println("Welcome to file decrypter");
      System.out.print("Please enter input file name: ");
      String inputFileName = keyboard.nextLine();
      System.out.print("Please enter output file name: ");
      String outputFileName = keyboard.nextLine();
     
     
      File inFile = new File(inputFileName);    //opening input file
      Scanner inScanner = new Scanner(inFile);  // Scanner to read input file
      inScanner.useDelimiter("");               // Configure scanner to read char by char


      File outFile = new File(outputFileName);  //opening output file
      FileOutputStream outFileStream = new FileOutputStream(outFile);   // creating output file strem
      PrintWriter outStream = new PrintWriter(outFileStream);           // creating print writer in order to write to output file stream

      int counter = 0;
      while(inScanner.hasNext())                // Reading input file till the end
      {
         char c = inScanner.next().charAt(0);   // reading the charachter
         char p = decrypt(c);                   // decrypting the char
         writeAndPrint(outStream, p);           // writing the decrypted char to output file and screen
      }
      inScanner.close();   //closing the input file resources
      outStream.close();   //closing the output file resources
      }
   }

import java.util.*;
import java.io.*;

class Encryptor
{
   // Encyrpting characters
   public static char encrypt(char c)
   {
      if(c >= 'a' && c < 'z')
         c++;
      else if(c >= 'A' && c < 'Z')
         c++;
      else if(c == 'z')
         c = 'a';
      else if(c == 'Z')
         c = 'A';
      return c;
   } 

   public static void main(String[] args) throws FileNotFoundException,
IOException
   {
      Scanner keyboard = new Scanner(System.in);

      System.out.println("Welcome to file encrypter");
      System.out.print("Please enter input file name: ");
      String inputFileName = keyboard.nextLine();
      System.out.print("Please enter output file name: ");
      String outputFileName = keyboard.nextLine();
     
     
      File inFile = new File(inputFileName);    // opening input file
      Scanner inScanner = new Scanner(inFile);  // Scanner to read input file
      inScanner.useDelimiter("");               // Configure scanner to read char by char


      File outFile = new File(outputFileName);  // opening output file
      FileOutputStream outFileStream = new FileOutputStream(outFile);   // creating output file strem
      PrintWriter outStream = new PrintWriter(outFileStream);           // creating print writer in order to write to output file stream

      int counter = 0;
      while(inScanner.hasNext())                // Reading input file till the end
      {
         char c = inScanner.next().charAt(0);   // reading the charachter
         char e = encrypt(c);                   // encrypting the char
         outStream.print(e);                    // writing to output file
      }
      inScanner.close();   //closing the input file resources
      outStream.close();   //closing the output file resources
   }
}

import java.util.*; 
import java.io.*;

class Lab13Ex1Program
   public static int[] readFileContent(String inputFileName) throws FileNotFoundException,
IOException

   {
      // First pass
      File inputFilePointer = new File(inputFileName);
      Scanner inputFileScanner = new Scanner(inputFilePointer);
      int counter = 0;
      while(inputFileScanner.hasNextInt())
      {
         int value = inputFileScanner.nextInt();
         counter++;
      }      
      
      inputFileScanner.close();

      int[] fileValues = new int[counter];
      int index = 0;
      //Second Pass
      inputFilePointer = new File(inputFileName);
      inputFileScanner = new Scanner(inputFilePointer);
      while(inputFileScanner.hasNextInt())
      {
         int value = inputFileScanner.nextInt();
         fileValues[index] = value;
         index++;
      }
      
      inputFileScanner.close();
      return fileValues;
   }
   
   public static void writeAndPrint(PrintWriter outStream, String data)
   {
      outStream.println(data);
      System.out.println(data);
   } 

   public static void main(String[] args) throws FileNotFoundException,
IOException
   {
      Scanner keyboard = new Scanner(System.in);

      System.out.println("Welcome to numeric anaylzer");
      System.out.print("Please enter input file name: ");
      String inputFileName = keyboard.nextLine();
      System.out.print("Please enter output file name: ");
      String outputFileName = keyboard.nextLine();
      
      int[] values = readFileContent(inputFileName);
      
      if(values.length > 0)
      {      
         int sum = 0;
         int max = values[0];
         int min = values[0];
         for(int i = 0; i<values.length;i++)
         {
            sum += values[i];
            if(values[i] > max) max = values[i];
            if(values[i] < min) min = values[i];
         }
         double avg = (double)sum / values.length;
         double stdDev = 0;
         for(int i = 0; i<values.length;i++)
         {
            stdDev += Math.pow(values[i] - avg, 2);
         }
         stdDev = Math.sqrt(stdDev/values.length);
         
         File outFile = new File(outputFileName); 
         FileOutputStream outFileStream = new FileOutputStream(outFile); 
         PrintWriter outStream = new PrintWriter(outFileStream);
         
         writeAndPrint(outStream, "Read from file: "+values.length+" values");
writeAndPrint(outStream, "\tMaximum value = " + max);
   writeAndPrint(outStream, "\tMinimum value = " + min);
   writeAndPrint(outStream, "\tAverage value = " + avg);                  
   writeAndPrint(outStream, "\tStandard Deviation = " + stdDev);
         outStream.close();
      }
      else 
      {
         System.out.println("File does not contain integers!");
      }
   } 
}


http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg

No comments:

Post a Comment