Sei sulla pagina 1di 26

class HelloJava { public static void main(String[] args) { System.out.

println("HelloJava"); } } -----------------------class charex { public static void main(String args[]) { char ch1, ch2; ch1 = 178; // code for X ch2 = 'Y'; System.out.print("ch1 and ch2: "); System.out.println(ch1 + " " + ch2); } } -----------------------class charex2 { public static void main(String[] args) { int number[]={1,2,3,4,5,6,5,4,3,2,1}; int n=number.length; System.out.println("The entered number are:"); for(int i=0;i<n;i++) { System.out.print(" "+number[i]); } } } -----------------------class arraydis { public static void main(String[] args) { int number[]={1,2,3,4,5,8,7,9,5,4,6,55,99}; int n=number.length; System.out.println("The entered values are: "); for(int i=0;i<n;i++) { System.out.print(" "+number[i]); } } } -----------------------class Array { public static void main(String[] args) { int num[]=new int[5]; num[0]=5; num[1]=4; num[2]=6; num[3]=7; num[4]=9; //num[-6]=1; System.out.println("Now we will print array");

for(int i=0; i<4; i++) { System.out.println(num[i]+""); } } } -----------------------class Trialngle { public static void main(String[] args) { for(int i=1; i<=5; i++) { for(int j=5; j>=i; j--) { System.out.print(" "+i); } System.out.println(""); } } } -----------------------class Triangleex { public static void main(String[] args) { for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print(j+" "); } System.out.println(""); } } } -----------------------class Tri { public static void main(String[] args) { for(int i=70; i>=65; i--) { for(int j=65; j<=i; j++) System.out.print(j+" "); System.out.println(""); } } } -----------------------class IfSimple { public static void main(String[] args) { int x,y; x=10; y=20; if(x<y) System.out.println("\n x is less than y ");

x=x*2; if(x==y) System.out.println("\n x is equal to y"); x=x*2; if(x>y) System.out.println("\n x is greater than y"); if(x==y) System.out.println(" You wont see this"); } } -----------------------class CharDemo { public static void main(String args[]) { char ch1, ch2; ch1=88; ch2='y'; System.out.println("Ch1 is "+ch1); System.out.println("Ch2 is "+ch2); } } -----------------------class ForTest { public static void main(String[] args) { for(int x=0; x<10; x++) System.out.println("The value of x:"+x); } } -----------------------class DynInit { public static void main(String args[]) { double a=3.0, b=4.0; double c=Math.sqrt(a*a+b*b); System.out.println("c is "+c); } } -----------------------class TriEx { public static void main(String[] args) { for(int i=1; i<=9; i=i+2) { for(int j=1; j<=i; j=j+2) System.out.print(j+" "); System.out.println(""); } } } -----------------------class Search { public static void main(String[] args) { int a[]={5,6,8,4,9,2,7};

int n=4, c=1, i; for(i=0; i<n; i++) { if(a[i]==n) break; else c++; } System.out.println(a[i]+" is found at "+c); } } -----------------------class Sorting { public static void main(String args[]) { int number[]={8,6,9,7,1,2,4,3}; int n=number.length; System.out.println("Entered values are: "); for(int i=0; i<n; i++) { System.out.print(" "+number[i]); } for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(number[i]>number[j]) { int temp=number[i]; number[i]=number[j]; number[j]=temp; } } } System.out.println("\nAfter sorting the elements are: "); for(int i=0; i<n; i++) { System.out.print(" "+number[i]); } } } -----------------------class Searchex { public static void main(String[] args) { int a[]={2,6,4,5,8,9,77,66,88,44,15,33,94,56,28,46}; int n=a.length; int s=94, c=1, i; for(i=0; i<n; i++) { if(a[i]==s) break; else c++; } System.out.println(a[i]+" is found at " System.out.println("\n\ni/j= "+d+"\n\n"); }

} -----------------------class NestedDemo { public static void main(String[] args) { for(int i=1; i<=4; i++) { for(int j=1; j<=i; j++) System.out.print(j+" "); System.out.println(""); } } } -----------------------class Tria { public static void main(String[] args) { for(int i=5; i>0; i--) { for(int j=5; j>=i; j--) System.out.print(j+" "); System.out.println(""); } } } -----------------------class testingg { public static void main(String[] args) { int i,j; for(i=5; i>=1; i--) { for(j=5; j>=i; j--) System.out.print(j+" "); System.out.println(""); } } } -----------------------class SumAvg { public static void main(String[] args) { int sum=0; for(int i=0; i<args.length; i++) { int value=5; sum=sum+value; } System.out.println("Sum= "+sum); float avg=(float)(sum)/args.length; System.out.println("Avg= "+avg); } } -----------------------class Randam {

public static void main(String[] args) { int value=(int)(Math.random()*100); System.out.println("Randomly generated value: "+value); if(value%2==0) System.out.println("Even"); else System.out.println("Odd"); } } -----------------------class StrEx { public static void main(String[] args) { String str=new String("Hello"); //String str="Hellllllllo"; System.out.println("The Entered string is: "+str); //System.out.println(str); } } -----------------------class Str1Ex { public static void main(String[] args) { // char ch[]={'I','N','D','I','A'}; // String st1=new String(ch); // String st2=new String(st1); // System.out.println(ch); // System.out.println("First String is "+st1); // System.out.println("Second String is "+st2); // // // // // // // // // // // // String str="java is a good language"; int len=str.length(); System.out.println("Length is: "+len); int time=9; String str1="Hello World "; String str2="Good Morning "; String str3=str2+time+str1+"How are you..?"; System.out.println("New String is "+str3); String str="Hot Java"; char ch; ch=str.charAt(4); System.out.println("The char at 4th place is "+ch);

} } -----------------------class StrGetChar { public static void main(String[] args) { StringBuffer sb=new StringBuffer("Welcome to the readers"); char arr[]=new char[7]; sb.getChars(0,7,arr,0); System.out.println(arr); }

} -----------------------class StrEqual { public static void main(String[] args) { // String st1="Java"; // String st2="Java"; // String st3="JAVA"; // System.out.println("First equals to second "+st1.equals(st2)); // System.out.println("Second equals to third "+st2.equalsIgnoreCase(st3) ); // String st="Hyderabad"; // System.out.println("The String starts with 'Hyd' "+st.startsWith("hyde ")); // System.out.println("The String ends with 'bad' "+st.endsWith("bad")); } } -----------------------class CompareDemo { public static void main(String[] args) { String st1="welcome"; String st2="welcome to MissWorld"; String st3="Welcome"; if(st1.compareTo(st2)>0) System.out.println("First string is Greater than Second"); else if(st1.compareTo(st2)<0) System.out.println("First string is Smaller than Second"); else System.out.println("Both are Equal"); if(st1.compareTo(st3)>0) System.out.println("First string is Greater than Third"); else if(st1.compareTo(st3)<0) System.out.println("First string is Smaller than Third"); else System.out.println("First and Third are same"); } } -----------------------class FindIndex { public static void main(String[] args) { // String st1="Four scores and seven years ago"; // int pos=st1.indexOf("a"); // int stpos=st1.indexOf("seven"); // System.out.println(pos); // System.out.println(stpos); String str=new String("Too much liberty is harmfultolove"); System.out.println(str); int pos=str.indexOf('l'); System.out.println("First 'l' at "+pos); pos=str.indexOf('l',pos+2); System.out.println("Second 'l' at "+pos); pos=str.indexOf("liberty"); System.out.println("Liberty at "+pos); // pos=str.indexOf('l',pos+2);

// }

System.out.println("Third 'l' at "+pos);

} -----------------------class Student { int Name; float Math; float Sci; float Eng; float Avg; //compute the avg and display the value void Average() { Avg=(Math+Sci+Eng)/3; System.out.println("Average is "+Avg); } } class Mark { public static void main(String args[]) { Student st=new Student(); st.Math=60; st.Sci=80; st.Eng=70; st.Average(); } } -----------------------class GetDetails { public static void main(String[] args) { int empno; float salary; char dept; void getdetails() { empno=10; salary=2000.50; dept='k'; } void showdetails() { System.out.println(empno); System.out.println(salary); System.out.println(dept); } } } -----------------------class Student { int rollno; float maths; float science;; float english; float calcavg(float maths, float science, float english) {

return(maths+science+english)/3; } } class ReturnValueDemo { public static void main(String[] args) { Student st=new Student(); float Average; st.maths=60; st.science=75; st.english=50; //Now calculate the average //Call the function calcavg Average=st.calcavg(st.maths, st.science, st.english); System.out.println("Average mark is "+Average); } } -----------------------class anand { int rollno; float maths; float science; float english; float anandavg(float maths, float science, float english) { return(maths+science+english)/3; } } class Student { public static void main(String[] args) { anand an=new anand(); float average; an.maths=65; an.science=86; an.english=75; //now calculate the average //call the function avg average=an.anandavg(an.maths,an.science,an.english); System.out.println("Average mark is "+average); } } -----------------------//class Students //{ // int stno; // float maths; // float science; // float english; // float avg; // //compare the average and display the value // void avg() // { // avg=(maths+science+english)/3; // System.out.println("Average mark is "+avg); // } //}

//class Student //{ // public static void main(String[] args) // { // Students st=new Students(); // //now assign values to the object // an.maths=80; // an.science=80; // an.english=90; // an.avg(); // } //} -----------------------class Student { int StuNO; float maths; float science; float english; float average; //compute the average and display the value void average() { average=(maths+science+english)/3; System.out.println("Average mark is "+average); } } class Studentex { public static void main(String[] args) { Student st=new Student(); st.maths=60; st.science=80; st.english=70; st.average(); } } -----------------------class Staticex { int x; static int y; public void inc() { x=x+1; y=y+1; } public void show() { System.out.println("The value of x is "+x); System.out.println("The value of y is "+y); } } class StaticDemo { public static void main(String[] args) { Staticex st1=new Staticex(); Staticex st2=new Staticex();

System.out.println("For the first object"); st1.inc(); st1.show(); System.out.println("For the second object"); st2.inc(); st2.show(); } } -----------------------class Student { int stuno; String name; int mark; static int stucount; public void show() { System.out.println("The student details are "); System.out.println("Name is "+name); System.out.println("No is "+stuno); System.out.println("Marks are "+mark); System.out.println("The studount is "+stucount); } } class StaMethEx { public static void main(String[] args) { Student st1=new Student(); Student st2=new Student(); Student st3=new Student(); st1.stucount++; st1.show(); st2.stucount++; st2.show(); st3.stucount++; st3.show(); } } -----------------------class First { int length; int breadth; int FindArea(int x, int y) { return(x*y); } } class Second { int length; int breadth; int height; int FindArea(int x, int y) { return(x*y); } int FindVolume(int x, int y, int z) {

return(x*y*z); } } class Inher { public static void main(String[] args) { First fi=new First(); fi.length=2; fi.breadth=3; int Area=fi.FindArea(fi.length, fi.breadth); System.out.println("For First element"); System.out.println("The Area is "+Area); Second se=new Second(); se.length=2; se.breadth=3; se.height=4; Area=se.FindArea(se.length, se.breadth); System.out.println("For Second element"); System.out.println("The Area is "+Area); int Volume=se.FindVolume(se.length, se.breadth, se.height); System.out.println("The Volume is "+Volume); } } -----------------------class FirstClass { void firstMethod() { System.out.println("Message from FirstClass"); } } class SecondClass extends FirstClass { void secondMethod() { System.out.println("Message from SecondClass"); } } class ThirdClass extends SecondClass { void thirdMethod() { System.out.println("Message from ThirdClass"); } } class FourthClass extends ThirdClass { void fourthMethod() { System.out.println("Message from FourthClass"); } } class TestInheritance { public static void main(String args[]) { FourthClass fc=new FourthClass(); fc.firstMethod(); fc.secondMethod();

fc.thirdMethod(); fc.fourthMethod(); System.out.println("All the messages were displayed"); } } -----------------------class SuperClass { void superMethod() { System.out.println("SuperClass called"); } } class FirstSubClass extends SuperClass { void firstsubMethod() { System.out.println("Message from FirstSubClass"); } } class SecondSubClass extends SuperClass { void secondsubMethod() { System.out.println("Message from SecondSubClass"); } } class MultiLevelTest { public static void main(String[] args) { SuperClass sc=new SuperClass(); FirstSubClass fsc=new FirstSubClass(); SecondSubClass ssc=new SecondSubClass(); sc.superMethod(); fsc.superMethod(); fsc.firstsubMethod(); ssc.superMethod(); ssc.secondsubMethod(); } } -----------------------class Rect { float length; float breadth; float height; float volume(float l, float b, float h) { return(l*b*h); } } class Volume extends Rect { public static void main(String[] args) { Rect r=new Rect(); float vol; r.length=5; r.breadth=6;

r.height=7; vol=r.volume(r.length, r.breadth, r.height); System.out.println("The Volume of the Rectangle is "+vol); } } -----------------------class First { void firstMethod() { System.out.println("Message from First"); } } class Second extends First { void secondMethod() { System.out.println("Message from Second"); } } class Third extends Second { void thirdMethod() { System.out.println("Message from Third"); } } class Fourth extends Third { void fourthMethod() { System.out.println("Message from Fourth"); } } class FourInher { public static void main(String[] args) { Second s=new Second(); Third t=new Third(); Fourth f=new Fourth(); s.firstMethod(); t.firstMethod(); f.firstMethod(); } } -----------------------class Student { int Rollno; float maths; float science; float english; float average(float m, float s, float e) { return(m+s+e)/3; } } class ReturnDemo {

public static void main(String[] args) { Student stu=new Student(); float avg; stu.Rollno=1206; stu.maths=88; stu.science=97; stu.english=86; avg=stu.average(stu.maths, stu.science, stu.english); System.out.println("Average mark is "+avg); } } -----------------------class Construct { int length; int breadth; Construct(int x, int y) { length=x; breadth=y; System.out.println("The values are "+length+","+breadth); } void display() { } } class TestConstruct { public static void main(String[] args) { Construct ct=new Construct(4,5); //Construct cr=new Construct(); ct.display(); //cr.display(); } } -----------------------class PolyTest { int r,l,b; public PolyTest(int x, int y, int z) { r=x; l=y; b=z; } void area(int rad) { double res; res=(double)rad*3.10*3.14; System.out.println("Area of Circle is: "+res); } void area(int len, int bre) { int res; res=len*bre; System.out.println("Area of Rectangle is: "+res);

} } class PolyAreaTest { public static void main(String[] args) { PolyTest pt=new PolyTest(4,5,6); PolyTest pp=pt; PolyTest p=pp; p.area(4); p.area(1,2); pp.area(6); pp.area(7,8); pt.area(5); pt.area(5,4); } } -----------------------class ObjectTest { int i,j; void setValues(int x, int y) { i=x; j=y; } void display() { System.out.println("Values are "+i+","+j); } } class ObjectMain { public static void main(String[] args) { ObjectTest ob=new ObjectTest(); ObjectTest ot=ob; ob.setValues(4,5); System.out.print("The present "); ob.display(); ot.setValues(10,20); System.out.print("The new "); ob.display(); ot.display(); } } -----------------------class MyObject { int a,b; void sendObject(MyObject m) { m.a=20; m.b=30; } void display() { System.out.println("The values are"+a+","+b); } }

class MainClass { public static void main(String[] args) { MyObject my=new MyObject(); my.sendObject(my); my.display(); } } -----------------------class RectArea { int length; int breadth; void Area(int l, int b) { length=l; breadth=b; System.out.println("Area is: "+(l*b)); } } class ConsFindArea { public static void main(String args[]) { RectArea ra=new RectArea(); ra.Area(4,5); } } -----------------------class ObjectDemo { int x; int y; void sendObject(ObjectDemo ob) { ob.x=5; ob.y=6; } void display() { System.out.println("The Values are:"+x+","+y); } } class ObjectTest { public static void main(String args[]) { ObjectDemo od=new ObjectDemo(); ObjectDemo ob=new ObjectDemo(); od.sendObject(od); od.display(); ob.sendObject(od); ob.display(); } } -----------------------class ThisDemo { int x,y;

void assignValues(int x, int y) { this.x=x; this.y=y; } void display() { System.out.println("The values of theis object are: "+x+","+y); } } class ThisTest { public static void main(String[] args) { ThisDemo th=new ThisDemo(); ThisDemo td=new ThisDemo(); th.assignValues(10,20); td.assignValues(4,5); th.display(); td.display(); } } -----------------------class TestReturn { int i,j; public TestReturn() {} TestReturn assignValues() { TestReturn re=new TestReturn(); re.i=10; re.j=20; return re; } } class ObjReturn { public static void main(String[] args) { TestReturn rt=new TestReturn(); TestReturn rt1=new TestReturn(); System.out.println("The Values are "+rt.i+","+rt.j); } } -----------------------//class TestReturn //{ // TestReturn assignValue() // { // TestReturn t=new TestReturn(); // return(t); // } //} //class ObjReturn //{ // public static void main(String[] args) // { // TestReturn tr=new TestReturn(); // tr.assignValue();

// } //} -----------------------package newpack; class Demo { int k, num; public void show(int a, int b) { System.out.println("The numbers are: "); System.out.println(a+" "+" "+b); System.out.println("Their sum is "+(a+b)); } } class PackageDemo { public static void main(String args[]) { Demo d=new Demo(); d.show(20,30); } } -----------------------package newpack; public class Demo1 { int k, num; public void show(int a, int b) { System.out.println("The numbers are: "); System.out.println(a+" "+" "+b); System.out.println("Their sum is "+(a+b)); } } -----------------------import newpack.*; class UsePackdeme { public static void main(String[] args) { Demo1 d=new Demo1(); d.show(40,60); } } -----------------------package newpack; class addition { int a,b; public void show(int x, int y) { System.out.println("The numbers are: "); System.out.println(a+" "+" "+b); System.out.println("The addition is: "+(a+b)); } } ======================== package newpack; public class addition {

int k, num; public void show(int a, int b) { System.out.println("The numbers are: "); System.out.println(a+" "+" "+b); System.out.println("Their sum is "+(a+b)); } } -----------------------import newpack.*; class add { public static void main(String[] args) { addition ad=new addition(); ad.show(1,2); } }interface MyDemo { public void show(int i, int j); public void result(int i, int j); } -----------------------class IntrDemo implements MyDemo { public void show(int i, int j) { System.out.println("The numbers are "+i+","+j); } public void result(int i, int j) { System.out.println("Sum is "+(i+j)); } } -----------------------class InterDemo { public static void main(String[] args) { MyDemo m=new IntrDemo(); m.show(10,20); m.result(20,30); } } -----------------------interface IDemo { public void show(int i, int j); } class first implements IDemo { public void show(int i, int j) { System.out.println("Values from first are "+i+","+j); } } class second implements IDemo { public void show(int i, int j)

{ System.out.println("The values from second are "+i+","+j); System.out.println("Their sum is "+(i+j)); } } class InterfaceDemo { public static void main(String[] args) { IDemo id=new first(); IDemo sd=new second(); id.show(10,20); sd.show(30,40); } } -----------------------interface interf { public void show(int i, int j); } interface First { public void one(int i, int j) { System.out.println("Values from first are "+i+","+j); } } interface Second { public void two(int i, int j) { System.out.println("Values from second are "+i+","+j); } } interface Third extends First extends Second { public void three(int i, int j) { System.out.println("Values from third are "+i+","+j); } } class MultiInherInter { public static void main(String[] args) { Third t=new Third(); Second s=new Second(); First f=new First(); } } -----------------------interface Mydemo { public void show(int i, int j); public void result(int i, int j); } class InterDemo implements Mydemo { public void show(int i, int j)

{ System.out.println("The values are: "+i+"'"+j); } public void result(int i, int j) { System.out.println("The sum is: "+(i+j)); } } class DemoMain { public static void main(String[] args) { Mydemo m=new InterDemo(); m.show(10,20); m.result(1,2); } } -----------------------interface Idemo { public void show(int i, int j); } class first implements Idemo { public void show(int i, int j) { System.out.println("Values from first are: "+i+","+j); } } class second implements Idemo { public void show(int i, int j) { System.out.println("The values from second are "+i+","+j); System.out.println("Their sum is "+(i+j)); } } class InterfaceDemo1 { public static void main(String[] args) { Idemo id=new first(); Idemo sd=new second(); id.show(10,20); sd.show(2,3); } } -----------------------class ExceptionDemo { public static void main(String args[]) { int x,y; x=10; y=0; int z=x/y; System.out.println("The result is "+z); } } ------------------------

class Array { public static void main(String[] args) { int num[]=new int[5]; try { num[0]=5; num[1]=4; num[2]=6; num[3]=7; num[4]=9; //num[-6]=1; System.out.println("Now we will print array"); for(int i=0; i<4; i++) { System.out.println(num[i]+""); } } catch(Exception e) { System.out.println("The Exception is: "+e); System.out.println("Array values are out of limit"); } } } -----------------------class AccessDemo { private int no; private int dt; int x,y; AccessDemo() { no=10; dt=23; x=5; y=6; } void print() { System.out.println("The values of variables are "+no+" "+dt+" "+x+" "+y) ; } } class AccessTest { public static void main(String[] args) { AccessDemo ad=new AccessDemo(); // System.out.println(ad.no); // System.out.println(ad.dt); System.out.println(ad.x); System.out.println(ad.y); ad.print(); } } -----------------------class ExceptionDemo1 {

public static void main(String args[]) { int i=2; int j=0; try { int z=i/j; System.out.println("The result is "+z); } catch(Exception e) { System.out.println("The Exception is: "+e); System.out.println("'j' should not be '0'"); System.out.println("Enter the correct value"); } } } -----------------------class FileText { public static void main(String[] args) { try { file f=new File("test.dat"); int i,j; i=0; int k=j/i; f.close(); } catch(ArithmeticException ae) { System.out.println("Arithmetic Exception Occured"); } catch(FileNotFoundException fe) { System.out.println("File not found exception"); } } } -----------------------class MultiCatch { public static void main(String args[]) { try { int x=System.in.read(); System.out.println(1000/x); } catch(ArrayIndexOutOfBoundsException aie) { System.out.println("printStackTrace()"); aie.printStackTrace(); } catch(ArithmeticException ae) { System.out.println("getMessage()"+ae.getMessage()); } catch(Exception a)

{ System.out.println("toString()"+a.toString()); } } } -----------------------import java.io.*; class FinallyDemo { public static void main(String[] args) { try { System.out.println("Enter a char action: "); int i=System.in.read(); System.out.println("Char action entered is: "+(char)i); } catch(IOException e) { System.out.println("Catch block happens only if exception occurs"); e.printStackTrace(); } finally { System.out.println("Finally always occurs"); } } } -----------------------class Throwsexception { static void myownexception() throws ClassNotFoundException { throw new ClassNotFoundException("myownexception"); } public static void main(String[] args) { try { myownexception(); } catch(ClassNotFoundException e) { System.out.println("myownexception exception caught"); } } } -----------------------class Exc { public static void main(String[] args) { int numerator=131; int denominator=0; int result; System.out.println("The Entered Values are: "+numerator+","+denominator); try { result=numerator/denominator; System.out.println("result is "+result);

} catch(Exception e) { System.out.println("denominator should not be 0"); } } } -----------------------class ThreadDemo { public static void main(String[] args) { Thread th=Thread.currentThread(); System.out.println("The current thread is "+th); th.setName("Anand"); System.out.println("The changed thread is "+th); } } -----------------------class SleepDemo { public static void main(String args[]) { Thread th=Thread.currentThread(); try { for (int i = 1; i < 10; i++) { System.out.println(i); Thread.sleep(555); } } catch(InterruptedException e) { System.out.println("Thread Interrupted"); } } }

Potrebbero piacerti anche