List of Java algorithms, java programmes:
import java.util.*;
public class SumOfHarmonicSeries
{
public staticc void main(string[] args)
{
scanner scan=new scanner(system.in);
system.out.print("enter number of terms (n:");
int n=scan.nextInt();
double sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+1.0/i;
}
System.out.rintln("sum of Harmonic Series="+sum);
}
}
------------------------------------------------------------------------------------------------------------------
2. Write a program to perform the following operations on string through interactive input.
a. Sort given strings in alphabetical order.
import java.io.*;
imort java.util.*;
public class MinString
{
privae stativ final int SIZE=10;
public static void main(String[] args)
{
string[] list=new string[SIZE];
int numItems;
numIterms=Initialize(list);
System.out.println(nu,Items);
}
private static int Intialize(string[] list)
{
string filename, stateInput;
int i=0;numIterm=0;
try
{
System.out.print("Input File:");
scanner stdin=new scanner(system.in);
filename=stdin.nextLine();
stdin=new scanner(new File(filename));
while ((stdin.hasNext()) && (i<list.length))
stateInput=stddin.nextLine();
System.out.println("S="+stataInput);
list[i]=stateInput;
i++;
}
numItems=i;
}
catch(IOException e)
{
system.out.println(e.getMessage());
}
return numItems;
}
// method find min ..
private static String FindMin(String[] list, numItems);
}
b. Check whether one string is sub string of another string or not.
public class CrunchifyFindString
{
public static void main(string[] args)
{
System.out.println("\nResult="+findMe("findMe", "Can you findMe from this string?"));
}
public static boolean findMe(String subString, String main String)
{
boolean foundme=false;
int max=mainString.length()-subString.length();
System.out.rintln
(mainString.contains(subString) ? "mainString.contains(subString) check passed.."
:"mainString.contains(subString) check Failed..");
checkrecursion: for(int i=0;i<=max;i++)
{
int n=subString.length();
int j=i;
int k=0;
while(n--!=0)
{
if(mainString.charAt(j++) !=subString.charAt(k++))
{
continue checkrucursion;
}
}
foundme=true;
break checkrecursion;
}
System.out.println(foundme ? "\nImlment yout own contains() Method - Result:yes,Match Found.."
: "\nImplement your own contains() Method - Result: Noe - No match found..");
return found me;
}
}
c. convert the string to uppercase.
import java.io.*;
public class Test
{
public static void main(String args[])
{
String Str=new String("Welcome to the world of java");
System.out.print("return value");
System.out.println(Str.toUerCase());
}
}
3. Program tosimulate online shopping:
import java.io.*;
class Shoping
{
public satic void main(String args[])throws Exception
{
int i=o,x,op,sum=0,m;
String con="";
int a={}=new int{10);
int n{}=new int[10];
String s[]={"Self Confidence-book(rs.200)",:My Experiments wit rtuth(Rs.100)", "java Apporva bool(Rs.150)","Java Comple Reference(Rs.300)"};
inputStramReader isr=new InutStramReader(System.in);
BufferedReader br=new BufferedReader(isr);
do
{
System.out.println("Select items from the list:n");
System.out.println("1.Self Confidence-book\tRs.200");
System.out.println("2.My Experiment with truth\tRs.100");
System.out.println("3. Java Apporva book\tRs.150");
System.out.println("4. Java complete Refrence\tRs.300");
System.out.println("\nSelect the item number:");
x=Integer.parseInt(br.readLine());
if(x<1||x>4) continue;
a[i]=x;
Sysem.out.print;n("Enter quantity:");
n[i]=Integer.parseInt(br.readLine());
i++;
System.out.println("Add more items?(y,n):");
con=br.readLine();
}
while(con.equals("y"));
m=i;
System.out.println("\n1. check out \n2. cancel order\n");
System.out.println("Enter option:");
op=integer.arseInt(br.readLine());
if(op==1)
{
System.out.println("============================");
System.out.println("\nItems\tQuantity");
System.out.ptintln("===========================");
for(i=0;i
{
if(a[i]==1)
sum=sum+n[i]*200;
else if(a[i]==2)
sum=sum+n[i]*100;
else if(a[i]==3)
sum=sum+n[i}*150;
else if(a[i]==4)
sum=sum+n[i}*200;
System.out.println("\n"+s[a[i]]+\t"n[i]);
}
System.out.println("==============================");
System,out.println("\nTotal bill:"+sum);
System.out.println("==============================");
}
}
}
------------------------------------------------------------------------------------------------------------------------
4. Program to find out duplicate value in vector:
import java.io.*;
import java.util.*;
class VetorTest
{
public static void main(String args[])throws Exception
{
int i,j,n;
Vector v1=new Vector(10);
Vector v2=new Vector(10);
InputStreamReader ist=new InputStreamReader(System.in);
BufferReader br=new BufferReader(ist);
System.out.println("How many strings:");
n=Integer.parseInt(br.readLine());
System.out.println("enter strings:\n)");
for(i=0;i
v1.add(br.readLine());
System.out.println("Vector Elaments:\n+v1);
for(i=0;i
(for(j=i+1;j
if(v1.elementAt(i).equals(v1.elementAt(j)))
v2.add(v1.elementAt(j));
System.out.println("\n duplicate elements:\m"+v2);
}
}
----------------------------------------------------------------------------------------------------------------------
5. Create two threads such that one of hte thread print even no's and another prints odd no's up to a given range.
class MyThread extends Thread
{
int initval,finalval;
MyThread(int iv,int fv)
{
initval=iv;
finalval=fv;
start();
}
public void run()
{
for(int i=initval;i<=finalval;i+-2)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
carch(Exetion e){}
}
}
}
class ThreadTest
{
public static void main(String args[])
{
MyThread t1=new MyThread(1,6);
MyThread(t2=new MyThread(2,6);
}
}
--------------------------------------------------------------------------------------------------------------------------
6. Define an exception called "Marks Out of Bound" Exception, that is thrown if the enered marks are greater than 100.
import java.io.*;
class MarksOutOfBounds extends Exception
{
public void showError()
{
System.out.println("invalid marks");
}
}
class ErrorTest
{
ublic statci void main(String args[]) throws Exception
{
InputStreamReader isr=new InutStreamReader(System.in);
BufferReader br=new BufferReader(isr);
int m=0;
try
{
System.out.println("Enter marks:");
m=Integer.parseInt(br.readLine());
if(m>100)
throw new MarksOutOfBounds();
System.out.println("your marks:"+m);
}
catch(MarksOutOfBounds e)
{
e.showError();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
7. Write Java program to shuffle the list elements using all the possible permutations.
class ShuffleString
{
public static void main(String args[])
{
char s[]={'a','b','c'};
int i,j,k,n=s.length;
for(i=0;i
for(j=0;j
for(k=0;k
if(i!=j&&j!=k&&i!=k)
System.out.println(s[i]+","+s[j]+","+s[k]);
}
}
--------------------------------------------------------------------------------------------------------------------------
8. Create a package called "Arithmetic" that contains methods to deal with all arithmetic operations. Also write a program to use a package.
first create a directory as"Arithmetic" and move to that directory, create the following file and compile the file in the same directory.
package arithmetic;
ublic class MyMath
{
public int add(intx,int y)
{
return x+y;
}
public int sub(int x,inty)
{
return x-y;
}
ublic int mul(int x,int y)
{
return x*y;
]
public double div(int x,int y)
{
rreturn (double)x/y;
}
public int mod(int x,int y)
{
return x%y;
}
}
// move to parent directory, write the following file and execute it.
import arithmetic.*;
class Test
{
ublic static void main(String args[])
{
MyMath m=newMyMath();
System.out.println(m.add(8,5));
System.out.println(m.sub(8,5));
System.out.println(m.mul(8,5));
System.out.println(m.div(8,5));
Sysem.out.rintln(m.mod(8,5));
}
}
--------------------------------------------------------------------------------------------------------------------------
5. Create two threads such that one of hte thread print even no's and another prints odd no's up to a given range.
class MyThread extends Thread
{
int initval,finalval;
MyThread(int iv,int fv)
{
initval=iv;
finalval=fv;
start();
}
public void run()
{
for(int i=initval;i<=finalval;i+-2)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
carch(Exetion e){}
}
}
}
class ThreadTest
{
public static void main(String args[])
{
MyThread t1=new MyThread(1,6);
MyThread(t2=new MyThread(2,6);
}
}
--------------------------------------------------------------------------------------------------------------------------
6. Define an exception called "Marks Out of Bound" Exception, that is thrown if the enered marks are greater than 100.
import java.io.*;
class MarksOutOfBounds extends Exception
{
public void showError()
{
System.out.println("invalid marks");
}
}
class ErrorTest
{
ublic statci void main(String args[]) throws Exception
{
InputStreamReader isr=new InutStreamReader(System.in);
BufferReader br=new BufferReader(isr);
int m=0;
try
{
System.out.println("Enter marks:");
m=Integer.parseInt(br.readLine());
if(m>100)
throw new MarksOutOfBounds();
System.out.println("your marks:"+m);
}
catch(MarksOutOfBounds e)
{
e.showError();
}
}
}
--------------------------------------------------------------------------------------------------------------------------
7. Write Java program to shuffle the list elements using all the possible permutations.
class ShuffleString
{
public static void main(String args[])
{
char s[]={'a','b','c'};
int i,j,k,n=s.length;
for(i=0;i
for(j=0;j
for(k=0;k
if(i!=j&&j!=k&&i!=k)
System.out.println(s[i]+","+s[j]+","+s[k]);
}
}
--------------------------------------------------------------------------------------------------------------------------
8. Create a package called "Arithmetic" that contains methods to deal with all arithmetic operations. Also write a program to use a package.
first create a directory as"Arithmetic" and move to that directory, create the following file and compile the file in the same directory.
package arithmetic;
ublic class MyMath
{
public int add(intx,int y)
{
return x+y;
}
public int sub(int x,inty)
{
return x-y;
}
ublic int mul(int x,int y)
{
return x*y;
]
public double div(int x,int y)
{
rreturn (double)x/y;
}
public int mod(int x,int y)
{
return x%y;
}
}
// move to parent directory, write the following file and execute it.
import arithmetic.*;
class Test
{
ublic static void main(String args[])
{
MyMath m=newMyMath();
System.out.println(m.add(8,5));
System.out.println(m.sub(8,5));
System.out.println(m.mul(8,5));
System.out.println(m.div(8,5));
Sysem.out.rintln(m.mod(8,5));
}
}
--------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment
May I Help you