Java Pratical
Java Pratical
UGCA-1938
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.
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");
}
}
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);
}
}
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");
}
}
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");
}
}
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]);}
}
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]);
}
}
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);
}
}
//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);
}
}
//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);
}
}
//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);
}
}
}
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");
}
}
}
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");}
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;
} catch (IOException e) {
e.printStackTrace();
}
}
}
32.Write a Java program to append text to an existing file.
import java.io.*;
import java.io.IOException;
} catch (IOException e) {
e.printStackTrace();
}
}}
33.Design a program in Java to get a list of all file/directory names
from the given.
import java.io.*;
// Main class
public class pr34 {
import java.io.File;
class pr35 {
public static void main(String[] args) {
} else {
System.out.println("The file does not exist.");
}
} }