[code] import java.lang.Math; public class Find_sqrt { public static void main(String s[]){ String s=”23″; int i=Integer.parseInt(s); int result=Math.sqrt(i); System.out.println(“Square root of “+i+” : “+result); } } [/code]
1. Write a class Foo that wraps two Integer objects (representing a fraction) and has method(s) for displaying itself as a fraction.
2. Write a class Foo2 that wraps two Foo objects (the two fractions to be operated on).
3. Write a Foo2 method for each of the four arithmetic operations. Each method will compute a resulting Foo object from the operation and will return that object and/or store it internally for later access.
You should be able to do something like this after completing the above:
// Add, subtract, multiply and divide 2/3 and 4/5 and display result.
numer_1 = new Integer(2); denom_1 = new Integer(3); numer_2 = new Integer(4); denom_2 = new Integer(5); frac1 = new Foo(numer_1, denom_1); frac2 = new Foo(numer_2, denom_2); operands = new Foo2(frac1, frac2); operands.add().display(); operands.sub().display(); operands.mult().display(); operands.div().display();
what if i wanted to find the square root of a previously supplied integer which is now saved in a string.
[code]
import java.lang.Math;
public class Find_sqrt
{
public static void main(String s[]){
String s=”23″;
int i=Integer.parseInt(s);
int result=Math.sqrt(i);
System.out.println(“Square root of “+i+” : “+result);
}
}
[/code]
Hi
Change the result data type as double
double result=Math.sqrt(i);
Output:
4.795831523312719
website is awsom..plz show examples on GUI
please give some examples of codes using stack and its methods: poping and pushing…
Please show examples with GUI…ur site is the best so far for Java…
What if i want to add,subtract,multuiply,divide fractions?
plz provide a servlet in the blog.
plz provide a servlet in this blog
What if i want to add,subtract,multuiply,divide fractions?
1. Write a class Foo that wraps two Integer objects (representing a fraction) and has method(s) for displaying itself as a fraction.
2. Write a class Foo2 that wraps two Foo objects (the two fractions to be operated on).
3. Write a Foo2 method for each of the four arithmetic operations. Each method will compute a resulting Foo object from the operation and will return that object and/or store it internally for later access.
You should be able to do something like this after completing the above:
Integer numer_1, numer_2, denom_1, denom_2;
Foo frac1, frac2;
Foo2 operands;
// Add, subtract, multiply and divide 2/3 and 4/5 and display result.
numer_1 = new Integer(2);
denom_1 = new Integer(3);
numer_2 = new Integer(4);
denom_2 = new Integer(5);
frac1 = new Foo(numer_1, denom_1);
frac2 = new Foo(numer_2, denom_2);
operands = new Foo2(frac1, frac2);
operands.add().display();
operands.sub().display();
operands.mult().display();
operands.div().display();