Java Programming Practical Exercises
Java Programming Practical Exercises
PRATICAL FILE
(Programming in Java UGCA1938)
10. Write program that gets a number from the user and generates an integer
between 1 and 7 subsequently should display the name of the weekday as 18
per that number.
11. Construct a Java program to find the number of days in a month. 19
13. Design & execute a program in Java to sort a numeric array and a string 21
array.
14. Calculate the average value of array elements through Java Program. 22
19. Write a Java program to insert an element (on a specific position) into 26
Multidimensional array.
20. Write a program to perform following operations on strings:
1) Compare two strings.
2) Count string length. 28-30
3) Convert upper case to lower case & vice versa.
4) Concatenate two strings.
5) Print a substring.
21. Developed Program & design a method to find the smallest number among 31
three numbers.
22. Compute the average of three numbers through a Java Program. 32
23. Write a Program & design a method to count all vowels in a string. 33
24. Write a Java method to count all words in a string. 34
25. Write a method in Java program to count all words in a string. 35
26. Write a Java program to handle following exceptions:
1) Divide by Zero Exception. 36
2) Array Index Out Of B bound Exception.
27. To represent the concept of Multithreading write a Java program. 37
28. To represent the concept of all types of inheritance supported by Java, 38-42
design a program.
29. Write a program to implement Multiple Inheritance using interface. 43
30. Construct a program to design a package in Java. 44
31. To write and read a plain text file, write a Java program. 45
32. Write a Java program to append text to an existing file. 46
33. Design a program in Java to get a list of all file/directory names from the 47
given.
34. Develop a Java program to check if a file or directory specified by pathname 48
exists or not.
35. Write a Java program to check if a file or directory has read and write 49
permission.
2
1. Wite a program to perform following operations on two
numbers input by the user: 1) Addition 2) subtraction 3)
multiplication 4) division
1. Addition
Input:
import [Link];
//ADDING
Scanner pks = new Scanner([Link]);
[Link]("------Enter NUMBERS FOR Adding------");
[Link]("Enter the First Number :");
int a = [Link]();
[Link]("Enter the Second Number :");
int b = [Link]();
int sum = a + b;
[Link]("The sum of Two number : " + sum);
[Link]("");
}
}
Output:
3
2. Subtraction
Input:
import [Link];
Output:
4
3. Multiplication
Input:
import [Link];
Output:
5
4. Division
Input:
import [Link];
Output:
6
2. Write a Java program to print result of the following operations.
1) -15 +58 * 45
2) (35+8) % 6
3) 24 + -5*3 / 7
4) 15 + 18 / 3 * 2 - 9 % 3
Input:
public class Practical2 {
[Link]("-----SECOND RESULT-----");
[Link](" : "+(35+8) % 6);
[Link]("-----THIRD RESULT-----");
[Link](" : "+(24 + -5*3 / 7));
[Link]("-----FOURTH RESULT-----");
[Link](" : "+(15 + 18 / 3 * 2 - 9 % 3));
}
Output:
7
3. Write a Java program to compute area of:
1) Circle
2) rectangle
3) triangle
4) square
1. Area of
Circle Input:
import [Link];
Output:
8
2. Area of
Rectangle Input:
import [Link];
Output:
9
3. Area of
Triangle Input:
import [Link];
Output:
10
4. Area of
Square Input:
import [Link];
Output:
11
4. Write a program to convert temperature from Fahrenheit to Celsius
degree using Java.
Input:
import [Link];
}
}
Output:
12
5. Write a program through Java that reads a number in
inches, converts it to meters.
Input:
import [Link];
Output:
13
6. Write a program to convert minutes into a number of years
and days.
Input:
import [Link];
public class practical6 {
public static void main(String[] Strings) {
Output:
14
7. Write a Java program that prints current time in GMT.
Input:
import [Link].*;
public class practical7 {
public static void main(String args[])
{
[Link]([Link]());
}
}
Output:
15
8. Design a program in Java to solve quadratic equations using if, if
else Input:
import [Link];
public class practical8 {
public static void main(String[] Strings)
{
Scanner input = new Scanner([Link]);
[Link]("Enter the value of a: ");
double a = [Link]();
[Link]("Enter the value of b: ");
double b = [Link]();
[Link]("Enter the value of c: ");
double c = [Link]();
double d= b * b - 4.0 * a * c;
if (d> 0.0)
{
double r1 = (-b + [Link](d, 0.5)) / (2.0 * a);
double r2 = (-b - [Link](d, 0.5)) / (2.0 * a);
[Link]("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
double r1 = -b / (2.0 * a);
[Link]("The root is " + r1);
}
else
{
[Link]("Roots are not real.");
}
}
}
Output:
16
9. Write a Java program to determine greatest number of
three numbers.
Input:
import [Link];
public class practical9 {
public static void main(String[] args)
{
int a, b, c;
Scanner pks = new Scanner([Link]);
[Link]("Enter the first number:");
a = [Link]();
[Link]("Enter the second number:");
b = [Link]();
[Link]("Enter the third number:");
c = [Link]();
if(a>=b && a>=c)
[Link](a+" is the largest Number");
else if (b>=a && b>=c)
[Link](b+" is the largest Number");
else
[Link](c+" is the largest number");
}
}
Output:
17
[Link] program that gets a number from the user and generates an
integer between 1 and 7 subsequently should display the name of
the weekday as per that number.
Input:
import [Link];
public class practical10 {
public static void main(String[] args)
{
Scanner pks = new Scanner([Link]);
[Link]("Enter the Number Between 1-7ç◆:
"); int day = [Link]();
[Link](getDayName(day));
}
public static String getDayName(int day)
{ String dayName = "";
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
default:dayName = "Invalid Please enter week number between 1-
7."; Input!
}
return dayName;
}
}
Output:
18
[Link] a Java program to find the number of days in a
month. Input:
import [Link];
public class practical12 {
public static void main(String[] strings)
{ Scanner pks = new Scanner([Link]);
[Link]("Enter a month number (1-12): ");
int month = [Link]();
[Link]("Enter a year:
"); int year = [Link]();
switch (month) {
case 1,3,5,7,8,10,12:
[Link]("\n 31 Days in this Month");
break;
case 4,6,9,11:
[Link]("\n 30 Days in this Month");
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{ [Link]("\n 29 Days in this Month");
} else {
[Link]("\n 28Days in this Month");
}
}
}
}
Output:
19
[Link] a program to sum values of an Single Dimensional array.
Input:
Output:
20
[Link] & execute a program in Java to sort a numeric array and
a string array.
Input:
import [Link];
public class practical14 {
public static void main(String[] args){
int[] my_array1 = {
1454, 64754, 165, 5564, 2010,
1010, 5050, 6455, 6455, 4644};
Output:
21
[Link] the average value of array elements through
Java Program.
Input:
import [Link];
public class pratica14 {
public static void main(String[] args)
Scanner([Link]);
Output:
22
[Link] a Java program to test if an array contains a specific value.
Input:
}
}
Output:
23
[Link] the index of an array element by writing a program in Java.
Input:
Output:
24
[Link] a Java program to remove a specific element from an array.
Input:
import [Link];
public class practical17 {
Output:
25
[Link] a program to copy an array by iterating the array.
Input:
import [Link];
public class practical18 {
public static void main(String[] args) {
int[] my_array = {14, 78, 41, 25, 32, 7, 15, 20, 23, 10};
int[] new_array = new int[11];
Output:
26
[Link] a Java program to insert an element (on a specific
position) into Multidimensional array.
Input:
import [Link];
public class practical19 {
int[] my_array = {14, 78, 41, 25, 32, 7, 15, 20, 23, 10};
int Index_position =
5; int newValue= 100;
[Link]("Original Array is : "+[Link](my_array));
for(int i=my_array.length-1; i > Index_position; i--){
my_array[i] = my_array[i-1];
}
my_array[Index_position] = newValue;
[Link]("New Array is : "+[Link](my_array));
}
}
Output:
27
[Link] a program to perform following operations on strings:
1) Compare two strings.
2) Count string length.
3) Convert upper case to lower case & vice versa.
4) Concatenate two strings.
5) Print a substring.
1. Compare to strings:
Input:
if(style1 == style2)
[Link]("String1 And String2 is :Equal");
else
[Link]("String1 And String2 is :Not Equal");
if(style3 == style4)
[Link]("String3 And String4 is :Equal");
else
[Link]("String3 And String4 is :Not Equal");
}
}
Output:
28
2. Count string
length. Input:
Output:
class practica20 {
public static void main(String[] args) {
String str = "pRAVEEN KUMAR SHARMA";
[Link]("After convert to Upper case to lower case :
"+[Link]());
}
}
Output:
29
4. Concatenate two
string: Input:
class pract20{
public static void main(String args[])
{ String s1="Praveen ";
String s2="Kumar ";
String s3="Sharma";
String s4=[Link](s2+s3);
[Link](s4);
}
}
Output:
5. Print a substring:
Input:
public class prac20 {
public static void SubString(String str, int n)
{
for (int i = 0; i < n; i++)
for (int j = i+1; j <= n; j++)
[Link]([Link](i,
j));
}
public static void main(String[] args)
{
String str = "cow";
SubString(str, [Link]());
}
}
Output:
30
21. Developed Program & design a method to find the smallest
number among three numbers.
Input:
import [Link];
public class practical21 {
private static Scanner pks;
public static void main(String[] args) {
int x, y, z;
pks = new Scanner([Link]);
[Link]("Please Enter three Numbers: ");
x = [Link]();
y = [Link]();
z = [Link]();
Output:
31
22. Compute the average of three numbers through a Java Program.
Input:
import [Link];
public class practical22 {
Output:
32
23. Write a Program & design a method to count all vowels in a string.
Input:
class practical23 {
public static void main(String[] args)
{ String str = "Prakash kumar
tiwari"; int vowelsCount = 0;
for(char c : [Link]()) {
c = [Link](c);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
++vowelsCount;
}
}
[Link]("String "+str+" has "+ vowelsCount + " vowels.");
}
}
Output:
33
24. Write a Java method to count all words in a string.
Input:
import [Link];
public class practical24 {
Output:
34
25. Write a method in Java program to count all words in a string.
Input:
int count = 1;
Output:
35
26. Write a Java program to handle following exceptions:
1) Divide by Zero Exception.
2) Array Index Out Of B bound Exception.
Output:
}
Output:
36
27. To represent the concept of Multithreading write a Java program.
Input:
Output:
37
28. To represent the concept of all types of inheritance supported by
Java, design a program.
Single
Inheritance
Input:
class Employee
{
float salary=45041*12;
}
public class Executive extends Employee
{
float bonus=2000*6;
public static void main(String args[])
{
Executive obj=new Executive();
[Link]("Total salary credited: "+[Link]);
[Link]("Bonus of six months: "+[Link]);
}
}
Output:
38
Multi-level
Inheritance Input:
public class student
{ int reg_no;
void getNo(int no)
{
reg_no=no;
}
void putNo()
{
[Link]("registration number= "+reg_no);
}
}
class Marks extends student
{
float marks;
void getMarks(float m)
{
marks=m;
}
void putMarks()
{
[Link]("marks= "+marks);
}
}
class Sports extends Marks
{
float score;
void getScore(float scr)
{
score= (float) scr;
}
void putScore()
{
[Link]("score= "+score);
}
}
class MultilevelInheritance
{
public static void main(String args[])
{
Sports ob=new Sports();
[Link](5247);
[Link]();
[Link](55);
[Link]();
[Link](60.5F);
[Link]();
}
}
39
Output Of Multi-level Inheritance:
Hierarchical
Inheritance Input:
Hybrid
Inheritance Input:
42
}
}
Output:
Input:
interface AnimalEat
{ void eat();
}
interface AnimalTravel
{ void travel();
}
class Animal implements AnimalEat, AnimalTravel
{ public void eat() {
[Link]("Animal is eating");
}
public void travel()
{ [Link]("Animal is travelling");
}
}
public class practical29 {
public static void main(String args[])
{ Animal a = new Animal();
[Link]();
[Link]();
}
}
Output:
43
30. Construct a program to design a package in
Java. Input:
package p5;
//importing pre-defined package
import [Link].*;
import [Link];
import [Link];
import [Link];
import
[Link]; public
class practical30
{
public static void main(String args[])
{
[Link]("Enter your choice [1 for Add, 2 for Sub, 3 for multiply, 4 for
divide]: ");
Scanner scan=new Scanner([Link]);
int t=[Link]();
switch(t)
{
case 1:
addtion a=new addtion();
[Link]();
break;
case 2:
subtraction s=new subtraction();
[Link]();
break;
case 3:
multuplication m=new multuplication();
[Link]();
break;
44
case 4:
division d=new division();
[Link]();
break;
}
}
}
Output:
31. To write and read a plain text file, write a Java program.
Input:
import [Link].*;
public class practical31 {
public static void main(String[] args) throws Exception
{
FileReader fr = new FileReader("E:\\[Link]");
int i;
while ((i = [Link]()) != -1)
[Link]((char)i);
}
}
Output:
45
32. Write a Java program to append text to an existing file.
Input:
import [Link].*;
public class practical32{
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter("[Link]", true);
[Link]("\nI'm the new content.");
[Link]();
[Link]("The content is successfully appended to the file.");
}
catch(IOException ioe)
{
[Link]("\nSomething went wrong!");
}
}
}
Output:
46
33. Design a program in Java to get a list of all file/directory names from
the given.
Input:
import [Link];
import [Link];
public class practical33
{
public static void main(String a[])
{
File file = new File("E:\\");
String[] fileList =
[Link](); for(String
name:fileList){
[Link](name);
}
}
}
Output:
47
34. Develop a Java program to check if a file or directory specified
by pathname exists or not.
Input:
import [Link];
public class practical34
{
public static void main(String[] args) {
}
}
}
Output:
48
35. Write a Java program to check if a file or directory has read and write
permission.
Input:
import [Link];
public class practical35
{
public static void main(String[] args) {
File my_file_dir = new
File("C:\\Users\\WELCOME\\IdeaProjects\\pratice1\\src\\[Link]");
if (my_file_dir.canWrite())
{
[Link](my_file_dir.getAbsolutePath() + " can write.\n");
}
else
{
[Link](my_file_dir.getAbsolutePath() + " cannot write.\n");
}
if (my_file_dir.canRead())
{
[Link](my_file_dir.getAbsolutePath() + " can read.\n");
}
else
{
[Link](my_file_dir.getAbsolutePath() + " cannot read.\n");
}
}
}
Output:
49