[Link]
com/java-tutorial
import [Link].*;
class Rectangle
int length;
int width;
void insert(int l, int w)
{
length=l;
width=w;
}
void calculateArea()
[Link](length*width);
}
}
class TestRectangle1
{
public static void main(String args[])
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
[Link](11,5);
[Link](3,15);
[Link]();
[Link]();
}
}
import [Link].*;
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
[Link](rollno+" "+name);
}
}
class TestStudent4
{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Karan");
[Link](222,"Aryan");
[Link]();
[Link](); } }
double x = 28;
double y = 4;
// return the maximum of two numbers
[Link]("Maximum number of x and y is: " +[Link](x, y));
// return the square root of y
[Link]("Square root of y is: " + [Link](y));
//returns 28 power of 4 i.e. 28*28*28*28
[Link]("Power of x and y is: " + [Link](x, y));
// return a power of 2
[Link]("exp of a is: " +[Link](x));
}
Round
1. public class RoundExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 79.52;
6. // find the closest int for the double
7. [Link]([Link](x));
8. }
9. }
Abs
int x = 78;
int y = -48;
//print the absolute value of int type
[Link]([Link](x));
[Link]([Link](y));
Random
1. public static void main(String[] args)
2. {
3. // generate random number
4. double a = [Link]();
5. double b = [Link]();
6. // Output is different every time this code is executed
7. [Link](a);
8. [Link](b);
9. }
10. }
1. public class AddExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 469;
6. int b = 737;
7. // Input two positive value, Output addition of a and b
8. [Link]([Link](a, b));
9. }
10. }
1. public class MultiplyExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 739;
6. int b = 5;
7. // Input two values, Output multiplication of a and b
8. [Link]([Link](a, b));
9. }
10. }
1. public class IncrementExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 674;
6. [Link]([Link](a));
7. }
8. }
1. public class DecrementExactExample1
2. {
3. public static void main(String[] args)
4. {
5. int a = 830;
6. [Link]([Link](a));
7. }
8. }
Matrix addition
1. public class MatrixAdditionExample{
2. public static void main(String args[]){
3. //creating two matrices
4. int a[][]={{1,3,4},{2,4,3},{3,4,5}};
5. int b[][]={{1,3,4},{2,4,3},{1,2,4}};
6.
7. //creating another matrix to store the sum of two matrices
8. int c[][]=new int[3][3]; //3 rows and 3 columns
9.
10. //adding and printing addition of 2 matrices
11. for(int i=0;i<3;i++){
12. for(int j=0;j<3;j++){
13. c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
14. [Link](c[i][j]+" ");
15. }
16. [Link]();//new line
17. }
18. }}
Sort array
1. import [Link];
2. public class SortArrayExample1
3. {
4. public static void main(String[] args)
5. {
6. //defining an array of integer type
7. int [] array = new int [] {90, 23, 5, 109, 12, 22, 67, 34};
8. //invoking sort() method of the Arrays class
9. [Link](array);
10. [Link]("Elements of array sorted in ascending order: ");
11. //prints array using the for loop
12. for (int i = 0; i < [Link]; i++)
13. {
14. [Link](array[i]);
15. }
16. }
17. }