# 1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.

## Presentation on theme: "1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public."— Presentation transcript:

1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public static void meth1(){ Test1 t1 = new Test1(); t1.meth2(); Test2 t2 = new Test2(); t2.meth3(); } public void meth2(){ System.out.println("this is meth2"); } public static void main(String [] args){ meth1(); } } class Test2{ public void meth3(){ System.out.println("Inside meth3"); }

2 final /* Compute volume for a given mass and generate a table for values of mass = 1.0, 2.0, 3.0, 4.0, 5.0 using a for loop */ class cylinder { public static void main (String args[]) { final double pi = 3.14159; double radius, height, volume; radius = 1.0; height = 2.0; volume = pi * radius * radius * height; System.out.println("Volume is: "+volume); radius = 1.0; height = 2.0; // pi = 3.0; volume = pi * radius * radius * height; System.out.println("Volume is: "+volume); }

3 final /* Compute volume for a given mass and generate a table for values of mass = 1.0, 2.0, 3.0, 4.0, 5.0 using a for loop */ class densityTableFor { public static void main (String args[]) { double mass, volume; final double density = 13.6; for(mass = 1.0; mass <=5.0; mass = mass + 1.0){ volume = mass/density; System.out.println("If mass is " + mass + " then the volume is " + volume); } System.out.println(); // density = 13.0; for(mass = 1.0; mass <=2.0; mass = mass + 0.1){ volume = mass/density; System.out.println("If mass is " + mass + " then the volume is " + volume); }

4 StringTokenizer import java.util.StringTokenizer; class tokenizerdemo{ public static void main(String arg[]){ ConsoleReader console = new ConsoleReader(System.in); System.out.println("Pls give string input"); String z = console.readLine(); StringTokenizer st1 = new StringTokenizer(z); while (st1.hasMoreTokens()){ String token = st1.nextToken(); System.out.println(token); } StringTokenizer st2 = new StringTokenizer(z); int tokencount = st2.countTokens(); for (int i=0; i<tokencount; i++){ String token = st2.nextToken(); System.out.println(token); }

5 Roots of an equation /* Solve x*x-3x+1=0 x = 1/3(x*x+1) OR x = 3 - 1/x */ public class equation{ public static void main(String args[]){ double error=0.000001; double x1=0.2; //double x1=1.0; //double x1=2.6; //double x1=3.0; double x2=(x1*x1+1)/3; while(Math.abs(x2-x1)>error){ System.out.println(x2); x1=x2; x2=(x1*x1+1)/3; } System.out.println(x1); System.out.println("FIRST LOOP ENDS");

6 Roots of an equation /* Solve x*x-3x+1=0 x = 1/3(x*x+1) OR x = 3 - 1/x */ x1=0.2; //x1=1.0; //x1=2.6; //x1=3.0; x2=3-1/x1; while(Math.abs(x2-x1)>error){ System.out.println(x2); x1=x2; x2=3-1/x1; } System.out.println(x1); }

7 0.3466666666666667 0.37339259259259255 0.3798073427343393 0.38141787253163995 0.3818265311621874 0.3819304999664496 0.3819569689348741 0.3819637087059721 0.3819654249228069 FIRST LOOP ENDS -2.0 3.5 2.7142857142857144 2.6315789473684212 2.62 2.618320610687023 2.618075801749271 2.61804008908686 2.6180348787749894

8 With x1=3.0 3.3333333333333335 4.037037037037037 5.76588934613626 11.41515998396254 43.768625819819896 638.8975353851349 136063.68690706659 6.171108965248081E9 1.2694195286988415E19 5.371419799473296E37 9.617383554057915E74 3.083135547528788E149 3.1685749348118803E298 Infinity FIRST LOOP ENDS 2.6666666666666665 2.625 2.619047619047619 2.618181818181818 2.6180555555555554 2.618037135278515 2.6180344478216817

Download ppt "1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public."

Similar presentations