DEPARTMENT OF COMPUTER SCIENCE ENGINEERING Java Lab Manual B.E IV Sem LORDS INSTITUTE OF ENGINEERING & TECHNOLOGY (A utonomous ) Approved by AICTE/Affiliated to Osmania Un iv ersity/Estd.2002. Accredited with ‘A’ grade by NAAC, Accredited by NBA Survey No.32, HimayathSagar, near TSPA Junction, Hyderabad - 500091 List of Experiments 1) Write a Java program to illustrate the concept of a) Command line arguments b) Type Casting 2) Illustrate the working of Scanner a) Write a java program to check the number is prime using Scanner b) Write a java program to find the factorial of a number using Scanner 3)Write a java program to accept two arrays and find the sum of corresponding elements using Buffered Reader 4) Write a Java program to illustrate the concept of Single level and Multi level Inheritance 5 ) Demonstrate how to implement polymorphism concept a) Write a Java program to illustrate the polymorphism concept with method overloading b) Write a Java program to illustrate the polymorphism concept with method overriding 6 ) Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java. util) 7) a) Write a java program to demonstrate Abstract class b) Write a Java program to demonstrate the Interface 8) Write a Java program to implement the concept of exception handling.(try, catch, throw, throws and finally) 9) Write a java program to implement thread concept a) create thread using Thread class b) create thread using Runnable interface 10) Write a Java program that reads a file name from the user, and then displays information about wh ether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes. 11) a) Write a java program to create a simple JList b) Write a java program to create simple chec k box using JCheckBox. 12) Case Study Importance of Java Programming Lab Java is fairly easy to learn and used in many mobile a nd desktop applications. There is also plenty of Java tools that make it easy for developers and beginners to use. If you’re considering a career in computer science , or are looking to build on your progra mming skills, learning Java is generally beneficial. Java is a high - level, object - oriented programming (OOP) language. Object - oriented programming languages are used by programmers to structure classes, which are used to create objects (bits of data and co de). This is what makes it different from functional programming languages such as Python, which cannot store data in objects. Java can also run on any device with Java Virtual Machine (JVM), which contains a compiler used to create byte code and is avail able on many operating systems. Students will be able to learn and understand following features and concepts. 1) Understand and Apply Object oriented features and Java concepts. 2) Understand how Java compiler is used, how to set path and other features of c ompiler which is necessary for developing java programs and developing software 3) To make the student learn an object oriented way of solving problems using java. 4) To make the students to write programs to implement basic concepts of java, overloading and overriding difference, hence implementing polymorphism principle, 5) Students will be able to understand and learn how to perform input output operation from standard input output console and other files, also understanding the difference between abstract class and interface. 6) Implementing multithreading concepts and handle exceptions. 7) Students will learn how to write programs that connects to a database and be able to perform various operations. 8) Develop applications using Console I/O and File I/O, 9) Stud ents will learn to create the Graphical User Interface using Applets, AWT Components & Swing Components. 10) They will also learn how to use web server and how to write server side programs 11) Students has to understand and implement on case study, that will be able to implement all the concepts they study in their course 12) At the end of java lab, students will be able to develop any mini projects in any domain, and can start the journey as a java developer Experiment 1: Aim : Write a Java program to illustrate the concept of a) Command line arguments b) Type Casting Objectives : Comma nd line arguments: The java command - line argument is an argument i.e. passed at the time of running the java program. The arguments passed from the console can be received in the java program and it can be used as an input. It provides a convenient way to check the behaviour of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt. Applications: Command line arguments: A command - line argument allows us to provide an unlimited number of arguments. The data is passed as strings as arguments, so we can easily convert it to numeric or other formats. It is useful for configuration information while launching our application. Objectives: Type Casting: It is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer. Applications: Type Casting : It is used in computer programming to ensure a function handles the variables correctly. A typecast example is the trans formation of an integer into a string. This could be used to compare two numbers if one is stored as a string and the other is an integer Program: class CommandLine { public static void main(String args[]) { for(int i=0; i<args.length; i++) System.out.println("args[" + i + "]: " + args[i]); } } Output: Program: public class TypeCastDemo { public static void main(String[] args) { int x = 8; double y = x; System.out.println(x); System.out.println(y); double z = 5.75d; int t = (int) z; System.out.println(z); System.out.println(t); } } Output: Experiment 2: Aim : Illustrate the working of Scanner a) Write a java program to check the number is prime using Scanner b) Write a java program to find the factorial of a number using Scanner Objectives: Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them. The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values. Applications: The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we can get input from the user in primitive types such as int, lon g, double, byte, float, short, etc Program: Write a java program to check the number is prime number using Scanner import java.util.Scanner; class Prime { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Enter a number : "); int number= sc.nextInt(); if(isPrime(number)) { System.out.println(number + " is prime number"); } e lse { System.out.println(number + " is a non - prime number"); } } static boolean isPrime(int num) { if(num<=1) { return false; } for(int i=2;i<=num/2;i++) { if((num%i)==0) return false; } return true; } } Output: Program: Write a java program to find the factorial of a number using Scanner import java.util.Scanner; class FactorialExample { public static void main(String args[]) { int i,fact=1; Scanner sc= new Scanner(System.in); System.out.println("Enter a number : "); int number= sc.nextInt(); for(i=1;i<=number;i++) { fact=fact*i; } System.out.println( "Factorial of "+number+" is: "+fact); } } Output: Experiment 3 : Aim : Write a java program to accept two arrays and find the sum of corresponding elements using Buffered Reader Objectives: Java BufferedReader class is used to read the text from a character - based input stream. It can be used to read data line by line by readLine() method. It makes the performance fast. It inherits Reade r class With help of InputStreamReader you can read the data into character stream. The InputStreamReader class reads the bytes from stream and then decode into characters. Applications: The BufferedReader class provides advanced functionality of reading the character from input stream and then buffering it for high performance reading of the stream into characters, arrays and lines. If you are writing a program to read a t ext file then you can use both classes InputStreamReader and BufferedReader classes for reading the file line by line very efficiently. Program: import java.io.*; public class BufferedReaderExample { static public void main(String args[]) { InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); int x[]=new int[5]; int y[]=new int[5]; int z[]=new int[5]; String str1,str2; System.out.println("Enter any 5 numbers in first array"); try { for(int i=0;i<5;i++) { str1 = console.readLine(); x[i] = Integer.parseInt(str1); } } catch(IOException e) { } System.out.println("Enter any 5 numbers in secon d array"); try { for(int i=0;i<5;i++) { str2 = console.readLine(); y[i] = Integer.parseInt(str2); z[i] = x[i]+y[i]; } } catch(IOException e) { } System.out.println("Resultant Array"); for(int i=0;i<5;i++) { System.out.println(z[i]); } } } Output: Experiment 4 : Aim : Write a Java program to illustrate the concept of Single level and Multi level Inheritance. Objectives: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classe s that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Y ou can add new methods and fields in your current class also. Inheritance represents the IS - A relationship which is also known as a parent - child relationship Applications: It is used f or Method Overriding (so runtime polymorphism can be achieved). It is also used for Code Reusability. We can reuse the code from the base class. Using inheritance, we can increase features of class or method by overriding. Inheritance is used to use the existing features of class. It is used to achieve runtime polymorphism i.e method overriding. Using inheritance, we can organize the information in a hierarchal form Program: a) Single Level Inheritance class A { void display() { System.out.println("I am a method from class A"); } } class B extends A { void print() { System.out.println("I am a method from class B"); } } public class SingleInheritDemo { public static void main(String[] args) { B objB = new B(); objB .display(); objB.print(); } } Output: b) Multilevel Inheritance class A { public void displayA() { System.out.println("Inside A"); } } class B extends A { public void displayB() { System.out.println("Inside B"); } } class C extends B { public void displayC() { System.out.println("Inside C"); } } public class MultilevelInherit { public static void main(String[] arguments) { C cobj = new C(); cobj.displayA(); cobj.displayB(); cobj.displayC(); } } Output: Experiment 5 : Aim : Demonstrate how to implement polymorphism concept a) Write a Java program to illustrate the polymorphism concept with method overloading b) Write a Java program to illustrate the polymorphism concept with method overriding Objectives: Polymorphism in Java is a concept by which we can perform a single action in different ways . Polymorphism is derived from 2 Greek words: poly and morphs. Th e word "poly" means many and "morphs" means forms. So polymorphism means many forms. There are two types of polymorphism in Java: compile - time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overr iding. Applications: Increases code reusability by allowing objects of different classes to be treated as objects of a common class. Improves readability and maintainability of code by reducing the amount of code that needs to be written and maintained. Supports dynamic binding, enabling the correct method to be called at runtime, based on the actual class of the object. Enables objects to be treated as a single type, making it easier to write generic code that can handle objects of different types. Program: a) Write a Java program to illustrate the polymorphism concept with method Overloading class Sum { int add(int a,int b) { return a+b; } int add(int a,int b,int c) { return a+b+c; } } class OverloadingExample { public static void main(String[] args) { Sum s = new Sum(); System.out.println(s.add(11,11)); System.out.println(s.add(11,11,11)); } } Output Program: b) Write a Java program to illustrate the polymorphism concept with method Overriding class Sample { void show() { System.out.println("show method belong to parent"); } } class Info extends Sample { void show() { System.out.println("show method belong to child"); } } class OverrideDemo { public static void main(String args[]) { Info i = new Info(); i.show(); } } Output Experiment 6 : Aim : Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use String Tokenizer class of java. util) Objectives: The java.util.StringTokenizer class allows you to break a String into tokens. It is simple way to break a String. It is a legacy class of Java. It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc. like StreamTokenizer class. In the StringTokenizer class, the delimiters can be provided at the time of creation or one by one to the tokens. Applications: The string tokenizer class allows an application to break a string into tokens The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments Program: Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use String To kenizer class of java. util) import java.util.StringTokenizer; import java.util.Scanner; public class Lineofint { public static void main(String args[]) { String s2; int sum=0,x; Scanner scan = new Scanner(System.in); String s = scan.next(); StringTokenizer st = new StringTokenizer(s,","); while (st.hasMoreTokens()) { s2=st.nextToken(); System.out.println(s2); x=Integer.parseInt(s2); sum=sum+x; } System.out.println(sum); } } Output