21DCE094 Java Pracfile
21DCE094 Java Pracfile
SET-1
Data Types, Variables, String, Control Statements,
Operators, Arrays
PRACTICAL – 1.1
Aim: Introduction to Object Oriented Concepts, comparison of Java with other object-
oriented programming languages. Introduction to JDK, JRE, JVM, Javadoc,
command line argument. Introduction to Eclipse or NetBeans IDE, or BlueJ and
Console Programming.
Theory:
Introduction to Object Oriented Concepts:
Java has some desirable features that make it a great asset to developers. It is platform-independent,
which means once you have written Java code, you can run the code on another platform (operating
system), and the code will run smoothly with no modification.
The main difference between Java and any other programming language is the unique method in
which Java code is executed. Unlike compiled languages such as C++, Java is compiled into
bytecode which can run on any device with the Java Virtual Machine (JVM). C++, on the other
hand, is compiled directly into machine code and therefore, can only run on the same platform in
which it was compiled.
JDK:
JDK stands for a Java Development Kit which is a cross-platformed software development
environment that offers a collection of tools and libraries necessary for developing Java-based
software applications and applets. It is a core package used in Java, along with the JVM (Java
Virtual Machine) and the JRE (Java Runtime Environment).
DEPSTAR-CE Page: 1
CE251: JAVA PROGRAMMING ID No: 21DCE094
JRE:
Java Run-time Environment (JRE) is the part of the Java Development Kit (JDK). It is a freely
available software distribution which has Java Class Library, specific tools, and a stand-alone
JVM. It is the most common environment available on devices to run java programs. The source
Java code gets compiled and converted to Java bytecode. The JRE loads classes, verify access to
memory, and retrieves the system resources. JRE acts as a layer on the top of the operating system.
JVM:
The Java Virtual Machine is the cornerstone of the Java platform. It is the component of the
technology responsible for its hardware- and operating system-independence, the small size of its
compiled code, and its ability to protect users from malicious programs. The Java Virtual Machine
is an abstract computing machine. Like a real computing machine, it has an instruction set and
manipulates various memory areas at run time.
Javadoc:
Javadoc is a tool that parses the declarations and documentation comments in a set of source files
and produces a set of HTML pages describing the classes, interfaces, constructors, methods, and
fields.
The java command-line argument is an argument that is passed at the time of running the java
program. The arguments passed from the console can be received in the java program and it can
be used as an input. So, it provides a convenient way to check the behavior of the program for the
different values.
Eclipse:
Eclipse is a free, Java-based development platform known for its plugins that allow developers to
develop and test code written in other programming languages.
NetBeans:
NetBeans IDE is a free, open source, integrated development environment (IDE) that enables you
to develop desktop, mobile and web applications. The IDE supports application development in
various languages, including Java, HTML5, PHP and C++.
DEPSTAR-CE Page: 2
CE251: JAVA PROGRAMMING ID No: 21DCE094
BlueJ:
BlueJ is an integrated development environment (IDE) for the Java programming language,
developed mainly for educational purposes, but also suitable for small-scale software
development. It runs with the help of Java Development Kit (JDK).
Console Programming:
A console application is a program which runs in an command prompt window.
DEPSTAR-CE Page: 3
CE251: JAVA PROGRAMMING ID No: 21DCE094
PRACTICAL – 1.2
Aim: Write a program that declares one integer variable called var1. Give value 10 to this
variable and then, using one println() statement, display the value on the screen like
this: “10 is the value of var1.”
Program:
public class pra2
{
public static void main(String arg[])
{
int var1=10;
System.out.print("\""+var1+" is the value of var1.\"");
}
}
Output:
Conclusion:
In this program I learnt to some basic syntax of printing output on screen, use variables and
how to run a java program.
DEPSTAR-CE Page: 4
CE251: JAVA PROGRAMMING ID No: 21DCE094
PRACTICAL – 1.3
Aim: Write a console program to declare and initialize a double variable with some value
such as 1234.5678. Then retrieve the integral part of the value and store it in a variable
of type long, and the first four digits of the fractional part and store them in an integer
of type short. Display the value of the double variable by outputting the two values
stored as integers.
Program:
public class pra3
{
public static void main(String args[])
{
double var1=1234.5678;
long var2;
var2=(long)var1;
System.out.println("The value of y is " +var2);
short var3;
var3=(short)((var1-var2)*10000);
System.out.println("The value of z is " +var3);
System.out.println("The value of z is " +var3);
System.out.println(var2+"."+var3);
}
}
Output:
Conclusion:
In this program I learnt how to implement type conversion in java program and store them in
different variables
DEPSTAR-CE Page: 5
CE251: JAVA PROGRAMMING ID No: 21DCE094
PRACTICAL – 1.4
Aim: Write an application that creates a two-dimension array with int values. The first,
second and third elements should be arrays with one, two and three numbers
respectively. Display the length of each dimension.
Program:
public class pra4
{
public static void main(String args[])
{
int arr[][]={{1},{3,4},{2,3,5}};
int i;
for(i=0;i<3;i++)
{
System.out.println("The length of the array"+(i+1)+" is:"+arr[i].length);
}
}
}
Output:
Conclusion:
In this program I learnt how to create two dimensional arrays in java program and to find length
of each dimension and also to use for loop.
DEPSTAR-CE Page: 6
CE251: JAVA PROGRAMMING ID No: 21DCE094
PRACTICAL – 1.5
Aim: An electric appliance shop assigns code 1 to motor,2 to fan,3 to tube and 4 for wires.
All other items have code 5 or more. While selling the goods, a sales tax of 8% to
motor,12% to fan,5% to tube light,7.5% to wires and 3% for all other items is
charged. A list containing the product code and price in two different arrays. Write a
java program using switch statement to prepare the bill.
Program:
class Practical5
{
public static void main(String args[])
{
int i;
double price=0;
int array[]={1,2,3,4,5};
double array1[]={100,200,300,400,60};
for(i=0;i<5;i++)
{
switch(array[i])
{
case 1:
array1[0]=(array1[0]*0.08)+array1[0];
System.out.println("The Bill of Motor is: "+array1[0]);
break;
case 2:
array1[1]=(array1[1]*0.12)+array1[1];
System.out.println("The Bill of Fan is: "+array1[1]);
break;
case 3:
array1[2]=(array1[2]*0.05)+array1[2];
System.out.println("The Bill of Tubelight is: "+array1[2]);
break;
case 4:
array1[3]=(array1[3]*0.075)+array1[3];
System.out.println("The Bill of Wires is: "+array1[3]);
break;
default:
DEPSTAR-CE Page: 7
CE251: JAVA PROGRAMMING ID No: 21DCE094
array1[4]=(array1[4]*0.03)+array1[4];
System.out.println("The Bill of other items is: "+array1[4]);
break;
}
}
for(i=0;i<5;i++)
{
price=price+array1[i];
}
System.out.println("Total bill prepared is: "+price);
}
}
Output:
Conclusion:
In this program I prepared a electricity bill using arrays and learnt how to use a switch
statement in java program and also used for loop.
DEPSTAR-CE Page: 8
CE251: JAVA PROGRAMMING ID No: 21DCE094
PRACTICAL – 1.6
Aim: Write a program to show output like:
*
**
***
****
*****
Program:
class Practical6
{
public static void main(String args[])
{
int i=1,j=0;
for(i=1;i<6;i++)
{
for(j=0;j<i;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}}
Output:
Conclusion:
In this program I learnt to print a pattern and used arrays and for loop.
DEPSTAR-CE Page: 9