Thursday, 1 September 2016

CORE JAVA LAB@HOME 10

Question-1 Accept the two number and perform the operation.

Answer:-

import java . util . Scanner;


public class Divison {


 public static void main (String args []) {

  

  int num1, num2, result;

  Scanner input = new Scanner (System . in);

  System.out.println ("Enter the 1st number");

  num1 = input . nextInt ();


 System.out.println ("Enter the 2nd number");


 num2 = input . nextInt ();


 try {


   result = num1 / num2;

   System.out.println ("The result is " + result);


 } catch (ArithmeticException e) {


 System,out.println ("Divison by zero not Possible! ");

  }


 }


}


Question-2 Crearte a class which handles the query details.

Answer:-

 import java . io . BufferReader;

import java  . io . BufferReader;

import java . io . FileReader;

import java . io . FileWriter;

import java . io . Scanner;


public class EnquiryForm

{


String enquiryrDetails [] = new String [5];


public void showMenu () {

int option;

Scanner sc = new Scanner (System . in);


System.out.println ("\n-----------Menu-----------");

System.out.println ("1. Initiate Enqiury");

System.out.println ("2. View Enquiry");

System.out.println ("3. Exit");

System.out.println ("\nChoose the option: ");


 option = sc . nextInt ();


 switch (option) {

    case 1:

       intEnq ();

       showMenu ();

       break;

    case 2:

        viewEnq ();

        showMenu ();

        break;

    case 3:

         exitMenu ();

         break;

      default:

        System.out.println ("Incorrect menu option");

          showMenu ();

          break;

      }

   }


 public void initEnq () {

 

  char choice = 'y';


 do {

      Scanner sc = new Scanner (System . in(;

      System.out.println ("Enter Name: ");

      enquiryDetails [0] = sc . nextLine () . to LowerCase () + ",";

  

      System.out.print ("Enter Contact No: ");

      enquiryDetails [1] = sc . nextLine () + ",";

      System.out,print ("Enter Address: () + ",";

      System.out.print ("Enter Enquiry Category

   (Prices/Local/Packages) : ");

     enquiryDetails [3] = sc . nextLine () + ",";

    System.out.print ("Enter Enquiry Description: ");

     enquiryDetails [4] = sc . nextLine () + ",";


      try {

          FileWriter fos = new FileWriter

   ("Enquiry . txt ", true);

               BufferedWWriter bw = new BufferedWriter

    (fos);

            for (int i = 0; i < enquiryDetails . length

    i++) {


             bw . write (enquiryDetails [i]);

         }

           bw . newLine ();

           bw . close ();

          fos . close ();

      } catch (Exception e) {

        System.out.println (e);

      }

      System.out.print ("\nDo you want to initiate more enquiry: ");

      String ch = sc . next () . toLowerCase ();

       choice = ch . charAt (0);

    } while (choice == 'y');

 }

    public void viewEnq () {

   Scanner sc = new Scanner (System . in);

   String name;

   int flag = 0;

   System.out.print("Enter the name of the user to view the details: ");

    name = sc . nextLine () . toLowerCase ();

   try (BufferedReader br = new BufferedReader(new

FileReader("Enquiry.txt ")))    {

            String s;

            while ((s = br.readLine())  != null)   {

               String plitData[] = s.split(",");

             if (splitData[O].equals(name))    {

                 System.out.println("Enquiry Detail:");

                 System.out.println

("=================================")

                  System.out.println("Name:

 " + splitData[O] );

                  System.out.println("contact No:

" + splitData[1] );

                  System.out.println("Address:

" + splitData[2] );

                  System.out.println("Enquiry Catagory:

" + splitData[3] );

                   System.out.println ("Enquiry Description: " + splitData [4]);

               System.out.println

 ("===================================");

           flag = 1;

        }

    }

      if (flag == 0) {

       System.out.println (name + " does not exists");

      }

    } catch (Exception e) {

    }


    public void wxitMenu () {


     System.exit (0);

}

   public static void main (String [] args) {

    EnqiuryForm obj = new EnquiryForm ();

    obj . showMenu ();

}


}

No comments:

Post a Comment