Sie sind auf Seite 1von 7

https://www.javatpoint.

com/java-tutorial

import java.io.*;

class Rectangle

  int length;  

 int width;  

 void insert(int l, int w)

{  

  length=l;  

  width=w;  

 }  

 void calculateArea()

System.out.println(length*width);

}  

}  

class TestRectangle1

{  

 public static void main(String args[])

  Rectangle r1=new Rectangle();  

  Rectangle r2=new Rectangle();  

  r1.insert(11,5);  

  r2.insert(3,15);  

r1.calculateArea();  

  r2.calculateArea();  

}  
}  

import java.io.*;

class Student

{  

 int rollno;  

 String name; 

  void insertRecord(int r, String n)

{  

  rollno=r;  

  name=n;  

 }  

 void displayInformation()

System.out.println(rollno+" "+name);

}  

}  

class TestStudent4

{  

 public static void main(String args[]){  

  Student s1=new Student();  

  Student s2=new Student();  

  s1.insertRecord(111,"Karan");  

  s2.insertRecord(222,"Aryan");  

  s1.displayInformation();  

  s2.displayInformation();   } }  
double x = 28;

double y = 4;

// return the maximum of two numbers

System.out.println("Maximum number of x and y is: " +Math.max(x, y));

// return the square root of y

System.out.println("Square root of y is: " + Math.sqrt(y));

//returns 28 power of 4 i.e. 28*28*28*28

System.out.println("Power of x and y is: " + Math.pow(x, y));

// return a power of 2

System.out.println("exp of a is: " +Math.exp(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.         System.out.println(Math.round(x));  
8.     }  
9. }  

Abs

int x = 78;

int y = -48;

//print the absolute value of int type

System.out.println(Math.abs(x));

System.out.println(Math.abs(y));

Random

1. public static void main(String[] args)   
2.     {  
3.         // generate random number  
4.         double a = Math.random();  
5.         double b = Math.random();  
6.         // Output is different every time this code is executed    
7.         System.out.println(a);  
8.         System.out.println(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.         System.out.println(Math.addExact(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.         System.out.println(Math.multiplyExact(a, b));  
9.     }  
10. }  

1. public class IncrementExactExample1  
2. {  
3.     public static void main(String[] args)   
4.     {  
5.         int a = 674;  
6.         System.out.println(Math.incrementExact(a));  
7.     }  
8. }  

1. public class DecrementExactExample1  
2. {  
3.     public static void main(String[] args)   
4.     {  
5.         int a = 830;  
6.         System.out.println(Math.decrementExact(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. System.out.print(c[i][j]+" ");    
15. }    
16. System.out.println();//new line    
17. }    
18. }}  

Sort array

1. import java.util.Arrays;   
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. Arrays.sort(array);   
10. System.out.println("Elements of array sorted in ascending order: ");  
11. //prints array using the for loop  
12. for (int i = 0; i < array.length; i++)   
13. {       
14. System.out.println(array[i]);   
15. }   
16. }  
17. }  

Das könnte Ihnen auch gefallen