Java workshop #2

Language basics

Recap

  • Java is case sensitive ( a vs A)
  • A class is saved in a .java file
  • The compiled class/code is stored in a .class file
  • The name of the class should be the same as the name of the file
  • In order to start a java program we need to have a class with a main() method

Recap (2)

Variables 

There are 2 types of variables:

  • Primitive (e.g.: numbers)
  • Object reference (e.g.: a "Dog" object)

Variables (2)

  • Variables must have a name 
  • Variables must have a type

Primitive types

  • They come in different sizes

Primitive types(2)

Boolean & char

Type Bit depth Value Range
boolean JVM Specific true/false
char 16 bit 0 to 65535

Primitive types(3)

Integer

Type Bit depth Value Range
byte 8 bits -128 to 127
short 16 bit 32768 to 32767
int 32 bits -2147483648 to 2147483647 
long 64 bits

Primitive types(4)

Floating point

Type Bit depth
float 32 bits
double 64 bit

Primitive types(5)

Creating a new project

  • From the IntelliJ menu choose: File > New Project

Choose project type: Java

Select project template

Set project name: Types

Define an integer variable

 

  • Add the following code in the main() method and run it
public static void main(String[] args){
            int i = 25;
	    System.out.println(i);
}

Compatibility

  • Add a new line to your program
public static void main(String[] args){
            int i = 25;
            byte b = i;
	    System.out.println(i);
}
  • What happened?

Let's tell Java we want a conversion

public static void main(String[] args) {
	int i = 25;
        byte b = (byte) i;

        System.out.println(b);
    }
  • Change the value of i to 128 or an integer bigger than 128. What happens?

Remember?

This means:

int i = 128;
byte b = (byte)i;

Let's put all primitive types in our project

public static void main(String[] args) {
        boolean result = true;
        char capitalC = 'C';
        byte b = 100;
        short s = 10000;
        int i = 100000;
        long l = 1234L;
        double d = 123.4;
        float f  = 123.4f;

        System.out.println (result);
        System.out.println(f);
        System.out.println(l);
        System.out.println(capitalC);
    }

We can even do calculations

 public static void main(String[] args) {
        int i = 23;
        int j = 5;

        int sum = i + j;

        int difference = i - j;

        int quotient = i / j;

        int remainder = i % j ;

        int product = i * j;

        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Quotient: " + quotient);
        System.out.println("Remainder: " + remainder);
        System.out.println("Product: " + product);
}

More calculations

 public static void main(String[] args) {
        double i = 21.7;
        int j = 5;

        double sum = i + j;

        double difference = i - j;

        double quotient = i / j;

        double product = i * j;

        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Quotient: " + quotient);
        System.out.println("Product: " + product);
}

If you want to practice more

Data types (primitives):
https://www.hackerrank.com/challenges/java-datatypes
https://www.hackerrank.com/challenges/30-data-types

(day 1)

Note: You need to register for 3- days of code and another challenge unlocks every day (for 30 days).
 

Useful to go with the primitives:
https://www.hackerrank.com/challenges/30-operators (day 2)

 

A bit more challenging:
https://www.hackerrank.com/challenges/java-int-to-string

Control Structures

  • They are used to tell the program to do something:
    • if - else (do something if a specific condition applies)
    • for / while (do something again and again)

if 

Let's create a new project (again)

  • Name the project "Greeting"
  • And add the following code in the main method
 public static void main(String[] args) {
        int hourOfTheDay = 10;

        if (hourOfTheDay < 11) {
            System.out.println("Good morning!");
        }
    }

if - else

Adding another greeting

public static void main(String[] args) {
        int hourOfTheDay = 17;

        if (hourOfTheDay < 11) {
            System.out.println("Good morning!");
        } else {
            System.out.println("Good afternoon!");
        }
    }

And another one

public static void main(String [] args){    
        int hourOfTheDay = 17;

        if (hourOfTheDay < 11) {
            System.out.println("Good morning!");
        } else if (hourOfTheDay < 15) {
            System.out.println("Good afternoon!");
        } else {
            System.out.println("Good evening!");
        }
}

for

for

public static void main(String [] args){    
         for (int x = 0; x < 10; x = x + 1) {
            System.out.println("x is now " + x);
        }
}

while

while

  public static void main(String[] args) {
        int x = 15;

        while (x > 12){
            System.out.println(x);
            x = x - 1;
        }

        System.out.println("Last value: " + x);
    }
  • After running the code, change the value of x to 11 and see what happens

do - while

do - while

public static void main(String[] args) {
        int x = 15;
    
        do {
            System.out.println(x);
            x = x - 1;
        } while (x > 12);

        System.out.println("Last value: " + x);
    }
  • Change the value of x to 12 and observe what happens

Let's combine

public static void main(String[] args) {
        int j = 5;

        for (int i = 0; i < 10; i++){
            if (i == j) {
                System.out.println("I found it!");
            } else {
                System.out.println(i);
            }
        }

    }
  • Check the output: when does the loop stop?

Let's stop the loop

public static void main(String[] args) {
        int j = 5;

        for (int i = 0; i < 10; i++){
            if (i == j) {
                System.out.println("I found it!");
                break;
            } else {
                System.out.println(i);
            }
        }

    }

If you want to practice more

Classes and objects

  • Classes are the building blocks of a java program
  • Classes contain the definition of what a program should do
  • An object is an instance of a class in the program
  • You can have multiple objects of the same class

Classes and objects(2)

Classes and objects(3)

Classes and objects (4)

A class contains

  • Class Name
  • Fields/variables - used for storing data 
  • Methods (functions/procedures) - used for manipulating data
  • The fields and methods of a class are called class members

Let's create a new project

  • Set "Dogs" as the project name

Create a new class

  • Right click on src.com.base
  • Select New > Java class 
  • Set the class name: Dog

Let's define the "Dog" class

public class Dog {

    //field
    String name;

    //methods
    public void setName(String newName){
        name = newName;
    }

    public String getName(){
        return name;
    }

    public void bark(){
        System.out.println(name + ": wuff! wuff!");
    }

}

Let's create a "Dog" object

 public static void main(String[] args) {
	    Dog dog = new Dog();
	    dog.setName("Benny");

	    System.out.println("The dog's name is: " + dog.getName());
	    dog.bark();
    }

Add the following code in the main method:

Constructors

Dog dog = new Dog();
	   
  • Constructors are used to tell the program to create a new object of a specific class
  • A constructor is a special method that belongs to the class 
  • The purpose of a constructor is to initialize fields

 

Java Workshop #2

By andr33a

Java Workshop #2

Java language basics: objects & classes, data types, control statements

  • 898