package com.
codewithharry;
import [Link];
public class Main {
static int sum(int a, int b){
return a+b;
}
public static void main(String[] args) {
// Write your code here
// [Link]("Hello World");
/* Variables
Just Like:
-Water - Bucket
-Masala - box
-Lunch - LunchBox
In Java:
Variables are containers which store data values
String, int, float, char, boolean
How to declare variables:
syntax - <dataType> <variableName> = <value>;
*/
String name = "Harry";
String channel = "CodeWithHarry";
// [Link](name);
// [Link]([Link]());
// [Link]([Link]());
// [Link]([Link]());
// [Link](name + " from\" " + channel);
// [Link](name + " from\\ " + channel);
// [Link](name + " from\t " + channel);
// [Link](name + " from\n " + channel);
// [Link]([Link]("Har"));
// [Link]([Link](2));
// [Link]([Link]("ry"));
// [Link]([Link]("rry"));
int numb1 = 4, numb2 = 7;
// [Link]([Link](numb1, numb2));
// [Link]([Link](numb1, numb2));
// [Link]([Link](36));
// [Link]([Link](-36));
// [Link]([Link](6));
// [Link]([Link]());
// [Link](4+(8-4)*[Link]());
// [Link](4+(8-4)*[Link]());
// [Link](4+(8-4)*[Link]());
// [Link](4+(8-4)*[Link]());
// [Link](4+(8-4)*[Link]());
int a = 45, x=56, y=67;
float b = 45.22f;
boolean isAdult = false;
// [Link](x);
// [Link](b);
// [Link](isAdult);
/* Rules for constructing name of variables in Java
1. Can contain digits, underscores, dollar signs, letters
2. Should begin with a letter, $ or _
3. Java is case sensitive language which means that
harry and Harry are two different variables altogether.
4. Should not contain whitespaces
5. You cannot use reserved keywords from Java
*/
/*
Two types of Java Data Types:
1. Primitive - byte (1 byte), short (2 bytes), int(4 bytes), long (8
bytes), float(4 bytes),
double (8 bytes), boolean(1 bit), char (2 bytes).
2. Non Primitive or Reference Data Type -
*/
byte u = -56;
double d = 45.635435345d;
// [Link](d);
char grade = 'A';
// [Link](grade);
/* Operators in Java
Operand, operator, Operand = Result
4 + 7 = 11
Types of operators in Java
Arithmetic operators
Assignment operators
Logical Operators
Comparison Operators
*/
int num1 = 45, num2=78;
num1 += 3;
num2 -= 8;
// Explore these operators - *=, /=, %=
// [Link]("The value of num1 + num2 is ");
// [Link](num1 + num2);
//
// [Link]("The value of num1 - num2 is ");
// [Link](num1 - num2);
//
// [Link]("The value of num1 * num2 is ");
// [Link](num1 * num2);
//
// [Link]("The value of num1 / num2 is ");
// [Link](num1 / num2);
//
// [Link]("The value of num1 % num2 is ");
// [Link](num1 % num2);
// [Link](num2++);
// [Link](++num1);
// [Link](num1--);
// [Link](--num1);
/*
* Comparison Operators:
* 1. == : checks for equality of two values
* 2. != : checks if two values are not equal
* 3. <
* 4. >
* 5. <=
* 6. >=
*
* Logical Operators:
* 1. && - Logical and operator - returns true only if both conditions are true
* 2. || - Logical or operator - returns true if any one condition is true
* 3. ! - Logical not - Reverse the result from true to false and vice versa
* */
// Taking user input in Java
// Scanner scan = new Scanner([Link]);
// [Link]("Enter Your Age");
// int age = [Link]();
// [Link](input);
// If-else conditionals
// if(age>20){
//
// [Link]("You are an adult");
// }
// else if(age>5){
// [Link]("You are not a kid");
//
// }
// else{
// [Link]("You are a kid");
// }
// Switch statement in Java
// switch (age){
// case 12:
// [Link]("You are 12 years old");
// break;
// case 56:
// [Link]("You are 56 years old");
// break;
// case 16:
// [Link]("You are 16 years old");
// break;
// default:
// [Link]("You did not match any of the cases");
// }
// Quick Quiz: print sunday to saturday based on numbers 1 to 7 typed by the
user
// Loops
/*
While Loop
while(condition){
// This code will keep executing until the condition is true
}
*/
// int i = 0;
// while(i<100){
// [Link](i);
// i += 1;
// }
/*
Do While Loop
do{
// This code will keep executing until the condition is true
}while(condition)
*/
// int j = 0;
// do{
// [Link](j);
// j += 1;
// }while(j>100);
/*
For Loop
for(st1;st2;st3){
//Code to be executed
}
*/
//
// for(int i=0;i<=10;i++){
// if(i==2){
// continue;
// }
//// else{
//// [Link](i);
//// }
// [Link](i);
// }
// Java Arrays
// int [] marks = {1,2,3,5};
// marks[3] = 34; // this will update marks[3]
// [Link](marks[0]);
// [Link](marks[3]);
//
// // Classical way to iterate an array
// for(int i=0;i<[Link];i++){
// [Link](marks[i]);
// }
// [Link]("This is for each loop:");
//
// // For each loop
// for(int value:marks){
// [Link](value);
// }
//
// int [][] matrix = {{1,2,3},
// {4,5,6}};
// [Link](matrix[0][1]);
//
// String [] cars = {"Maruti Harry", "Maruti", "Suzuki", "Innova", "Ford
Titanium"};
// for(String value:cars){
// [Link](value);
// }
// Try - Catch
// String [] cars = {"Maruti Harry", "Maruti", "Suzuki", "Innova", "Ford
Titanium"};
//
// try{
// [Link](cars[3]);
// }
// catch(Exception e){
// [Link](e);
// }
//
// [Link]("Masoom");
// [Link](sum(5, 7));
float number_1, number_2;
[Link]("Enter first number");
Scanner scan = new Scanner([Link]);
number_1 = [Link]();
[Link]("Enter second number");
// Scanner scan2 = new Scanner([Link]);
number_2 = [Link]();
[Link]("You have Entered ");
[Link](number_1);
[Link](" and ");
[Link](number_2);
String prompt = "Enter 0 for addition, 1 for " +
"subtraction, 2 for multiplication and 3 for division";
[Link](prompt);
int input = [Link]();
switch (input){
case 0:
[Link]("Adding these numbers");
[Link]("The result is: ");
[Link](number_1 + number_2);
break;
case 1:
[Link]("Subtracting these numbers");
[Link]("The result is: ");
[Link](number_1 - number_2);
break;
case 2:
[Link]("Multiplying these numbers");
[Link]("The result is: ");
[Link](number_1 * number_2);
break;
case 3:
[Link]("Dividing these numbers");
[Link]("The result is: ");
[Link](number_1 / number_2);
break;
default:
[Link]("Invalid input");
}
}
}