Section 2
Section 2
2.0 INTRODUCTION
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:
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
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.
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.
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.
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).
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
7
Web Technologies Lab
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.
To download Apache Netbeans Idea, visit the official site of Apache Netbeans-
https://netbeans.apache.org/download/index.html
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.
Welcome to the First look of Apache NetBeans IDE 12.5 as shown in figure –
2.10.
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 .
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 (;).
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
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
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.
12
Figure 2.16: Selecting Project (Application) category Web Technologies Lab
You will get the following screen. This environment is your starting point to
write your 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
Once your program is compiled successfully, you will get the following
message in the output window.
Finally, you will get the result displayed in the output window.
14
Web Technologies Lab
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
15
Web Technologies Lab
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:
package multithreadingdemo;
import java.lang.*;
public class MultiThreadingDemo extends Thread
{
public void run()
{
17
Web Technologies Lab
System.out.println("Inside run method");
}
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
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:
20
Web Technologies Lab
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
Also, when you verify the table creation and records in table pgdcastudent in
jdbc2 database of MySQL you will get:
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.
Marks: 50 70 65 80 56
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.
26