Java Math class

provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.

Unlike some of the StrictMath class numeric methods, all implementations of the equivalent function of Math class can't define to return the bit-for-bit same results. This relaxation permits implementation with better-performance where strict reproducibility is not required.

If the size is int or long and the results overflow the range of value, the methods addExact(), subtractExact(), multiplyExact(), and toIntExact() throw an ArithmeticException.

For other arithmetic operations like increment, decrement, divide, absolute value, and negation overflow occur only with a specific minimum or maximum value. It should be checked against the maximum and minimum value as appropriate.

public class JavaMathExample1    
{    
    public static void main(String[] args)     
    {    
        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 the logarithm of given value       
        System.out.println("Logarithm of x is: " + Math.log(x));   
        System.out.println("Logarithm of y is: " + Math.log(y));  
          
        // return the logarithm of given value when base is 10      
        System.out.println("log10 of x is: " + Math.log10(x));   
        System.out.println("log10 of y is: " + Math.log10(y));    
          
        // return the log of x + 1  
        System.out.println("log1p of x is: " +Math.log1p(x));    
  
        // return a power of 2    
        System.out.println("exp of a is: " +Math.exp(x));    
    }
}

JavaMathExample1.main(null)
Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12

OOP notes

As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming, they use objects as a primary source to implement what is to happen in the code. Objects are seen by the viewer or user, performing tasks assigned by you. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

Let us discuss prerequisites by polishing concepts of method declaration and message passing. Starting off with the method declaration, it consists of six components:

Access Modifier: Defines the access type of the method i.e. from where it can be accessed in your application. In Java, there are 4 types of access specifiers: public: Accessible in all classes in your application. protected: Accessible within the package in which it is defined and in its subclass(es) (including subclasses declared outside the package). private: Accessible only within the class in which it is defined. default (declared/defined without using any modifier): Accessible within the same class and package within which its class is defined. The return type: The data type of the value returned by the method or void if it does not return a value. Method Name: The rules for field names apply to method names as well, but the convention is a little different. Parameter list: Comma-separated list of the input parameters that are defined, preceded by their data type, within the enclosed parentheses. If there are no parameters, you must use empty parentheses (). Exception list: The exceptions you expect the method to throw. You can specify these exception(s). Method body: It is the block of code, enclosed between braces, that you need to execute to perform your intended operations.

Quizlet

Group Goblin Homework

/*    import java.util.Random;
public class Goblin {
    private String name;
    private int HP;
    private int DMG;
    private double hitChance;

    public String getName() {
        return name;
    }

    public int getHP() {
        return HP;
    }

    public int getDMG() {
        return DMG;
    }

    public double getHitChance() {
        return hitChance;
    }

    public boolean isAlive() {
        if (this.HP > 0) {
            return true;
        } else {
            return false;
        }
    }

    public void setName(String newName) {
        this.name = newName;
    }

    public void setHP(int newHP) {
        this.HP = newHP;
    }

    public void takeDMG(int takenDamage) {
        this.HP -= takenDamage;
    }

    public void setDMG(int newDMG) {
        this.DMG = newDMG;
    }

    public void setHitChance(double newHitChance) {
        this.hitChance = newHitChance;
    }
}
import java.util.Random;
public class Duel {

    public static void fight(Goblin goblin1, Goblin goblin2, Double rand) {
        while (goblin1.isAlive() && goblin2.isAlive()) {
            for(int i = 0; i < 5; i++) {
                if (rand > goblin1.getHitChance()) {
                    System.out.println("Missed");
            }   else {
                    System.out.println("Hit");
                    break;
            }}
            goblin1.takeDMG(goblin2.getDMG());
            System.out.println(goblin1.getName() + " takes " + goblin2.getDMG() + " damage");
            System.out.println(goblin1.getName() + " HP: " + goblin1.getHP());

            if (!goblin1.isAlive()) {
                System.out.println(goblin1.getName() + " has perished");
                break;
            }

            goblin2.takeDMG(goblin1.getDMG());
            System.out.println(goblin2.getName() + " takes " + goblin1.getDMG() + " damage");
            System.out.println(goblin2.getName() + " HP: " + goblin2.getHP());

            if (!goblin2.isAlive()) {
                System.out.println(goblin2.getName() + " has perished");
                break;
            }
        }
    }

    public static void main(String[] args) {
        Goblin goblin1 = new Goblin();
        goblin1.setName("jeffrey");
        goblin1.setHP(12);
        goblin1.setDMG(2);
        goblin1.setHitChance(2);

        Goblin goblin2 = new Goblin();
        goblin2.setName("Gunther the great");
        goblin2.setHP(4);
        goblin2.setDMG(1);
        goblin2.setHitChance(1);
        Random x = new Random();
        double rand = x.nextDouble();
        fight(goblin1, goblin2, rand);
    }
}

Duel.main(null);
gob.txt
3 KB