0% found this document useful (0 votes)
2 views

JavaPractical_2003552 (1)

The document is a practical file for a Bachelor of Computer Applications program at DAV Institute of Engineering & Technology, Jalandhar, detailing various Java programming exercises. It includes a comprehensive index of practical tasks ranging from basic arithmetic operations to more complex concepts like multithreading and file handling. Each task is accompanied by input code examples and expected outputs.

Uploaded by

ankitkumar90991
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaPractical_2003552 (1)

The document is a practical file for a Bachelor of Computer Applications program at DAV Institute of Engineering & Technology, Jalandhar, detailing various Java programming exercises. It includes a comprehensive index of practical tasks ranging from basic arithmetic operations to more complex concepts like multithreading and file handling. Each task is accompanied by input code examples and expected outputs.

Uploaded by

ankitkumar90991
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

DAV Institute of Engineering & Technology, Jalandhar

Department of computer Applications

BACHELOR OF COMPUTER APPLICATIONS

PRATICAL FILE
(Programming in Java UGCA1938)

Submitted To: Submitted By:


Ms. Aastha Sharma Prakash Kumar Tiwari
Abhay Kumar
Assistant professor (CA) Class Roll no.-3154/20
Sharma Class - BCA
5th Sem Roll-No -
Uni.roll.No.-2003552
3101/20th
BCA 5 Semester
Uni- Roll- No- 2003499
Index:
S.no. Practical Name Page no. Remarks

1. Write a program to perform following operations on two numbers input by 3-6


the user: 1) Addition 2) subtraction 3) multiplication 4) division
2. Write a Java program to print result of the following operations.
1. -15 +58 * 45
2. (35+8) % 6 7
3. 24 + -5*3 / 7
4. 15 + 18 / 3 * 2 - 9 % 3
3. Write a Java program to compute area of: 1) Circle2) rectangle 3) triangle 8-11
4) square
4. Write a program to convert temperature from Fahrenheit to Celsius degree 12
using Java
5. Write a program through Java that reads a number in inches, converts it to 13
meters.
6. Write a program to convert minutes into a number of years and days. 14

7. Write a Java program that prints current time in GMT. 15

8. Design a program in Java to solve quadratic equations using if, if else 16

9. Write a Java program to determine greatest number of three numbers. 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 18
per that number.
11. Construct a Java program to find the number of days in a month. 19

12. Write a program to sum values of an Single Dimensional array. 20

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

15. Write a Java program to test if an array contains a specific value. 23

16. Find the index of an array element by writing a program in Java. 24

17. Write a Java program to remove a specific element from an array. 25

18. Design a program to copy an array by iterating the array. 26

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;

public class add {


public static void main(String[] args) {

//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;

public class subtraction {


public static void main(String[] args)
{ Scanner pks = new Scanner(System.in);
System.out.println("-----Enter NUMBERS FOR Substration-----");
System.out.println("Enter the First Number : ");
int c=pks.nextInt();
System.out.println("Enter the Second Number : ");
int d=pks.nextInt();
int sub= c-d;
System.out.println("The subtraction of Two number : " +sub);
System.out.println("");
}
}

Output:

4
3. Multiplication

Input:

import java.util.Scanner;

public class multiplication {


public static void main(String[] args)
{ Scanner pks = new Scanner(System.in);
System.out.println("-----Enter NUMBERS FOR Multiplication-----");
System.out.println("Enter the First Number : ");
int a=pks.nextInt();
System.out.println("Enter the Second Number : ");
int b=pks.nextInt();
int multi= a*b;
System.out.println("The multiplicatin of Two number : " +multi);
System.out.println("");
}
}

Output:

5
4. Division

Input:

import java.util.Scanner;

public class division {


public static void main(String[] args)
{ Scanner pks = new Scanner(System.in);
System.out.println("-----Enter NUMBERS FOR Division-----");
System.out.println("Enter the First Number:");
int a=pks.nextInt();
System.out.println("Enter the Second Number:");
int b=pks.nextInt();
int div= a/b;
System.out.println("The division of Two number : " +div);
}
}

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 {

public static void main(String[] args)


{ System.out.println("-----FIRST RESULT- -");
System.out.println(" : "+(-15 + 58 * 45));

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;

public class practical {


public static void main(String[] args) {
//Area Of Circle
Scanner pks = new Scanner(System.in);
int radius;
double pi = 3.14;
double area;
System.out.println("-----Enter the Radius-----");
radius = pks.nextInt();
area = pi * radius *
radius;
System.out.println("Area of Circle is : " + area);
}
}

Output:

8
2. Area of

Rectangle Input:

import java.util.Scanner;

public class practical {


public static void main(String[] args) {
// Area of rectangle
Scanner pks = new Scanner(System.in);
double length;
double breadth;
System.out.println("---Enter the Length---");
length= pks.nextDouble();
System.out.println("---Enter the
Breadth---"); breadth=pks.nextDouble();
double area = length * breadth;
System.out.println("Area of Rectangle : "+area);
}
}

Output:

9
3. Area of

Triangle Input:

import java.util.Scanner;

public class practical {


public static void main(String[] args) {
//Area of triangle
Scanner pks = new Scanner(System.in);
int l;
int b;
int h;
System.out.println("---Enter the Length---");
l=pks.nextInt();
System.out.println("---Enter the breadth---");
b=pks.nextInt();
System.out.println("---Enter the height---");
h=pks.nextInt();
int area1=l*b*h;
System.out.println("Area of Triangle : "+area1);
}
}

Output:

10
4. Area of

Square Input:

import java.util.Scanner;

public class practical {


public static void main(String[] args) {
//Area of square
Scanner pks = new Scanner(System.in);
int k;
System.out.println("---Enter the length---");
k= pks.nextInt();
int area = k * k;
System.out.println("Area of square : "+area);
}
}

Output:

11
4. Write a program to convert temperature from Fahrenheit to Celsius
degree using Java.

Input:

import java.util.Scanner;

public class pratical4 {


public static void main(String[] args)
{ Scanner pks=new
Scanner(System.in); double c;
double f;
System.out.println("--Enter Temperature Fahrenheit--");
f=pks.nextDouble();
c=(f-32)*5/9;
System.out.println("Result : "+c);

}
}

Output:

12
5. Write a program through Java that reads a number in
inches, converts it to meters.

Input:

import java.util.Scanner;

public class practical5 {


public static void main(String[] args)
{ Scanner pks= new
Scanner(System.in); double inch ;
double meter;
System.out.println("--Enter the value in inch--");
inch= pks.nextDouble();
meter= inch*0.0254;
System.out.println("Result in meter : "+meter);
}

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) {

double minutesInYear = 60 * 24 * 365;

Scanner pks = new Scanner(System.in);

System.out.print("Enter the number of minutes: ");

double min = pks.nextDouble();

long years = (long) (min / minutesInYear);


int days = (int) (min / 60 / 24) % 365;

System.out.println((int) min + " minutes is approximately " + years + "


years and " + days + " days");
}
}

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:

public class practical13 {


public static void main(String[] args)
{ int[] my_array;
my_array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;

for (int i : my_array)


sum += i;
System.out.println("The sum Single Dimensional array is : " + sum);
}
}

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};

String[] my_array2 = {"Java", "Python", "PHP", "C#", "C Programming", "C+


+","DSA","English",};

System.out.println("Original numeric array : "+Arrays.toString(my_array1));


System.out.println("");
Arrays.sort(my_array1);
System.out.println("Sorted numeric array : "+Arrays.toString(my_array1));
System.out.println("");

System.out.println("Original string array : "+Arrays.toString(my_array2));


System.out.println("");
Arrays.sort(my_array2);
System.out.println("Sorted string array : "+Arrays.toString(my_array2));
}
}

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 pks = new

Scanner(System.in);

System.out.print("Enter number of elements: ");


int size = pks.nextInt();
double array[] = new double[size];
double sum = 0.0;
double avg = 0.0;
System.out.println("Enter array elements: ");
for (int i=0; i<array.length; i++) {
array[i] = pks.nextDouble();
sum = sum + array[i];
}
avg = sum/array.length;
System.out.println("Average: " + avg );
}
}

Output:

22
15.Write a Java program to test if an array contains a specific value.

Input:

public class practical15 {


public static void main(String[] args) {
int[] my_array1 = {2005, 2040, 2023, 1556, 2013, 1998};
int toFind = 2005;
boolean found = false;

for (int n : my_array1) {


if (n == toFind) {
found = true;
break;
}
}if(found)
System.out.println(toFind + " is found(TRUE).");
else
System.out.println(toFind + " is not found(FALSE).");

}
}

Output:

23
16.Find the index of an array element by writing a program in Java.

Input:

public class practical16 {


public static void main(String[] args) {
int[] numbers = {8, 10, 5, 7, 4, 9};
int element = 7;
int index = -1;

for(int i = 0; i < numbers.length; i++)


{ if(numbers[i] == element) {
index = i;
break;
}
}

System.out.println("Index of "+element+" is : "+index);


}
}

Output:

24
17.Write a Java program to remove a specific element from an array.

Input:

import java.util.Arrays;
public class practical17 {

public static void main(String[] args) {


int[] my_array = {14, 78, 41, 25, 32, 7, 15, 20, 23, 10};

System.out.println("Original Array is : "+Arrays.toString(my_array));


int removeIndex = 3;
for(int i = removeIndex; i < my_array.length -1; i++)
{ my_array[i] = my_array[i + 1];
}
System.out.println("After removing the second element is :
"+Arrays.toString(my_array));
}
}

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];

System.out.println("Source Array is : "+Arrays.toString(my_array));

for(int i=0; i < my_array.length; i++) {


new_array[i] = my_array[i];
}
System.out.println("New Array is : "+Arrays.toString(new_array));
}
}

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 {

public static void main(String[] args) {

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:

public class practical20 {

public static void main(String[] args)


{ String style1 = "Bold";
String style2 = "Bold";
String style3 = "ITALIC";
String style4 = "Bold";

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:

public class practi20 {


public static void main(String args[])
{ String A1 = "Praveen Kumar Sharma";
String B2 = "Prveen Sharma";
System.out.println("string A length is: " + A1.length());
System.out.println("string B length is: " + B2.length());
}
}

Output:

3. Convert upper case to lower case & vice


versa. Input:

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();

if (x < y && x < z) {


System.out.format("\n%d is the Smallest Number", x);
}
else if (y < x && y < z) {
System.out.format("\n%d is the Smallest Number", y);
}
else if (z < x && z < y) {
System.out.format("\n%d is the Smallest Number", z);
}
else {
System.out.println("\nEither any 2 values or all of them are equal");
}
}
}

Output:

31
22. Compute the average of three numbers through a Java Program.

Input:

import java.util.Scanner;
public class practical22 {

public static void main(String[] args)


{
Scanner pks = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = pks.nextDouble();
System.out.print("Enter the second number: ");
double num2 = pks.nextDouble();
System.out.print("Enter the third number: ");
double num3 = pks.nextDouble();
pks.close();
System.out.print("The average of entered numbers is:" + avr(num1, num2, num3) );
}
public static double avr(double a, double b, double c)
{
return (a + b + c) / 3;
}
}

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 {

public static void main(String[] args)


{
Scanner pks = new Scanner(System.in);
System.out.print("Enter the string: ");
String str = pks.nextLine();

System.out.print("Number of words in the string: " + count_Words(str)+"\n");


}
public static int count_Words(String str)
{
int count = 0;
if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1))))
{
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == ' ')
{
count++;
}
}
count = count + 1;
}
return count;
}
}

Output:

34
25. Write a method in Java program to count all words in a string.

Input:

public class practical25 {


public static void main(String[] args)
{ String str = "I Live In mySelf😎";

int count = 1;

for (int i = 0; i < str.length() - 1; i++)


{
if ((str.charAt(i) == ' ') && (str.charAt(i + 1) != ' '))
{
count++;
}
}
System.out.println("Words in a string : " + count);
}
}

Output:

35
26. Write a Java program to handle following exceptions:
1) Divide by Zero Exception.
2) Array Index Out Of B bound Exception.

1. Divide by Zero Exception.


Input:
public class practical26 {
public static void main(String[] args) {
int a = 5;
int b = 0;
try {
System.out.println(a / b);
} catch (ArithmeticException e) {
System.out.println(
"Divided by zero operation cannot possible");
}
}
}

Output:

2) Array Index Out Of B bound Exception.


Input:

public class practica26 {


public static void main(String[] args)
{
int ar[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i <= ar.length; i++)
System.out.println(ar[i]);
}

}
Output:

36
27. To represent the concept of Multithreading write a Java program.

Input:

class MultithreadingDemo extends Thread


{ public void run()
{
try {
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e)
{ System.out.println("Exception is caught");
}
}
}
public class practical27 {
public static void main(String[] args)
{
int n = 5;
for (int i = 0; i < n; i++)
{ MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}

Output:

37
28. To represent the concept of all types of inheritance supported by
Java, design a program.

Java supports the following four types of inheritance:


 Single Inheritance
 Multi-level Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance

 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:

public class venicle {


double basePrice = 50000;
public void showPrice() {
System.out.println("The price of Vehicle is: Rs." + basePrice);
}
}
class TwoWheeler extends venicle
{ double increasePriceBy = 0.20;
void finalPrice() {
basePrice = basePrice + (basePrice * increasePriceBy);
System.out.println("After modification, The price of bike is: Rs." + basePrice);
}
}
class FourWheeler extends venicle
{ double increasePriceBy = 1;
void finalPrice() {
basePrice = basePrice + (basePrice * increasePriceBy);
System.out.println("After modification, The price of car is: Rs." + basePrice);
}
}
class Main {
public static void main(String[] args)
{ TwoWheeler bike = new TwoWheeler();
FourWheeler car = new FourWheeler();
bike.showPrice();
bike.finalPrice();
car.showPrice();
car.finalPrice();
}
}
40
Output:

 Hybrid

Inheritance Input:

public class Grandfather


{ public void show()
{
System.out.println("I am grandfather.");
}
}
class Father extends Grandfather
{
public void show()
{
System.out.println("I am father.");
}
}
class Son extends Father
{
public void show()
{
System.out.println("I am son.");
}
}
class Daughter extends Father
{
public void show()
{
System.out.println("I am a daughter.");
}
public static void main(String args[])
{
Daughter obj = new Daughter();
41
obj.show();

42
}
}

Output:

29. Write a program to implement Multiple Inheritance using interface.

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) {

File my_file1_dir = new File("C:\\Users\\WELCOME\\IdeaProjects\\pratice1\\src\\


test.txt");
if (my_file1_dir.exists()) {
System.out.println("The directory or file1 exists.\n");
} else {
System.out.println("The directory or file1 does not exist.\n");
}
File my_file2_dir = new File("/c:/xyz.txt");
if (my_file2_dir.exists()){
System.out.println("The directory or file2 exists.\n");
}else {
System.out.println("The directory or file2 does not exist.\n");

}
}
}

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

You might also like