Java Basics -1
~30 mins
Week1 - Day 1
package week1.day1;
public class Airplane {
// Actions -> applyBrake , takeLeft, turnHeadLight, turnAC !!
/*
* 4 Pillars
*
* 1) Access Modifier - public, private, protected, default/package
* 2) Return Type (output) - primitive data type / Class / void -> not to return anything
* 3) Method Name - Name of the method (meaningful name -> verbNoun)
* 4) Input Argument -> Input to your method (0/1/...) -> Primitive/class
*
* last one -> logic !!
*
*/
public void applyBrake(String brakeType) {
System.out.println("The brake with type "+brakeType+" applied");
}
public boolean takeLeft() {
return true;
}
public String getFlightName() {
return "Boeing";
}
/*Mobile class
write 3 methods
one method with void as return type -> syso
method 2 -> should return boolean
method 3 -> should return String */
/*
* 4 Access Modifiers for a method / variable:
*
* public : Anyone can access (within or outside the project)
* private : Only within the class alone can access
* protected : Others within the same package can access
* or inherited members can access
* default / package : Others within the same package can access
*
* 2 Access Modifiers for a class
*
* public : Anyone can access
* default : Only member of the same package can access
*
*/
/*
* public - profile / name / blood group
* package - posts, photo, status
* private - password / dob
* protected - dob, activities, mobile number
*
*/
/*
* public - common area, mini hall, door bell, lift, security
* package - Gym, Swimming pool, terrace, car park, play area
* private - keys / bedroom
* protected - credit card / mobile /
*
*/
/*
* Input argument -> Zero or multiple argument
* primitive data type or it can be class
*
* add numbers -> what you need as input?
* 1) How many inputs? 2 inputs -> data type and number of inputs
* 2) What data type? int / long / float / double
*
* 2 input, both are int !! -> add 2 numbers -> int as return type
* 3,4 -> 7 (int)
* 3.2f, 4.5f -> 7.7f(float)
*
* did you eat? do you need input from me? (zero argument)
* what will you return -> boolean
*
* Assume, you have 2 cars with you and 2 different color !!
* what is color of your car? -> Do you need an input?
* What input will you ask me? which car? Model / brand
* What will you return? Color -> String
*
* What was your previous project name? (Assume you worked on multiple)
* Do you need more input? 1, 2, 3 (int)
* What will you return back to me? String -> Name of the project
* "Automax"
*
*
*/
}
package week1.day1;
// Java reserved keywords - lowercase - dark pink / purple
// interface, enum, @interface
public class Car {
// primitive data type works
boolean isNewCar = false;
// boolean -> data type of the variable
// isNewCar -> name of the variable
// = -> assignment operator
// true -> value of that variable
char logo = 'M';
// character is represented in single quote
// Kilometers the car has run
int run = 200000;
// Chasis number (long number)
long chasis = 8283883788l;
// How many liters of petrol you to be filled
float petrol = 1.12f;
// Represent long decimal
double percentage = 10.31232;
// Exception: String -> Class
String brand = "Maruti";
String regNum = "TN22BU0005";
}
package week1.day1;
public class ConditionalStatements {
public static void main(String[] args) {
int regNumber = 4000;
// Audi -> 1234 => Red , BMW -> 4567 -> White , Ferrari -> 1001 -> Gray,
// Hyundai -> 4000 -> Red , MG -> 5005 -> Black !!
if(regNumber == 1234 || regNumber == 4000) // Compare operator
System.out.println("Red");
else if(regNumber == 4567)
System.out.println("White");
else if(regNumber == 1001)
System.out.println("Gray");
else
System.out.println("Black");
// Expected print -> Red
// Arithmetic Operations
// + (add) - (sub) * (multiply) / (division -> quotient)
// remainder % (mod)
// 12 % 5 -> 2
}
}
package week1.day1;
public class MyVehicles {
/*
* Main method is the entry point for java execution
* Any code you write inside main method -> will get executed
* public -> access modifier
* static -> create single memory in Java
* void -> return type
* main -> method name
* String[] args -> input arguments
*/
public static void main(String[] args) {
// How to access other classes -> you are trying to call your school mate?
// Store the number in the phone (your memory) -> call or sms or whatsapp
// Syntax 1: Create reference to that class (saving to the memory)
// Syntax: ClassName reference = new ClassName();
Airplane myFlight = new Airplane();
// Send SMS to him stating that you will call him tomorrow
// Syntax 2: Call the method of that class using the reference
// Syntax: reference.methodName();
myFlight.applyBrake("Back");
boolean isLeft = myFlight.takeLeft();
System.out.println(isLeft);
String flightName = myFlight.getFlightName();
System.out.println(flightName);
System.out.println("^^^^^^^^^^^^^^^^^");
// How to access variables
// Create reference, call variable
Car myCar = new Car();
boolean isNewCar = myCar.isNewCar;
System.out.println(isNewCar);
System.out.println(myCar.run);
}
/* -> Call Mobile class
-> Create a main method
-> Within main method,
Call each of the 3 methods and print the values */
}
package week1.day1;
public class Welcome {
public static void main(String[] args) {
// Single line comment
System.out.println("Welcome each one of you !!");
}
}