Anda di halaman 1dari 13

1

MC0078-Spring Drive Assignment-2012


1. Discuss various features of Java and its applications. Ans: Features of Java: Java is one of the most popular language in the field of software programming. The reason behind its widely acceptance in software programming is its large number of important and robust features. Java can be used to develop wide variety of dynamic, fully secure and platform independent applications. Java has various important features: SIMPLE Java is easy to write and understand. Java programmer doesn't have to work with pointers and doesn't need to manage memory explicitly. Java eliminated the use of pointers for security reasons. Its virtual machine is able to handle the memory management and so removes the occupied memory automatically when it is no longer referenced . Programmer can now concentrate on the required application logic rather than wasting time and logic with these managements. JAVA IS OBJECT-ORIENTED There are several languages like C and C++ which are not pure object oriented but Java is fully object oriented language which follows all the rules of Oops like Inheritance, Encapsulation, Polymorphism etc and every thing can be treated in the form of objects. Even primitive data types can also be treated as objects using wrapper classes to make it fully object oriented. Class is the basic unit in Java and objects are entities following the prototype defined by the class. JAVA IS DISTRIBUTED Java supports network programming to communicate with remote objects distributed over the network. Java provides libraries like RMI and CORBA to develop network applications. JAVA IS INTERPRETED Java is called interpreted language because its byte code, which is generated after compiling the source code, is interpreted by the JVM and converts to machine dependent code which is also called native code. JAVA IS ROBUST AND SECURE Java applications are reliable in various ways. It offers compile time checking to detect early the causes of bugs, run time checking, eliminates the use of pointers which can cause memory corruption or unwanted access of memory, Chaudhary satish satish2114@yahoo.in

2 garbage collection management to free the unused memories automatically, exception handling to handle the situation at the time of occurrence of any error and a lot more. JAVA IS ARCHITECTURALLY NEUTRAL Java is designed to make the application which are neutral to systems of any architecture with variety of CPUs and Operating systems. For this, Java compiler converts the source code to the intermediate platform independent code, called byte code, at the time of compilation which has nothing to do with any system architecture. This byte code makes Java an architecture neutral language because this code can be interpreted by any system which has Java virtual machine (JVM) installed in it. MULTITHREADED In Java, multi-threaded applications are easy to develop using synchronization in comparison to other programming languages. Application performing several tasks at the same time are called multi-threaded application which are useful for providing quick and real time response. JAVA APPLICATIONS : Java-programming language was only developed for the small devices but now it can be found in a variety of devices like cell phones, e-commerce application, PCs and almost all network or computing devices. Java is available in different form: JSP : Like PHP and ASP, Java Server Pages based on a code with normal HTML tags, which helps in creating dynamic web pages. Java Applets : This is another type of Java program that used within a web page to add many new features to a web browser. These are small program used in the programming of instant messaging, chat service, solving some complex calculation and for many other purposes. J2EE : The software Java 2 Enterprise Edition are used by various companies to transfer data based on XML structured documents between one another. JavaBeans : This is something like Visual Basic and a reusable software component that can be easily assemble to create some new and advanced application. As far as syntax is concerned, Java is similar as the C programming language but a distinct style of coding. It follows all the general programming features like loops, data types, conditions, curly braces, semicolon etc. Its a Chaudhary satish satish2114@yahoo.in

3 fully featured Object Oriented Programming (OOP) language as it supports all OOP features including classes, modules, inheritance, Polymorphism etc. Mobile Java - Besides the above technology, Java is also used for various entertainment devices especially mobile phone. Mobile Information Devices Profile (MIDP) uses Java run time environment in cell phones, mobile tracking systems and other traditional PDA devices. Java technology enabled application is key to the games and services available in the mobile world. This also plays an important role in the field of telemedicine such as Pulse Meter. As far as mobile technology is concerned, it offers off-line facility, so that users can get service even if they face loss of connection. Today, all leading mobile service provider like Nokia, Siemens, Vodafone are using Java technology. Sun Java Wireless Toolkit offers complete support for developing different MIDP application. Java technology is enabled with healthy content ecosystem by offering a healthy development and deployment environment, protecting users and operators from down time and viruses. The increase volume of users now encouraging manufactures and developers to apply Java technology in numerous other productive and functional ways including MP3 players, digital TV, video, 3D, simplifying games, etc.

Chaudhary satish satish2114@yahoo.in

4 2. Write a program to perform the basic arithmetic operations: a) Addition b) Subtraction c) Multiplication d) Division Use 4 different methods for the above and invoke them as necessary from the main() method. Ans: import java.io.*; class ArithmeticOperator { public int num1; public int num2; ArithmeticOperator(int num1,int num2) { num1=num1; num2=num2; } public int Addition() { return(num1+num2); } public int Subtract() { return(num1-num2); } public int Multiply() { return(num1*num2); } public int Divide() { return(num1/num2); } } class ArthemeticOperation { public static void main(String args[]) { try { ArithmeticOperator arithmeticOperator= new ArithmeticOperator(); BufferedReader object= new BufferedReader(newInputStreamReader(System.in)); System.out.println("Enter the Number"); arithmeticOperator.num1=Integer.parseInt(object.readLine()); arithmeticOperator.num2=Integer.parseInt(object.readLine()); int addition=arithmeticOperator.Addition(); Chaudhary satish satish2114@yahoo.in

5 int subtract=arithmeticOperator.Subtract(); int multiply=arithmeticOperator.Multiply(); int divide=arithmeticOperator.Divide(); System.out.println("values is num="+ arithmeticOperator.num); System.out.println("value is num1="+ arithmeticOperator.num1); System.out.println("Addtion is="+ arithmeticOperator.Addition()); System.out.println("Subtruct is="+ arithmeticOperator.Subtract()); System.out.println("Multiply is="+ arithmeticOperator.Multiply()); System.out.println("Divide is="+ arithmeticOperator.Divide()); } catch(Exception e){} } }

Chaudhary satish satish2114@yahoo.in

6 3 Write an event driven program to perform the arithmetic operations as shown in the interface below: ans: import java.awt.*; import javax.swing.*; import java.awt.event.*; class SimpleCalculate { public static void main(String[] args) { JFrame f=new JFrame(); f.setLayout(null); JLabel lab1=new JLabel("Enter Number 1: "); JLabel lab2=new JLabel("Enter Number 2: "); JLabel lab3=new JLabel("Result: "); final JTextField text1=new JTextField(20); final JTextField text2=new JTextField(20); final JTextField text3=new JTextField(20); JButton b1=new JButton("Add"); JButton b2=new JButton("Subtract"); JButton b3=new JButton("Multiply"); JButton b4=new JButton("Division"); lab1.setBounds(20,20,100,20); text1.setBounds(140,20,100,20); lab2.setBounds(20,50,100,20); text2.setBounds(140,50,100,20); lab3.setBounds(20,80,100,20); text3.setBounds(140,80,100,20); b1.setBounds(260,80,80,20); b2.setBounds(360,80,80,20); b3.setBounds(460,80,80,20); b4.setBounds(560,80,80,20); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ int n1=Integer.parseInt(text1.getText()); int n2=Integer.parseInt(text2.getText()); int cal=n1+n2; text3.setText(Integer.toString(cal)); } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ Chaudhary satish satish2114@yahoo.in

7 int n1=Integer.parseInt(text1.getText()); int n2=Integer.parseInt(text2.getText()); int cal=0; if(n1>n2){ cal=n1-n2; } else if(n2>n1){ cal=n2-n1; } text3.setText(Integer.toString(cal)); } }); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ int n1=Integer.parseInt(text1.getText()); int n2=Integer.parseInt(text2.getText()); int cal=n1*n2; text3.setText(Integer.toString(cal)); } }); b4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ int n1=Integer.parseInt(text1.getText()); int n2=Integer.parseInt(text2.getText()); int cal=0; if(n1>n2){ cal=n1/n2; } else if(n2>n1){ cal=n2/n1; } text3.setText(Integer.toString(cal)); } }); f.add(lab1); f.add(text1); f.add(lab2); f.add(text2); f.add(lab3); f.add(text3); f.add(b1); f.add(b2); Chaudhary satish satish2114@yahoo.in

8 f.add(b3); f.add(b4); f.setVisible(true); f.setSize(700,250); } } 4. Write a complete JDBC based application where in the user supplies the following table as input and the program should have the facilities of Adding, Modifying, Viewing and Deleting the Records of the given table. Stud_No Stud_Name Course Fees Duration 1 Abc MCA 200000 3 years ANS: import java.sql.*;public class CreateTable{ public static void main(String[] args) { System.out.println("JDBC Example!"); Connection con = null; String url= "jdbc:mysql://localhost:3306/"; String dbName = "jdbcdemo"; String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "password"; try{ Class.forName(driverName).newInstance(); con = DriverManager.getConnection(url+dbName,userName,password); try{ Statement st = con.createStatement(); String create_table = "CREATE TABLE student(Stud_No integer,Stud_Name varchar(10),Course varchar(10),Fees integer,Duration varchar(10))"; String insert_table = "INSERT student VALUES(1,'Ravi','MCA',200000,'3 years')"; String insert_table = "INSERT student VALUES(1,'Ravi','MCA',200000,'3 years')"; String update_table = "UPDATE student Set Stud_Name = 'Rajesh' WHERE Stud_No = 1"; String view_table = "SELECT * FROM student"; String delete_table = "DELETE FROM student WHERE Stud_No = 1"; st.executeUpdate(create_table); st.executeUpdate(insert_table); st.executeUpdate(update_table); st.executeUpdate(view_table); st.executeUpdate(delete_table); } catch(SQLException s){ System.out.println("query did not execute successfully"); } con.close(); Chaudhary satish satish2114@yahoo.in

9 } catch(Exception e) { e.printStackTrace(); } } } 5. Explain the process of implementing a CORBA client using a Java programming example and the steps involved. ANS: Writing a CORBA client application includes the following steps. 1. Creating HelloClient.java 2. Compiling HelloClient.java 3. Running the Client Application 1.Creating HelloClient.java To create HelloClient.java, 1. Start your text editor and create a file named HelloClient.java in your main project directory, Hello. 2. Enter the following code for HelloClient.java in the text file. HelloClient.java 3. // Copyright and License 4. 5. import HelloApp.*; 6. import org.omg.CosNaming.*; 7. import org.omg.CosNaming.NamingContextPackage.*; 8. import org.omg.CORBA.*; 9. 10. public class HelloClient 11. { 12. static Hello helloImpl; 13. 14. public static void main(String args[]) 15. { 16. try{ 17. // create and initialize the ORB 18. ORB orb = ORB.init(args, null); 19. 20. // get the root naming context 21. org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService"); 22. // Use NamingContextExt instead of NamingContext. This is 23. // part of the Interoperable naming Service. 24. NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef); 25. 26. // resolve the Object Reference in Naming 27. String name = "Hello"; 28. helloImpl = HelloHelper.narrow(ncRef.resolve_str(name)); 29. Chaudhary satish satish2114@yahoo.in

10 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. System.out.println("Obtained a handle on server object: " + helloImpl); System.out.println(helloImpl.sayHello()); helloImpl.shutdown(); } catch (Exception e) { System.out.println("ERROR : " + e) ; e.printStackTrace(System.out); } } } Save and close HelloClient.java.

2. Compiling HelloClient.java Now we will compile HelloClient.java so that we can correct any errors before continuing with this tutorial. Windows users note that you should substitute backslashes (\) for the slashes (/) in all paths in this document. To compile HelloClient.java, 1. Change to the Hello directory. 2. Run the Java compiler on HelloClient.java: javac HelloClient.java HelloApp/*.java 3. Correct any errors in your file and recompile if necessary. 4. The HelloClient.class is generated to the Hello directory. 3. Running the Client Application To run this client-server application on your development machine: 1. Start orbd. To start orbd from a UNIX command shell, enter: orbd -ORBInitialPort 1050 -ORBInitialHost localhost& From an MS-DOS system prompt (Windows), enter: start orbd -ORBInitialPort 1050 -ORBInitialHost localhost Note that 1050 is the port on which you want the name server to run. -ORBInitialPort is a required command-line argument. Note that when using Solaris software, you must become root to start a process on a port under 1024. For this reason, we recommend that you use a port number greater than or equal to 1024. Note that -ORBInitialHost is also a required command-line argument. For this example, since both client and server on running on the development machine, we have set the host to localhost. When developing on more than one machine, you will replace this with the name of the host. For an example of how to run this program on two machines, see The Hello World Example on Two Machines.

Chaudhary satish satish2114@yahoo.in

11 2. Start the Hello server. To start the Hello server from a UNIX command shell, enter: java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost& From an MS-DOS system prompt (Windows), enter: start java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost For this example, you can omit -ORBInitialHost localhost since the name server is running on the same host as the Hello server. If the name server is running on a different host, use ORBInitialHost nameserverhost to specify the host on which the IDL name server is running. Specify the name server (orbd) port as done in the previous step, for example, ORBInitialPort 1050. 3. Run the client application: java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost For this example, you can omit -ORBInitialHost localhost since the name server is running on the same host as the Hello client. If the name server is running on a different host, use ORBInitialHost nameserverhost to specify the host on which the IDL name server is running. Specify the name server (orbd) port as done in the previous step, for example, ORBInitialPort 1050. 4. The client prints the string from the server to the command line: Hello world!! The name server, like many CORBA servers, runs until you explicitly stop it. To avoid having many servers running, kill the name server process after the client application returns successfully. To do this from a DOS prompt, select the window that is running the server and enter Ctrl+C to shut it down. To do this from a Unix shell, find the process, and kill it.

Chaudhary satish satish2114@yahoo.in

12 6. Write a program using iostreams to take as input two multi-dimensional arrays and print their sum as output (Matrix Addition) in Matrix format. ANS: import java.io.*; import java.util.Scanner; class add_arr { public static void main(String args[]) throws IOException { int arr1[][] = new int[3][3]; int arr2[][] = new int[3][3]; int arr3[][] = new int[3][3]; Scanner scanner = new Scanner(System.in); System.out.println("Enter Matrix 1 : ");

for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { arr1[i][j] = scanner.nextInt(); } } System.out.println("Enter Matrix 2 : "); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { arr2[i][j] = scanner.nextInt(); } } System.out.println("Addition of Matrix 1 and Matrix 2: "); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { arr3[i][j] = arr1[i][j]+arr2[i][j]; } } for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(arr3[i][j]); } Chaudhary satish satish2114@yahoo.in

13 System.out.println(); } } }

Chaudhary satish satish2114@yahoo.in

Anda mungkin juga menyukai