KEMBAR78
Section 2 | PDF | Java Virtual Machine | Method (Computer Programming)
0% found this document useful (0 votes)
28 views26 pages

Section 2

Uploaded by

Rupa Devi
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)
28 views26 pages

Section 2

Uploaded by

Rupa Devi
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/ 26

Section-2: Java Programming Lab

Structure Page No.


2.0 Introduction
2.1 Objective
2.2 Introduction to Java Programming
2.3 Setup Development Environment
2.3.1 Installation of Java Development Kit (JDK)
2.3.2 Installation of Netbean IDE
2.3.3 Installing Apache Netbeans In Windows 10
2.3.4 Setting CLASSPATH
2.4 Some Example Programs
2.4.1 Core Java Programs
2.4.2 JDBC Programs
2.5 Lab Sessions
2.6 References/Further Readings

2.0 INTRODUCTION

For writing a program, you need to use IDE (Integrated Development


Environment) for better and smooth programming experiences. Section-2 of
this lab manual is for Java programming, which is based on MCS-206: Java
Programming course. At the end of this section, you will get a list of lab
sessions consisting of lab exercises that cover almost all the topics and
concepts you will be learning in course MCS-206. These ten lab sessions
consist of several programming exercises which you have to implement/write
code and execute using NetBean IDE. Some popular Java IDEs include
NetBeans, Eclipse, and IntelliJ IDEA. In this lab manual, we will use
NetBeans, an open-source IDEs, to write our programs.

In this section, we will talk about the use of the development environment of
NetBeans, and all the example programs has been executed using this IDE. To
use IDE for Java programming, you need to install Java Development
Kit(JDK).

2.1OBJECTIVE
The objectives of this section are:

 To install different IDEs for Java/J2EE programming,


 Develop simple applications using IDEs, and
1
Web Technologies Lab
 Use the given list of exercises for your assigned lab sessions.

2.2 INTRODUCTION TO JAVA PROGRAMMING


Java is a high-level language, and the language that a computer can directly
understand is called machine language. The compiler job is the translation of
a program from a high-level language(c/c++/Java etc.) to a low-level language.
So have to run your program written in a high-level language, using the
compiler. The machine-language program that is the output of the compiler is
often called the object program or object code. When you do this, you say
that you have compiled your program. An interesting thing to note is that the
Java compiler does not translate your program into the machine language that
can run on your computer; it translates your Java program into a code called
Bytecode. The Bytecode is not the machine language for any particular
operating system(computer). Instead, it is a machine language for a
hypothetical computer that is known as a virtual machine. The program that
translates source code into Bytecode is a kind of interpreter called the Java
Virtual Machine( JVM).
To run your Java program on your computer, first, you have to use the
compiler to translate your Java program into Bytecode. Then you have to use
the particular JVM for your computer system for translating each bytecode
instruction into machine language and run it.
Java Bytecode
When you compile a Java program, the Java compiler does not translate your
program into the machine language for your particular computer. Instead, your
Java program is translated to Bytecode. Remember that Java Bytecode is not
the machine language for any particular computer. Instead, it is a machine
language for a hypothetical computer known as a virtual machine. Translating
Java bytecode into a machine-language program for an actual computer is
performed in JVM. Also, the JVM runs the Java bytecode.
To run your Java program first, you have to translate your Java program into
Bytecode using the compiler; then, you can use the particular JVM to translate
each bytecode instruction into machine language and run it. Figures 2.1 and
figure 2.2 show the whole process of Java program execution.

Figure: 2.1: Java Program Writing and Execution


2
Web Technologies Lab

Figure: 2.2: Compiling and Running a Java Program

Writing, Compiling, and Running a Java Program


For writing Java programs, you are advised to use IDE as it will make your
programming job easy. Also, you can write a Java program in a simple text
editor, for example, Notepad, in a Windows environment. Generally, in a
complex program, there may be more than one class. In that case, the name of
that file must be the name of the class in which the main method is defined
with .java added to the end.
Signature of main method :
public static void main(Srting[ ] args)
If you are writing a Java program using a simple editor, then to compile that
Java class, you have to use a free Java system distributed by Oracle for
Windows, Linux, or some other Operating System; you have to use the
command javac followed by program file with extension .java. For example,
to compile a java program that has a class named MyClass that is in a program
file named MyClass.java, you give the following command to the operating
system:
javac MyClass.java

3
Web Technologies Lab
Thus, to compile the class in your program, you would give the following
command:
javac YourProgram.java
When you compile a Java class, it is translated into the class file-its bytecode
and is placed in a file whose name is the name of the class followed by .class.
That means after executing javac YourProgram.java you will get
YourProgram.class file as a result. Now you can run YuorProgram.class file
To run a Java program by giving the command java, followed by the name of
the class you think of as the program. For example, to run the program in
YuorProgram.class ,you would give the following one-line command:
java YourProgram
Now let us try to write a simple Java program using Notepad. The program to
display Welcome to Java Programming is written as shown in figure 2.3

Figure 2.3: Java Program Written in Notepad

Now we can save and compile this program using command prompt. This
program is saved in C:\MyJava directory. The Java program file name is
HelloJava.java. Before you copile this program, you have to set the path for
the location where your javac and java files are saved. Here we have set the
path to the bin directory of your JDK folder:
set path = C:\Program Files\Java\jdk-10.0.2\bin
Details about Classpath you will learn later in this section.
You can see in the figure given 2.4, which is the screenshot of the compilation
and execution of this program.

Figure 2.4: Screenshot of the Command Prompt

4
Web Technologies Lab
Remember that when you run a Java program, actually, you are running the
Java bytecode interpreter on the compiled version of your program, and hence
you are not writing .class after the file name.
Java programs can be written and executed easily using an integrated
development environment(IDE). An IDE provides you with a text editor with
menu commands for compiling and running the Java programs. An IDE
provides many useful GUI tools which are useful for developing applications.
Some of the common features of IDEs are:
● Code Editor: It is a space for writing and editing the source code.
● Compiler: It is used to transform the source code of the program
written by you into a form that is executable by a computer.
● Debugger: It is used during the testing of the application programs. It
helps to debug application programs during development.
● Builder: This automates common tasks of programmers, such as
building the application to run/execute the application.
● Browsers:
o Class Browser is used to examine and reference the properties
of an object-oriented class hierarchy.
o Object Browser is used to examine the objects instantiated in a
running application program.
● Class hierarchy Diagram: It allows the programmer to see the project
structure of programming code, including different classes in the
project.

This lab section is based on the MCS-206 course in which you will be learning
about basic constructs of Java programming, including data types, operators,
basic OOP concepts and other Java programming concepts. You need to learn
Java programming in the MS-206 course and try to implement the
programming exercises given in that course along with the exercises given in
this section of the lab manual.
Now let us see how to set up the development environment for Java
programming using Netbean IDE.

2.3SETUP DEVELOPMENT ENVIRONMENT

2.3.1 Installation of Java Development Kit (JDK)

The best thing about IDE is that its intelligence provides code completion hints
during coding in IDE. It also provides easy code, file and folder navigation.
For writing java program using IDE, we need to set up the development
environment and set up a compiler, builder, and editor (mainly JDK and IDE).
The Java Development Kit (JDK) is a software development environment that
is used to develop java applications. Before installation of any IDE ( in our
case NetBean), JDK installation is required. That is why we need to install
the latest version of JDK in our system. In this section, first, we will see how
to install JDK and NetBean IDE and then to use this IDE, we will write and
execute our java programs.

5
Web Technologies Lab
For the Setup development environment, we need to set up a compiler, builder,
and editor (mainly JDK and IDE). That is why we need to install the latest
version of JDK in our system. The best thing about IDE is that its intelligence
provides code completion hints during coding in IDE. It also provides easy
code, file and folder navigation. In this unit Windows 10 based installation and
environment setup are shown.
 First, check the java version is already installed by executing the
command “java –version” in the terminal or command prompt as
shown in Figure 2.5.

Figure 2.5: Checking Java Version

 If Java is not installed in your system, then, first of all, download the
latest version of JDK from its official website
(https://www.oracle.com/in/java/technologies/javase-downloads.html).
You can choose any latest version of JDK for development practice,
but it is strongly recommended to choose the LTS (Long Term
Support) version only for the production environment. Choose the
JDK package as per your system/platform.
 Install the JDK in your system and recheck the installed version.
 Remember that you need Java Runtime Environment(JRE) to run a
Java program. A JRE provides you with a Java virtual machine
(JVM), Java class libraries, and the Java class loader. You do not
have to download JRE separately as a JRE is part of a Java
development kit (JDK).

2.3.2 Downloading and Installation of Java Development Kit


(JDK)

 If Java is not installed in your system, then you have to download the
latest version of JDK from its official website
(java.com/download/ie_manual.jsp). For practicals of this course, you
can choose the latest version of JDK, but it is advised that you choose
the LTS (Long Term Support) version only for the project
development/ production environment.
 Install the JDK in your system, and then you may recheck the
installed version.

6
Web Technologies Lab

Figure 2.6: Downloading JDK

Figure 2.7: Accepting Licencing Terms for Downloading JDK

Figure 2.8: Installing JDK

7
Web Technologies Lab

Figure 2.9: JDK Installed

Once JDK is successfully installed, then you can download and install
NetBean IDE. The steps for installing NetBean IDE for Windows 10 is given
below.

3.3.3 Installing Apache Netbeans In Windows 10


NetBeans is an open-source Java IDE. It is one of the biggest and most popular
IDE. It is supported by a variety of operating systems like Linux, Windows,
macOS, Solaris, etc., along with feature-limited OS-independent versions.

Using NetBeans, it is very easy to create custom software applications. The


NetBeans highlights Java code syntactically as well as semantically. Also,
there are many tools in NetBeans that help in writing bug-free code. The
NetBeans is primarily a Java IDE, but it also has extensions for working in
other programming languages, including C, C++, PHP, HTML5, JavaScript,
etc.

To download Apache Netbeans Idea, visit the official site of Apache Netbeans-
https://netbeans.apache.org/download/index.html

Download the package as per your platform(Linux/Windows/MacOS). In this


section, we are using Windows 10 as a platform for installing NetBean IDE.

For this lab manual, Apache NetBeans IDE 12.5 is used. To install it, locate
the Apache-NetBeans-12.5-bin-windows-x64.exe setup file in your system,
where it is saved after download and run as administrator. The User access
control wizard will be displayed. Click on Yes. Setup will configure the
installer. If you want to customize the installation during the installation
process, click on the Customize button and choose the features per your need.
Follow the instructions of installation Wizard, and finally, click on Install to
install the software; the installation process will begin; wait till the process
finishes. Now, click on the Finish Button to close the wizard. After installation,
you are ready to use the NetBeans IDE.

8
Web Technologies Lab
Go to the start program, navigate to Apache NetBeans, click on Apache
Apache NetBeans IDE 12.5 link to open the IDE, or double click on the
Apache NetBeans IDE 12.5 icon created in Desktop as well to open the IDE.

Figure 2.10: NetBean IDE

Welcome to the First look of Apache NetBeans IDE 12.5 as shown in figure –
2.10.

2.3.4 Setting CLASSPATH

The PATH and CLASSPATH are the two very important environment
variables of the Java environment. JDK uses these variables to find the JDK
binaries used to compile and run Java class files which are compiled Java
bytecodes. For example, you can add the bin directory path of JDK or JRE in
CLASSPATH, so that any binaries under the directory can be accessed directly
without specifying the absolute path. As a programmer, you need to set the
CLASSPATH when there is a need to load a class that is not present in the
current directory or any sub-directories. The Classpath tells JDK tools and
applications where to find third-party and user-defined classes, i.e classes
that are not Java extensions or part of the Java platform. You can use Classpath
as a parameter in the JVM or the Java compiler that specifies the location of
user-defined classes and packages. Also, the CLASSPATH is used to define
the path and to find third-party and user-defined classes. Now let us see how to
set CLASSPATH .

PATH is the environment variable where we specify the locations of binaries.


The main difference between PATH and CLASSPATH is that path is set for
java tools in java programs like Java and javac, which are used to compile your
code. Whereas CLASSPATH is used by System or Application class loader to
locate and load compile Java bytecodes stored in the .class file.

You need to set the CLASSPATH in the situation :

 When there is a need to load a class that is not present in the current
directory or any sub-directories.

9
Web Technologies Lab
The CLASSPATH will have what you are setting as the CLASSPATH
variable. In the CLASSPATH you will have a directory name or file name at
the end. The following points list what should be the end of the CLASSPATH.

 If you have a JAR or zip file, which contains class files, the
CLASSPATH end with the name of that zip or JAR file.
 If class files are placed in an unnamed package, then the CLASSPATH
will end with the class file’s directory.
 If class files are placed in a named package, the CLASSPATH will end
with the directory containing the root package in the full package name.

If you have to set multiple classpaths, then you need to separate each
CLASSPATH by a semicolon (;).

There are two ways for setting the Path in Java:


1. Temporary setting
2. Permanent setting

You can set the temporary Path of JDK by performing the following steps:
 Open the command prompt, then
 Copy the path of the JDK/bin directory, and finally
 Write/paste the JDK/bin in the command prompt

set path = C:\Program Files\Java\jdk-10.0.2\bin

Let us see it in figure 2.11 given below:

Figure 2.11: Setting Path of JDK/bin directory

Set Permanent Path of JDK in Windows

To set the permanent Path of JDK, you have to perform the following steps:
1. Go to MyComputer properties -> Select the advanced tab
2. Select environment variables -> new tab of the user variable
3. Write the Path in variable name -> write Path of JDK/ bin folder in variable
value
4. Select ok three times as shown in figures 2.12 to figures 2.14
 First, click on environment variables.

10
Web Technologies Lab

Figure 2.12: Setting Environment Variable

 Then click on the new tab of user variables.

Figure 2.13: Add New User Variable

 Finally, write the path in the variable name.

Figure: 2.14: Writing Path

11
Web Technologies Lab

Before you get ready for writing java programs, you are advised to read at least
the first two units of the MCS-206 course. There you will learn basic
constructs for java programming. You must know the basic structure of the
java program, data types, variables declarations and conventions used for
naming variables, java keywords, programming comments etc. Also, you are
advised to write the program code given in your course MCS-206 and execute
them for better learning and understanding of concepts.

2.4 SOME EXAMPLE PROGRAMS


2.4.1 Core Java Programs

Example 1: First Java Program which displays a welcome message


Step-1: Open Netbean IDE -> New Project

Figure 2.15: Opening NeBean IDE

Step-2: Select Java as a category, then select Java Applications as categories.

12
Figure 2.16: Selecting Project (Application) category Web Technologies Lab

Step-3: Rename your project name as FirstApplication, then proceed further

Figure 2.16: Giving a Name to Project

You will get the following screen. This environment is your starting point to
write your program.

Figure 2.17: Using IDE for Writing Program

Now let us write our first java program using NetBean IDE.
As you can see in figure 2.18 the first statement written is -
System.out.println("Welcome to Java Programming");
Now save and compile this program.

13
Web Technologies Lab

Figure 2.18: Java Program

Once your program is compiled successfully, you will get the following
message in the output window.

Figure 2.19 : Compiling Java Program

For final output, you need to run the project.

Figure 2.20: Running Application

Finally, you will get the result displayed in the output window.
14
Web Technologies Lab

Figure 2.21: Output of Application

Now you can write and run ( execute) your program using the steps mentioned
in Example -1 above. You need to remember that when you write and compile
some complex programs, there is the possibility that your program will not be
successfully compiled in the first attempt. You may have some errors,
including syntax errors. You have to address(remove) those errors then you
have to compile your program again.
Further, we will have some more example programs to help you learn about
writing java programs.
Example-2: Java Program to show the use of Scanner class
/*
* Use of Scanner Class
*/
package useofscannerclass;
import java.util.Scanner; //Scanner class

public class UseofScannerClass


{
public static void main(String[] args)
{
Scanner myObj1 = new Scanner(System.in); // Scanner object
System.out.println("Please Enter Your Name:-");
String name = myObj1.nextLine();
Scanner myObj2 = new Scanner(System.in);
System.out.println("Please Enter Your Address:-");
String add = myObj2.nextLine();
System.out.println("Name: " + name);
System.out.println("Address: " + add);
}
}
Output:

15
Web Technologies Lab

Figure 2.22: Compiling and Running Program

Example-3: Java program to create BankAccount class.

package bankaccount;
import java.util.*;
public class BankAccount {
Scanner s=new Scanner(System.in);
int accno;
int am1;
int amount;
int num1;
void deposit(){
System.out.println("Please enter account number:");
accno=s.nextInt();
System.out.println("Please enter the amount:");
am1=s.nextInt();
amount += am1;
System.out.println("You have sucessfully added the the amount:"+am1);
menu();
}
void widraw(){
System.out.println("Please enter the amount:");
am1=s.nextInt();
amount -= am1;
System.out.println("You have sucessfully withrawed the the amount:"+am1);
menu();
}
void balance(){
System.out.println("Your available balance is: "+amount);
menu();
}
void menu(){
System.out.println("MENU:")
System.out.println("1. To deposit");
16
System.out.println("2. To widraw the amount"); Web Technologies Lab
System.out.println("3 To check balance");
System.out.println("Please choose the option: ");
num1 = s.nextInt();
switch(num1)
{
case 1: deposit();
break;
case 2: widraw();
break;
case 3: balance();
break;
default:
menu();
}

}
public static void main(String[] args)
{
BankAccount MyAccount = new BankAccount();
MyAccount.menu();
}
}

Output:

Figure 2.23: Compiling and Running Program


Example-4: Java program to demonstrate the use of Thead class methods-
setPriority and getPriority.

package multithreadingdemo;
import java.lang.*;
public class MultiThreadingDemo extends Thread
{
public void run()
{
17
Web Technologies Lab
System.out.println("Inside run method");
}

public static void main(String[] args)


{
MultiThreadingDemo t1 = new MultiThreadingDemo();
MultiThreadingDemo t2 = new MultiThreadingDemo();
MultiThreadingDemo t3 = new MultiThreadingDemo();
System.out.println("t1 thread priority : "+ t1.getPriority());
System.out.println("t2 thread priority : "+ t2.getPriority());
System.out.println("t3 thread priority : "+ t3.getPriority());
t1.setPriority(4);
t2.setPriority(5);
t3.setPriority(8);
System.out.println("t1 thread priority : "+ t1.getPriority());
System.out.println("t2 thread priority : "+ t2.getPriority());
System.out.println("t3 thread priority : "+ t3.getPriority());
System.out.println("Currently Executing Thread : "
+ Thread.currentThread().getName());
System.out.println("Main thread priority : "
+ Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : "
+ Thread.currentThread().getPriority());
}
}
Output:

Figure 2.23: Compiling and Running Program

2.4.2 JDBC Programs


For managing a database, you need to use a database server. Here we are using
MySQL database for our program writing. You have to download and install
MySQL from: https://www.mysql.com/downloads/

Suppose you have to compile the following JDBC program :


18
Web Technologies Lab

Example 5: JDBC Program to manage PGDCA Student's records.


/*JDBC Program */
package jdbcapplication;
import java.sql.*;
public class JDBCApplication
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbc2","root","mpmishra");
//Here jdbc2 is database name, root is username and password is mpmishra
System.out.println("Welcome to JDBC Programming");
Statement stmt=con.createStatement();
String sql = "CREATE TABLE pgdcastudent( RollNumber int(10),Name
varchar(40), ProgrammeCode char(8))";
stmt.executeUpdate(sql);
sql = "INSERT INTO pgdcastudent VALUES (111102, 'Ravi', 'PGDCA')";
stmt.executeUpdate(sql);
sql = "INSERT INTO pgdcastudent VALUES (111103, 'Rajeev','GDCA')";
stmt.executeUpdate(sql);
sql = "INSERT INTO pgdcastudent VALUES (111104,'Rahul', 'GDCA')";
stmt.executeUpdate(sql);
sql = "INSERT INTO pgdcastudent VALUES (111105,'Rajesh', 'PGDCA')";
stmt.executeUpdate(sql);
sql = "INSERT INTO pgdcastudent VALUES (111101,'Mohan', 'PGDCA')";
stmt.executeUpdate(sql);
ResultSet rs=stmt.executeQuery("select * from pgdcastudent");
while(rs.next())
{
System.out.println("RollNumber:"+rs.getInt(1)+" Name:"+rs.getString(2)+"
ProgrammeCode:"+rs.getString(3));
}
con.close();
}
catch(Exception e)
{
System.out.println("My Exception:"+e.toString());
}
}
}

To compile and run jedbc program, you need to provide a connecter jar file.
This file you have to download from:
https://dev.mysql.com/downloads/connector/j/

19
Web Technologies Lab

Figure 2.24: Downloading MySQL-Java Connecter file

The JAR file - mysql-connector-java-8.0.28.jar is downloaded at


"C:\Users\ignou\Desktop\mysql-connector-java-8.0.28\mysql-connector-java-
8.0.28.jar".

Figure 2.25: Download Connecter File

This file you have to include in the library of your JDBC Program. As you can
see in the figure2.26 given below. For adding this jar file, you need to do the
followings:

1. Righ click on Application and select properties.

20
Web Technologies Lab

Figure 2.26 : Screenshot of Selecting Application Properties

2. Select Libraries, then add the file: "C:\Users\ignou\Desktop\mysql-


connector-java-8.0.28\mysql-connector-java-8.0.28.jar" in Classpath
(Remember that you have downloaded Java – MYSQL connecter and add it
in the Classpath of JDBC program/application library). Now you can
compile and run this JDBC program.

Figure 2.27: Adding MYSQL JDBC connecter JAR file in Application


Library

Output:

As you can see in figure 2.28, after the successful execution of the program
given in example -5 above, you will get a database table named ‘pgdca’ created
and five rows inserted in it.

21
Web Technologies Lab

Figur 2.28: Output of JDBC Program

Also, when you verify the table creation and records in table pgdcastudent in
jdbc2 database of MySQL you will get:

Figure 2.29: Screenshot of SQL commonds execution on jdbc2 database

22
Web Technologies Lab
2.5 LIST OF LAB ASSIGNMENTS ( SESSION WISE)
In this section session wise problem/exercise are given for your lab.
SESSION 1: JAVA BASICS- VARIABLES, EXPRESSIONS,
OPERATORS, LOOPS
1. Write a Java program that displays Java primitive data types default values.
2. Write a Java program that takes any two numbers as input and displays:
a. Average of the numbers
b. Largest number
c. Smallest number
3. Write a Java program that takes your name, address, enrolment number and
mobile number as input and displays it.
4. Write a Java program that takes a number between 1 to 20 as input and
displays its multiplication table.
5. Write a Java program to calculate the roots of a given quadratic equation.
6. Write a Java program to display whether a given number is prime or not.

SESSION 2: LOOPS AND ARRAYS


7. Write a Java program that demonstrates the use of the followings:
a. for loop
b. while loop
8. Write a Java program that reverses a given array.
9. Write a Java program for performing the following operations on two 4X4
matrices.
a. Addition
b. Multiplication
10. Write a program in Java to find the average of marks obtained by a student
in five papers as given below:

Paper: Paper 1 Paper 2 Paper 3 Paper 4 Paper 5

Marks: 50 70 65 80 56

SESSION 3: CLASSES,OBJECTS , METHODS, ACCESS SPECIFIERS


11. Write a Java program to create a class Factorial with a method display
that displays a given number's factorial.
12. Write a program to create a room class, the attributes of this class is
Room_no,Rroom_type, Room_AC_Status, Room_Rent. In this class,
implement the member functions setdata and displaydata to insert date and
display data respectively.
13. Write Java program to calculate employee salary. Make necessary
asuptions.
14. Write a Java program to create a class Rectangle with the data fields
width, length, area and color. Define the methods Set_ Length () , Set_Width ()
and Find_ Area (). Create two objects of Rectangle and compare their area.

15.Create a class Account to represent a Saving Banks account. Define two


overloaded constructors, in which the first constructor is to be used for
initializing the name of account holder, account number and initial balance in
the account. Second constructor to be used for initializing name of an account
23
Web Technologies Lab
holder, account number, addresses, mobile_number and balance. Define
methods Deposit (), Withdraw (), and Get_Balance() for performing respective
operations on accounts. Make necessary assumptions as required. Create
objects of the Account class and use them to demonstrate the working of
methods defined in the class.

SESSION 4: INHERITANCE, POLYMORPHISM


16. Write a program in Java to create a class Player. Inherit classes Cricket
_Player, Football _Player and Hockey_ Player from class Player. Make
necessary assumptions.
17. Write a Java program to create an abstract class Shape that has two data
members of integer types to store the value of two sides of the shape and an
empty method FindArea(). Derive three classes Rectangle, Triangle and Circle
from Shape classs. Each of these classes contains only the method printArea()
that prints the area of that shape. Make necessary assumptions.
18. Write a Java program to demonstrate the concept of method overriding.
Use Online Shopping as an example.
SESSION 5: INTERFACE, PACKAGE AND EXCEPTIONS
HANDLING
19. Create your own package, which provides a service to find whether a given
number is odd or even and import this package in another class.
20. Create an Interface having three methods multiplication, division and
addition. Create a class that overrides these methods. Make necessary
assumptions.
21. Write a program in Java that implement interface Student, which has two
methods Display_Grade and ShoAttendance for PG_Students and
UG_Students, where PG_Students and UG_Students are two different classes
for Post Graduate and Under Graduate students respectively.
22. Write a program in Java to display the name and roll number of students.
Initialize respective array variables for ten students. Handle
ArrayIndexOutOfBoundsExeption, to avoid illegal termination of the program.
23. Write a program to illustrate subclass exception precedence over base
class.
24. Write a Java program to create custom exceptions in the process of
validating IGNOU PGDCA student records. Make necessary assumptions.
25. Assume that two vehicles are running on the single track of a road. When
both the vehicles are going in the same direction, there is no problem. But
when the vehicles are running in the opposite direction, there is a chance of
collision. To avoid collisions, write a java program using exception handling.
You are free to make necessary assumptions.
SESSION 6: MULTITHREADING AND NUMBER & STRINGS
26. Write Java program to show the use of String class and its operations:
i) Find string length.
ii) Reverse the contents of a string given on the console and convert the
resultant string in the upper case.
iii) Read a string from the console and append it to the given string.
27. Write a Java program to illustrate how the thread priorities are
implemented. Imagine that the first thread has just begun to run, even before it

24
Web Technologies Lab
has a chance to do anything. Now comes the higher priority thread that wants
to run as well. Now the higher priority thread has to do its work before the first
thread starts.
28. Write a java program that creates five threads T1,T2,T3,T4, and T5 with
different priorities. Send two threads that have the highest priority in the sleep
state. Check the aliveness of the threads and also mark which thread is long
losting.
29. Write a program to explain the use of Byte, Double, Float, Integer, Long
classes in Java.
30. Write a Java Program to demonstrate the use of String and StringBuilder.
31. Write a Java program that correctly implements the producer-consumer
problem using the concept of inter-thread communication.

SESSION 7: I/O AND JAVA API


32. Write a Java program that takes the name of a file as input from a user,
read the file's contents, and display it on the console.
33. Write a Java program to copy a file into another file.
34. Write a Java program to count characters, words & lines of a given file.
35. Write a Java program to delete an existing file.
36. Write a Java program to list all the files in a directory, including those
present in all its subdirectories.
37. Write Java program to demonstrate the use of Set API.
38. Write Java program to demonstrate the use of Vector API.
39. Write Java program to demonstrate the use of List API.
SESSION 8: GUI PROGRAMMING
40. Write a java program to simulate a traffic light. The program lets the
user select one of the three lights: red, yellow or green. On selecting a button,
an appropriate message with ”Stop” or “Ready” or “ Go” should appear
above the buttons selected color. Initially, there is no message shown.
41. Draw line oval rectangle using graphics/GUI methods
42. Create a dummy page for email accounts using proper GUI components.
43. . Write a Java program to create a simple calculator using proper GUI
components. Handle any possible exceptions including divide by zero in your
program.
SESSION 9: JDBC PROGRAMMING
44. Write a Java program to demonstrate connecting database. Use appropriate
database created in MySQL for demonstration.
45. Write a Java program to create a database table named Account which have
fields of a Saving Bank Account and perform followings on this table:
a. insert records,
25
Web Technologies Lab
b. update records,
c. select records, and
c. delete records.
SESSION 10: JDBC PROGRAMMING
46. Write a Java program to manage student's records using JDBC. Use
appropriate GUI components to create the user interface. Demonstrate uses of
commit and rollback in your program. Make necessary assumptions.
47. Write a Java program to execute a batch of SQL statements.

2.6 REFERENCES/FURTHER READINGS

● Walter Savitch ,“Java: An Introduction to Problem Solving and


Programming”, Pearson Education,2017.
● Download JDK: https://www.oracle.com/in/java/technologies/javase-
downloads.html
● JDK Installation Document for Multiple Platform:
https://docs.oracle.com/en/java/javase/16/install/overview-jdk-
installation.html#GUID-8677A77F-231A-40F7-98B9-1FD0B48C346A
● Java Environment PATH AND CLASSPATH Setup for the various
platform:
https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
● Download NetBeans IDE:
https://netbeans.apache.org/download/index.html
● https://www.javatpoint.com/how-to-set-classpath-in-java
● Download MySQL:https://www.mysql.com/downloads/
● Download Java-MySQL Connecter:
https://dev.mysql.com/downloads/connector/j/

26

You might also like