Department of Information Science and Engineering 1 Department of Information Science and Engineering JAVA AND J2EE LAB [18IS6DLJVA ] Faculty : Mrs.Radhika T V Assistant Professor , Dept of ISE DSCE 2 Course OutcomesJAVA AND J2EE LAB [18IS6DLJVA ] CO1 - Write, compile and execute java programs. CO2- Apply the basic concepts of object oriented programming in writing java programs. CO3-Design and use classes, packages and interfaces CO4-Analyze and implement exception handling, event handling CO5-Develop graphical user interface using swings CO6-Construct web applications using Servlets and JSP Program 1 a) Write a Java program to implement linear search. Linear Search •It is also known as sequential search- A method for finding an element within a list. • Given a list of elements in an array, a[] •We need to search given element x in a[] Input and output statements in Java • In Java we need to use special methods to perform i/o. • Every class has – Properties (variables) – Actions(Methods) • In order to access members of class, we need object. • Input and output functions in java are called through class name. • In Java, there are 3 main I/O streams. All these streams are attached with the console. 1) System.in: standard input stream 2) System.out: standard output stream 3) System.err: standard error stream • All the above I/O streams are available in Java.lang package • import.java.lang.* Cntd.. • Following are the output statements in java – System.out.print – System.out.println – System.out.printf • Input functions are used to read lines of input at runtime • To do this following classes are used – Scanner – Buffer Reader Scanner Class in Java • Scanner is a class in Java used to read input from keyboard. • java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. • Make use of -import java.util.scanner -import java.util.* • To access methods of Scanner class –create object. • Methods available are – – – – next() next Int() next Float() next Double() • Syntax for reading input through Scanner class: Classname objectname=new constructor(); Ex: Scanner sc=new Scanner(System.in); Program 1)a import java.util.Scanner; Class LinearS { Public static void main(String args[]) { int counter, num, item, array[]; Scanner input =new Scanner(System.in); System.out.println("Enter number of elements"); num =input.nextInt(); array = new int[num]; System.out.println("Enter "+ num +" integers"); for(counter =0; counter < n; counter++) array[counter]=input.nextInt(); System.out.println("Enter the search value"); item=input.nextInt(); Program 1)a cntd.. for(counter =0; counter < n; counter++) { if(array[counter]== item) { System.out.println(item +" is present at location "+(counter+1)); break; } } if(counter == num) System.out.println(item +" is not present in array."); } } Output: Enter the number of elements 4 Enter 4 integer 20 52 17 92 Enter search value 17 17 is present at location 3 Buffer Reader • Java BufferedReader class is used to read the text from a character-based input stream. • Reads input through keyboard or via existing file • Following classes are used – InputstreamReader (System.in) – FileReader • • • • Need to include IOException Buffer Reader class is available in java.io package Import java.io.* Methods available in BufferReader Class are – read() – readLine() Cntd.. • For type casting following methods are used – Integer.parseInt() – Float.parseFloat() • Syntax for reading bytes and converting to character set: (i) InputStreamReader ir=new InputStreamReader(System.in) • To read character set data from buffer (ii)BufferReader br=new BufferReader(ir) • compareToIgnoreCase() - compares two strings lexicographically Program 1)b Write a java program for sorting a given list of names. import java.io.*; class SortingNames { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter The number of Names :"); int n = Integer.parseInt(br.readLine()); String names[] = new String[n]; System.out.println(); for (int i = 1; i <= n; i++) { System.out.print("Enter Name " + i + ":"); names[i-1] = br.readLine(); } Cntd.. System.out.println("\nNames in Ascending Order"); System.out.println(); for (int j = 0; j < names.length; j++) Output: { Enter the number of for (int i = j + 1; i < names.length; i++) names:3 { Enter name 1: ab if (names[i].compareToIgnoreCase(names[j]) < 0) Enter name 2: abc { Enter name 3:a String temp = names[j]; Names in Ascending Order names[j] = names[i]; a names[i] = temp; ab } abc } System.out.println(names[j]); } } }