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

Java Pratical

Uploaded by

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

Java Pratical

Uploaded by

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

Programming in Java Laboratory

UGCA-1938

SUBMITTED TO:- SUBMITTED BY:


Dr.Vineet Kumar Shubham Pal BCA 5
Roll no.2011253
1.Write a program to perform following operations on
two numbers input by the user:
1)Addition 2) subtraction 3) multiplication 4) division

import java.util.*;
public class
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b,s,ch;
System.out.println("Enter 2 numbers");
a=sc.nextInt();
b=sc.nextInt();
do
{
System.out.println("1:sum");
System.out.println("2:Difference");
System.out.println("3:Product");
System.out.println("4:Division");
System.out.println("enter ur choice 1/2/3/4");
ch=sc.nextInt();
switch(ch)
{ case 1: s=a+b;
System.out.println("sum is"+s);
break;
case 2:s=a-b;
System.out.println("Difference is"+s);
break;
case 3: s=a*b;
System.out.println("Product is"+s);
break;
case 4:s=a/b;
System.out.println("Divison is"+s);
break;
default: System.out.println("invalid choice");
}
}while (ch<5);

}
}
3.Write a Java program to compute area of:
1) Circle 2) rectangle 3) triangle 4) square

import java.util.*;
. class pr3
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double r,s,a,b1,c,ar,l,b;
System.out.println("Enter radius");
r=sc.nextFloat();
ar=3.14*r*r;
System.out.println("Area of circle "+ar);
System.out.println("Enter length,breadth ");
l=sc.nextFloat();
b=sc.nextFloat();
ar=l*b;
System.out.println("Area of rectangle "+ar);
System.out.println("Enter side of square");
s=sc.nextFloat();
ar=s*s;
System.out.println("area of square"+ar);
System.out.println("enter sides of triangle");
a=sc.nextFloat();
b1=sc.nextFloat();
c=sc.nextFloat();
s=(a+b1+c)/2;
ar=Math.sqrt(s*(s-a)*(s-b1)*(s-c));
System.out.println("Area of triangle"+ar);
}
}
4.Write a program to convert temperature from
Fahrenheit to Celsius degree using Java.

import java.util.*;
class pr4
{
public static void main(String[] args)
{
double f,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter fahreneit temperature");
f=sc.nextDouble();
c=(5.0/9.0)*(f-32);
System.out.println("temperature in celsius "+c);
}
}
5.Write a program through Java that reads a number
in inches, converts it to meters.

//inches to meters
import java.util.*;
class pr5
{
public static void main(String[] args)
{
double in,m;
Scanner sc=new Scanner(System.in);
System.out.println("enter distance in inches");
in=sc.nextDouble();
m=in*0.0254;
System.out.println("Distance in meters"+m);
}
}
6. Write a program to convert minutes into a number of years and days.

//minutes to year and days


import java.util.*;
class minutes
{
public static void main(String[] args)
{
int m,y,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of minutes");
m=sc.nextInt();
y=m/525600;
m=m%525600;
d=m/1440;
System.out.println("Number of years="+y);
System.out.println("Number of days="+d);
}
}
7.Write a Java program that prints
current time in GMT.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
class pr7 {
public static void main(String[] args){
System.out.println("The required packages have been imported");
Date localTime = new Date();
System.out.println("A Date object is defined");
DateFormat GMT_format = new SimpleDateFormat("dd/MM/yyyy" + " " + "
HH:mm:ss");
GMT_format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("\nThe local time is: " + localTime);
System.out.println("The time in Gmt is: " + GMT_format.format(localTime));
}
}
8.Design a program in Java to solve quadratic equations using if, if
else

import java.util.*;
class pr8
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double a,b,c,d;
double x1,x2;
System.out.println("Enter the value of a b c ");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
d=b*b-4*a*c;
if (d==0)
{
System.out.println("Roots are real and equal");
x1=x2=-b/(2*a);
System.out.println("Roots are"+x1+x2);
}
else if(d>0)
{
x1=-b+Math.sqrt(d)/(2*a);
x2=-b-Math.sqrt(d)/(2*a);
System.out.println("Roots are real and unequal");
System.out.println("Roots are"+x1+x2);
}
else
System.out.println("Roots are imaginary");
}
}

9.Write a Java program to determine


greatest number of three numbers

import java.util.*;
class pr8
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
double a,b,c,d;
double x1,x2;
System.out.println("Enter the value of a b c ");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
d=b*b-4*a*c;
if (d==0)
{
System.out.println("Roots are real and
equal");
x1=x2=-b/(2*a);
System.out.println("Roots are"+x1+x2);
}
else if(d>0)
{
x1=-b+Math.sqrt(d)/(2*a);
x2=-b-Math.sqrt(d)/(2*a);
System.out.println("Roots are real and
unequal");
System.out.println("Roots are"+x1+x2);
}
else
System.out.println("Roots are imaginary");
}
}

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
import java.util.*;
class pr10
{
public static void main(String[] args)
{
int d;
String n;
Scanner sc=new Scanner (System.in);
System.out.println("enter day number");
d=sc.nextInt();
switch(d)
{
case 1:System.out.println("Sunday");
break;
case 2:System.out.println("Monday");
break;
case 3:System.out.println("Tuesday");
break;
case 4:System.out.println("Wednesday");
break;
case 5:System.out.println("Thursday");
break;
case 6:System.out.println("Friday");
break;
case 7:System.out.println("Saturday");
break;
default:System.out.println("Invalid day");
}
}
11.Construct a Java program to find the number of days in a month.

import java.util.*;
class pr11
{
public static void main(String[] args)
{
int d=0,m;
Scanner sc=new Scanner (System.in);
System.out.println("enter month number");
m=sc.nextInt();
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;
break;
case 4:
case 6:
case 9:
case 11: d=30;
break;
case 2:
d=28;
break;
default:
System.out.println("Invalid month");

}
System.out.println("Number of days="+d);
}
}
12.Write a program to sum values of an Single Dimensional
array.
import java.util.*;
class pr12
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
int[] arr=new int[20];
System.out.println("enter total elements");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int s=0;
for(int i=0;i<n;i++)
s+=arr[i];
System.out.println("sum of numbers "+s);
}
}

13. Design & execute a program in Java to sort a numeric array and
a string array.

import java.util.*;
class pr13a
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[20];
int n,temp;
System.out.println("enter total numbers");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
for (int i=0;i<n;i++)
for(int j=0;j<n-i-1;j++)
if (arr[j]>arr[j+1])
{temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
System.out.println("Resultant array");
for(int i=0;i<n;i++)
System.out.println(arr[i]);}}

import java.util.*;
import java.io.*;
class pr13b
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n;
String temp;
String[] x=new String[10];
System.out.println("Enter total number of Strings");
n=sc.nextInt();
System.out.println("Enter Strings");
for(int i=0;i<n;i++)
x[i]=sc.next();
for(int i=0;i<n;i++)
for(int j=0;j<n-i-1;j++)
if (x[j].compareTo(x[j+1])<0)
{temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
System.out.println("resultant strings");
for(int i=0;i<n;i++)
System.out.println(x[i]);
}
}

14. Calculate the average value of array elements through Java Program.

import java.util.*;
class pr14
{public static void main(String[] args)
{
int n;
float av;
Scanner sc=new Scanner(System.in);
float[] arr=new float[20];
System.out.println("enter total elements");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
float s=0;
for(int i=0;i<n;i++)
s+=arr[i];
System.out.println("sum of numbers "+s);
av=s/n;
System.out.println("avg of numbers "+av);
}
}

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

import java.util.*;
class pr15
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
float[] arr=new float[20];
System.out.println("enter total elements");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
float num;
System.out.println("enter element to search");
num=sc.nextFloat();
int f=0;
for(int i=0;i<n;i++)
if (arr[i]==num)
{
f=1;
System.out.println("element found ");
break;
}
if (f==0)
System.out.println("element not found");
}
}

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

import java.util.*;
class pr16
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
float[] arr=new float[20];
System.out.println("enter total elements");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
float num;
System.out.println("enter element whose index to search");
num=sc.nextFloat();
int f=0;
for(int i=0;i<n;i++)
if (arr[i]==num)
{
f=1;
System.out.println("element found at index "+i);
break;
}
if (f==0)
System.out.println("element not found");
}
}

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

import java.util.*;
class pr17
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
float[] arr=new float[20];
System.out.println("enter total elements");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextFloat();
float num;
System.out.println("enter element to remove");
num=sc.nextFloat();
int f=0;
for(int i=0;i<n;i++)
if (arr[i]==num)
{
f=1;
System.out.println("element found at index "+i);
System.out.println("element deleted"+arr[i]);
for (int j=i;j<n-1;j++)
arr[j]=arr[j+1];
n--;
break;
}
if (f==0)
System.out.println("element not found");
System.out.println("Resultant array after deletion is ");
for(int i=0;i<n;i++)
System.out.println(arr[i]);}
}

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

import java.util.*;
class pr18
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
float[] arr=new float[20];
float[] x=new float[20];
System.out.println("enter total elements");
n=sc.nextInt();
System.out.println("enter array");
for(int i=0;i<n;i++)
arr[i]=sc.nextFloat();
for(int j=0,i=0;i<n;i++,j++)
x[j]=arr[i];
System.out.println("Resultant array after copy is");
for(int i=0;i<n;i++)
System.out.println(x[i]);

}
}

19. Write a Java program to insert an element (on a specific position)


into Multidimensional array

import java.util.Scanner;
public class pr19{
public static void main(String[] args)
{
int m,n,x,y,a;
int[][] two = new int[3][2]; // declared and created array object
Scanner s1 = new Scanner(System.in); //created Scanner object
System.out.println("enter number of rows and cols");
m=s1.nextInt();
n=s1.nextInt();
System.out.println("Please enter the matrix");
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n; j++)
{
two[i][j] = s1.nextInt();
}
System.out.println();
}
System.out.println("Please enter the row and col index ");
x=s1.nextInt();
y=s1.nextInt();
System.out.println("Please enter the value ");
a=s1.nextInt();
two[x][y]=a;
System.out.println("Your output would be as below:");
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n; j++)
{
System.out.print(two[i][j] + " " );
}
System.out.println();
}
}
}
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
//program 20
import java.util.*;
class pr20
{
public static void main(String[] args)
{
Scanner data =new Scanner(System.in);
System.out.println("Enter string1 ");
String str;int n;
str=data.nextLine();
System.out.println("Enter string 2 ");
String str1;int n1;
str1=data.nextLine();
if (str.compareTo(str1)==0)
System.out.println("Strings are
equal");
else
System.out.println("Strings are not
equal");
n=str.length();
System.out.println("length of string is"+n);
System.out.println("String in
uppercase"+str.toUpperCase());
System.out.println("String in
lowercase"+str.toLowerCase());
System.out.println("String joined
are"+str.concat(str1));
System.out.println("Substring is
"+str.substring(2,6));
}}
21.Developed Program & design a method to find the smallest
number among three numbers.
import java.util.*;

class pr21
{
static void smallest(int a,int b,int c)
{ int g;
if(a<b && a<c)
g=a;
else if(b<a && b<c)
g=b;
else
g=c;
System.out.println("Smallest of 3 numbers"+g);
}
public static void main(String[] args)
{
int a,b,c,g;
Scanner i=new Scanner(System.in);
System.out.println("Enter three numbers");
a=i.nextInt();//first number
b=i.nextInt();
c=i.nextInt();
smallest(a,b,c);
}
}

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


import java.io.*;
class pr22
{
public static void main(String[] args) throws IOException
{
BufferedReader bf=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter 3 numbers");
int a=Integer.parseInt(bf.readLine());
int b=Integer.parseInt(bf.readLine());
int c=Integer.parseInt(bf.readLine());
int av=(a+b+c)/3;
System.out.println("Average of 3 numbers"+av);
}
}

23.Write a Program & design a method to count all vowels in a


string.

//program 23
import java.util.*;
class pr23
{
public static void main(String[] args)
{
Scanner data =new Scanner(System.in);
System.out.println("Enter a string");
String str;int n;
str=data.nextLine();
char ch;
n=str.length();
int c=0;
for (int i=0;i<n;i++)
{
ch=str.charAt(i);
//ch=ch.toUpperCase();
if (ch=='A' ||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a' ||ch=='e'||ch=='i'||
ch=='u'||ch=='o')
c++;
}
System.out.println("Number of vowels are"+c);
}
}

24.Write a Java method to count all words in a string

//program 24
import java.util.*;
class pr24
{
public static void main(String[] args)
{
Scanner data =new Scanner(System.in);
System.out.println("Enter a string");
String str;int n;
str=data.nextLine();
char ch;
n=str.length();
int c=1;
for (int i=0;i<n;i++)
{
ch=str.charAt(i);
if (ch==' ')
c++;
}
System.out.println("Number of words are"+c);
}
}

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

//program 25
import java.util.*;
class pr25
{
static void countwords(String str)
{
char ch;
int n=str.length();
int c=1;
for (int i=0;i<n;i++)
{
ch=str.charAt(i);
if (ch==' ')
c++;
}
System.out.println("Number of words are"+c);
}

public static void main(String[] args)


{
Scanner data =new Scanner(System.in);
System.out.println("Enter a string");
String str;int n;
str=data.nextLine();
countwords(str);

}
}

26.Write a Java program to handle following exceptions:


1) Divide by Zero Exception.

import java.io.*;
class pr26 {
public static void main(String[] args)
{
int a = 5;
int b = 0;
try {
System.out.println(a / b); // throw Exception
}
catch (ArithmeticException e) {
// Exception handler
System.out.println(
"Divided by zero operation cannot possible");
}
}
}

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

// Java code for thread creation by extending


// the Thread class
class MultithreadingDemo extends Thread {
public void run()
{
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
// Main Class
public class pr27 {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}

28.To represent the concept of all types of inheritance supported by


Java, design a program.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class pr28{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}

29. Write a program to implement Multiple Inheritance using


interface.

interface Printable{
void print();
}
interface Showable{
void show();
}
class pr29 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}

public static void main(String args[]){


pr29 obj = new pr29();
obj.print();
obj.show();
} }

30.Construct a program to design a package in Java.

package mypack;
public class pr30{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
31.To write and read a plain text file, write a Java program.

import java.io.*;
import java.io.IOException;

public class pr31 {

public static void main(String[] args) {


try {
FileWriter writer = new FileWriter("MyFile.txt", true);
writer.write("Hello World");
writer.write("\r\n"); // write new line
writer.write("Good Bye!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader reader = new FileReader("MyFile.txt");
int character;

while ((character = reader.read()) != -1) {


System.out.print((char) character);
}
reader.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
32.Write a Java program to append text to an existing file.

import java.io.*;
import java.io.IOException;

public class pr32 {

public static void main(String[] args) {


try {
FileWriter writer = new FileWriter("MyFile.txt", true);
writer.write("Hello Dear");
writer.write("\r\n"); // write new line
writer.write("Good luck");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader reader = new FileReader("MyFile.txt");
int character;

while ((character = reader.read()) != -1) {


System.out.print((char) character);
}
reader.close();

} catch (IOException e) {
e.printStackTrace();
}
}}
33.Design a program in Java to get a list of all file/directory names
from the given.

// Java program to demonstrate the


// use of getName() function

import java.io.*;

public class pr33{


public static void main(String args[])
{

// try-catch block to handle exceptions


try {

// Create a file object


File f = new File("c:\\users\\program.txt");

// Get the Name of the given file f


String Name = f.getName();

// Display the file Name of the file object


System.out.println("File Name : " + Name);
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}}
34.Develop a Java program to check if a file or directory specified by
pathname exists or not.

// Java Program to Illustrate exists() method of File Class

// Importing input output classes


import java.io.*;

// Main class
public class pr34 {

// Main driver method


public static void main(String args[])
{
// Getting the file by creating object of File class
File f = new File("F:\\program.txt");

// Checking if the specified file exists or not


if (f.exists())

// Show if the file exists


System.out.println("Exists");
else

// Show if the file does not exists


System.out.println("Does not Exists");
}
}
35.Write a Java program to check if a file or directory has read and
write permission.

import java.io.File;
class pr35 {
public static void main(String[] args) {

File f0 = new File("myfile.txt");


if (f0.exists()) {

System.out.println("Is file writeable?: " + f0.canWrite());

System.out.println("Is file readable " + f0.canRead());

} else {
System.out.println("The file does not exist.");
}
} }

You might also like