KEMBAR78
Java Programming Lab Manual | PDF | Method (Computer Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
634 views87 pages

Java Programming Lab Manual

The document provides important instructions for students regarding the online conduct of computer science laboratory courses during the Covid-19 pandemic. It instructs students to complete all lab exercises and assignments using their own systems from their respective places. It advises students to submit weekly assignments on time and maintain soft copies of their completed lab files in the prescribed format. It also provides credits to the committee members involved in updating and originally designing the lab manual.

Uploaded by

Gaurav Varshney
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
634 views87 pages

Java Programming Lab Manual

The document provides important instructions for students regarding the online conduct of computer science laboratory courses during the Covid-19 pandemic. It instructs students to complete all lab exercises and assignments using their own systems from their respective places. It advises students to submit weekly assignments on time and maintain soft copies of their completed lab files in the prescribed format. It also provides credits to the committee members involved in updating and originally designing the lab manual.

Uploaded by

Gaurav Varshney
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

Computer Lab Manual

2020-21
Look forward,
learn modern
knowledge, and
do not waste
time in studies
of old subjects of
no values.

M.C.A.
Semester-II

CSM-2271: LABORATORY COURSE – II


DEPARTMENT OF COMPUTER SCIENCE
ALIGARH MUSLIM UNIVERSITY ALIGARH
Important Instructions to the students in the
ongoing Covid-19 Scenario

❒ In the present Covid-19 scenario, all the labs will be conducted


ONLINE till the further instructions issued by the University.

❒ Students are instructed to perform their lab exercises/assignments at


their own system from their respective places.

❒ During this prevailing situation (COVID-19 crises), the mode


(policies/rules) of conducting Lecture/Lab classes will be as per the
instructions issued by the University from time to time.

❒ The students are advised to complete the weekly


activities/assignments well in time (i.e., within the same week).

❒ The students are also advised to maintain the soft copy of the Lab
File of their completed activities/assignments in the prescribed
format.

!! Stay Home, Stay Safe !!


CREDITS
s

The following Lab Manual Up-gradation Committee updated the Lab


Manual:

 Prof. Aasim Zafar (Chairperson)


 Prof. Rafiqul Zaman Khan
 Mr. Suhel Mustajab
 Dr. Arman Rasool Faridi
 Dr. Faisal Anwer
 Dr. Mohammad Nadeem
The following committee members originally designed the Lab Manual
under the supervision of former Chairperson Prof. Jamshed Siddiqui:

 Prof. Mohammad UbaidullahBokhari


 Dr. Arman Rasool Faridi
 Dr. Faisal Anwer
 Prof. Aasim Zafar (Convener)

Design & Compilation:


Mr. Sunil Kumar Sharma

Revised Edition: January, 2021

Department of Computer Science


A.M.U., Aligarh, (U.P.), India
Lab Manual: Laboratory Course – II (CSM – 2271)

CONTENTS PAGE NO.

WEEK WISE CONTENTS 2

COURSE DESCRIPTION 3

CONTENT 3

OBJECTIVES 3

OUTCOMES 4

HOW TO DO WELL IN THIS COURSE 5

RULES & REGULATIONS 7


WEEK WISE CONTENTS

Week
Contents Page No.
No.
Introduction of Java programming, JDK, Installation of
#1 9
Java and different IDEs of Java on different platform.
Understanding the importance of programming in Object
#2 58
Oriented environment using JAVA

#3 Concepts of operators used in JAVA 59

Concept of different control statements and loops available


#4 61
in JAVA.
Concept of different control statements and loops available
#5 63
in JAVA.
Concept to learn the object oriented features, various types
#6 65
of selection constructs in a JAVA program
Concept of java features like threading., use of arrays,
#7 68
access protection, wrapper classes
Concept to handle interfaces, converting between class and
#8 interface types, callbacks, Polymorphism, Inheritance, 71
Inheriting instance fields and methods.
Concepts of the use of toString method, constructor in
#9 73
class, access the member functions of a class.
Concept of Access control, its purpose, its characteristics,
#10 75
how many types of Access control.
#11 Concept of Exception handling, use throws exceptional. 77
#12 Concept of file handling using Java 79

#13 Concept of applet 81

#14 Applet applications 83

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 2
COURSE TITLE: Laboratory Course-II COURSE CODE: CSM – 2271
CREDIT: 04 PERIODS PER WEEK: 06
CONTINUOUS ASSESSMENT: 40 Marks EXAMS: 60 Marks

COURSE DESCRIPTION
Today almost every branch of computer science is feeling presence of object-
orientation. Object oriented technology is successfully incorporated in various
fields of computer science. Since its arrival on the scene in 1995, the Java has been
accepted as one of the primary programming language.

This course is designed to give you exposure to basic concepts of object-


oriented technology. This course will help in learning to write programs in Java
using object-oriented paradigm. Approach in this course is to take Java as a
language that is used as a primary tool in many different areas of programming
work.

COURSE CONTENT
This course is designed to provide the students the opportunity of learning the
differences between C++ and JAVA programming, to develop, debug, and execute
JAVA programs.

OBJECTIVES
This course is designed to help students in:

 Gaining knowledge about basic Java language syntax and semantics to write
Java programs and use concepts such as variables, conditional and iterative
execution methods etc.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 3
 Understanding the fundamentals of object-oriented programming in Java,
including defining classes, objects, invoking methods etc. and exception
handling mechanisms.
 Learning the use of arrays, access protection, wrapper classes etc.
 Understanding the principles of inheritance, packages and interfaces.
 Learning to create applet and application and event handling, AWT controls,
able to create GUI (frame, menu, button, text boxes, layout manager).
 Understanding to use the Java SDK environment to create, debug and run
simple Java programs.

OUTCOMES

After completing this course, the students would be able to:

 Understand the fundamental features of an object oriented language like


JAVA: object classes and interfaces, exceptions and libraries of object
collections.
 Understand how to implement, compile, test and run JAVA programs,
comprising more than one class, to address a particular software problem.
 Understand how to include arithmetic operators and constants in a JAVA
program and able to deploy JAVA applet and application program.
 Understand the concept of multithreading, multitasking, and
multiprogramming.
 Understand the use of members of classes found in the JAVA API (such as
the Math class, Wrapper classes).
 Understand the implement the keyboard/mouse events.
 Understand the concept of object oriented programming, inheritance,
constructor, interfaces, and package.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 4
 Understand the concept of Exception Handling, Files and Streams, Applets
and Graphics and also learn the concept of Applet classes, Applet life cycle
and JAVA Swing (introductory part).

HOW TO DO WELL IN THIS COURSE


❒ The students are advised to attend all their theory classes and respective labs
regularly as both are integrated to each other. If any student will miss the
theory lecture, he/she may not able to do well in lab related to that topic.

❒ The students are advised to submit the assignments given in theory and lab
classes timely to their respective Teachers/Instructors online.

❒ The students should demonstrate disciplined and well behaved demeanor in


the Department.

❒ Each student shall be assigned a system in their introductory lab. They are
advised to do their work on that system only for the whole semester. Students
should store all their lab activities regularly.

❒ All students are advised to understand course objectives and outcomes and
achieve both during their lab work.

❒ The students are advised to follow books/eBooks/online tutorial/other online


study material links given in lecture/lab manual/ syllabus references. These
study materials are very helpful in terms of skills, knowledge and placement.

❒ This Lab course is very important in terms of placement. Therefore, students


are advised to implement all the problems by her /him given in the individual
week.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 5
❒ All students are advised to solve old placement papers for campus selection.
Following links may be useful for the preparation of your campus
placements.

➢ https://www.indiabix.com/placement-papers/companies/
➢ https://www.offcampusjobs4u.com/download-tcs-placement-test-
question-papers-with-solutions/
➢ https://www.indiabix.com/placement-papers/tcs/
➢ https://www.firstnaukri.com/career-guidance/infosys-placement-papers-
with-solutions-2019-firstnaukri-prep
➢ https://prepinsta.com/ibm/
➢ https://www.faceprep.in/infosys/infosys-aptitude-questions/
➢ https://alpingi.com/infosys-placement-papers-solution-pdf-download/
➢ http://placement.freshersworld.com/

 The Students are advised to follow mentioned tutorials links:

➢ http://drrafiq.org/
➢ https://www.w3schools.com/java/
➢ https://www.tutorialspoint.com/java/index.htm
➢ https://www.javatpoint.com/java-tutorial
 The Students are advised to follow below Links for installing application
software:
➢ http://www.jcreator.org/download.htm
➢ https://www.eclipse.org/downloads/

➢ https://netbeans.org/downloads/6.1/index.html

 The students are advised to use the following online editors which are much
helpful during the online classes:
➢ https://www.jdoodle.com/online-java-compiler/
➢ https://www.codechef.com/ide

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 6
➢ https://www.onlinegdb.com/online_java_compiler
➢ https://rextester.com/l/java_online_compiler
➢ https://paiza.io/en/projects/new?language=java

RULES AND REGULATIONS

Students are required to strictly adhere to the following rules.

 The students must complete the weekly activities/assignments well in time


(i.e., within the same week).
 The students must maintain the Lab File of their completed
activities/assignments in the prescribed format (Appendix-1).
 The students must get the completed weekly activities/assignments checked
and signed by the concerned teachers in the Lab in the immediate
succeeding week. Failing which the activities/assignments for that week will
be treated as incomplete.
 At least TEN (10) such timely completed and duly signed weekly
activities/assignments are compulsory, failing which students will not be
allowed to appear in the final Lab Examination.
 The students need to submit the following three deliverables for each
exercise duly signed by the Teacher:
❖ Coding
❖ Input /Output
 The students need to ensure that each question is assessed and signed by the
Teacher in the week/time.
 Late submission would not be accepted after the due date.
 Cooperate, collaborate and explore for the best individual learning outcomes
but copying is strictly prohibited.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 7
APPENDIX-1
WEEK NO. Template for the Index of Lab File

SIGNATURE

PAGE NO.
OF THE
PROBLEMS WITH DESCRIPTION TEACHER
WITH DATE

1#

1 2#

3#

1#

2 2#

3#

1#

3 2#

3#

Note: The students should use Header and Footer mentioning their roll no. & name
in footer and page no in header.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 8
WEEK #1

OBJECTIVES
 To learn the Java programming.
 To learn the JDK kit.
 To learn the installation of JDK on windows and Linux operating system.
 To learn the different IDEs of Java.
 To learn how to configure your machine for Java programming.

OUTCOMES
After completing this, the students would be able to:

 understand the Java Programming.


 understand how JDK is installed on the machine.
 understand the different IDE of Java and how to install them on the different
operating system.

Install Java JDK 8 in Windows

This Java Development Kit(JDK) allows you to code and run Java programs. It's
possible that you install multiple JDK versions on the same PC. But Its
recommended that you install only latest version.

Following are steps to install Java in Windows

Step 1)

Go to link https://www.oracle.com/java/technologies/javase-downloads.html. Click


on Download JDK for java latest version.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 9
Step 2) Next,

1. Accept License Agreement


2. Download latest Java JDK for your version(32 or 64 bit) of java for
Windows.

Step 3)Once the download is complete, run the exe for install JDK. Click Next

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 10
Step 4) Select the PATH for Java installation and click next.

Step 5)Once installation is complete click Close

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 11
How to set Environment Variables in Java: Path and Classpath

The PATH variable gives the location of executables like javac, java etc. It is
possible to run a program without specifying the PATH but you will need to give
full path of executable like C:\Program Files\Java\jdk-13.0.1\bin\javac A.java
instead of simple javac A.java

The CLASSPATH variable gives location of the Library Files.

Let's look into the steps to set the PATH and CLASSPATH

Step 1) Right Click on the My Computer and Select the properties

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 12
Step 2) Click on advanced system settings

Step 3) Click on Environment Variables

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 13
Step 4) Click on new Button of User variables

Step 5) Type PATH in the Variable name.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 14
Step 6) Copy the path of bin folder which is installed in JDK folder.

Step 7) Paste Path of bin folder in Variable value and click on OK Button.

Note: In case you already have a PATH variable created in your PC, edit the PATH
variable to

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 15
PATH = <JDK installation directory>\bin;%PATH%;

Here, %PATH% appends the existing path variable to our new value

Step 8) You can follow a similar process to set CLASSPATH.

Note: In case you java installation does not work after installation, change classpath
to

CLASSPATH = <JDK installation directory>\lib\tools.jar;

Step 9) Click on OK button

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 16
Step 10) Go to command prompt and type javac commands.

If you see a screen like below, Java is installed.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 17
JAVA IDE: NetBean for JAVA

NetBeans (http://netbeans.org) is an open-source Integrated Development


Environment (IDE). NetBeans began in 1996 as a Java IDE student project at
Charles University in Prague. Sun Microsystems acquired NetBeans in 1999. In
2010, Oracle acquired Sun (and thus NetBeans).

Compared with its rival Eclipse (http://www.elicpse.org) (both are open-source, so I


don't know what are they competing for?), NetBeans provides seamless support for
Java AWT/Swing, Java ME mobility pack, Java EE, and bundled with an excellent
profiler for performance tuning.

How to Install NetBeans on Windows

Step 0: Install JDK

To use NetBeans for Java programming, you need to first install Java Development
Kit (JDK).

Step 1: Download

Download "NetBeans IDE" installer from


http://netbeans.org/downloads/index.html. There are many "bundles" available. For
beginners, choose the 1st entry "Java SE" (e.g., "netbeans-8.2-javase-windows.exe"
95MB).

Step 2: Run the Installer

Run the downloaded installer.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 18
How to Install NetBeans on Mac OS X

To use NetBeans for Java programming, you need to first install JDK.

To install NetBeans:

1. Download NetBeans from http://netbeans.org/downloads/. Set "Platform" to


"Mac OS X". There are many "bundles" available. For beginners, choose
"Java SE" (e.g., "netbeans-8.2-javase-macosx.dmg" 116MB).
2. Double-click the download Disk Image (DMG) file.
3. Double-click the "NetBeans 8.x.mpkg", and follow the instructions to install
NetBeans. NetBeans will be installed under "/Applications/NetBeans".
4. Eject the Disk Image (".dmg").

You can launch NetBeans from the "Applications".

Notes: To uninstall NetBeans, drag the "/Applications/NetBeans" folder to trash.

How to Install NetBeans on Ubuntu Linux

To use NetBeans for Java programming, you need to first install JDK. To install
NetBeans:

1. Download NetBeans from http://netbeans.org/downloads/. Choose platform


"Linux (x86/x64)" ⇒ "Java SE". You shall receive a sh file (e.g., "netbeans-
7.x-ml-javase-linux.sh") in "~/Downloads".
2. Set the downloaded sh file to executable and run the sh file. Open a
Terminal:
3. $ cd ~/Downloads
4. $ chmoda+x netbeans-7.x-ml-javase-linux.sh// Set to executable for all
(a+x)

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 19
$ ./netbeans-7.x-ml-javase-linux.sh// Run

Follow the instructions to install NetBeans.

To start NetBeans, run the script "netbeans" in the NetBeans' bin directory:

$ cdnetbeans-bin-directory
$ ./netbeans

Writing a Hello-world Java Program in NetBeans

Step 0: Launch NetBeans

Launch NetBeans. If the "Start Page" appears, close it by clicking the "cross" button
next to the "Start Page" title.

Step 1: Create a New Project

For each Java application, you need to create a "project" to keep all the source files,
classes and relevant resources.

1. From "File" menu ⇒ Choose "New Project...".


2. The "Choose Project" diglog pops up ⇒ Under "Categories", choose "Java"
⇒ Under "Projects", choose "Java Application" ⇒ "Next".
3. The "Name and Location" dialog pops up ⇒ Under "Project Name", enter
"FirstProject" ⇒ In "Project Location", select a suitable directory to save
your works ⇒ Uncheck "Use Dedicated Folder for Storing Libraries"
⇒Uncheck "Create Main class"⇒ Finish.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 20
Step 2: Write a Hello-world Java Program

1. Right-click on "FirstProject" ⇒ New ⇒ Java Class (OR choose the "File"


menu ⇒ "New File..." ⇒ Categories: "Java", File Types: "Java Class" ⇒
"Next").
2. The "Name and Location" dialog pops up ⇒ In "Class Name", enter "Hello"
⇒ Delete the content in "Package" if it is not empty ⇒ "Finish".
3. The source file "Hello.java" appears in the editor panel. Enter the following
codes:

public class Hello {


public static void main(String[] args) {
System.out.println("Hello, world");
}
}

Step 3: Compile & Execute

There is no need to "compile" the source code in NetBeans explicitly, as NetBeans


performs the so-called incremental compilation (i.e., the source statement is
compiled as and when it is entered).

To run the program, right-click anywhere in the source (or from the "Run" menu) ⇒
Run File. Observe the output on the output console.

Notes:

• You should create a NEW Java project for EACH of your Java application.
• Nonetheless, NetBeans allows you to keep more than one programs in a
project, which is handy for writing toy programs (such as your tutorial

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 21
exercises). To run a particular program, open and right-click on the source
file ⇒ Run File.

Installing Java and JCreator on your computer

In order to experiment Java programming on your own computer you have to install
Java Development Kit (JDK), if it is not already installed. Also an Integrated
Development Environment (IDE) is helpful. In the class lectures and also in the labs
we will use JCreator as the IDE. JCreator is available only for Windows operating
systems. In the rest of this document, you will find a detailed explanation for
preparing your computer for the CS 101 course, by installing JDK and JCreator.

You should install JDK first and then JCreator.

1. Installing JDK:

1.1. Download the most recent JDK

1.1.1. Start an internet browser

1.1.2. Go to the page

"http://www.oracle.com/technetwork/java/javase/downloads/index.html".

1.1.3. Click the icon about Java Platform (JDK) Download, as shown below:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 22
1.1.4. Accept the License Agreement

1.1.5. Click the link jdk-8u31-windows-x64.exe. If your computer running 32 bit


operating system (Windows x86), you must download jdk-8u31-windows-i586.exe.

1.1.6. Go back to the page

"http://www.oracle.com/technetwork/java/javase/downloads/index.html".

1.1.7. Scroll down to the section called "Additional Resources" Click the icon
labelled "Download", as shown below:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 23
1.1.8. Accept the license agreement.

1.1.9. Download the docs-all.zip file.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 24
1.2. Installing JDK

1.2.1. Start the installation by clicking the "jdk-8u31-windows-x64.exe" file or


"jdk-8u31-windows-i586.exe" that you just downloaded.

1.2.2. Click "Yes" to the question "Do you want to allow the following program to
make changes to this computer?"

1.2.3. Press the "Next" button.

1.2.4. Press the "Next" button again.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 25
Wait during the installation of the JDK.

1.2.5. Press next for installing JRE.

Wait during the installation of the JRE.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 26
1.2.6. Press "Close" button to complete the installation of the JDK.

1.3. Check the installation folder

1.3.1. Click the "Computer icon.

1.3.2. Click the "Local Disk (C:)

1.3.3. Then click "Program Files". You should see a folder named "Java".

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 27
1.3.4. Click that folder named "Java". The contents of the folder should look like
the following:

1.3.5. Unzip the "jdk-8u31-docs-all.zip" file by double clicking in the folder you
downloaded.
You should see the folder "docs" as shown below:

1.3.6. Drag the "docs" folder in the zip window into the "Java" window.

1.3.7. The contents of the "Java" window should contain three folders as shown
below:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 28
2. Installing JCreator:

2.1. Download JCreator LE version

2.1.1. Start an internet browser

2.1.2. Go to the page http://www.jcreator.org/download.htm.

2.1.3. Click the "Download" button next to "JCreator Classics, as shown below:

(You can also download jcrea500_setup.exe here.

2.1.4. Fill in your name and email address and press the "SUBMIT" button.
Do not use Turkish characters in the name area.
You can uncheck "Yes, keep me informed about new releases and discounts" part if
do not want to get emails about JCreator.

2.1.5. Wait for an email message from JCreator to the address that you gave.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 29
2.1.6. Open the message. Copy the line under the sentence "Thank you for your
interest in JCreator LE. You can download the setup file from the following url:"
below:

2.1.7. Paste it into the address field on your internet browser.

2.1.8. Wait for the download to complete. The name of the file is
"jcrea450_setup.exe".

2.2. Install JCreator LE version

2.2.1. Click the "jcrea450_setup.exe" file to start installation

2.2.2. Click the "Run" button.

2.2.3. Press the "Yes" button.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 30
2.2.4. Press the "Next" button.

2.2.5. Select "I accept the agreement" then press the "Next" button.

2.2.6. Keep the default destination location. Just press the "Next" button.

2.2.7. Press the "Yes" button for folder creation window.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 31
2.2.8. Press the "Next" button for the start menu folder.

2.2.9. Press the "Next" button for the additional tasks.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 32
2.2.10. Press the "Install" button to start installation.

2.2.11. Press the "Finish" button to end installation.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 33
2.3. Setup the JCreator

2.3.1. The JCreator setup wizard window will open. Press the "Next" button.

2.3.2. Press "Next" on the file association window.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 34
2.3.3. On the JDK Home Directory Window, press the "Browse" button.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 35
2.3.4. Click "Computer", "Local Disk (C:)", "Program Files", "Java", "jdk1.7.0_51",
in this order. Then press the "OK" button.

2.3.5. Press the "Next" button.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 36
2.3.6. Click the "Browse" button on the JDK JavaDoc Directory.

2.3.7. Click "Computer", "Local Disk (C:)", "Program Files", "Java", "doc", in this
order. Then press the "OK" button.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 37
2.3.8. Press "Finish" to complete the setup.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 38
3. Running JCreator:

3.1. Edit Source Code

3.1.1. The "Start Page" will open. Press "Close" to close the tip window.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 39
3.1.2. Press the "New File" icon on the upper left corner.

3.1.3. Make sure "Java Class" highlighted, press the "Next" button.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 40
3.1.4. Type HelloWorld in the "Name" area. Press the "Finish" button.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 41
3.1.5. A window by the name "HelloWorld.java" will open. This is the source code
that you will edit. It contains some default code as shown below:

3.1.6. Replace the public HelloWorld() part with public static void main(String[]
args).

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 42
3.1.7. Type in System.out.println("Hello World"); between the curly brace symbols,
as shown below.

3.2. Compile Source Code

3.2.1. Compile your source code by pressing the icon.

3.2.2. You should see the "Process completed." message in the "Build output" part.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 43
3.3. Run Application

3.3.1. Compile your application by pressing the icon.

3.3.2. You should see Hello World in the "General output" part.

Setup Eclipse IDE for Java on Windows


Install Java

Follow the below steps to complete your Java installation.

Step 1: Go to the Java Downloads Page and click on the option of Download.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 44
Step 2: Once you click on Download, you will be redirected to a page, where you
have to select the Accept License Agreement radio button. After that, you have to
choose the download link according to your matching system configuration as
below.

Step 3: Now, once the file is downloaded, run the installer and keep clicking on
Next, till you finally get a dialog box, which say, you have finished downloading.

Step 4: Once the installation is over follow the below instructions to set the path of
the file.

Step 4.1: Go to start and search for ‘System’. Then, click on ‘System’ and go to
Advanced System Settings. Refer below.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 45
Step 4.2: Now, click on ‘Environment Variables’ under ‘Advanced’ tab as shown
below:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 46
Step 4.3: Next, under System Variables choose New.

Step 4.4: Enter the variable name as ‘JAVA_HOME’ and the full path to Java
installation directory as per your system as shown below:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 47
Step 4.5: Next thing that you have to do is to configure your environment variables.
Let’s see how to do that. Here, you have to edit the path of the system variable as
shown below.

Step 4.6: Under ‘Variable value’, at the end of the line, enter the path of the folder.
Now, you can click ‘OK’ and you are done.

Now to cross-check the installation, just run following command in cmd – java -
version. It should display the installed version of Java in your system.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 48
Install Eclipse

Follow the below steps to configure Eclipse on your system:

Step 1: Navigate to the following URL –

https://www.eclipse.org/downloads/packages/ and select the download link


depending on your system architecture – (Windows, Mac OS or Linux) and
download it.

Step 2: Once the download is over, extract the zipped file by right-clicking on the
folder and choose Extract All. Refer below.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 49
Step 3: You will be then redirected to a dialog box, where you have to choose the
directory in which you wish to extract the files. Then click on Extract. Refer
below.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 50
Programming & Frameworks Training

Step 4: After extracting files, open the folder and launch eclipse.exe.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 51
Step 5: Then, you have to choose the Launch directory for Eclipse and then click
on Launch. Refer below.

Once Eclipse launches, you will see the below window:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 52
Hello World Program

Step 1: Launch Eclipse IDE and go to File →New → Java Project

Step 2: Mention the project name and click on Finish.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 53
Step 3: Now, go to the Project, Right-Click on the Project and choose Package. In
the dialog box, which opens up, mention the Package name as below and click on
Finish.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 54
Step 4: Now, right click on the Package, go to New and choose Class. Mention the
class name and click on Finish. Refer below.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 55
Step 5: Now, mention the following code in the workspace.

Package Edureka;

Public class helloworld {

Public static void main(String args[]){

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 56
System.out.println(“Hello World”);

Step 6: Now, execute your file, by right-clicking on the helloworld.java file and
choose Run As → Java Application. You will see Hello World printed on the
console.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 57
WEEK #2

OBJECTIVES

 To help the students in understanding the importance of programming in


Object Oriented environment using JAVA
 To help the students in understanding the basic structure of JAVA Program
 To help students getting familiar with JAVA programming environment and
IDE.
 To learn the students in writing, debugging and executing JAVA programs.
 How to read input from keyboard?
 To learn the constant and variables.

OUTCOMES

After completing this, the students would be able to:

 write, debug and execute simple JAVA programs.


 use the constant and variables in JAVA.

PROBLEMS

1# Write a java program to print ‘Hello World’.

2# Write a java program to print the corresponding address of a student.

3# Write a Java program to calculate sum of two numbers.

4# Write a Java program to convert the given temperature in Fahrenheit to


Celsius using the following conversion formula C = F–32 / 1.8 and display the
value in a tabular form.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 58
WEEK #3

OBJECTIVES

 To learn the concepts of operators used in JAVA.


 To learn the various mathematics library in Java.

OUTCOMES

After completing this, the students would be able to:

 use the operators in JAVA.


 use the various mathematic library functions.

PROBLEMS
1# Write a java program for finding the sum, difference, product, quotient,
minimum and maximum of any two integers.

2# Write a java program 'MyNumber.java" that performs following operations on


a variable 'num' of type double:

i) Finds the round value of 'num' and stores the result in a variable
numRound of type double.

ii) Finds the ceil value of 'num' and stores the result in a variable numCeil of
type double.

iii) Finds the floor value of 'num' and stores the result in a variable numFloor
of type double.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 59
Cast 'num' to type int and stores the result in a variable numInteger of type
int.Display output of numRound, numCeil, numFloor and numInteger on
screen.

Note: Test your program with following value of


numnum=56.764num=36.432

3# Write Java program to show uses of all Math class methods.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 60
WEEK #4

OBJECTIVES

 To learn using JAVA programming coding: data types, variable, constants,


operators, Control Statement (if, switch, loops)
 To learn break, continue statements, ternary operator, bit-wise operators,
user-defined data types in JAVA, order of evaluation of different operators in
java.
 To learn the controls statements and loops.

OUTCOMES

After completing this,

 The students would be able to use different control statements and loops
available in JAVA.

PROBLEMS

1# Write a java program to prints the count of odd and even no’s entered.
2# Write a java program to print the squares and cubes for the numbers 1 to 5.

3# Write a java program that computes the sum of the reciprocals:


1 1 1 1 1
+ + + ……………………………..+
1 2 3 4 10
4# Write Java program to compute the sum of the 2+4+6+---------N Terms.

5# Shown below is a Floyd’s triangle:


1
2 3

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 61
4 5 6
7 8 9 10
11 12 13 14
15 16 17 18 19

i) Write a program to print the above triangle.


ii) Modify the program to produce the following form of Floyd’s triangle.

1
0 1
1 0 1
0 10 1
1 01 0 1
0 1 0 1 0 1

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 62
WEEK #5

OBJECTIVES

 To learn using JAVA programming coding: data types, variable, constants,


operators, Control Statement (if, switch, loops)
 To learn break, continue statements, ternary operator, bit-wise operators, user-
defined data types in JAVA, order of evaluation of different operators in java.
 To learn the controls statements and loops.

OUTCOMES

After completing this,

 The students would be able to use different control statements and loops
available in JAVA.

PROBLEMS

1# Write a program in the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21 ……………… is


called Fibonacci numbers. Write a program using a do…while loop to
calculate and print the first m Fibonacci numbers.

2# Write a program to accept three digits (i.e. 0 - 9) and print all its possible
combinations. (For example if the three digits are 1, 2, 3 than all possible
combinations are: 123, 132,213, 231, 312, 321)

3# Write a Java Program which prompts the user to enter 4 numbers. The program
will then computes and display their sum and their product.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 63
4# Write a Java program which reads a 4-digit number and prints the digits on
separate lines. (Each digit is printed on one line).Output of your program must
be in the following format:

5# The intersection method computes the intersection of two rectangles- that is,
the rectangle that is formed by two overlapping rectangles: You call this
method as follows: Rectangle r3 =r1.intersection (r2);

Write a program that constructs two rectangle objects, prints them, and then
prints their intersection. What happens when the rectangles do not overlap?

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 64
WEEK #6

OBJECTIVES

 To learn the object oriented features (classification, encapsulation, inheritance,


polymorphism, abstraction) and java features like secure, platform independent,
portable, threading.
 To learn the concept of class like how to declare a class, how to define
methods inside the class, how the access the properties of base class into
derived class, Code reusability.
 To learn the use of arrays, access protection, wrapper classes.
 To learn the use of this keyword, use of toString ( ) method.

OUTCOMES

After completing this, the students would be able to:


 employ various types of selection constructs in a JAVA program

 demonstrate the hierarchy of JAVA classes to provide a solution to a given set


of requirements.

PROBLEMS

1# Write Java program involving two classes:


OddAndEven&TestOddAndEven.OddAndEven has the following:

• Instance varaibalescountOfOdd and countOfEven both of type int.

• A method addNumber that takes a number as parameter and increment


countOfOdd, if the number is odd, else increment countOfEven.

• A method toString that returns a string in the form: “Number of Odd: x,

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 65
• Number of Even: y”, where x and y are the values of the instance
variables.

The TestOddAndEven class first creates OddAndEven object, then in a loop,


read a number and use it to call the addNumber method until the user enters Q.
Finally, it prints the count of odd and even numbers entered.

2# Design a class Circle and implement the following methods:

• Define a circle method to compute its area


• Define a circle method to compute its perimeter
• Define a method that takes a given point represented by a pair of numbers
and checks whether or not the point is inside the circle.

The circle class needs to have instance variables to store the radius of the
circle, and the x and y coordinates of the center. Add main program to test the
class Circle repeatedly, until user enters negative value for the radius of the
circle.

3# Write a program in Java that reads in text and prints as output the following:

• The number of words in the text


• The number of sentences in the text
• The number of times the letter “e” occurs in the text
• The number of times the letter “z” occurs in the text

(Note: Use StringTokenizer class)

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 66
4# A sales person is paid commission based on the sales he makes as shown by
the following table:

Sales Commission
Under ₹500 2% of Sales
Between ₹500 to ₹5000 5% of Sales
₹5000 and Above 8% of Sales

Write a classCommissionwhich has an instance variable sale, an appropriate


constructor, and a methodcommission () that returns the commission.

Now write a demo class to test the Commission class by reading a sale from
the user, using it to create a Commission object after validating that the value
is not negative. Finally, call the commission() method to get and print the
commission. If the sales are negative, your demo should print the message
“Invalid Input”.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 67
WEEK #7

OBJECTIVES

 To learn the object oriented features (classification, encapsulation,


inheritance, polymorphism, abstraction) and java features like secure,
platform independent, portable, threading.
 To learn the concept of class like how to declare a class, how to define
methods inside the class, how the access the properties of base class into
derived class, Code reusability.
 To learn the use of arrays, access protection, wrapper classes.
 To learn the use of this keyword, use of toString ( ) method.

OUTCOMES

After completing this, the students would be able to:


 employ various types of selection constructs in a JAVA program
 demonstrate the hierarchy of JAVA classes to provide a solution to a given
set of requirements.

PROBLEMS

1# The certain instructor assigns letter grade for his course based on the following
table:

Score Grade
>=90 A+
>=85 A
>=80 B+
>=75 B

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 68
>=65 C+
>=60 C
>=55 D+
>=50 D
<50 F
Write a class, Grader, which has an instance variable, score, an appropriate
constructor and appropriate method letterGrade() that returns the letter grade
as a String.

Now write a demo class to test the Grader class by reading a score from the
user, using it to create a Grader object after validating that the value is not
negative and is not greater than 100. Finally, call the letterGrade() method to
get and print the grade.

2# Implement a Student class with the following fields, constructors and methods:
Fields:
• name;
• totalScore;
• numberOfQuizzes;
Constructors:
• public Student(String name, double score)
• public Student(double score, String name)
• public Student(String name)
Methods:
• public String getName()
• public double getAverage() //this should return zero if no quiz has been
taken.
• public double getTotalScore()

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 69
• public void addQuiz(double score)
• public void printStudent() //this should print the student’s name and
average score.
• public String toString()
Write an application TestStudent that reads a student name and use the Student
class to create a Student object. Then read the scores of the student in three
quizzes and add each to the totalScore of the student using addQuiz() method
and print the student object.
( Note: Make use of this key word wherever it can be used ).

3# Write a program to design a class to represent a bank account. Include the


following members.
Date members:
• Name of depositor
• Account Number
• Type of account
• Balance account in the account
Methods:
• To assign initial values
• To deposit an account
• To withdraw an account after checking balance.
• To display the name and balance

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 70
WEEK #8

OBJECTIVES

 To learn the concept Interface, how to implement it,

 To learn the use of member functions and how we access them, using
interfaces for code reusing,
 To learn converting between class and interface types, using interfaces for
callbacks; Polymorphism, Inheritance

OUTCOMES

After completing this, the students would be able to:

 handle interfaces, converting between class and interface types, callbacks,


Polymorphism, Inheritance, Inheriting instance fields and methods.
 access the member functions of a class.

PROBLEMS

1# Write a program that reads in a sentence from the user and prints it out with
each word reversed, but with the words and punctuation in the original order:

2# Write a program where interface can be used to support multiple inheritances.


Develop a standalone Java program for this.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 71
3# Write a program that reads in three strings and sorts them lexicographically.
Hint: Enter strings: Charlie Able Banker
Output: Able Banker Charlie
4# Implement the classes for the shapes using an interface for the common
methods, rather than inheritance from the superclass, while still Shape as a
base class.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 72
WEEK #9

OBJECTIVES
 To learn the toString method in Java.
 To learn the use of member functions and how we access them.,

 To learn the concept and usage of constructor in Java.

OUTCOMES
After completing this, the students would be able to:

 understand the use of toString method in Java.


 understand the usage of constructor in class.
 access the member functions of a class.

PROBLEMS
1# Implement a super class Person. Make two classes, Student and Instructor,
inherit from Person. A person has a name and a year of birth. A student has
a major, and an instructor has a salary. Write the class definitions, the
constructors, and the methods toString for all classes. Supply a test program
that tests these classes and methods.

2# Define a class Employee having private members – id, name, department,


salary. Define default and parameterized constructors. Create a subclass
called “Manager” with private member bonus. Define methods accept and
display in both the classes. Create n objects of the Manager class and display
the details of the manager having the maximum total salary (salary+bonus).

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 73
3# Write a Java program to create a super class Vehicle having members
Company and price. Derive 2 different classes LightMotorVehicle (members –
mileage) and HeavyMotorVehicle (members – capacity-in-tons). Accept the
information for n vehicles and display the information in appropriate form.
While taking data, ask the user about the type of vehicle first.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 74
WEEK #10

OBJECTIVES

 To learn the concept of Access control: private access, public access,


protected access and package access.
 To learn the use of member functions and how we access them.
 To learn the concept and usage of constructor in Java.

OUTCOMES

After completing this, the students would be able to:

 understand the concept of Access control, its purpose, its characteristics,


how many types of Access control.

 understand the usage of constructor in class.

 access the member functions of a class.

PROBLEMS

1# A bank maintains two kinds of accounts - Savings Account and Current


Account. The savings account provides compound interest, deposit and
withdrawal facilities. The current account only provides deposit and
withdrawal facilities. Current account holders should also maintain a
minimum balance. If balance falls below this level, a service charge is
imposed. Create a class Account that stores customer name, account
number, and type of account. From this derive the classes Curr-acct and

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 75
Sav-acct. Include the necessary methods in order to achieve the following
tasks:

• Accept deposit from a customer and update the balance.


• Display the balance.
• Compute interest and add to balance.
• Permit withdrawal and update the balance (Check for the minimum
balance, impose penalty if necessary).
2# Define a class called fruit with the following attributes:

• Name of the fruit


• Single fruit or bunch fruit
Define a suitable constructor and displayFruit() method that displays values
of all the attributes. Write a program that creates 2 objects of fruit class and
display their attributes.

3# Write a program where interface can be used to support multiple


inheritances. Develop a standalone Java program for this.

4# Implement the classes for the shapes using an interface for the common
methods, rather than inheritance from the superclass, while still Shape as a
base class.

5# Create a package called "Arithmetic" that contains methods to deal all


arithmetic operations. Also, write a program to use the package.

6# Write a program to make use of a parameterized method inside a class. Take


the following case: Create a class Box and define a method in this class
which will return the volume of the box. Initialize two objects for your class
and print out the volumes respectively.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 76
WEEK #11

OBJECTIVES

 To learn the concept of Exception handling.


 To learn the importance of exceptions.
 To learn the concept and way of throwing exceptions.

OUTCOMES

After completing this, the students would be able to:

 understand the features of Exception handling.


 understand the concept of Exception handling.
 use throwing exceptions.

PROBLEMS

1# Write a program that calls a method that throws an exception of type


ArithmeticException at a random iteration in a for loop. Catch the exception in
the method and pass the iteration count when the exception occurred to the
calling method by using an object of an exception class you define.

2# Write a program that will count the number of character in a file

3# In a small firm employee numbers are given in serial numerical order, that is 1,
2, 3 etc.Write a menu driven program to perform following operations:

i) Create a file of employee data with following information: Employee No,


Name, Sex, Gross Salary.
ii) Append the data of a new employee joining the firm.
iii) If a given employee leaves, delete his record.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 77
iv) If gross salary of a given employee increases, update the gross salary.
v) Display the record of :
a. a given employee or
b. all employees.
4# Write a program to create a sequential file that could store details about five
products. Details include product code, cost, and number of items available and
are provided through the keyboard.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 78
WEEK #12

OBJECTIVES
 To learn the file handling using Java.
 To learn various operations performed in file using file handling concepts.

OUTCOMES

After completing this,the students would be able to:

 work with Files and Streams.

PROBLESM

1# Write a Java program which reads student grades from a text file called
grades.txt and prints only the corresponding letter grades into a file called
letter.txt. The letter grades are assigned according to the following table.
Assume that the grades.txt file can have any number of students’ grades. Hint:
The last number in the grades.txt file is -1

Score Grade
>= 90 A+
>= 85 A
>= 80 B+
>= 75 B
>= 65 C+
>= 60 C
>= 55 D+
>= 50 D

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 79
< 50 F

2# Write a program to read a, b, c from data file and store roots of the quadratic
equation in output file. You must open your output file in append mode.

3# Develop an applet that receives three numeric values as input from the user and
then displays the largest of the three on the screen. Write a HTML pages and
test the applet.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 80
WEEK #13

OBJECTIVES

 To learn the concept of applet and their applications.

OUTCOMES

After completing this, the students would be able to:

 understandapplets in terms of drawing graphical shapes, colours, fonts,


drawing complex shapes, reading text input inside applet.

PROBLEMS

1# Write applets to draw the following shapes:


a) Cone b)Cylinder c) Square inside a circle
2# Write an applet that will display the following on a green background. Use the
following dimension:

Rectangle : (10, 10, 300, 150), Fill colour: blue


Left Circle : (10, 10, 50, 150), Fill Colour: Yellow
Right Circle: (159, 10, 150, 150), Fill colour: Yellow
Text : (110, 90), colour: Red

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 81
3# Write a JAVA Applet program to plot the following face:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 82
WEEK #14
OBJECTIVES
 To learn the concept of applet and their applications.

OUTCOMES
After completing this, the students would be able to:

 Understand applets in terms of drawing graphical shapes, colours, fonts,


drawing complex shapes, reading text input inside applet.

PROBLEMS
1# Write a graphics program that draws a clock face with a time that the user
enters in a text field. (The user must enter the time in the format hh:mm, for
example 09:45).

Hint: You need to find out the angles of the hour hand and the minute hand.
The angle of the hour hand is harder; it travels 360 degree in 12 x 60 minutes.
Your output must be in the following format:

2# Write a program that draws the picture of a house.


Sample Output:

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 83
3# Write an applet to display the following figure:
Sample Output:

4# Draw a “bull’s eye” a set of concentric rings in alternation black and white
colours: Fill a black circle, and then fill a smaller white circle on top, and so
on.

LAB MANUAL/MCA/CSM–2271/LAB-II/CSD-AMU 84

You might also like