|
| 1 | +public class NumberMethods { |
| 2 | + public static void main(String[] args) { |
| 3 | + /* Number Methods */ |
| 4 | + /*1.xxxValue() |
| 5 | + converts the value of this number object to |
| 6 | + the xxx datatype and returns it.*/ |
| 7 | + System.out.println("xxxValue() Method:"); |
| 8 | + Integer x = 5; |
| 9 | + System.out.println(x.byteValue()); |
| 10 | + System.out.println(x.doubleValue()); |
| 11 | + |
| 12 | + /*2.compareTo() |
| 13 | + compares the Number object to the argument |
| 14 | + if the arguments is equal, returns 0; if less then returns -1 |
| 15 | + if greater then returns 1. |
| 16 | + */ |
| 17 | + System.out.println("compareTo() Method:"); |
| 18 | + Integer t = 10; |
| 19 | + System.out.println(t.compareTo(5)); |
| 20 | + Float fl = 3.14f; |
| 21 | + System.out.println(fl.compareTo(3.14f)); |
| 22 | + |
| 23 | + /*3.equals() boolean method |
| 24 | + check the number object is equal or not to the argument |
| 25 | + */ |
| 26 | + System.out.println("equals() Method:"); |
| 27 | + Integer a = 10; |
| 28 | + Integer Y = 20; |
| 29 | + Short y = 20; |
| 30 | + System.out.println(a.equals(Y)); |
| 31 | + System.out.println(y.equals(Y)); |
| 32 | + /*4.valueOf() static method it also takes string argument and radix |
| 33 | + returns the Integer Objects holding the value of specified primitive |
| 34 | + */ |
| 35 | + System.out.println("valueOf() Method:"); |
| 36 | + int A = 16;//Hexadecimal base |
| 37 | + Integer X = Integer.valueOf(5); |
| 38 | + Integer b = Integer.valueOf("444",A); |
| 39 | + Double c = Double.valueOf(5); |
| 40 | + System.out.println(X); |
| 41 | + System.out.println(b); |
| 42 | + System.out.println(c); |
| 43 | + |
| 44 | + /*5.toString() |
| 45 | + returns a String Object representing the value of a specified int or Integer |
| 46 | + */ |
| 47 | + System.out.println("toString() Method:"); |
| 48 | + Integer d = 10; |
| 49 | + System.out.println(d.toString()); |
| 50 | + System.out.println(Integer.toString(400,8)); |
| 51 | + /*6.parseInt() is also used for CommandLine Input |
| 52 | + this method is used to get the primitive data type of a certain String |
| 53 | + */ |
| 54 | + System.out.println("parseInt() Method:"); |
| 55 | + int xx = Integer.parseInt("10"); |
| 56 | + double cc = Double.parseDouble("5"); |
| 57 | + int bb = Integer.parseInt("222",8); |
| 58 | + System.out.println(xx); |
| 59 | + System.out.println(cc); |
| 60 | + System.out.println(bb); |
| 61 | + |
| 62 | + /*7.abs() |
| 63 | + returns the absolute value of the argument |
| 64 | + */ |
| 65 | + System.out.println("abs() Method:"); |
| 66 | + Integer ff = -8; |
| 67 | + double h = -100; |
| 68 | + float k = -90; |
| 69 | + System.out.println(Math.abs(ff)); |
| 70 | + System.out.println(Math.abs(h)); |
| 71 | + System.out.println(Math.abs(k)); |
| 72 | + |
| 73 | + /*8.ceil() |
| 74 | + returns the smallest integer that is greater then or equal to the |
| 75 | + argument,returned in double. |
| 76 | + */ |
| 77 | + System.out.println("ceil() Method:"); |
| 78 | + double dd = -100.675; |
| 79 | + float fh = -90; |
| 80 | + System.out.println(Math.ceil(dd)); |
| 81 | + System.out.println(Math.ceil(fh)); |
| 82 | + |
| 83 | + /*9.floor() |
| 84 | + returns the largest integer that is less then or equal to the argument, |
| 85 | + returned as double.*/ |
| 86 | + System.out.println("floor() Method:"); |
| 87 | + double du = -100.675; |
| 88 | + float fll = -90; |
| 89 | + |
| 90 | + System.out.println(Math.floor(du)); |
| 91 | + System.out.println(Math.floor(fll)); |
| 92 | + |
| 93 | + /*10.rint() |
| 94 | + returns the integer that is closest in value to the argument , |
| 95 | + returned as the double. |
| 96 | + */ |
| 97 | + System.out.println("rint() Method:"); |
| 98 | + double o = 100.675; |
| 99 | + double op = 100.500; |
| 100 | + System.out.println(o); |
| 101 | + System.out.println(op); |
| 102 | + |
| 103 | + /*11.round() |
| 104 | + returns the closest long or int ,as indicated by the methods return type |
| 105 | + to the argument.*/ |
| 106 | + double n = 100.675; |
| 107 | + double e = 1000.576; |
| 108 | + float fx = 99.1f; |
| 109 | + |
| 110 | + System.out.println(n); |
| 111 | + System.out.println(e); |
| 112 | + System.out.println(fx); |
| 113 | + /*12.min() |
| 114 | + returns the smaller of the two argument.*/ |
| 115 | + System.out.println("min() Method:"); |
| 116 | + System.out.println(Math.min(5,6)); |
| 117 | + System.out.println(Math.min(3.14,2.24)); |
| 118 | + |
| 119 | + /*13.max() |
| 120 | + returns the larger of the two arguments.*/ |
| 121 | + System.out.println("max() Method:"); |
| 122 | + System.out.println(Math.max(9,5)); |
| 123 | + System.out.println(Math.max(3.12,1.234)); |
| 124 | + |
| 125 | + /*14.exp() |
| 126 | + returns the base of the natural logarithms, e, to the power of the argument |
| 127 | + */ |
| 128 | + System.out.println("exp() method:"); |
| 129 | + double fj = 11.0464; |
| 130 | + double gg = 2.76; |
| 131 | + System.out.printf("The Value of e is %.4f%n",Math.E); |
| 132 | + System.out.println(Math.exp(x)); |
| 133 | + /*15.log() |
| 134 | + returns the natural logarithms of the argument |
| 135 | + */ |
| 136 | + System.out.println("log() Method:"); |
| 137 | + double x1 = 11.635; |
| 138 | + double y1 = 2.76; |
| 139 | + System.out.printf("The value of e is %.4f%n", Math.E); |
| 140 | + System.out.println(Math.log(x1)); |
| 141 | + /*16.pow() |
| 142 | + returns the value of the first argument raised to the power of the second |
| 143 | + argument.*/ |
| 144 | + System.out.println("pow() Method:"); |
| 145 | + double xr = 11.635; |
| 146 | + double yr = 2.76; |
| 147 | + System.out.printf("The value of e is %.4f%n", Math.E); |
| 148 | + System.out.println(Math.pow(xr, yr)); |
| 149 | + /*17.sqrt() |
| 150 | + returns the square root of the argument*/ |
| 151 | + System.out.println("sqrt() Method:"); |
| 152 | + System.out.printf("The value of e is %.4f%n", Math.E); |
| 153 | + System.out.println(Math.sqrt(x)); |
| 154 | + /*18.sin() |
| 155 | + returns the sine of the specified double value*/ |
| 156 | + System.out.println("sin() Method:"); |
| 157 | + double degrees = 45.0; |
| 158 | + double radians = Math.toRadians(degrees); |
| 159 | + System.out.format("The value of pi is %.4f%n", Math.PI); |
| 160 | + System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians)); |
| 161 | + /*19.cos() |
| 162 | + returns the cosine of the double value*/ |
| 163 | + System.out.println("cos() Method:"); |
| 164 | + System.out.format("The value of pi is %.4f%n", Math.PI); |
| 165 | + System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians)); |
| 166 | + /*20.tan() |
| 167 | + returns the tangent of the double value*/ |
| 168 | + System.out.println("tan() Method:"); |
| 169 | + System.out.format("The value of pi is %.4f%n", Math.PI); |
| 170 | + System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians)); |
| 171 | + /*21.asin() |
| 172 | + returns the arcsine of the specified double value*/ |
| 173 | + System.out.println("asin() Method:"); |
| 174 | + System.out.format("The value of pi is %.4f%n", Math.PI); |
| 175 | + System.out.format("The arcsine of %.4f is %.4f degrees %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians)))); |
| 176 | + /*22.acos() |
| 177 | + returns the arccosine of the double value |
| 178 | + 23.atan() |
| 179 | + returns the arctangent of the double value. similarly you may use this*/ |
| 180 | + |
| 181 | + /*24.atan2() |
| 182 | + converts rectangular coordinates (x,y) to polar coordinate (r,theta) |
| 183 | + and returns theta.*/ |
| 184 | + System.out.println("atan2() Method:"); |
| 185 | + double xz = 45.0; |
| 186 | + double yz = 30.0; |
| 187 | + System.out.println( Math.atan2(xz, yz) ); |
| 188 | + |
| 189 | + /*25.toDegrees() |
| 190 | + converts the argument to degrees*/ |
| 191 | + System.out.println("toDegrees() method:"); |
| 192 | + System.out.println( Math.toDegrees(xz) ); |
| 193 | + System.out.println( Math.toDegrees(yz) ); |
| 194 | + |
| 195 | + /*26.toRadians() |
| 196 | + converts the arguments to radians*/ |
| 197 | + System.out.println("toRadians() Method:"); |
| 198 | + System.out.println( Math.toRadians(xz) ); |
| 199 | + System.out.println( Math.toRadians(yz) ); |
| 200 | + /*27.random() |
| 201 | + returns a random number. |
| 202 | + */ |
| 203 | + System.out.println("random() Method:"); |
| 204 | + System.out.println( Math.random() ); |
| 205 | + System.out.println( Math.random() ); |
| 206 | + } |
| 207 | +} |
0 commit comments