0% found this document useful (0 votes)
12 views2 pages

Java Lab Exercises on Math Class

This document contains three Java code examples that demonstrate the use of the Math class. The first example generates 10 random numbers between 1 and 6 using Math.random(). The second example calculates the square root and power of a number. The third example tests a method that returns the minimum of two integer arguments by calling the method in a loop with random numbers. The document also provides additional examples of equations to solve in Java and identifies unnecessary parentheses in arithmetic expressions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Java Lab Exercises on Math Class

This document contains three Java code examples that demonstrate the use of the Math class. The first example generates 10 random numbers between 1 and 6 using Math.random(). The second example calculates the square root and power of a number. The third example tests a method that returns the minimum of two integer arguments by calling the method in a loop with random numbers. The document also provides additional examples of equations to solve in Java and identifies unnecessary parentheses in arithmetic expressions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[] J A V A

LAB

JavaLab_6
%Lab6_1:

Math class

To demonstrate the use of Math class.

Write a Java program to generate 10 numbers between 1 and 6. // double x = [Link]() gives numbers from 0.0 to less than 1.0. import [Link] ; public class random { public static void main(String[] args) { int value; String out = " "; for(int i=1 ; i<= 10; i++) { value = 1 + (int)([Link]() * 6) ;

// from 1 to 6

out = out + value + " "; } [Link](null,out,"10 RANDOM values ", JOptionPane.PLAIN_MESSAGE); [Link](0); } }

%Lab6_2:
This program computes the square root and power of x import [Link]; class squareRoot{ public static void main (String args[]) { double y, x=16; y = [Link](x); [Link]("Square root of " + x + " is " + y); y = [Link](x, 2); [Link](x+ " power to 2 is " + y); } } // we can omitted this

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 1

[] J A V A

LAB

%Lab6_3:
This program tests a method named min ( ) that returns the minimum of its two integer arguments: import [Link]; public class TestMin{ public static void main(String[] args){ Random random = new Random(); for (int i = 0; i < 5; i++) { float x = [Link](); int m = [Link]( 100 * x); x = [Link](); int n = [Link](100*x); int y = min(m, n); [Link]("min(" + m + , + n + " ) = + y); } } static int min(int x, int y) if (x < y) return x; else return y; } } {

Try this:
Write a program to solve the following equation(user must enters the values of variable): Y = 2x3 + 4 z Write Java assignment statements to evaluate the following equations:

Area = r2 + 2 rh Side = Obj = x 3 * y2 / 2*z1/3


Identify unnecessary parentheses in the following arithmetic expressions: (x-(y/5) + z) +25, (ii) ((x-y)*p) +q (iii) (m*n)+(-x/y)

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 2

You might also like