Sunday, 12 December 2021

JAVA PROGRAMMING CODES || AryanTechsource

 



PART-A

1. Implement a stack using a stack class which has methods push, pop.

import java.util.Stack;
class StackMain
{
public static void main(String[] args)
{
Stack<String> courses= new Stack <String>();
courses.push("BBA");
courses.push("BCA");
courses.push("BA");
System.out.println("stack: " + courses);
courses.pop();
System.out.println("stack after pop: " + courses);
}
}



2.Shapes using function overloading.

3.Geometrical figures using abstract class.

4.Sort the vector and display.

import java.util.*;
import java.util.Vector;
public class VectorSort
{
public static void main(String args[])
{
Vector<Integer>in=new Vector<Integer>();
in.add(30);
in.add(70);
in.add(50);
in.add(10);
in.add(40);
in.add(20);
in.add(60);
in.add(100);
System.out.println("Original vector:"+ in);
Collections.sort(in);
System.out.println("ln sorted vector:"+ in);
}
}



5.Multiple catch claues to handle exceptions

public class MultipleCatchBlock1
{
public static void main(String[] args)
{
try
{
int a[]=new int[5];
a[5]=25/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
System.out.println("rest of the code");
}
}



6.Applet which recieves font name, font size as parameters.

7.Reads a file and displays the files with a line no before each line.

import java.util.*;
import java.io.*;
class Rfile
{
public static void main(String args[])throws IOException
{
int j=1;
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter File name:");
String str=scr.next();
FileInputStream f=new FileInputStream(str);
System.out.println("\n contents of the files are");
int n=f.available();
System.out.print(j+": ");
for(int i=0;i<n;i++)
{
ch=(char)f.read();
System.out.println(ch);
if(ch=='\n')
{
System.out.print(++j+": ");
}
}
}
}



8.Displays the number of characters, lines and words in a text file.

import java.util.*;
import java.io.*;
class Cfile
{
public static void main(String args[])throws IOException
{
int n1=1,nw=0;
char ch;
Scanner scr=new Scanner(System.in);
System.out.println("\nEnter file name:");
String str=scr.nextLine();
FileInputStream f=new FileInputStream(str);
int n=f.available();
for(int i=0;i<n;i++)
{
ch=(char)f.read();
if(ch=='\n')
n1++;
else if(ch==' ')
nw++;
}
System.out.println("\nNumber of lines:"+ n1);
System.out.println("\nNumber of words:"+(n1+nw));
System.out.println("\nNumber of character:"+n);
}
}



                                  PART-B

1.Find Factorial.

public class Factorial
{
public static void main(String args[])
{
int[] arr=new int[10];
int fact;
if(args.length==0)
{
System.out.println("No command line arguments");
return;
}
for(int i=0;i<args.length;i++)
{
arr[i]=Integer.parseInt(args[i]);
}
for(int i=0;i<args.length;i++)
{
fact=1;
while(arr[i]>0)
{
fact=fact*arr[i];
arr[i]--;
}
System.out.println("Factorial of"+ args[i]+"is:"+fact);
}
}
}



2.Prime no. b/w two limits.

class PrimeNumber
{
public static void main(String args[])
{
int i,j;
if(args.length<2)
{
System.out.println("No command line Arguments");
return;
}
int num1=Integer.parseInt(args[0]);
int num2=Integer.parseInt(args[1]);
System.out.println("Prime number between"+num1+ "and"+ num2+" are:");
for(i=num1; i<num2; i++)
{
for(j=2;j<i;j++)
{
int n=i%j;
if(n==0)
{
break;
}
}
if(i==j)
{
System.out.println("  "+i);
}
}
}
}



3.Implement all string operation.

class Stringoperation
{
public static void main(String args[])
{
String s1="Skyward";
String s2="Publishers";
System.out.println("The Strings are" +s1+"and" +s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("length of"+s1+"is="+ len1);
System.out.println("Length of"+s2+"is="+ len2);
System.out.println("The concatenation of two strings="+s1.concat(s2));
System.out.println("First character of" +s1+"is="+s1.charAt(0));
System.out.println("The Uppercase of"+s1+"is="+s1.toUpperCase());
System.out.println("The Lowercase of"+s1+"is="+s1.toLowerCase());
System.out.println("y occurs at position"+s1.indexOf("y")+"in"+s1);
System.out.println("Substring of" +s1+"starting from index 3 and ending at 6 is="+s1.substring(3,7));
System.out.println("Replacing'a' with 'o' in"+s1+"is="+ s1.replace('a','o'));
boolean check=s1.equals(s2);
if(check==false)
{
System.out.println(""+s1 +" and"+s2+" are not same");
}
else
{
System.out.println(""+s1+"and"+s2+"are same");
}
}
}




4.Determine if the given year is leap year or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Leapyear
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter year");
int year=Integer.parseInt(br.readLine());
if((year%400==0) || (year%4==0) && (year%100!=0))
System.out.println("year"+year+"is a leap year");
else
System.out.println("year"+year+" is a not a leap year");
}
}



5.Find max. & min. elements in the array.

public class Number
{
public static void main (String args[])
{
int nums []={88,-3,70,59,100};
int min,max;
min=max=nums[0];
for(int i=1;i<nums.length;i++)
{
if(nums[i]<min)
min=nums[i];
if(nums[i]>max)
max=nums[i];
}
System.out.println("min="+min+"max="+max);
}
}



6.Find sum of digits of a number.

import java.io.*;
public class Digitsum
{
public static void main(String args[]) throws IOException
{
int r=0, sum=0,num;
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter Number:");
num=Integer.parseInt(br.readLine());
while(num>0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
System.out.println("sum of digits is:"+sum);
}
}



7.Find reverse of a number.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Reverse
{
public static void main(String []args) throws IOException
{
int rev=0,num,digit;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
num=Integer.parseInt(br.readLine());
while(num>0)
{
digit=num%10;
num=num/10;
rev=rev*10+digit;
}
System.out.println("Reverse of number is:" +rev);
}
}




8.Generate fibonacci series.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Fibonacci
{
public static void main(String args[]) throws IOException
{
int fib1=0,fib2=1,temp=0,num;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter limit:");
num=Integer.parseInt(br.readLine());
System.out.print(fib1);
System.out.print("\t");
System.out.print(fib2);
for(int i=2; i<num; i++)
{
temp=fib1+fib2;
System.out.print("\t");
System.out.print(temp);
fib1= fib2;
fib2= temp;
}
}
}



9.Check the given number is palindrome or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Palindrome
{
public static void main (String args[]) throws IOException
{
int num,rev=0,digit,temp;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a number:");
num = Integer.parseInt(br.readLine());
temp=num;
while(num>0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(temp==rev)
System.out.println("The no is plaindrome");
else
System.out.println("The no is not plaindrome");
}
}






JAVA PROGRAMMING CODES || AryanTechsource

  PART-A 1. Implement a stack using a stack class which has methods push, pop. import java.util.Stack; class StackMain { public static void ...

Popular Posts