JavaPractical_2003552 (1)
JavaPractical_2003552 (1)
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 java.util.Scanner;
//ADDING
Scanner pks = new Scanner(System.in);
System.out.println("------Enter NUMBERS FOR Adding------");
System.out.println("Enter the First Number :");
int a = pks.nextInt();
System.out.println("Enter the Second Number :");
int b = pks.nextInt();
int sum = a + b;
System.out.println("The sum of Two number : " + sum);
System.out.println("");
}
}
Output:
3
2. Subtraction
Input:
import java.util.Scanner;
Output:
4
3. Multiplication
Input:
import java.util.Scanner;
Output:
5
4. Division
Input:
import java.util.Scanner;
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 {
System.out.println("-----SECOND RESULT-----");
System.out.println(" : "+(35+8) % 6);
System.out.println("-----THIRD RESULT-----");
System.out.println(" : "+(24 + -5*3 / 7));
System.out.println("-----FOURTH RESULT-----");
System.out.println(" : "+(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 java.util.Scanner;
Output:
8
2. Area of
Rectangle Input:
import java.util.Scanner;
Output:
9
3. Area of
Triangle Input:
import java.util.Scanner;
Output:
10
4. Area of
Square Input:
import java.util.Scanner;
Output:
11
4. Write a program to convert temperature from Fahrenheit to Celsius
degree using Java.
Input:
import java.util.Scanner;
}
}
Output:
12
5. Write a program through Java that reads a number in
inches, converts it to meters.
Input:
import java.util.Scanner;
Output:
13
6. Write a program to convert minutes into a number of years
and days.
Input:
import java.util.Scanner;
public class practical6 {
public static void main(String[] Strings) {
Output:
14
7. Write a Java program that prints current time in GMT.
Input:
import java.time.*;
public class practical7 {
public static void main(String args[])
{
System.out.println(LocalTime.now());
}
}
Output:
15
8. Design a program in Java to solve quadratic equations using if, if
else Input:
import java.util.Scanner;
public class practical8 {
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d= b * b - 4.0 * a * c;
if (d> 0.0)
{
double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
}
else if (d == 0.0)
{
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
}
else
{
System.out.println("Roots are not real.");
}
}
}
Output:
16
9. Write a Java program to determine greatest number of
three numbers.
Input:
import java.util.Scanner;
public class practical9 {
public static void main(String[] args)
{
int a, b, c;
Scanner pks = new Scanner(System.in);
System.out.println("Enter the first number:");
a = pks.nextInt();
System.out.println("Enter the second number:");
b = pks.nextInt();
System.out.println("Enter the third number:");
c = pks.nextInt();
if(a>=b && a>=c)
System.out.println(a+" is the largest Number");
else if (b>=a && b>=c)
System.out.println(b+" is the largest Number");
else
System.out.println(c+" is the largest number");
}
}
Output:
17
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 per that number.
Input:
import java.util.Scanner;
public class practical10 {
public static void main(String[] args)
{
Scanner pks = new Scanner(System.in);
System.out.print("Enter the Number Between 1-7ç◆:
"); int day = pks.nextInt();
System.out.println(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
11.Construct a Java program to find the number of days in a
month. Input:
import java.util.Scanner;
public class practical12 {
public static void main(String[] strings)
{ Scanner pks = new Scanner(System.in);
System.out.print("Enter a month number (1-12): ");
int month = pks.nextInt();
System.out.print("Enter a year:
"); int year = pks.nextInt();
switch (month) {
case 1,3,5,7,8,10,12:
System.out.println("\n 31 Days in this Month");
break;
case 4,6,9,11:
System.out.println("\n 30 Days in this Month");
break;
case 2:
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{ System.out.println("\n 29 Days in this Month");
} else {
System.out.println("\n 28Days in this Month");
}
}
}
}
Output:
19
12.Write a program to sum values of an Single Dimensional array.
Input:
Output:
20
13.Design & execute a program in Java to sort a numeric array and
a string array.
Input:
import java.util.Arrays;
public class practical14 {
public static void main(String[] args){
int[] my_array1 = {
1454, 64754, 165, 5564, 2010,
1010, 5050, 6455, 6455, 4644};
Output:
21
14.Calculate the average value of array elements through
Java Program.
Input:
import java.util.Scanner;
public class pratica14 {
public static void main(String[] args)
Scanner(System.in);
Output:
22
15.Write a Java program to test if an array contains a specific value.
Input:
}
}
Output:
23
16.Find the index of an array element by writing a program in Java.
Input:
Output:
24
17.Write a Java program to remove a specific element from an array.
Input:
import java.util.Arrays;
public class practical17 {
Output:
25
18.Design a program to copy an array by iterating the array.
Input:
import java.util.Arrays;
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
19.Write a Java program to insert an element (on a specific
position) into Multidimensional array.
Input:
import java.util.Arrays;
public class practical19 {
int[] my_array = {14, 78, 41, 25, 32, 7, 15, 20, 23, 10};
int Index_position =
5; int newValue= 100;
System.out.println("Original Array is : "+Arrays.toString(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;
System.out.println("New Array is : "+Arrays.toString(my_array));
}
}
Output:
27
20.Write 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)
System.out.println("String1 And String2 is :Equal");
else
System.out.println("String1 And String2 is :Not Equal");
if(style3 == style4)
System.out.println("String3 And String4 is :Equal");
else
System.out.println("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";
System.out.println("After convert to Upper case to lower case :
"+str.toLowerCase());
}
}
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=s1.concat(s2+s3);
System.out.println(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++)
System.out.println(str.substring(i,
j));
}
public static void main(String[] args)
{
String str = "cow";
SubString(str, str.length());
}
}
Output:
30
21. Developed Program & design a method to find the smallest
number among three numbers.
Input:
import java.util.Scanner;
public class practical21 {
private static Scanner pks;
public static void main(String[] args) {
int x, y, z;
pks = new Scanner(System.in);
System.out.println("Please Enter three Numbers: ");
x = pks.nextInt();
y = pks.nextInt();
z = pks.nextInt();
Output:
31
22. Compute the average of three numbers through a Java Program.
Input:
import java.util.Scanner;
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 : str.toCharArray()) {
c = Character.toLowerCase(c);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
++vowelsCount;
}
}
System.out.println("String "+str+" has "+ vowelsCount + " vowels.");
}
}
Output:
33
24. Write a Java method to count all words in a string.
Input:
import java.util.Scanner;
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();
System.out.println("Total salary credited: "+obj.salary);
System.out.println("Bonus of six months: "+obj.bonus);
}
}
Output:
38
Multi-level
Inheritance Input:
public class student
{ int reg_no;
void getNo(int no)
{
reg_no=no;
}
void putNo()
{
System.out.println("registration number= "+reg_no);
}
}
class Marks extends student
{
float marks;
void getMarks(float m)
{
marks=m;
}
void putMarks()
{
System.out.println("marks= "+marks);
}
}
class Sports extends Marks
{
float score;
void getScore(float scr)
{
score= (float) scr;
}
void putScore()
{
System.out.println("score= "+score);
}
}
class MultilevelInheritance
{
public static void main(String args[])
{
Sports ob=new Sports();
ob.getNo(5247);
ob.putNo();
ob.getMarks(55);
ob.putMarks();
ob.getScore(60.5F);
ob.putScore();
}
}
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() {
System.out.println("Animal is eating");
}
public void travel()
{ System.out.println("Animal is travelling");
}
}
public class practical29 {
public static void main(String args[])
{ Animal a = new Animal();
a.eat();
a.travel();
}
}
Output:
43
30. Construct a program to design a package in
Java. Input:
package p5;
//importing pre-defined package
import java.util.*;
import p1.addtion;
import p2.subtraction;
import p3.division;
import
p4.multuplication; public
class practical30
{
public static void main(String args[])
{
System.out.print("Enter your choice [1 for Add, 2 for Sub, 3 for multiply, 4 for
divide]: ");
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
switch(t)
{
case 1:
addtion a=new addtion();
a.sum();
break;
case 2:
subtraction s=new subtraction();
s.diff();
break;
case 3:
multuplication m=new multuplication();
m.pro();
break;
44
case 4:
division d=new division();
d.divd();
break;
}
}
}
Output:
31. To write and read a plain text file, write a Java program.
Input:
import java.io.*;
public class practical31 {
public static void main(String[] args) throws Exception
{
FileReader fr = new FileReader("E:\\Prakash.txt");
int i;
while ((i = fr.read()) != -1)
System.out.print((char)i);
}
}
Output:
45
32. Write a Java program to append text to an existing file.
Input:
import java.io.*;
public class practical32{
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter("praveen.txt", true);
fw.write("\nI'm the new content.");
fw.close();
System.out.println("The content is successfully appended to the file.");
}
catch(IOException ioe)
{
System.out.print("\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 java.io.File;
import java.util.Date;
public class practical33
{
public static void main(String a[])
{
File file = new File("E:\\");
String[] fileList =
file.list(); for(String
name:fileList){
System.out.println(name);
}
}
}
Output:
47
34. Develop a Java program to check if a file or directory specified
by pathname exists or not.
Input:
import java.io.File;
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 java.io.File;
public class practical35
{
public static void main(String[] args) {
File my_file_dir = new
File("C:\\Users\\WELCOME\\IdeaProjects\\pratice1\\src\\test.txt");
if (my_file_dir.canWrite())
{
System.out.println(my_file_dir.getAbsolutePath() + " can write.\n");
}
else
{
System.out.println(my_file_dir.getAbsolutePath() + " cannot write.\n");
}
if (my_file_dir.canRead())
{
System.out.println(my_file_dir.getAbsolutePath() + " can read.\n");
}
else
{
System.out.println(my_file_dir.getAbsolutePath() + " cannot read.\n");
}
}
}
Output:
49