Posts

Java Basics #4

  Java  Math The Java Math class has many methods that allows you to perform mathematical tasks on numbers. Math.max( x,y ) The  Math.max( x , y )  method can be used to find the highest value of  x  and  y. Example: Math.max(5, 10); Math.min( x,y ) The  Math.min( x , y )  method can be used to find the lowest value of  x  and  y. Example: Math.min(5, 10); Math.sqrt( x ) The  Math.sqrt( x )  method returns the square root of  x. Example: Math.sqrt(64); Math.abs( x ) The  Math.abs( x )  method returns the absolute (positive) value of  x. Example: Math.abs(-4.7); Random Numbers Math.random()  returns a random number between 0.0 (inclusive), and 1.0 (exclusive). Example: Math.random(); To get more control over the random number, e.g. you only want a random number between 0 and 100, you can use the following formula. Example: int randomNum = (int)(Math.random() * 101);  // 0 to 100 More Math ...

Java Basics #3

  Java Constructors A constructor in Java is a  special method  that is used to initialise objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. // Create a Main class public class Main {   int x;  // Create a class attribute   // Create a class constructor for the Main class   public Main() {     x = 5;  // Set the initial value for the class attribute x   }   public static void main(String[] args) {     Main myObj = new Main(); // Create an object of class Main (This will call the constructor)     System.out.println(myObj.x); // Print the value of x   } } // Outputs 5 Note that the constructor name must  match the class name , and it cannot have a  return type  (like  void ). Also note that the constructor is called when the object is created. All classes have constructors by default: if you do not creat...

Java Basics #2

Image
  Java Operators Operators are used to perform operations on variables and values. Example: int x = 100 + 50; Although the  +  operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable. Example: int sum1 = 100 + 50;        // 150 (100 + 50) int sum2 = sum1 + 250;      // 400 (150 + 250) int sum3 = sum2 + sum2;     // 800 (400 + 400) Java divides the operators into the following groups: Arithmetic operators Assignment operators Comparison operators Logical operators Bitwise operators Java Assignment Operators Assignment operators are used to assign values to variables. Example: Java Strings Strings are used for storing text. A  String  variable contains a collection of characters surrounded by double quotes. Example: String greeting = "Hello"; String Length A String in Java is actually an object, whic...

Java Basics #1

Simple Java program: public class Main {   public static void main(String[] args) {     System.out.println("Hello World");   } } Every line of code that runs in Java must be inside a  class . In our example, we named the class  Main . A class should always start with an uppercase first letter. The main Method The  main()  method is required and you will see it in every Java program. Any code inside the  main()  method will be executed. You don't have to understand the keywords before and after main. System.out.println() Inside the  main()  method, we can use the  println()  method to print a line of text to the screen. Java Comments Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Single-line comments start with two forward slashes ( // ). Java Variables Variables are containers for storing data values. In Java, there are d...