Java Labs 6
Java Labs 6
LAB
JavaLab_6
%Lab6_1:
Math class
Write a Java program to generate 10 numbers between 1 and 6. // double x = Math.random() gives numbers from 0.0 to less than 1.0. import javax.swing.JOptionPane ; public class random { public static void main(String[] args) { int value; String out = " "; for(int i=1 ; i<= 10; i++) { value = 1 + (int)(Math.random() * 6) ;
// from 1 to 6
out = out + value + " "; } JOptionPane.showMessageDialog(null,out,"10 RANDOM values ", JOptionPane.PLAIN_MESSAGE); System.exit(0); } }
%Lab6_2:
This program computes the square root and power of x import java.lang.Math; class squareRoot{ public static void main (String args[]) { double y, x=16; y = Math.sqrt(x); System.out.println("Square root of " + x + " is " + y); y = Math.pow(x, 2); System.out.println(x+ " power to 2 is " + y); } } // we can omitted this
[] J A V A
LAB
%Lab6_3:
This program tests a method named min ( ) that returns the minimum of its two integer arguments: import java.util.Random; public class TestMin{ public static void main(String[] args){ Random random = new Random(); for (int i = 0; i < 5; i++) { float x = random.nextFloat(); int m = Math.round( 100 * x); x = random.nextFloat(); int n = Math.round(100*x); int y = min(m, n); System.out.println("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: