Laboratory Manual Fundamentals of Programming Languages-II First Year Engineering Teaching Scheme Examination Scheme Theory : 1 Hrs/Week Term Work: Online Exam : 2 Hrs/Week Online Exam : 50 Marks Oral : —— —— Prepared By Prof. Bhagwat K. S. Prof. Katkar M. G. Prof. Rampurkar V. V. Department of First Year Engineering Vidya Pratishthan’s College of Engineering Baramati – 413133, Dist- Pune (M.S.) INDIA December 2014-15 Table of Contents 1 Linux Basic Commands 1.1 Problem Statement . . . . . . . . . . 1.2 Pre Lab . . . . . . . . . . . . . . . . 1.3 Hardware and Software Requirement 1.4 Theory . . . . . . . . . . . . . . . . . 1.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 6 2 First C++ Hello World program. 2.1 Problem Statement . . . . . . . . . . 2.2 Pre Lab . . . . . . . . . . . . . . . . 2.3 Hardware and Software Requirement 2.4 Theory . . . . . . . . . . . . . . . . . 2.4.1 Object oriented programming 2.4.2 steps to operate Eclipse . . . 2.4.3 Required Output . . . . . . . 2.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 7 7 7 7 7 8 9 10 3 First Java Hello World program 3.1 Problem Statement . . . . . . . . . . 3.2 Pre Lab . . . . . . . . . . . . . . . . 3.3 Hardware and Software Requirement 3.4 Theory . . . . . . . . . . . . . . . . . 3.4.1 Required Output . . . . . . . 3.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 11 11 11 11 14 16 4 Web Technology Programming 4.1 Problem Statement . . . . . . . . . . 4.2 Pre Lab . . . . . . . . . . . . . . . . 4.3 Hardware and Software Requirement 4.4 Theory . . . . . . . . . . . . . . . . . 4.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 17 17 17 17 21 5 Java class to add integer numbers 5.1 Problem Statement . . . . . . . . . . 5.2 Pre Lab . . . . . . . . . . . . . . . . 5.3 Hardware and Software Requirement 5.4 Theory . . . . . . . . . . . . . . . . . 5.4.1 Algorithm . . . . . . . . . . . 5.4.2 Required Output . . . . . . . 5.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 22 22 22 22 22 23 23 . . . . . . . . . . . . . . . i . . . . . TABLE OF CONTENTS TABLE OF CONTENTS 6 Java class for binary search 6.1 Problem Statement . . . . . . . . . . 6.2 Pre Lab . . . . . . . . . . . . . . . . 6.3 Hardware and Software Requirement 6.4 Theory . . . . . . . . . . . . . . . . . 6.4.1 Binary search algorithm . . . 6.4.2 Required Output . . . . . . . 6.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 24 24 24 24 24 27 27 7 Java class to find palindrome 7.1 Problem Statement . . . . . . . . . . 7.2 Pre Lab . . . . . . . . . . . . . . . . 7.3 Hardware and Software Requirement 7.4 Theory . . . . . . . . . . . . . . . . . 7.4.1 Algorithm . . . . . . . . . . . 7.4.2 Required Output . . . . . . . 7.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 28 28 28 28 28 30 30 8 Java program for arithmetic operations 8.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Pre Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Hardware and Software Requirement . . . . . . . . . . . . . . . 8.4 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.4.1 Simple Calculator Using Switch Case Statement in java 8.4.2 Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 8.4.3 Required Output . . . . . . . . . . . . . . . . . . . . . . 8.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 31 31 31 31 31 32 33 33 9 C++ Class for sorting numbers 9.1 Problem Statement . . . . . . . . . . 9.2 Pre Lab . . . . . . . . . . . . . . . . 9.3 Hardware and Software Requirement 9.4 Theory . . . . . . . . . . . . . . . . . 9.4.1 Required Output . . . . . . . 9.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 34 34 34 34 36 36 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Java class from Char to String and String to 10.1 Problem Statement . . . . . . . . . . . . . . . 10.2 Pre Lab . . . . . . . . . . . . . . . . . . . . . 10.3 Hardware and Software Requirement . . . . . 10.4 Theory . . . . . . . . . . . . . . . . . . . . . . 10.5 Algorithm . . . . . . . . . . . . . . . . . . . . 10.5.1 Required output . . . . . . . . . . . . 10.6 Post Lab . . . . . . . . . . . . . . . . . . . . . Char . . . . . . . . . . . . . . . . . . . . . . . . . . . . conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 38 38 38 38 41 41 41 11 Java class for showing the current Time 11.1 Problem Statement . . . . . . . . . . . . 11.2 Pre Lab . . . . . . . . . . . . . . . . . . 11.3 Hardware and Software Requirement . . 11.4 Theory . . . . . . . . . . . . . . . . . . . 11.4.1 Algorithm . . . . . . . . . . . . . 11.4.2 Expected output . . . . . . . . . 11.5 Post Lab . . . . . . . . . . . . . . . . . . Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 42 42 42 42 42 43 43 and . . . . . . . . . . . . . . . . . . . . . ii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . TABLE OF CONTENTS TABLE OF CONTENTS 12 Installation of Android ADT for Eclipse 12.1 Problem Statement . . . . . . . . . . . . 12.2 Pre Lab . . . . . . . . . . . . . . . . . . 12.3 Hardware and Software Requirement . . 12.4 Theory . . . . . . . . . . . . . . . . . . . 12.4.1 Installing the Eclipse Plugin . . . 12.4.2 Download the ADT Plugin . . . 12.4.3 Configure the ADT Plugin . . . 12.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 44 44 44 44 44 45 45 45 13 implementation on Android Simulator 13.1 Problem Statement . . . . . . . . . . . 13.2 Pre Lab . . . . . . . . . . . . . . . . . 13.3 Hardware and Software Requirement . 13.4 Theory . . . . . . . . . . . . . . . . . . 13.5 Post Lab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 46 46 46 46 50 . . . . . References 51 iii List of Tables iv List of Figures 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 Linux commands . . . . . . . Linux commands . . . . . . . Linux commands . . . . . . . Linux commands . . . . . . . Linux commands . . . . . . . Linux commands . . . . . . . Linux commands . . . . . . . Snapshot of Linux commands . . . . . . . . 2 2 3 3 4 4 5 6 2.1 First C++ hello world program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.1 3.2 3.3 3.4 3.5 3.6 Main eclipse window. . add new file window. . new java class window. editor window. . . . . editor window. . . . . editor window. . . . . 4.1 4.2 4.3 4.4 HTML HTML HTML HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 13 13 14 15 15 example example example example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 19 20 20 5.1 Required output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 6.1 6.2 Examples of binary search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Required output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 27 7.1 Required output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 8.1 Required Arithmetic operation output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 9.1 9.2 Sorting steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sorting output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 37 10.1 10.2 10.3 10.4 String variable declaration . . . . . . . . String is stored in memory . . . . . . . array of characters is stored in memory: String to char . . . . . . . . . . . . . . . . . . 39 39 40 41 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 13.1 New Project window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Tags Tags Tags Tags with with with with 11.1 Output window . . . . . . . . . . . . . . . . . . v . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . LIST OF FIGURES LIST OF FIGURES 13.2 Android Application Output window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi 49 Assignment 1 Linux Basic Commands 1.1 Problem Statement Use and study of Linux commands 1.2 Pre Lab Knowledge of Linux GUI 1.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-Linux OS 1.4 Theory To invoke terminal application to run linux commands follow the following steps: From desktop select ”Application” tab; Then select ”Accessories” sub menu; Then select ”Terminal” option..it will display terminal window for command interaction. 1 1.4. THEORY Linux Basic Commands Figure 1.1: Linux commands Figure 1.2: Linux commands Lab Manual - Fundamentals of Programming Languages-II 2 VPCOE, Baramati 1.4. THEORY Linux Basic Commands Figure 1.3: Linux commands Figure 1.4: Linux commands Lab Manual - Fundamentals of Programming Languages-II 3 VPCOE, Baramati 1.4. THEORY Linux Basic Commands Figure 1.5: Linux commands Figure 1.6: Linux commands Lab Manual - Fundamentals of Programming Languages-II 4 VPCOE, Baramati 1.4. THEORY Linux Basic Commands Figure 1.7: Linux commands Lab Manual - Fundamentals of Programming Languages-II 5 VPCOE, Baramati 1.5. POST LAB Linux Basic Commands Figure 1.8: Snapshot of Linux commands 1.5 Post Lab Hence from this assignment you can learn linux basic commands. Lab Manual - Fundamentals of Programming Languages-II 6 VPCOE, Baramati Assignment 2 First C++ Hello World program. 2.1 Problem Statement Write a C++ program in Eclipse to display Hello World string. 2.2 Pre Lab Knowledge of programming in C . Knowledge of object oriented concepts of C++ and programming in C++. 2.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-Linux O.S.,Eclipse 2.4 Theory 2.4.1 Object oriented programming Concepts 1. Class: Class is a way to bind the data and its associated functions together. It allows data to be hidden if necessary from external use.It has two parts 1) Class declaration and Class function definition.The class declaration describes the type and scope of its members.The class function definitions describe how the class functions are implemented. General form of class declaration is: class classname 7 2.4. THEORY First C++ Hello World program. { private: variable declarations; function declarations; public: variable declarations; function declarations; }; 2. Object Objects are basic run time entities in an object oriented system.When a program is executed, the object intrcts by sending messages to one another. for example, if ”customer” and ”account” are two objects in program then a customer object may send message to account object requesting for the bank balance. 2.4.2 steps to operate Eclipse Run Eclipse:- 1. Doubleclick on the Eclipse icon in the Eclipse folder. 2. Hit OK in the dialog box that asks which workspace it should use. Workspaces in Eclipse are folders on your computer where Eclipse will store all the files for your projects. Create a C++ project:- Now you need to create a C++ project. An Eclipse project is a complete set of files which will be one program when it is done, same as your complete robot project. 1. Go to the File menu and choose New. 2. Pick C++ Project. 3. A dialog box will pop up offering you a set of possibilities. Since you are trying to learn Eclipse, choose Empty Project and use Hello World for the project name and click the Finish button. Create a C++ Source File:- Lab Manual - Fundamentals of Programming Languages-II 8 VPCOE, Baramati 2.4. THEORY First C++ Hello World program. 1. Right click on the HelloWorld entry in the Project Explorer. The Project Explorer will be on the left side of the Eclipse Window. 2. Select New from the menu that appears, then Source File. 3. A dialog box will appear. Type hello.cpp in the Source File text box and click the Finish button. Type In Your Program:- Type in the following code in the hello.cpp window. figure 1.1 shows the code of first C++ hello world program : Figure 2.1: First C++ hello world program. Build Your Program:- Now we need to build your program, which means we convert the C++ code into something that your computer actually understands. 1. Click on the HelloWorld project in the Project Explorer. 2. Go to the Project menu and chose Build Project. You should see a bunch of output at the bottom of the screen. Run Your Program:Now try running your program. 1.Go to the Run menu and chose Run. You should see Hello World!! appear in a tab called Console at the bottom of the screen. If not, did you type something wrong in your program. If so, fix it and build and run again. 2.4.3 Required Output Hello world! Lab Manual - Fundamentals of Programming Languages-II 9 VPCOE, Baramati 2.5. POST LAB 2.5 First C++ Hello World program. Post Lab Hence from this assignment you can learn how to compile and execute simple C++ program in Eclips IDE. Lab Manual - Fundamentals of Programming Languages-II 10 VPCOE, Baramati Assignment 3 First Java Hello World program 3.1 Problem Statement Write a Java program in eclipse to display Hello World string. 3.2 Pre Lab Knowledge of programming in C . Knowledge of object oriented concepts of C++. 3.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.,Eclipse 3.4 Theory Initializing Eclipse:When you develop Java applications in Eclipse, it stores all the created files in a directory called ”workspace”. When Eclipse is run for the first time, it will ask you where you want the workspace to be placed: You can just use the default location or specify your preferred location. To avoid getting asked this question every time you start Eclipse, check ”Use this as the default and do not ask again” option and press ”OK” button. Once Eclipse finishes its startup process, you will see the welcome window: Click the ”Workbench” icon on the right, which will lead you to the main Eclipse window: 11 3.4. THEORY First Java Hello World program Figure 3.1: Main eclipse window. Creating a Project:- Now that you’ve got Eclipse up and running, it’s time to create your first Java project. To do this, you’ll want to go File − > New − > Java Project. Type your project name (say, cs144project1) in the ”Project name” field and click Finish. Then the name of your newly created project will appear on the left side of the Eclipse window (this part of the window is called ”Package explorer pane”): As you create more projects in Eclipse, other project names will appear in the Package explorer pane and you will be able to switch between your projects by clicking the name of a project. Adding a New File to a Project: Now that you’ve created your first project, you now want to create a new Java file (with .java extension) and add it into your project. To create a new Java file, right click on the name of your project (cs144project1) in the Package explorer pane and select New − > Class as follows: This command will show you a window that looks like the following: In the ”Name: ” section provide the name of the file (or the class) you want to create, HelloWorld, and click ”Finish” button. Lab Manual - Fundamentals of Programming Languages-II 12 VPCOE, Baramati 3.4. THEORY First Java Hello World program Figure 3.2: add new file window. Figure 3.3: new java class window. Congratulations! Now you have created your first Java code in eclipse. As you can see from the Package explore pane, your project now includes HelloWorld.java file. The ”Editor pane” to the right of the Package explorer pane shows the actual content of the HelloWorld.java file, which simply declares HelloWorld as a public class. You can edit the content of the Java code inside the Editor pane. Saving, compiling, and running Java code:Now let us learn how to code, compile and run a Java program in Eclipse. Now save the file by selecting File − > Save, or pressing Ctrl-S (Option-S on Mac). When you save a Java file, Eclipse will automatically compile the file also, so that you don’t need to compile it later when you want to run it. Lab Manual - Fundamentals of Programming Languages-II 13 VPCOE, Baramati 3.4. THEORY First Java Hello World program Figure 3.4: editor window. Now that your code has been saved and compiled, you can run your program by selecting Run − > Run, or by pressing Ctrl-F11 (Option-F11 on Mac) or by clicking on the ”Run” button near the top of the window. Once your program finishes running, you will be able to see the output of your program by selecting the ”Console tab” at the bottom of the window. 3.4.1 Required Output Hello world! Lab Manual - Fundamentals of Programming Languages-II 14 VPCOE, Baramati 3.4. THEORY First Java Hello World program Figure 3.5: editor window. Figure 3.6: editor window. Lab Manual - Fundamentals of Programming Languages-II 15 VPCOE, Baramati 3.5. POST LAB 3.5 First Java Hello World program Post Lab Hence from this assignment you can learn how to create first java Hello world program in Eclipse. Lab Manual - Fundamentals of Programming Languages-II 16 VPCOE, Baramati Assignment 4 Web Technology Programming 4.1 Problem Statement Web technology programming using HTML for Hello World program, display images, web links 4.2 Pre Lab Knowledge of web technology programming in HTML . 4.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-Internet Explorer 4.4 Theory 17 4.4. THEORY Web Technology Programming Figure 4.1: HTML Tags with example Lab Manual - Fundamentals of Programming Languages-II 18 VPCOE, Baramati 4.4. THEORY Web Technology Programming Figure 4.2: HTML Tags with example Lab Manual - Fundamentals of Programming Languages-II 19 VPCOE, Baramati 4.4. THEORY Web Technology Programming Figure 4.3: HTML Tags with example Figure 4.4: HTML Tags with example Lab Manual - Fundamentals of Programming Languages-II 20 VPCOE, Baramati 4.5. POST LAB 4.5 Web Technology Programming Post Lab hence from this assignment you learn creation of Web technology programming using HTML for Hello World program, display images, web links Lab Manual - Fundamentals of Programming Languages-II 21 VPCOE, Baramati Assignment 5 Java class to add integer numbers 5.1 Problem Statement Write a class in java to add integer numbers 5.2 Pre Lab Knowledge of programming in C . Knowledge of object oriented concepts of C++. 5.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8, Eclipse 5.4 Theory 5.4.1 Algorithm 1. import following package import java.util.Scanner; 2. create class-class AddNumbers 3. define methodpublic static void main(String args[]) 22 5.5. POST LAB Java class to add integer numbers 4. create object of Scanner class Scanner in = new Scanner(System.in); 5. read input from user as integer values by using: x = in.nextInt(); y = in.nextInt(); z = x + y; 6. display addition of integer numbers using : System.out.println(”Sum of entered integers = ”+z); 5.4.2 Required Output Figure 5.1: Required output 5.5 Post Lab Hence from this assignment you learn creation of java program to add integer numbers. Lab Manual - Fundamentals of Programming Languages-II 23 VPCOE, Baramati Assignment 6 Java class for binary search 6.1 Problem Statement Write a class for binary search. 6.2 Pre Lab Knowledge of programming in C. Knowledge of object oriented concepts of C++. 6.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.,Eclipse 6.4 6.4.1 Theory Binary search algorithm Generally, to find a value in unsorted array, we should look through elements of an array one by one, until searched value is found. In case of searched value is absent from array, we go through all elements. In average, complexity of such an algorithm is proportional to the length of the array. Situation changes significantly, when array is sorted. If we know it, random access capability can be utilized very efficiently to find searched value quick. Cost of searching algorithm reduces to binary logarithm of the array length. For reference, log2(1 000 000) 20. It means, that in worst case, algorithm 24 6.4. THEORY Java class for binary search makes 20 steps to find a value in sorted array of a million elements or to say, that it doesn’t present it the array. Algorithm: Algorithm is quite simple. It can be done either recursively or iteratively: 1. get the middle element; 2. if the middle element equals to the searched value, the algorithm stops; 3. otherwise, two cases are possible: -searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element. -searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element. Now we should define, when iterations should stop. First case is when searched element is found. Second one is when subarray has no elements. In this case, we can conclude, that searched value doesn’t present in the array. Lab Manual - Fundamentals of Programming Languages-II 25 VPCOE, Baramati 6.4. THEORY Java class for binary search Figure 6.1: Examples of binary search Lab Manual - Fundamentals of Programming Languages-II 26 VPCOE, Baramati 6.5. POST LAB 6.4.2 Java class for binary search Required Output Figure 6.2: Required output 6.5 Post Lab Hence from this assignment students can learn how to write java class for binary search. Lab Manual - Fundamentals of Programming Languages-II 27 VPCOE, Baramati Assignment 7 Java class to find palindrome 7.1 Problem Statement Write a java program for finding palindrome. 7.2 Pre Lab Knowledge of programming in C . Knowledge of object oriented concepts of C++. 7.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8. 7.4 Theory Java palindrome program: Java program to check if a string is a palindrome or not. Remember a string is a palindrome if it remains unchanged when reversed, for example ”dad” is a palindrome as reverse of ”dad” is ”dad” whereas ”program” is not a palindrome. 7.4.1 Algorithm 1. first import following package import java.util.*; 28 7.4. THEORY Java class to find palindrome for taking input string 2. define class Palindrome 3. define inside the class follwing method public static void main(String args[]) 4. declare variables and object of Scanner class to take user input String original, reverse=””; Scanner in = new Scanner(System.in); 5. Enter a string to check if it is a palindrome by using following statement original = in.nextLine(); 6. find length by using following statement int length = original.length(); 7. make reverse of entered string using following statements for ( int i = length − 1; i >= 0; i − − ) reverse = reverse + original.charAt(i); 8. check reversed string with original one using if statement if (original.equals(reverse)) 9. if condition is true then display messages on console ”Entered string is a palindrome.” 10. else display message ”Entered string is not a palindrome.” Lab Manual - Fundamentals of Programming Languages-II 29 VPCOE, Baramati 7.5. POST LAB 7.4.2 Java class to find palindrome Required Output Figure 7.1: Required output 7.5 Post Lab Hence from this assignment you learn how to create java program to check string is palindrome or not Lab Manual - Fundamentals of Programming Languages-II 30 VPCOE, Baramati Assignment 8 Java program for arithmetic operations 8.1 Problem Statement Write a java program to calculate addition, subtraction,multiplication and division. 8.2 Pre Lab Knowledge of programming in C . Knowledge of object oriented concepts of C++. 8.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.,Eclipse 8.4 8.4.1 Theory Simple Calculator Using Switch Case Statement in java This program is a simple calculator designed in java using switch case statement to perform some basic arithmetic operation on two integers entered by user. It enables the user to enter a choice that corresponds to one of the four basic arithmetic operation, addition, subtraction, multiplication and division. The control structure used in this code is switch 31 8.4. THEORY Java program for arithmetic operations case, which works in the same manner as that in C or C++. 8.4.2 Algorithm 1. import package import java.util.Scanner; 2. create object of Scanner class to take user input Scanner s=new Scanner(System.in); 3. Display menu ”YOU HAVE FOLLOWING CHOICES : ” ”1. ADDITION” ”2. SUBTRACTION ” ”3. MULTIPLICATION ” ”4. DIVISION” ”ENTER YOUR CHOICE : ” 4. read user choice using statement int i=s.nextInt(); 5. take two numbers from user using statements int a=s.nextInt(); int b=s.nextInt(); 6. write switch case and perform each separate arithmetic operation in separate case case 1: result=a+b; break; case 2: result=a-b; break; case 3: result=a*b; break; Lab Manual - Fundamentals of Programming Languages-II 32 VPCOE, Baramati 8.5. POST LAB Java program for arithmetic operations case 4: result=a/b; default: System.out.println(”YOU HAVE ENTERED A WRONG CHOICE”); 7. display result 8.4.3 Required Output Figure 8.1: Required Arithmetic operation output 8.5 Post Lab Hence from this assignment you learn use of JAVA program for arithmetic operation. Lab Manual - Fundamentals of Programming Languages-II 33 VPCOE, Baramati Assignment 9 C++ Class for sorting numbers 9.1 Problem Statement Write a C++ class for sorting Numbers in Ascending or Descending order 9.2 Pre Lab Knowledge of programming in C . Knowledge of object oriented concepts of C++. 9.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8.,Eclipse 9.4 Theory Number Sorting Techniques: 1. Bubble Sort 2. Selection Sort 3. Insertion Sort 4. Quick Sort 34 9.4. THEORY C++ Class for sorting numbers 5. Merge Sort 6. Bucket Sort Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements ”bubble” to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists. Bubble sort is not a stable sort which means that if two same elements are there in the list, they may not get their same order with respect to each other. Step-by-step example: Let us take the array of numbers ”5 1 4 2 8”, and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required. Figure 9.1: Sorting steps Lab Manual - Fundamentals of Programming Languages-II 35 VPCOE, Baramati 9.5. POST LAB C++ Class for sorting numbers Algorithm: 1. Enter the no. of elements to sort and store in n. 2. Enter the actual elements and store in array a[]. 3. for i=0 to n { for j=i to n-1 { if(a[i] > a[j+1] ) { temp= a[i]; a[i]= a[j+1]; a[j+1]= temp; } } } 4. Display sorted array a[] 9.4.1 9.5 Required Output Post Lab Hence from this assignment students can learn C++ class for sorting numbers in ascending or descending order. Lab Manual - Fundamentals of Programming Languages-II 36 VPCOE, Baramati 9.5. POST LAB C++ Class for sorting numbers Figure 9.2: sorting output Lab Manual - Fundamentals of Programming Languages-II 37 VPCOE, Baramati Assignment 10 Java class from Char to String and String to Char conversion 10.1 Problem Statement Write a class to convert String into chars and chars into String. 10.2 Pre Lab Knowledge of programming in C. Knowledge of object oriented concepts of C++. 10.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software-JDK 1.7, NetBeans IDE 6.8. 10.4 Theory Strings in Java refer figure: How a String is stored in memory refer figure: Now compare with how an array of characters is stored in memory: 38 10.4. THEORY Java class from Char to String and String to Char conversion Figure 10.1: String variable declaration Figure 10.2: String is stored in memory Lab Manual - Fundamentals of Programming Languages-II 39 VPCOE, Baramati 10.4. THEORY Java class from Char to String and String to Char conversion Figure 10.3: array of characters is stored in memory: Lab Manual - Fundamentals of Programming Languages-II 40 VPCOE, Baramati 10.5. ALGORITHM 10.5 Java class from Char to String and String to Char conversion Algorithm 1. read string and store in s = ”abc”; 2. create character array A[] of type char. char[] A= new char[4];; 3. To convert String s into an array of char use A = s.toCharArray(); 4. Display string s into an array of char for i = 0 to A.length do System.out.println( ”A [ ” + i + ” ] = ” + A [i] ); 5. To convert an array of char A into string, assign characters to array A. A[0] = ’v’; A[1] = ’p’; A[2] = ’c’; A[3] = ’o’; Then allocate memory to s = new String( A ); 6. for int i = 0 to A.length System.out.println( ”A[” + i + ”] = ” + A[i] ); 7. Display string s; 10.5.1 Required output Figure 10.4: String to char 10.6 Post Lab Hence from this assignment students can learn creation of java class to convert String into chars and chars into String. Lab Manual - Fundamentals of Programming Languages-II 41 VPCOE, Baramati Assignment 11 Java class for showing the current Time and Date 11.1 Problem Statement Write a class for showing the current Time and Date. 11.2 Pre Lab Knowledge of Java Programming Knowledge of C++ programming 11.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software:JDK 1.7,Eclipse 11.4 Theory Java date and time program :- Java code to print or display current system date and time. This program prints current date and time. We are using GregorianCalendar class in our program. 11.4.1 Algorithm 1. first import following package import java.util.*; 42 11.5. POST LAB Java class for showing the current Time and Date 2. create a class GetCurrentDateAndTime 3. create a method public static void main(String args[]) 4. declare variables int day, month, year; int second, minute, hour; 5. create object of GregorianCalendar class by using new keyword 6. call the following methods of GregorianCalendar to obtain current day,month and year.Also copy into the declared variables date.get(Calendar.DAY OF MONTH); date.get(Calendar.MONTH); date.get(Calendar.YEAR); 7. call the following methods of GregorianCalendar to obtain current hours,minutes and seconds.Also copy into the declared variables date.get(Calendar.SECOND); date.get(Calendar.MINUTE); date.get(Calendar.HOUR); 8. display by using following statements System.out.println() 11.4.2 Expected output Figure 11.1: Output window 11.5 Post Lab Hence from this assignment you can learn how to write a java class for showing the current Time and Date. Lab Manual - Fundamentals of Programming Languages-II 43 VPCOE, Baramati Assignment 12 Installation of Android ADT for Eclipse 12.1 Problem Statement Installation of Android ADT for Eclipse 12.2 Pre Lab Knowledge of Eclipse IDE 12.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software 12.4 Theory 12.4.1 Installing the Eclipse Plugin Android offers a custom plugin for the Eclipse IDE, called Android Development Tools (ADT). This plugin provides a powerful, integrated environment in which to develop Android apps. It extends the capabilities of Eclipse to let you quickly set up new Android projects, build an app UI, debug your app, and export signed (or unsigned) app packages (APKs) for distribution. 44 12.5. POST LAB Installation of Android ADT for Eclipse If you need to install Eclipse, you can download it from eclipse.org/downloads/. 12.4.2 Download the ADT Plugin 1. Start Eclipse, then select Help − > Install New Software. 2. Click Add, in the top-right corner. 3. In the Add Repository dialog that appears, enter ”ADT Plugin” for the Name and the following URL for the Location: https://dl-ssl.google.com/android/eclipse/ 4. Click OK. If you have trouble acquiring the plugin, try using ”http” in the Location URL, instead of ”https” (https is preferred for security reasons). 5. In the Available Software dialog, select the checkbox next to Developer Tools and click Next. 6. In the next window, you’ll see a list of the tools to be downloaded. Click Next. 7. Read and accept the license agreements, then click Finish. If you get a security warning saying that the authenticity or validity of the software can’t be established, click OK. 8. When the installation completes, restart Eclipse. 12.4.3 Configure the ADT Plugin Once Eclipse restarts, you must specify the location of your Android SDK directory: (a) In the ”Welcome to Android Development” window that appears, select Use existing SDKs. (b) Browse and select the location of the Android SDK directory you recently downloaded and unpacked. (c) Click Next. Your Eclipse IDE is now set up to develop Android apps 12.5 Post Lab Hence from this assignment you can learn Installation of Android ADT for Eclipse Lab Manual - Fundamentals of Programming Languages-II 45 VPCOE, Baramati Assignment 13 implementation on Android Simulator 13.1 Problem Statement Study and implementation on Android Simulator,”Hello World” 13.2 Pre Lab Knowledge of Eclipse IDE 13.3 Hardware and Software Requirement Hardware-P4 machine,1GB Ram,40 GB Hard Disk.Software:Eclipse 13.4 Theory Assumption is that you have already installed: i) Eclipse ii) Downloaded Android SDK iii) Installed the ADT plugin for Eclipse Then follow the steps: 1. Launch Eclipse 2. Then select Menu and sub menus as: 46 13.4. THEORY implementation on Android Simulator Figure 13.1: New Project window 3. New− >Project− >Android− >Android Project 4. Then click next button. following screen will appear. Filled details are shown for students assistance as- Then click on finish button. workspace for developing ”Hello World” application will be created for you by Eclipse environment as: Then from ”src” folder, select ”com.example.HW”. Then you will see ”HelloWorldActivity.java” file below it. Double click on it. You will see template code as- package com.example.HW; import android.app.Activity; import android.os.Bundle; public class helloWorldActivity extends Activity { / *called when activity is first created */ @Override Lab Manual - Fundamentals of Programming Languages-II 47 VPCOE, Baramati 13.4. THEORY implementation on Android Simulator public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.Layout.main); } } Add the lines to get our desired output of HelloWorld in above code as– package com.example.HW; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; / * this line allows you to output text to the screen* / public class helloWorldActivity extends Activity { / *called when activity is first created */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.Layout.main); TextView tv=new TextView(this); tv.setText(”HelloWorld”); setContentView(tv); } } To test this code, now switch to package Explorer on left hand side click on your workspace ”HelloWorldApp”.Then right click and then you will see menu like this. Then click ”Run As” sub option and again select ”Android Application” sub option. Eclipse environment will take a while and you will get emulators window. Click on ”Menu” button of your emulator. Then within a moment you will going to see following HelloWorld application. Lab Manual - Fundamentals of Programming Languages-II 48 VPCOE, Baramati 13.4. THEORY implementation on Android Simulator Figure 13.2: Android Application Output window Lab Manual - Fundamentals of Programming Languages-II 49 VPCOE, Baramati 13.5. POST LAB 13.5 implementation on Android Simulator Post Lab Hence from this assignment you can Study and implementation on Android Simulator,”Hello World” Lab Manual - Fundamentals of Programming Languages-II 50 VPCOE, Baramati References [1] Herbert Schildt, ”The Complete Reference JAVA2”, Tata McGraw Hill Publication, [2] Balgurusamy, ”Object Oriented programming in C++”, Tata McGraw Hill Publication, 51