Created by Saksham Agarwal / Piyush Bhopalka
A customary program
public class MyFirstProgram {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Type javac <filename.java> and press enter
Then type java <filename> and press enter
user:~$ javac MyFirstProgram.java
user:~$ java MyFirstProgram
1. Find the sum of even numbers between 1 to 100
2. Find the maximum in the array a[ ] = {4, 7, 5, 3, 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.........
}
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;
}
}
public class Vehicle {
....Variable declarations
----Constructor function----
----Selector functions-----
public void move(int miles) {
numberOfMilesTravelled += miles;
}
}
The source-code of the Vehicle class is available here
Create a class Human with the following properties:
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;
}
}
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;
}
}
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");
}
}
The source-code of the Car class is available here
Extend the class Superhero as a subclass of Human.
public abstract class Vehicle {
....Variable declarations
----Constructor function----
----Selector functions-----
public abstract void move(int miles);
}
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");
}
}
The source-code of the Vehicle abstract class is available here
The new source-code of the Car class is available here
interface Movable {
public void move(int miles);
}
public class Vehicle implements Movable {
....Variable declarations
----Constructor function----
----Selector functions-----
public void move(int miles){
numberOfMilesTravelled += miles;
}
}
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");
}
}
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
Give super powers to super humans
- 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.