Wednesday, June 27, 2018

12 to 2 batch : 27 - JUNE - 2018

==========================================
import java.util.Random;

public class DemoOfRandom01
{
public static void main(String[] args)
{
// create object of random class
Random r = new Random();

// get a random number in the range of -2 ^ 31
// 2^31 - 1 and store it
int x = r.nextInt();

System.out.println(x);

// get a random number in the user defined range
int y = r.nextInt(-1);

System.out.println(y);


}
}

==========================================
import java.util.Random;

public class DemoOfRandom02
{
public static void main(String[] args)
{
Random r = new Random();

System.out.println("start");

for(int i = 1; i <= 25; i++)
{
// get a random number from 0 to 500
int a = r.nextInt(501);

// get a random number from 0 to 50
int b = r.nextInt(51);

int c = a / b;

System.out.println(i+":");
}

System.out.println("end");

}
}
==========================================
import java.util.Scanner;

public class Rectangle
{
// non static variables
float length, width, area;
String createdBy;

public static void main(String[] args)
{
// for int/float/single word
Scanner sc1 = new Scanner(System.in);

// for string with spaces
Scanner sc2 = new Scanner(System.in);

// create two recangles
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();

System.out.println("# provide data for first recangle #");
System.out.print("Input length: ");
rect1.length = sc1.nextFloat(); // store data inside length

System.out.print("Input width: ");
rect1.width = sc1.nextFloat(); // store data inside width

System.out.print("Input created by: ");
rect1.createdBy = sc2.nextLine(); // store data inside createdBy

// calculate area of rect1
rect1.area = rect1.length * rect1.width;

System.out.println("# provide data for second recangle #");
System.out.print("Input length: ");
rect2.length = sc1.nextFloat(); // store data inside length

System.out.print("Input width: ");
rect2.width = sc1.nextFloat(); // store data inside width

System.out.print("Input created by: ");
rect2.createdBy = sc2.nextLine(); // store data inside createdBy

// calculate area of rect1
rect2.area = rect2.length * rect2.width;

// compare the area of two rectangles
if(rect1.area > rect2.area)
{
// show data of rect1
System.out.println("rect1 is bigger =>");
System.out.println(rect1.length);
System.out.println(rect1.width);
System.out.println(rect1.area);
System.out.println(rect1.createdBy);
}
else
{
// show data of rect2
System.out.println("rect2 is bigger =>");
System.out.println(rect2.length);
System.out.println(rect2.width);
System.out.println(rect2.area);
System.out.println(rect2.createdBy);
}
}
}
==========================================
import java.util.Scanner;

public class Shoe
{
String brand;
int cost;

// define a non static method to store data of shoe
void inputShoeInfo()
{
Scanner sc = new Scanner(System.in);

brand = sc.next();

cost = sc.nextInt();
}

// define a method to show data of shoe
void displayShoeInfo()
{
System.out.println(brand+":"+cost);
}

public static void main(String[] args)
{
// create a shoe
Shoe s1 = new Shoe();

// call method to input data inside s1
s1.inputShoeInfo();

// call method to show data of s1
s1.displayShoeInfo();
}

}
==========================================
import java.util.Scanner;

public class TheAssignment02
{
int a; // non static variable
static int b; // static variable
int c; // non static variable

public static void main(String[] args)
{
// create object of scanner to get input from keyboard
Scanner sc = new Scanner(System.in);

// create object of class to access non static variables
TheAssignment02 ref = new TheAssignment02();

System.out.print("Input value for a: ");
// store data inside non static variable
ref.a = sc.nextInt();

System.out.print("Input value for b: ");
// store data inside static variable
TheAssignment02.b = sc.nextInt();

// store result inside non static variable
ref.c = ref.a + TheAssignment02.b;

System.out.println(ref.c);
}
}

==========================================
note: add two numbers without using + and - operator

public class TheAssignment01
{
public static void main(String[] args)
{
int a = 1;
int b = 2;

while(b != 0)
{
int c = a & b; // take AND of a and b

a = a ^ b; // take XOR of a and b, variable 'a' will store result

b = c << 1; // take left shift of c by 1
}

                // show the result
System.out.println(a);
}
}
==========================================


No comments:

Post a Comment