Java

A basic Introduction for Android Development

Created by Saksham Agarwal / Piyush Bhopalka

Limitations of Procedural Programming

Introduction to Structures

Classes and Objects

Lets get started


A customary program

Hello World Program


public class MyFirstProgram {

  public static void main(String[] args) {
	System.out.println("Hello World!");
  }

}
					

Compiling Java Code

Type javac <filename.java> and press enter

Then type java <filename> and press enter



user:~$ javac MyFirstProgram.java
user:~$ java MyFirstProgram
          

Some Practice

1. Find the sum of even numbers between 1 to 100

2. Find the maximum in the array a[ ] = {4, 7, 5, 3, 1}

Declaring Class - Example 1


public class Vehicle {
  private int numberOfWheels;
  private String regNo;
  private int numberOfMilesTravelled;

  public Vehicle (int wheels, String reg, int miles) {
    numberOfWheels = wheels;
    regNo = reg;
    numberOfMilesTravelled = miles;
  }

  .....Remaining Codes.........

}
					

Add Selectors - Example 1


public class Vehicle {

  ....Variable declarations

  public Vehicle (int wheels, String reg, int miles) {}

  public String getRegNumber() {
    return regNo;
  }

  public int getNumberOfWheels() {
	   return numberOfWheels;
   }

  public int getMilesTravelled() {
    return numberOfMilesTravelled;
  }

}
					

Adding functions to a class - Example 1


public class Vehicle {

	....Variable declarations

	----Constructor function----

	----Selector functions-----

	public void move(int miles) {
		numberOfMilesTravelled += miles;
	}
}
					

Check your entire code - Example 1


The source-code of the Vehicle class is available here

PROJECT 1.1

Create a class Human with the following properties:


  • Each human has a name, contact number and age
  • Each human introduces himself as "My name is Sheldon Cooper. I am 25 years old and my contact is 11111."
  • Create appropriate constructors and selectors

Inheritance

Declaring a class - Example 2


import java.util.*;

public class Car extends Vehicle{
  int numberOfSeats;

  public Car(int wheels, String reg, int miles, int seats) {
    super(wheels, reg, miles);
    numberOfSeats = seats;
  }

}
					

Add Selector - Example 2


import java.util.*;

public class Car extends Vehicle{

  int numberOfSeats;

  public Car(int wheels, String reg, int miles, int seats) {}

  public int getNumberOfSeats() {
    return numberOfSeats;
  }

}
					

Add member functions - Example 2


import java.util.*;

public class Car extends Vehicle{

  .....Variable declaration

  ----Constructor function----

  ----Selector function----

  public void move(int miles) {
    super.move(miles);
    System.out.println("Car is moving, feel the breeze");
  }

}
					

Check your entire code - Example 2


The source-code of the Car class is available here

PROJECT 1.2

Extend the class Superhero as a subclass of Human.


  • Add a String field that stores the hero's alter ego!
  • Modify the given constructor by adding the alter ego as an additional parameter and setting it.
  • Override the introduce() method to ALSO include the alter ego! (In other words, you should call the superclass introduce method when you override the method.)

Abstract Classes

Implement Abstract Classes


public abstract class Vehicle {

	....Variable declarations

	----Constructor function----

	----Selector functions-----

  public abstract void move(int miles);

}
					

Implement Abstract Class (Continued)


import java.util.*;

public class Car extends Vehicle{

  .....Variable declaration

  ----Constructor function----

  ----Selector function----

  public void move(int miles) {
    //super.move(miles);      ->Remove this line
    System.out.println("Car is moving, feel the breeze");
  }

}
					

Check code for abstract class


The source-code of the Vehicle abstract class is available here

The new source-code of the Car class is available here

Interface

Implement Interface


interface Movable {
  public void move(int miles);
}
					

Implement Interface (Continued)


public class Vehicle implements Movable {

	....Variable declarations

	----Constructor function----

	----Selector functions-----

  public void move(int miles){
    numberOfMilesTravelled += miles;
  }

}
					

Implement Interface (Continued)


import java.util.*;

public class Car extends Vehicle{

  .....Variable declaration

  ----Constructor function----

  ----Selector function----

  public void move(int miles) {
    super.move(miles);      //<-Bring back this line
    System.out.println("Car is moving, feel the breeze");
  }

}
					

Check code for Interface


The source-code of Movable interface is available here

The source-code of the Vehicle class is available here

The new source-code of the Car class is available here

PROJECT 1.3

Give super powers to super humans


THE END

- That's it for Week 0
- Make sure Android Studio is properly installed before the next session
- The workshop will have a fully reimbursable fee of Rs. 100-200. Only under the completion of the workshop will the amount be returned.

Back to Home page