Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Programmes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 176

Java Hello World Program

package com.company; // Groups classes

public class Main{ // Entrypoint into the application

public static void main(String[]args){

System.out.println(“Hello World”);

Literals in Java

package com.company;

public class CWH_04_literals {

public static void main(String[] args) {

byte age = 34;

int age2 = 56;

short age3 = 87;

long ageDino = 5666666666666L;

char ch = 'A';

float f1 = 5.6f;

double d1 = 4.66;

boolean a = true;

System.out.print(age);

String str = "Harry";

System.out.println(str);
}

Reading data from the Keyboard

Scanner S = new Scanner(System.in); //(Read from the keyboard)

int a = S.nextInt(); //(Method to read from the keyboard)

Code as Described in the Video


package com.company;

import java.util.Scanner;

public class CWH_05_TakingInpu {

public static void main(String[] args) {

System.out.println("Taking Input From the User");

Scanner sc = new Scanner(System.in);

// System.out.println("Enter number 1");

// int a = sc.nextInt();

// float a = sc.nextFloat();

// System.out.println("Enter number 2");

// int b = sc.nextInt();

// float b = sc.nextFloat();

// int sum = a +b;

// float sum = a +b;

// System.out.println("The sum of these numbers is");

// System.out.println(sum);

// boolean b1 = sc.hasNextInt();

// System.out.println(b1);
// String str = sc.next();

String str = sc.nextLine();

System.out.println(str);

1. Write a program to sum three numbers in Java.


2. Write a program to calculate CGPA using marks of three subjects (out of 100)
3. Write a Java program that asks the user to enter his/her name and greets them with “Hello
<name>, have a good day” text.
4. Write a Java program to convert Kilometers to miles.
5. Write a Java program to detect whether a number entered by the user is an integer or not.

package com.company;

import java.util.Scanner;

public class CWH_Ch1_PS {

public static void main(String[] args) {

// Question1

// int a = 4;

// int b = 17;

// int c =6;

// int sum = a + b+c;

// System.out.println(sum);

// Question2

// float subject1 = 45;

// float subject2 = 95;


// float subject3 = 48;

// float cgpa = (subject1 + subject2 +subject3)/30;

// System.out.println(cgpa);

// Question 3

// System.out.println("What is your name");

// Scanner sc = new Scanner(System.in);

// String name = sc.next();

// System.out.println("Hello " + name + " have a good day!");

// Question 5

System.out.println("Enter your number");

Scanner sc = new Scanner(System.in);

System.out.println(sc.hasNextInt());

Precedence of operators
package com.company;

public class CWH_Ch2_Operators {

public static void main(String[] args) {

// 1. Arithmetic Operators

int a = 4;

// int b = 6 % a; // Modulo Operator

// 4.8%1.1 --> Returns Decimal Remainder


// 2. Assignment Operators

int b = 9;

b *= 3;

System.out.println(b);

// 3. Comparison Operators

// System.out.println(64<6);

// 4. Logical Operators

// System.out.println(64>5 && 64>98);

System.out.println(64>5 || 64>98);

// 5. Bitwise Operators

System.out.println(2&3);

// 10

// 11

// ----

// 10

Associativity of Operators

package com.company;
public class cwh_09_ch2_op_pre {

public static void main(String[] args) {

// Precedence & Associativity

//int a = 6*5-34/2;

/*

Highest precedence goes to * and /. They are then evaluated on the


basis

of left to right associativity

=30-34/2

=30-17

=13

*/

//int b = 60/5-34*2;

/*

= 12-34*2

=12-68

=-56

*/

//System.out.println(a);
//System.out.println(b);

// Quick Quiz

int x =6;

int y = 1;

// int k = x * y/2;

int b = 0;

int c = 0;

int a = 10;

int k = b*b - (4*a*c)/(2*a);

System.out.println(k);

}
 increment and decrement 

package com.company;

public class cwh_10_resulting_data_type {

public static void main(String[] args) {


/* byte x = 5;

int y = 6;

short z = 8;

int a = y + z;

float b = 6.54f + x;

System.out.println(b); */

// Increment and Decrement Operators

int i = 56;

// int b = i++; // first b is assigned i (56) then i is incremented

int j = 67;

int c = ++j; // first j is incremented then c is assigned j (68)

System.out.println(i++);

System.out.println(i);

System.out.println(++i);

System.out.println(i);

int y = 7;

System.out.println( ++y *8);

char ch = 'a';

System.out.println(++ch);
}

}
Write a program to calculate the percentage of a given student in the CBSE board exam. His
marks from 5 subjects must be taken as input from the keyboard. (Marks are out of 100)

package com.company;

import java.util.Scanner;

public class cwh_11_ex1_sol {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter your Physics marks : ");

int physics = scan.nextInt();

System.out.println("Enter your English marks : ");

int English = scan.nextInt();

System.out.println("Enter your Chemistry marks : ");

int chemistry = scan.nextInt();

System.out.println("Enter your Mathematics marks : ");

int mathematics = scan.nextInt();


System.out.println("Enter your Computer Science marks : ");

int computer = scan.nextInt();

float percentage = ((physics + English + chemistry + mathematics +


computer)/500.0f)*100;

System.out.println("percentage : ");

System.out.println(percentage);

}
1. What will be the result of the following expression:

float a = 7/4 * 9/2

1. Write a java program to encrypt a grade by adding 8 to it. Decrypt it to show the correct
grade.
2. Use comparison operators to find out whether a given number is greater than the user
entered number or not.
3. Write the following expression in a java program:

v2-u2/2as

5. Find the value of the following expression:

int x = 7

int a = 7*49/7 + 35/7

Value of a?
package com.company;

public class cwh_12_ps2_pr01 {

public static void main(String[] args) {

float a = 7/4.0f * 9/2.0f;

System.out.println(a);

2ANS:

package com.company;

public class cwh_12_ps2_pr02 {

public static void main(String[] args) {

char grade = 'B';

grade = (char)(grade + 8);

System.out.println(grade);

// Decrypting the grade

grade = (char)(grade - 8);

System.out.println(grade);

}
3ANS

package com.company;

import java.util.Scanner;

public class cwh_12_ps2_pr_03 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

System.out.println(a>8);

System.out.println(7*49/7+35/7);

Strings

package com.company;

import java.util.Scanner;

public class cwh_13_strings {

public static void main(String[] args) {

// String name = new String("Harry");

// String name = "Harry";


// System.out.print("The name is: ");

// System.out.print(name);

int a = 6;

float b = 5.6454f;

System.out.printf("The value of a is %d and value of b is %8.2f", a, b);

//System.out.format("The value of a is %d and value of b is %f", a, b);

Scanner sc = new Scanner(System.in);

// String st = sc.next();

// String st = sc.nextLine();

// System.out.println(st);

String Methods

package com.company;

public class cwh_14_string_methods {

public static void main(String[] args) {

String name = "Harry";

// System.out.println(name);

int value = name.length();


//System.out.println(value);

//String lstring = name.toLowerCase();

//System.out.println(lstring);

//String ustring = name.toUpperCase();

//System.out.println(ustring);

//String nonTrimmedString = " Harry ";

//System.out.println(nonTrimmedString);

//String trimmedString = nonTrimmedString.trim();

//System.out.println(trimmedString);

//System.out.println(name.substring(1));

//System.out.println(name.substring(1,5));

//System.out.println(name.replace('r', 'p'));

//System.out.println(name.replace("r", "ier"));
//System.out.println(name.startsWith("Har"));

//System.out.println(name.endsWith("dd"));

//System.out.println(name.charAt(4));

//String modifiedName = "Harryrryrry";

//System.out.println(modifiedName.indexOf("rry"));

//System.out.println(modifiedName.indexOf("rry", 4));

//System.out.println(modifiedName.lastIndexOf("rry", 7));

//System.out.println(name.equals("Harry"));
System.out.println(name.equalsIgnoreCase("HarRY"));

System.out.println("I am escape sequence\tdouble quote");

}
1. Write a Java program to convert a string to lowercase.
2. Write a Java program to replace spaces with underscores.
3. Write a Java program to fill in a letter template which looks like below:

package com.company;

public class cwh_15_ps3 {

public static void main(String[] args) {

// Problem 1

//String name = "Jack Parker";

//name = name.toLowerCase();

//System.out.println(name);

// Problem 2

//String text = "To My Friend";

//text = text.replace(" ", "_");

//System.out.println(text);

// Problem 3
String letter = "Dear <|name|>, Thanks a lot!";

letter = letter.replace("<|name|>", "Sachin");

System.out.println(letter);

// Problem 4

String myString = "This string contains double and triple spaces";

System.out.println(myString.indexOf(" "));

System.out.println(myString.indexOf(" "));

// Problem 5

String myLetter = "Dear Harry,\n\tThis Java Course is Nice.\


nThanks!";

System.out.println(myLetter);

If-Else Statement
/* if (condition-to-be-checked) {

statements-if-condition-true;

else {

statements-if-condition-false;
} */

else-if clause
/* if (condition1) {

//Statements;

else if {

// Statements;

else {

//Statements

} */

Code as Described in the Video


package com.company;

public class cwh_17_logical {

public static void main(String[] args) {

System.out.println("For Logical AND...");

boolean a = true;

boolean b = false;

// if (a && b){
// System.out.println("Y");

// }

// else{

// System.out.println("N");

// }

System.out.println("For Logical OR...");

// if (a || b){

// System.out.println("Y");

// }

// else{

// System.out.println("N");

// }

System.out.println("For Logical NOT");

System.out.print("Not(a) is ");

System.out.println(!a);

System.out.print("Not(b) is ");

System.out.println(!b);

Switch Case-Control Instruction


/* Switch(var) {

Case C1:

//Code;
break;

Case C2:

//Code;

break;

Case C3:

//Code

break;

default:

//Code

} */

Code as Described in the Video


package com.company;

import java.util.Scanner;

public class cwh_18_elseif {

public static void main(String[] args) {

String var = "Saurabh";

switch (var) {

case "Shubham" -> {

System.out.println("You are going to become an Adult!");

System.out.println("You are going to become an Adult!");

System.out.println("You are going to become an Adult!");

case "Saurabh" -> System.out.println("You are going to join a Job!");


case "Vishaka" -> System.out.println("You are going to get retired!");

default -> System.out.println("Enjoy Your life!");

System.out.println("Thanks for using my Java Code!");

/*

int age;

System.out.println("Enter Your Age");

Scanner sc = new Scanner(System.in);

age = sc.nextInt();

if (age>56){

System.out.println("You are experienced!");

else if(age>46){

System.out.println("You are semi-experienced!");

else if(age>36){

System.out.println("You are semi-semi-experienced!");

else{

System.out.println("You are not experienced");

if(age>2){

System.out.println("You are not a baby!");


}

*/

1. What will be the output of this program:

int a = 10;
if (a=11)
System.out.println(“I am 11”);
else
System.out.println(“I am not 11”);

Copy

2. Write a program to find out whether a student is pass or fail; if it requires total 40% and at least
33% in each subject to pass. Assume 3 subjects and take marks as an input from the user.
3. Calculate income tax paid by an employee to the government as per the slabs mentioned below:

Income
Tax
Slab
2.5L –
5%
5.0L  
5.0L –
20%
10.0L 
Above
30%
10.0L
Note that there is not tax below 2.5L. Take the input amount as input from the user.

4. Write a Java program to find out the day of the week given the number [1 for Monday, 2 for
Tuesday … and so on!]
5. Write a Java program to find whether a year entered by the user is a leap year or not.
6. Write a program to find out the type of website from the URL:

 .com – commercial website


 .org – organization website
 .in – Indian website

package com.company;

import java.util.Scanner;

import java.util.Random;
public class cwh_19_ch4_ps {

public static void main(String[] args) {

// Question 1:

// int a = 11;

// if(a=11){

//

// }

// Question 2

// byte m1, m2, m3;

// Scanner sc = new Scanner(System.in);

// System.out.println("Enter your marks in Physics");

// m1 = sc.nextByte();

//

// System.out.println("Enter your marks in Chemistry");

// m2= sc.nextByte();

//

// System.out.println("Enter your marks in Mathematics");

// m3 = sc.nextByte();

// float avg = (m1+m2+m3)/3.0f;

// System.out.println("Your Overall percentage is: " + avg);

// if(avg>=40 && m1>=33 && m2>=33 && m3>=33){

// System.out.println("Congratulations, You have been promoted");

// }

// else{
// System.out.println("Sorry, You have not been promoted! Please try again.");

// }

// Question 3

// Scanner sc = new Scanner(System.in);

// System.out.println("Enter your income in Lakhs per annum");

// float tax = 0;

// float income = sc.nextFloat();

// if(income<=2.5){

// tax = tax + 0;

// }

// else if(income>2.5f && income <= 5f){

// tax = tax + 0.05f * (income - 2.5f);

// }

// else if(income>5f && income <= 10.0f){

// tax = tax + 0.05f * (5.0f - 2.5f);

// tax = tax + 0.2f * (income - 5f);

// }

// else if(income>10.0f){

// tax = tax + 0.05f * (5.0f - 2.5f);

// tax = tax + 0.2f * (10.0f - 5f);

// tax = tax + 0.3f * (income - 10.0f);

// }

//

// System.out.println("The total tax paid by the employee is: " + tax);


// Question 4:

// Scanner sc = new Scanner(System.in);

// int day = sc.nextInt();

//

// switch (day){

// case 1 -> System.out.println("Monday");

// case 2 -> System.out.println("Tuesday");

// case 3 -> System.out.println("Wednesday");

// case 4 -> System.out.println("Thursday");

// case 5 -> System.out.println("Friday");

// case 6 -> System.out.println("Saturday");

// case 7 -> System.out.println("Sunday");

// }

// Question 6

// Scanner sc = new Scanner(System.in);

// String website = sc.next();

// if(website.endsWith(".org")){

// System.out.println("This is an organizational website");

// }

// else if(website.endsWith(".com")){

// System.out.println("This is a Commercial website");

// }
// else if(website.endsWith(".in")){

// System.out.println("This is an Indian website");

// }

Random r = new Random();

int a = r.nextInt();

System.out.println(a);

While loops
/*

while (Boolean condition)

// Statements -> This keeps executing as long as the condition is true.

*/

Write a program to print natural numbers from 100 to 200

package com.company;

public class cwh_21_ch5_loops {

public static void main(String[] args) {

System.out.println(1);

System.out.println(2);
System.out.println(3);

System.out.println("Using Loops:");

int i = 100;

while(i<=200){

System.out.println(i);

i++;

System.out.println("Finish Running While Loop!");

// while(true){

// System.out.println("I am an infinite while loop!");

// }

do-while loop:
/* do {

//code

} while (condition); //Note this semicolon */

rite a program to print first n natural numbers using a do-while loop

package com.company;

public class cwh_22_ch4_do_while {

public static void main(String[] args) {


// int a = 0;

// while(a<5){

// System.out.println(a);

// a++;

// }

int b = 10;

do {

System.out.println(b);

b++;

}while(b<5);

int c = 1;

do{

System.out.println(c);

c++;

}while(c<=45);

For loop:

/* for (initialize; check_bool_expression; update){

//code;

} */
Decrementing for loop
for (i=7; i!=0; i--){

System.out.println(i);

Write a program to print first n natural numbers in reverse order.

package com.company;

public class cwh_23_for_loop {

public static void main(String[] args) {

// for (int i=1; i<=10; i++){

// System.out.println(i);

// }

// 2i = Even Numbers = 0, 2, 4, 6, 8

// 2i+1 = Odd Numbers = 1, 3, 5, 7, 9

//int n = 3;

//for (int i =0; i<n; i++){

// System.out.println(2*i+1);

//}

for(int i=5; i!=0; i--){

System.out.println(i);

}
break and continue in Java

package com.company;

public class cwh_24_break_and_continue {

public static void main(String[] args) {

// Break and continue using loops!

// for (int i=0;i<50;i++){

// System.out.println(i);

// System.out.println("Java is great");

// if(i==2){

// System.out.println("Ending the loop");

// break;

// }

// }

// int i=0;

// do{

// System.out.println(i);

// System.out.println("Java is great");

// if(i==2){

// System.out.println("Ending the loop");


// break;

// }

// i++;

// }while(i<5);

// System.out.println("Loop ends here");

// for(int i=0;i<50;i++){

// if(i==2){

// System.out.println("Ending the loop");

// continue;

// }

// System.out.println(i);

// System.out.println("Java is great");

// }

int i=0;

do{

i++;

if(i==2){

System.out.println("Ending the loop");


continue;

System.out.println(i);

System.out.println("Java is great");

}while(i<5);

System.out.println("Loop ends here");

}
Write a program to print the following pattern

****

***

**

2. Write a program to sum first n even numbers using a while loop.


3. Write a program to print the multiplication table of a given number n.
4. Write a program to print a multiplication table of 10 in reverse order.
5. Write a program to find factorial of a given number using for loops.
6. Repeat problem 5 using a while loop.
7. Repeat problem 1 using for/while loop.
8. What can be done using one type of loop can also be done using the other two types of
loops - True or False.
9. Write a program to calculate the sum of the numbers occurring in the multiplication table
of 8.

 A do-while loop is executed:

       At least once

       At least twice

      At most once
Repeat problem 2 using for loop.

package com.company;

public class cwh_25_practice_set_5 {

public static void main(String[] args) {

// Practice Problem 1

// int n = 4;

// for (int i=n; i>0; i--){

// for(int j=0;j<i;j++){

// System.out.print("*");

// }

// System.out.print("\n");

// }

// Practice Problem 2

// int sum=0;

// int n=4;

// for(int i=0;i<n;i++){

// sum = sum + (2*i);

// }

// System.out.print("Sum of even numbers is: ");

// System.out.println(sum);

// First 4 even numbers are - 0 2 4 6

// Practice Problem 3
// int n = 5;

// //for(int i=0; i<10; i++) - Goes from i=0 to i=9

// for(int i=1;i<=10;i++){

// System.out.printf("%d X %d = %d\n", n, i, n*i);

// }

// Practice Problem 4

// int n = 10;

// //for(int i=0; i<10; i++) - Goes from i=0 to i=9

// for(int i=10;i>=1;i--){

// System.out.printf("%d X %d = %d\n", n, i, n*i);

// }

// Practice Problem 6

// int n = 5;

// // What is factorial n = n * n-1 * n-2 ..... 1

// // 5! = 5*4*3*2*1 = 120

// int i = 1;

// int factorial = 1;

// while(i<=n){

// factorial *= i;

// i++;

// }

// System.out.println(factorial);
// Practice Problem 9

// int n = 8;

// int sum = 0;

// //for(int i=0; i<10; i++) - Goes from i=0 to i=9

// for(int i=1;i<=10;i++){

// sum += n*i;

// }

// System.out.println(sum);

Displaying an Array

for (int i=0; i<marks.length; i++)

Sout(marks[i]); //Array Traversal

Code as Described
package com.company;

public class cwh_26_arrays

public static void main(String[] args) {

/* Classroom of 500 students - You have to store marks of these 500 students

You have 2 options:

1. Create 500 variables


2. Use Arrays (recommended)

*/

// There are three main ways to create an array in Java

// 1. Declaration and memory allocation

// int [] marks = new int[5];

// 2. Declaration and then memory allocation

// int [] marks;

// marks = new int[5];

// Initialization

// marks[0] = 100;

// marks[1] = 60;

// marks[2] = 70;

// marks[3] = 90;

// marks[4] = 86;

// 3. Declaration, memory allocation and initialization together

int [] marks = {98, 45, 79, 99, 80};

// marks[5] = 96; - throws an error

System.out.println(marks[4]);

For Each Loop in Java


Array elements can also be traversed as follows:
/* for (int element:Arr) {

Sout(element); //Prints all the elements

} */

Code as Described in the Video


package com.company;

public class cwh_27_arrays {

public static void main(String[] args) {

/*

float [] marks = {98.5f, 45.5f, 79.5f, 99.5f, 80.5f};

String [] students ={"Harry", "Rohan", "Shubham", "Lovish"};

System.out.println(students.length);

System.out.println(students[2]);

*/

int [] marks = {98, 45, 79, 99, 80};

// System.out.println(marks.length);

// Displaying the Array (Naive way)

System.out.println("Printing using Naive way");

System.out.println(marks[0]);

System.out.println(marks[1]);

System.out.println(marks[2]);

System.out.println(marks[3]);

System.out.println(marks[4]);
// Displaying the Array (for loop)

System.out.println("Printing using for loop");

for(int i=0;i<marks.length;i++){

System.out.println(marks[i]);

// Quick Quiz: Displaying the Array in Reverse order (for loop)

System.out.println("Printing using for loop in reverse order");

for(int i=marks.length -1;i>=0;i--){

System.out.println(marks[i]);

// Quick Quiz: Displaying the Array (for-each loop)

System.out.println("Printing using for-each loop");

for(int element: marks){

System.out.println(element);

Multidimensional 2-D Array


int [][] flats = new int[2][3] //A 2-D array of 2 rows + 3 columns

We can add elements to this array as follow

flats[0][0] = 100

flats[0][1] = 101
flats[0][2] = 102

// … & so on!

 3-D array 

String[][][] arr = new String [2][3][4]

CODE

package com.company;

public class cwh_28_multi_dim_arrays {

public static void main(String[] args) {

int [] marks; // A 1-D Array

int [][] flats; // A 2-D Array

flats = new int [2][3];

flats[0][0] = 101;

flats[0][1] = 102;

flats[0][2] = 103;

flats[1][0] = 201;

flats[1][1] = 202;

flats[1][2] = 203;

// Displaying the 2-D Array (for loop)

System.out.println("Printing a 2-D array using for loop");

for(int i=0;i<flats.length;i++){

for(int j=0;j<flats[i].length;j++) {

System.out.print(flats[i][j]);

System.out.print(" ");

}
System.out.println("");

1. Create an array of 5 floats and calculate their sum.


2. Write a program to find out whether a given integer is present in an array or not.
3. Calculate the average marks from an array containing marks of all students in physics using a for-
each loop.
4. Create a Java program to add two matrices of size 2x3.
5. Write a Java program to reverse an array.
6. Write a Java program to find the maximum element in an array.
7. Write a Java program to find the maximum element in a Java array.
8. Write a Java program to find whether an array is sorted or not.

Code Solution:
package com.company;

public class cwh_29_Practice_Set_6 {

public static void main(String[] args) {

// Practice Problem 1

/* float [] marks = {45.7f, 67.8f, 63.4f, 99.2f, 100.0f};

float sum = 0;

for(float element:marks){

sum = sum + element;

System.out.println("The value of sum is " + sum);

// Practice Problem 2

float [] marks = {45.7f, 67.8f, 63.4f, 99.2f, 100.0f};

float num = 45.57f;


boolean isInArray = false;

for(float element:marks){

if(num==element){

isInArray = true;

break;

if(isInArray){

System.out.println("The value is present in the array");

else{

System.out.println("The value is not present in the array");

// Practice Problem 3

float [] marks = {45.7f, 67.8f, 63.4f, 99.2f, 100.0f};

float sum = 0;

for(float element:marks){

sum = sum + element;

System.out.println("The value of average marks is " + sum/marks.length);

// Practice Problem 4
int [][] mat1 = {{1, 2, 3},

{4, 5, 6}};

int [][] mat2 = {{2, 6, 13},

{3, 7, 1}};

int [][] result = {{0, 0, 0},

{0, 0, 0}};

for (int i=0;i<mat1.length;i++){ // row number of times

for (int j=0;j<mat1[i].length;j++) { // column number of time

System.out.format(" Setting value for i=%d and j=%d\n", i, j);

result[i][j] = mat1[i][j] + mat2[i][j];

// Printing the elements of a 2-D Array

for (int i=0;i<mat1.length;i++){ // row number of times

for (int j=0;j<mat1[i].length;j++) { // column number of time

System.out.print(result[i][j] + " ");

result[i][j] = mat1[i][j] + mat2[i][j];

System.out.println(""); // Prints a new line

// Practice Problem 5

int [] arr = {1, 21, 3, 4, 5, 34, 67};

int l = arr.length;
int n = Math.floorDiv(l, 2);

int temp;

for(int i=0; i<n; i++){

// Swap a[i] and a[l-1-i]

// a b temp

// |4| |3| ||

temp = arr[i];

arr[i] = arr[l-i-1];

arr[l-i-1] = temp;

for(int element: arr){

System.out.print(element + " ");

// Practice Problem 6

int [] arr = {1, 2100, 3, 455, 5, 34, 67};

int max = Integer.MIN_VALUE;

for(int e: arr){

if(e>max){

max = e;

System.out.println("the value of the maximum element in this array is: "+ max);
// Practice Problem 6

System.out.println(Integer.MIN_VALUE);

System.out.println(Integer.MAX_VALUE);

*/

// Practice Problem 7

boolean isSorted = true;

int [] arr = {1, 12, 3, 4, 5, 34, 67};

for(int i=0;i<arr.length-1;i++){

if(arr[i] > arr[i+1]){

isSorted = false;

break;

if(isSorted){

System.out.println("The Array is sorted");

else{

System.out.println("The Array is not sorted");

Methods in Java

Syntax of a Method
dataType name() {
//Method body

 returns the sum of two numbers


int mySum(int a, int b) {

int c = a+b;

return c; //Return value

Source code 
package com.company;

public class cwh_31_methods {

static int logic(int x, int y){

int z;

if(x>y){

z = x+y;

else {

z = (x +y) * 5;

x = 566;

return z;

public static void main(String[] args) {


int a = 5;

int b = 7;

int c;

// Method invocation using Object creation

//cwh_31_methods obj = new cwh_31_methods();

//c = obj.logic(a, b);

c = logic(a, b);

System.out.println(a + " "+ b);

int a1 = 2;

int b1 = 1;

int c1;

c1 = logic(a1, b1);

System.out.println(c);

System.out.println(c1);

Overloading in Java

package com.company;

public class cwh_32_method_overloading {

static void foo(){

System.out.println("Good Morning bro!");

}
static void foo(int a){

System.out.println("Good morning " + a + " bro!");

static void foo(int a, int b){

System.out.println("Good morning " + a + " bro!");

System.out.println("Good morning " + b + " bro!");

static void foo(int a, int b, int c){

System.out.println("Good morning " + a + " bro!");

System.out.println("Good morning " + b + " bro!");

static void change(int a){

a = 98;

static void change2(int [] arr){

arr[0] = 98;

static void tellJoke(){

System.out.println("I invented a new word!\n" +

"Plagiarism!");

}
public static void main(String[] args) {

// tellJoke();

// Case 1: Changing the Integer

//int x = 45;

//change(x);

//System.out.println("The value of x after running change is: " + x);

// Case 1: Changing the Array

// int [] marks = {52, 73, 77, 89, 98, 94};

// change2(marks);

// System.out.println("The value of x after running change is: " + marks[0]);

// Method Overloading

foo();

foo(3000);

foo(3000, 4000);

// Arguments are actual!

Write a program to calculate (recursion must be used) factorial of a number


in Java?
package com.company;

public class cwh_34_recursion {

// factorial(0) = 1

// factorial(n) = n * n-1 *....1

// factorial(5) = 5 * 4 * 3 * 2 * 1 = 120

// factorial(n) = n * factorial(n-1)

static int factorial(int n){

if(n==0 || n==1){

return 1;

else{

return n * factorial(n-1);

static int factorial_iterative(int n){

if(n==0 || n==1){

return 1;

else{

int product = 1;

for (int i=1;i<=n;i++){ // 1 to n

product *= i;

}
return product;

public static void main(String[] args) {

int x = 0;

System.out.println("The value of factorial x is: " + factorial(x));

System.out.println("The value of factorial x is: " + factorial_iterative(x));

1. Write a Java method to print the multiplication table of a number n.


2. Write a program using functions to print the following pattern:

   *

     **

    ***

   ****

3. Write a recursive function to calculate the sum of first n natural


numbers.
4. Write a function to print the following pattern:

    ****

    ***

    **

  *

5. Write a function to print the nth term of the Fibonacci series using


recursion.
6. Write a function to find the average of a set of numbers passed as
arguments.
7. Repeat problem 4 using Recursion.
8. Repeat problem 2 using Recursion.
9. Write a function to convert Celsius temperature into Fahrenheit.
10. Repeat problem 3 using an iterative approach.

Code Solution:
package com.company;

public class cwh_35_practice_set_on_methods {

static void multiplication(int n) {

for (int i = 1; i <= 10; i++) {

System.out.format("%d X %d = %d\n", n, i, n * i);

static void pattern1(int n) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < i + 1; j++) {

System.out.print("*");

System.out.println();

static void pattern1_rec(int n) {

if (n > 0) {

pattern1_rec(n - 1);

for (int i = 0; i < n; i++) {


System.out.print("*");

System.out.println();

// pattern1_rec(3)

// pattern1_rec(2) + 3 times star and new line

// pattern1_rec(1) + 2 times star and new line + 3 times star and new line

// pattern1_rec(0) + 1 times star and new line + 2 times star and new line + 3 times star and new line

// sum(n) = 1 + 2 + 3... + n

// sum(n) = 1 + 2 + 3... + n-1 + n

// sum(n) = sum(n-1) + n

// sum(3) = 3 + sum(2)

// sum(3) = 3 + 2 + sum(1)

// sum(3) = 3 + 2 + 1

static int sumRec(int n) {

// Base condition

if (n == 1) {

return 1;

return n + sumRec(n - 1);

}
static int fib(int n) {

/* if(n==1){

return 0;

else if(n==2){

return 1;

} */

if (n == 1 || n == 2) {

return n - 1;

} else {

return fib(n - 1) + fib(n - 2);

public static void main(String[] args) {

// Problem 1

// multiplication(7);

// Problem 2

// pattern1(9);

// Problem 3

// int c = sumRec(4);

// System.out.println(c);
// Problem 4

// fibonacci series - 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

// int result = fib(7);

// System.out.println(result);

// Problem 8

pattern1(9);

Creating Our Own Java Class

Writing a Custom Class


public class Employee {

int id; // Attribute 1

String name; // Attribute 2

A Class with Methods


/*

public class Employee {

public int id;

public String name;

public int getSalary(){

//code

public void getDetails(){


//code

};

*/

Code as Described in the Video


package com.company;

class Employee{

int id;

int salary;

String name;

public void printDetails(){

System.out.println("My id is " + id);

System.out.println("and my name is "+ name);

public int getSalary(){

return salary;

public class cwh_38_custom_class {

public static void main(String[] args) {

System.out.println("This is our custom class");

Employee harry = new Employee(); // Instantiating a new Employee Object

Employee john = new Employee(); // Instantiating a new Employee Object


// Setting Attributes for Harry

harry.id = 12;

harry.salary = 34;

harry.name = "CodeWithHarry";

// Setting Attributes for John

john.id = 17;

john.salary = 12;

john.name = "John Khandelwal";

// Printing the Attributes

harry.printDetails();

john.printDetails();

int salary = john.getSalary();

System.out.println(salary);

// System.out.println(harry.id);

// System.out.println(harry.name);

1. Create a class Employee with the following properties and methods:

 Salary (property) (int)


 getSalary (method returning int)
 name (property) (String)
 getName (method returning String)
 setName (method changing name)
2. Create a class cellphone with methods to print “ringing…”,
“vibrating…”, etc.
3. Create a class Square with a method to initialize its side, calculating
area, perimeter etc.
4. Create a class Rectangle & problem 3.
5. Create a class TommyVecetti for Rockstar Games capable of hitting
(print hitting…), running, firing, etc.
6. Repeat problem 4 for a circle.

Code as Described in the Video


package com.company;

class Employee{

int salary;

String name;

public int getSalary(){

return salary;

public String getName(){

return name;

public void setName(String n){

name = n;

class CellPhone{

public void ring(){


System.out.println("Ringing...");

public void vibrate(){

System.out.println("Vibrating...");

public void callFriend(){

System.out.println("Calling Mukul...");

class Square{

int side;

public int area(){

return side*side;

public int perimeter(){

return 4*side;

class Tommy{

public void hit(){

System.out.println("Hitting the enemy");

}
public void run(){

System.out.println("Running from the enemy");

public void fire(){

System.out.println("Firing on the enemy");

public class cwh_39_ch8ps {

public static void main(String[] args) {

/*

// Problem 1

Employee harry = new Employee();

harry.setName("CodeWithHarry");

harry.salary = 233;

System.out.println(harry.getSalary());

System.out.println(harry.getName());

// Problem 2

CellPhone asus = new CellPhone();

asus.callFriend();

asus.vibrate();

//asus.ring();

// Problem 3
Square sq = new Square();

sq.side = 3;

System.out.println(sq.area());

System.out.println(sq.perimeter());

*/

// Problem 5

Tommy player1 = new Tommy();

player1.fire();

player1.run();

player1.hit();

Access modifiers, getters & setters in Java

package com.company;

class MyEmployee{

private int id;

private String name;

public String getName(){

return name;

}
public void setName(String n){

this.name = n;

public void setId(int i){

this.id = i;

public int getId(){

return id;

public class cwh_40_ch9 {

public static void main(String[] args) {

MyEmployee harry = new MyEmployee();

// harry.id = 45;

// harry.name = "CodeWithHarry"; --> Throws an error due to private access modifier

harry.setName("CodeWithHarry");

System.out.println(harry.getName());

harry.setId(234);

System.out.println(harry.getId());

Exercise 2

package com.company;

import java.util.Random;
import java.util.Scanner;

public class cwh_41_ex2sol {

public static void main(String[] args) {

// 0 for Rock

// 1 for Paper

// 2 for Scissor

Scanner sc = new Scanner(System.in);

System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor ");

int userInput = sc.nextInt();

Random random = new Random();

int computerInput = random.nextInt(3);

if (userInput == computerInput) {

System.out.println("Draw");

else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0

|| userInput == 2 && computerInput == 1) {

System.out.println("You Win!");

} else {

System.out.println("Computer Win!");

// System.out.println("Computer choice: " + computerInput);


if(computerInput==0){

System.out.println("Computer choice: Rock");

else if(computerInput==1){

System.out.println("Computer choice: Paper");

else if(computerInput==2){

System.out.println("Computer choice: Scissors");

 Constructors in Java

package com.company;

class MyMainEmployee{

private int id;

private String name;

public MyMainEmployee(){

id = 0;

name = "Your-Name-Here";

public MyMainEmployee(String myName, int myId){

id = myId;

name = myName;
}

public MyMainEmployee(String myName){

id = 1;

name = myName;

public String getName(){

return name;

public void setName(String n){

this.name = n;

public void setId(int i){

this.id = i;

public int getId(){

return id;

public class cwh_42_constructors {

public static void main(String[] args) {

//MyMainEmployee harry = new MyMainEmployee("ProgrammingWithHarry", 12);

MyMainEmployee harry = new MyMainEmployee();

//harry.setName("CodeWithHarry");

//harry.setId(34);
System.out.println(harry.getId());

System.out.println(harry.getName());

1. create a class cylinder and use getter and setters to set its radius and
height 
2. use ➊ to calculate surface and volume of the cylinder 
3. Use a constructor and repeat ➊
4. Overload a constructor used to initialize a rectangle of length and
breath 5 for using custom parameters 
5. Repeat ➊ for a sphere

Source code as described in the video:


package com.company;

class Cylinder{

private int radius;

private int height;

public Cylinder(int radius, int height) {

this.radius = radius;

this.height = height;

public int getRadius() {

return radius;

}
public void setRadius(int radius) {

this.radius = radius;

public int getHeight() {

return height;

public void setHeight(int height) {

this.height = height;

public double surfaceArea(){

return 2* Math.PI* radius * radius + 2*Math.PI*radius*height;

public double volume(){

return Math.PI * radius * radius * height;

class Rectangle{

private int length;

private int breadth;


public Rectangle() {

this.length = 4;

this.breadth = 5;

public Rectangle(int length, int breadth) {

this.length = length;

this.breadth = breadth;

public int getLength() {

return length;

public int getBreadth() {

return breadth;

public class cwh_44_ps09 {

public static void main(String[] args) {

/*

// Problem 1

Cylinder myCylinder = new Cylinder(9, 12);


//myCylinder.setHeight(12);

System.out.println(myCylinder.getHeight());

//myCylinder.setRadius(9);

System.out.println(myCylinder.getRadius());

// Problem 2

System.out.println(myCylinder.surfaceArea());

System.out.println(myCylinder.volume());

*/

// Problem 3

Rectangle r = new Rectangle(12, 56);

System.out.println(r.getLength());

System.out.println(r.getBreadth());

Inheritance in Java

package com.company;

class Base{

public int x;

public int getX() {

return x;

}
public void setX(int x) {

System.out.println("I am in base and setting x now");

this.x = x;

public void printMe(){

System.out.println("I am a constructor");

class Derived extends Base{

public int y;

public int getY() {

return y;

public void setY(int y) {

this.y = y;

public class cwh_45_inheritance {

public static void main(String[] args) {


// Creating an Object of base class

Base b = new Base();

b.setX(4);

System.out.println(b.getX());

// Creating an object of derived class

Derived d = new Derived();

d.setY(43);

System.out.println(d.getY());

Constructors in Inheritance in Java

package com.company;

class Base1{

Base1(){

System.out.println("I am a constructor");

Base1(int x){

System.out.println("I am an overloaded constructor with value of x as: " + x);

class Derived1 extends Base1{

Derived1(){
//super(0);

System.out.println("I am a derived class constructor");

Derived1(int x, int y){

super(x);

System.out.println("I am an overloaded constructor of Derived with value of y as: " + y);

class ChildOfDerived extends Derived1{

ChildOfDerived(){

System.out.println("I am a child of derived constructor");

ChildOfDerived(int x, int y, int z){

super(x, y);

System.out.println("I am an overloaded constructor of Derived with value of z as: " + z);

public class cwh_46_constructors_in_inheritance {

public static void main(String[] args) {

// Base1 b = new Base1();

// Derived1 d = new Derived1();

// Derived1 d = new Derived1(14, 9);

// ChildOfDerived cd = new ChildOfDerived();

ChildOfDerived cd = new ChildOfDerived(12, 13, 15);


}

this and super keyword in Java

package com.company;

import javax.print.Doc;

class EkClass{

int a;

public int getA() {

return a;

EkClass(int a){

this.a = a;

public int returnone(){

return 1;

class DoClass extends EkClass{

DoClass(int c){

super(c);

System.out.println("I am a constructor");
}

public class cwh_47_this_super {

public static void main(String[] args) {

EkClass e = new EkClass(65);

DoClass d = new DoClass(5);

System.out.println(e.getA());

this keyword 
this is a way for us  to reference an object of the class which is being
created / referenced.

In this.area = 2   --> this is a reference to current object.

Super keyword 
A reference variable used to refer immediate parent class object 

 can be used to refer immediate parent class instance variable 


 can be used to invoke parent class method 
 can be used to invoke parent class constructors 

Method Overriding in Java

package com.company;

class A{

public int a;

public int harry(){

return 4;

}
public void meth2(){

System.out.println("I am method 2 of class A");

class B extends A{

@Override

public void meth2(){

System.out.println("I am method 2 of class B");

public void meth3(){

System.out.println("I am method 3 of class B");

public class cwh_48_method_overriding {

public static void main(String[] args) {

A a = new A();

a.meth2();

B b = new B();

b.meth2();

Dynamic Method Dispatch in Java

package com.company;
class Phone{

public void showTime(){

System.out.println("Time is 8 am");

public void on(){

System.out.println("Turning on Phone...");

class SmartPhone extends Phone{

public void music(){

System.out.println("Playing music...");

public void on(){

System.out.println("Turning on SmartPhone...");

public class cwh_49_dynamic_method_dispatch {

public static void main(String[] args) {

// Phone obj = new Phone(); // Allowed


// SmartPhone smobj = new SmartPhone(); // Allowed

// obj.name();

Phone obj = new SmartPhone(); // Yes it is allowed

// SmartPhone obj2 = new Phone(); // Not allowed

obj.showTime();

obj.on();

// obj.music(); Not Allowed

Create a class Game, which allows a user to play "Guess the Number"
game once.

 Game should have the following methods:


 Constructor to generate the random number
 takeUserInput() to take a user input of number
 isCorrectNumber() to detect whether the number entered by the user
is true
 getter and setter for noOfGuesses

Use properties such as noOfGuesses(int), etc to get this task done!

package com.company;
import java.util.Random;

import java.util.Scanner;

class Game{

public int number;

public int inputNumber;

public int noOfGuesses = 0;

public int getNoOfGuesses() {

return noOfGuesses;

public void setNoOfGuesses(int noOfGuesses) {

this.noOfGuesses = noOfGuesses;

Game(){

Random rand = new Random();

this.number = rand.nextInt(100);

}
void takeUserInput(){

System.out.println("Guess the number");

Scanner sc = new Scanner(System.in);

inputNumber = sc.nextInt();

boolean isCorrectNumber(){

noOfGuesses++;

if (inputNumber==number){

System.out.format("Yes you guessed it right, it was %d\nYou


guessed it in %d attempts", number, noOfGuesses);

return true;

else if(inputNumber<number){

System.out.println("Too low...");

else if(inputNumber>number){

System.out.println("Too high...");

return false;

}
}

public class cwh_50_ex3sol {

public static void main(String[] args) {

/*

Create a class Game, which allows a user to play "Guess the


Number"

game once. Game should have the following methods:

1. Constructor to generate the random number

2. takeUserInput() to take a user input of number

3. isCorrectNumber() to detect whether the number entered by


the user is true

4. getter and setter for noOfGuesses

Use properties such as noOfGuesses(int), etc to get this task done!

*/

Game g = new Game();

boolean b= false;

while(!b){

g.takeUserInput();

b = g.isCorrectNumber();

}
}

 Methods: addBook, issueBook, returnBook, showAvailableBooks


 Properties: Array to store the available books,
 Array to store the issued books

package com.company;

public class cwh_51_exercise4 {

public static void main(String[] args) {

// You have to implement a library using Java Class "Library"

// Methods: addBook, issueBook, returnBook, showAvailableBooks

// Properties: Array to store the available books,

// Array to store the issued books

//

1. Create a class circle and use inheritance to create another class


cylinder from it
2. Create a class retangle and use inheritance to create another class
cuboid . try to keep it as close to real world sceario as passible 
3. Create method for area and volume in 1 
4. create methods for area & volume in 2 also create getters and
setters 
5. What is the order of constructor execution for the following
inheritance hirarchy 
                    Base 
                                
                   Derived 1
                                    
                   Derived 2

Derived obj = new Derived 2( ); 


Which constructor(s) will be exwcutwd & in what order ?

package com.company;

class Circle{

public int radius;

Circle(){

System.out.println("I am non param of circle");

Circle(int r){

System.out.println("I am circle parameterized constructor");

this.radius = r;

public double area(){

return Math.PI*this.radius*this.radius;

}
}

class Cylinder1 extends Circle{

public int height;

Cylinder1(int r, int h){

super(r);

System.out.println("I am cylinder1 parameterized constructor");

this.height = h;

public double volume(){

return Math.PI*this.radius*this.radius*this.height;

public class cwh_52_ch10ps {

public static void main(String[] args) {

// Problem 1

// Circle objC = new Circle(12);

Cylinder1 obj = new Cylinder1(12, 4);

}
Abstract Class & Abstract Methods

package com.company;

abstract class Parent2{

public Parent2(){

System.out.println("Mai base2 ka constructor hoon");

public void sayHello(){

System.out.println("Hello");

abstract public void greet();

abstract public void greet2();

class Child2 extends Parent2{

@Override

public void greet(){

System.out.println("Good morning");

@Override
public void greet2(){

System.out.println("Good afternoon");

abstract class Child3 extends Parent2{

public void th(){

System.out.println("I am good");

public class cwh_53_abstract {

public static void main(String[] args) {

//Parent2 p = new Parent2(); -- error

Child2 c = new Child2();

//Child3 c3 = new Child3(); -- error

Interfaces in java 

interface Bicycle {

void apply brake ( int decrement );


void speed up ( int increment );

class Avon cycle implements Bicycle {

int speed = 7 ;

void apply brake ( int decrement ) {

speed = speed - decrement ;

void speedup ( int increment ){

speed = speed + increment ;

CODE :

package com.company;

interface Bicycle{

int a = 45;

void applyBrake(int decrement);

void speedUp(int increment);

}
interface HornBicycle{

int x = 45;

void blowHornK3g();

void blowHornmhn();

class AvonCycle implements Bicycle, HornBicycle{

//public int x = 5;

void blowHorn(){

System.out.println("Pee Pee Poo Poo");

public void applyBrake(int decrement){

System.out.println("Applying Brake");

public void speedUp(int increment){

System.out.println("Applying SpeedUP");

public void blowHornK3g(){

System.out.println("Kabhi khushi kabhi gum pee pee pee pee");

}
public void blowHornmhn(){

System.out.println("Main hoon naa po po po po");

public class cwh_54_interfaces {

public static void main(String[] args) {

AvonCycle cycleHarry = new AvonCycle();

cycleHarry.applyBrake(1);

// You can create properties in Interfaces

System.out.println(cycleHarry.a);

System.out.println(cycleHarry.x);

// You cannot modify the properties in Interfaces as they are final

// cycleHarry.a = 454;

//System.out.println(cycleHarry.a);

cycleHarry.blowHornK3g();

cycleHarry.blowHornmhn();

}
Java Interfaces Example & Default Methods

Default method 
package com.company;

interface MyCamera{

void takeSnap();

void recordVideo();

private void greet(){

System.out.println("Good Morning");

default void record4KVideo(){

greet();

System.out.println("Recording in 4k...");

interface MyWifi{

String[] getNetworks();

void connectToNetwork(String network);

class MyCellPhone{

void callNumber(int phoneNumber){

System.out.println("Calling "+ phoneNumber);

void pickCall(){
System.out.println("Connecting... ");

class MySmartPhone extends MyCellPhone implements MyWifi, MyCamera{

public void takeSnap(){

System.out.println("Taking snap");

public void recordVideo(){

System.out.println("Taking snap");

// public void record4KVideo(){

// System.out.println("Taking snap and recoding in 4k");

// }

public String[] getNetworks(){

System.out.println("Getting List of Networks");

String[] networkList = {"Harry", "Prashanth", "Anjali5G"};

return networkList;

public void connectToNetwork(String network){

System.out.println("Connecting to " + network);

public class cwh_57_default_methods {


public static void main(String[] args) {

MySmartPhone ms = new MySmartPhone();

ms.record4KVideo();

// ms.greet(); --> Throws an error!

String[] ar = ms.getNetworks();

for (String item: ar) {

System.out.println(item);

Inheritance in Interfaces :

public interface Interface 1 {

void meth1 ();

public interface Interface 2 extends Interface 1 {

void meth 2( );

Remember that interface cannot implement another interface only


classes can do that !

package com.company;

interface sampleInterface{
void meth1();

void meth2();

interface childSampleInterface extends sampleInterface{

void meth3();

void meth4();

class MySampleClass implements childSampleInterface{

public void meth1(){

System.out.println("meth1");

public void meth2(){

System.out.println("meth2");

public void meth3(){

System.out.println("meth3");

public void meth4(){

System.out.println("meth4");

}
}

public class cwh_58_inheritance_interfaces {

public static void main(String[] args) {

MySampleClass obj = new MySampleClass();

obj.meth1();

obj.meth2();

obj.meth3();

Polymorphism in Interfaces

package com.company;

interface MyCamera2{

void takeSnap();

void recordVideo();

private void greet(){

System.out.println("Good Morning");

default void record4KVideo(){

greet();
System.out.println("Recording in 4k...");

interface MyWifi2{

String[] getNetworks();

void connectToNetwork(String network);

class MyCellPhone2{

void callNumber(int phoneNumber){

System.out.println("Calling "+ phoneNumber);

void pickCall(){

System.out.println("Connecting... ");

class MySmartPhone2 extends MyCellPhone2 implements MyWifi2,


MyCamera2{
public void takeSnap(){

System.out.println("Taking snap");

public void recordVideo(){

System.out.println("Taking snap");

// public void record4KVideo(){

// System.out.println("Taking snap and recoding in 4k");

// }

public String[] getNetworks(){

System.out.println("Getting List of Networks");

String[] networkList = {"Harry", "Prashanth", "Anjali5G"};

return networkList;

public void connectToNetwork(String network){

System.out.println("Connecting to " + network);

public void sampleMeth(){

System.out.println("meth");

}
}

public class cwh_59_polymorphism {

public static void main(String[] args) {

MyCamera2 cam1 = new MySmartPhone2(); // This is a smartphone


but, use it as a camera

// cam1.getNetworks(); --> Not allowed

// cam1.sampleMeth(); --> Not allowed

cam1.record4KVideo();

MySmartPhone2 s = new MySmartPhone2();

s.sampleMeth();

s.recordVideo();

s.getNetworks();

s.callNumber(7979);

1. Create an abstract class pen with methods write () and refill () as


abstract methods 
2. Use the pen class from Q1 to create a concrete class fountain pen
with additional method change Nib ()
3. Create a class monkey with jump ( ) and bite ( ) methods Create a
class human whichinherits this monkey class and implements
basicanimal interface with eat ( ) and sleep methods 
4. Create a class telephone with ( ) , lift ( ) and disconnected ( ) methods
as abstract methods create another class smart telephone and
demonstrate polymorphism 
5. Demonstrate polymorphism using using monkey  class from Q3 
6. Create an interface TVremote and use it to inherit another interface
smart TVremote 
7. Create a class TV which implements TVremote interface from Q6

package com.company;

abstract class Pen{

abstract void write();

abstract void refill();

class FountainPen extends Pen{

void write(){

System.out.println("Write");

void refill(){

System.out.println("Refill");

void changeNib(){

System.out.println("Changing the nib");


}

class Monkey{

void jump(){

System.out.println("Jumping...");

void bite(){

System.out.println("Biting...");

interface BasicAnimal{

void eat();

void sleep();

class Human extends Monkey implements BasicAnimal{

void speak(){

System.out.println("Hello sir!");

}
@Override

public void eat() {

System.out.println("Eating");

@Override

public void sleep() {

System.out.println("Sleeping");

public class cwh_60_ch11ps {

public static void main(String[] args) {

// Q1 + Q2

FountainPen pen = new FountainPen();

pen.changeNib();

// Q3

Human harry = new Human();


harry.sleep();

// Q5

Monkey m1 = new Human();

m1.jump();

m1.bite();

// m1.speak(); --> Cannot use speak method because the reference is


monkey which does not have speak method

BasicAnimal lovish = new Human();

// lovish.speak(); --> error

lovish.eat();

lovish.sleep();

olution & Shoutouts!

package com.company;

class Library{

String[] books;

int no_of_books;
Library(){

this.books = new String[100];

this.no_of_books = 0;

void addBook(String book){

this.books[no_of_books] = book;

no_of_books++;

System.out.println(book+ " has been added!");

void showAvailableBooks(){

System.out.println("Available Books are:");

for (String book : this.books) {

if (book == null){

continue;

System.out.println("* " + book);

}
void issueBook(String book){

for (int i=0;i<this.books.length;i++){

if (this.books[i].equals(book)){

System.out.println("The book has been issued!");

this.books[i] = null;

return;

System.out.println("This book does not exist");

void returnBook(String book){

addBook(book);

public class cwh_61_ex4sol {

public static void main(String[] args) {

// You have to implement a library using Java Class "Library"


// Methods: addBook, issueBook, returnBook, showAvailableBooks

// Properties: Array to store the available books,

// Array to store the issued books

Library centralLibrary = new Library();

centralLibrary.addBook("Think and grow Rich");

centralLibrary.addBook("Algorithms");

centralLibrary.addBook("C++");

centralLibrary.showAvailableBooks();

centralLibrary.issueBook("C++");

centralLibrary.showAvailableBooks();

centralLibrary.returnBook("C++");

centralLibrary.showAvailableBooks();

Creating Packages in Java

package com.company;

import java.util.Scanner;
//import java.util.*;

public class cwh_65_packages {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// java.util.Scanner sc = new java.util.Scanner(System.in);

int a = sc.nextInt();

System.out.println("This is my scanner taking input as " + a);

Access Modifiers in Java

package com.company;

class C1{

public int x = 5;

protected int y =45;

int z = 6;

private int a = 78;

public void meth1(){

System.out.println(x);

System.out.println(y);
System.out.println(z);

System.out.println(a);

public class cwh_66_access_modifiers {

public static void main(String[] args) {

C1 c = new C1();

// c.meth1();

System.out.println(c.x);

System.out.println(c.y);

System.out.println(c.z);

// System.out.println(c.a);

 Creating a Custom Package

package com.company;

/*

*** WRITE THIS CODE IN NOTEPAD ***

You have to create a package named com.codewithharry.shape


This package should have individual classes for Rectangle, Square, Circle,
Cylinder, Sphere

These classes should use inheritance to properly manage the code!

Include methods like volume, surface area and getters/setters for


dimensions

*/

public class cwh_68_ex5 {

public static void main(String[] args) {

Creating a Thread by Extending Thread class

package com.company;

class MyThread1 extends Thread{

@Override

public void run(){

int i =0;

while(i<40000){

System.out.println("My Cooking Thread is Running");


System.out.println("I am happy!");

i++;

class MyThread2 extends Thread{

@Override

public void run(){

int i =0;

while(i<40000){

System.out.println("Thread 2 for Chatting with her");

System.out.println("I am sad!");

i++;

public class cwh_70 {

public static void main(String[] args) {


MyThread1 t1 = new MyThread1();

MyThread2 t2 = new MyThread2();

t1.start();

t2.start();

Creating a Java Thread Using Runnable Interface

package com.company;

class MyThreadRunnable1 implements Runnable{

public void run(){

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");


System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");


System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");


System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");


System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");


System.out.println("I am a thread 1 not a threat 1");

System.out.println("I am a thread 1 not a threat 1");

class MyThreadRunnable2 implements Runnable{

public void run(){

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");


System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

System.out.println("I am a thread 2 not a threat 2");

public class cwh_71_runnable {

public static void main(String[] args) {

MyThreadRunnable1 bullet1 = new MyThreadRunnable1();

Thread gun1 = new Thread(bullet1);

MyThreadRunnable2 bullet2 = new MyThreadRunnable2();

Thread gun2 = new Thread(bullet2);

gun1.start();

gun2.start();

Constructors from Thread class in Java

package com.company;
class MyThr extends Thread{

public MyThr(String name){

super(name);

public void run(){

int i = 34;

System.out.println("Thank you");

// while(true){

// System.out.println("I am a thread");

// }

public class cwh_73_thread_constructor {

public static void main(String[] args) {

MyThr t1 = new MyThr("Harry");

MyThr t2 = new MyThr("Ram Candr");

t1.start();

t2.start();

System.out.println("The id of the thread t is " + t1.getId());


System.out.println("The name of the thread t is " + t1.getName());

System.out.println("The id of the thread t is " + t2.getId());

System.out.println("The name of the thread t is " + t2.getName());

Java Thread Priorities

package com.company;

class MyThr1 extends Thread{

public MyThr1(String name){

super(name);

public void run(){

int i = 34;

while(true){

// System.out.println("I am a thread");

System.out.println("Thank you: " + this.getName());

}
}

public class cwh_74_thread_priorities {

public static void main(String[] args) {

// Ready Queue: T1 T2 T3 T4 T5

MyThr1 t1 = new MyThr1("Harry1");

MyThr1 t2 = new MyThr1("Harry2");

MyThr1 t3 = new MyThr1("Harry3");

MyThr1 t4 = new MyThr1("Harry4");

MyThr1 t5 = new MyThr1("Harry5 (most Important)");

t5.setPriority(Thread.MAX_PRIORITY);

t1.setPriority(Thread.MIN_PRIORITY);

t2.setPriority(Thread.MIN_PRIORITY);

t3.setPriority(Thread.MIN_PRIORITY);

t4.setPriority(Thread.MIN_PRIORITY);

t5.setPriority(Thread.MIN_PRIORITY);

t1.start();
t2.start();

t3.start();

t4.start();

t5.start();

Java Thread Methods

package com.company;

class MyNewThr1 extends Thread{

public void run(){

int i = 0;

while(true){

// System.out.println("I am a thread");

System.out.println("Thank you: ");

try {

Thread.sleep(455);

} catch (InterruptedException e) {

e.printStackTrace();
}

i++;

class MyNewThr2 extends Thread{

public void run(){

while(true){

// System.out.println("I am a thread");

System.out.println("My Thank you: ");

public class cwh_75_thread_methods {

public static void main(String[] args){

MyNewThr1 t1 = new MyNewThr1();

MyNewThr2 t2 = new MyNewThr2();


t1.start();

// try{

// t1.join();

// }

// catch(Exception e){

// System.out.println(e);

// }

t2.start();

Java Tutorial: Practice Questions on Thread

package com.company;

class Practice13 extends Thread{

public void run(){

while(true){

System.out.println("Good Morning!");

}
}

class Practice13b extends Thread{

public void run(){

// while(false){

// try {

// Thread.sleep(200);

// }

// catch (Exception e){

// System.out.println(e);

// }

// System.out.println("Welcome");

// }

public class cwh_76_practice13 {

public static void main(String[] args) {

Practice13 p1 = new Practice13();

Practice13b p2 = new Practice13b();


// p1.setPriority(6);

// p2.setPriority(9);

System.out.println(p1.getPriority());

System.out.println(p2.getPriority());

System.out.println(p2.getState());

// p1.start();

p2.start();

System.out.println(p2.getState());

System.out.println(Thread.currentThread().getState());

Errors & Exception in Java

package com.company;

public class cwh_78_errors {

public static void main(String[] args) {

int a = 5;

int b = 9;

System.out.println(a+b);

}
}

Syntax Errors, Runtime Errors & Logical Errors in Java (Demo)

package com.company;

import java.util.Scanner;

public class cwh_79_errorsdemo {

public static void main(String[] args) {

// SYNTAX ERROR DEMO

// int a = 0 // Error: no semicolon!

// b = 8; // Error: b not declared!

// LOGICAL ERROR DEMO

// Write a program to print all prime numbers between 1 to 10

System.out.println(2);

for (int i=1; i<5; i++){

System.out.println(2*i+1);

// RUNTIME ERROR
int k;

Scanner sc = new Scanner(System.in);

k = sc.nextInt();

System.out.println("Integer part of 1000 divided by k is "+ 1000/k);

Exceptions & Try-Catch Block in Java

package com.company;

public class cwh_80_try {

public static void main(String[] args) {

int a = 6000;

int b = 0;

// Without Try:

// int c = a / b;

// System.out.println("The result is " + c);

// With Try:

try {

int c = a / b;
System.out.println("The result is " + c);

catch(Exception e) {

System.out.println("We failed to divide. Reason: ");

System.out.println(e);

System.out.println("End of the program");

Handling Specific Exceptions in Java

try (

// code

Catch (To Exception e) - Handle all Exceptions of type IO Exception

// code
)

Catch (Exception e) - Handle all Exceptions of type Arithmetic


Exception

// code

Catch (Exception e) - Handle all other Exceptions

// code

ANOTHER CODE

package com.company;

import java.util.Scanner;

public class cwh_81 {

public static void main(String[] args) {

int [] marks = new int[3];

marks[0] = 7;

marks[1] = 56;
marks[2] = 6;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the array index");

int ind = sc.nextInt();

System.out.println("Enter the number you want to divide the value with");

int number = sc.nextInt();

try{

System.out.println("The value at array index entered is: " + marks[ind]);

System.out.println("The value of array-value/number is: " + marks[ind]/number);

catch (ArithmeticException e){

System.out.println("ArithmeticException occured!");

System.out.println(e);

catch (ArrayIndexOutOfBoundsException e){

System.out.println("ArrayIndexOutOfBoundsException occured!");

System.out.println(e);

catch (Exception e){

System.out.println("Some other exception occured!");

System.out.println(e);

}
Nested Try-Catch in Java

package com.company;

import java.util.Scanner;

public class cwh_82_nested_try_catch {

public static void main(String[] args) {

int [] marks = new int[3];

marks[0] = 7;

marks[1] = 56;

marks[2] = 6;

Scanner sc = new Scanner(System.in);

boolean flag = true;

while(flag) {

System.out.println("Enter the value of index");

int ind = sc.nextInt();

try {

System.out.println("Welcome to video no 82");

try {

System.out.println(marks[ind]);

flag = false;

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Sorry this index does not exist");

System.out.println("Exception in level 2");

}
} catch (Exception e) {

System.out.println("Exception in level 1");

System.out.println("Thanks for using this program");

The Exception class in Java

package com.company;

import java.util.Scanner;

class MyException extends Exception{

@Override

public String toString() {

return "I am toString()";

@Override

public String getMessage() {

return "I am getMessage()";

class MaxAgeException extends Exception{

@Override
public String toString() {

return "Age cannot be greater than 125";

@Override

public String getMessage() {

return "Make sure that the value of age entered is correct";

public class cwh_83_exception_class {

public static void main(String[] args) {

int a;

Scanner sc = new Scanner(System.in);

a = sc.nextInt();

if (a<9){

try{

// throw new MyException();

throw new ArithmeticException("This is an exception");

catch (Exception e){

System.out.println(e.getMessage());

System.out.println(e.toString());

e.printStackTrace();

System.out.println("Finished");

}
System.out.println("Yes Finished");

Throw vs Throws in Java

The Throw Keyword:


The throw keyword is used to throw an exception explicitly by the
programmer
if ( b===0 ) {
throw new ArithmeticException("Div by 0");
}
else{
return a/b ;
}
Copy

In a similar manner, we can throw user defined exceptions:                         


throw new My Exception ( "Exception throw" );
Copy
The throws Keyword
Throws java throws keyword is used to declare an exception.
package com.company;

class NegativeRadiusException extends Exception{

@Override

public String toString() {

return "Radius cannot be negative!";

}
@Override

public String getMessage() {

return "Radius cannot be negative!";

public class cwh_84_throw_throws {

public static double area(int r) throws NegativeRadiusException{

if (r<0){

throw new NegativeRadiusException();

double result = Math.PI * r * r;

return result;

public static int divide(int a, int b) throws ArithmeticException{

// Made By Harry

int result = a/b;

return result;

public static void main(String[] args) {

// Shivam - uses divide function created by Harry

try{
// int c = divide(6, 0);

// System.out.println(c);

double ar = area(6);

System.out.println(ar);

catch(Exception e){

System.out.println("Exception");

Java finally block


package com.company;

public class cwh_85_finally {

public static int greet(){

try{

int a = 50;

int b = 10;

int c = a/b;

return c;

catch(Exception e){

System.out.println(e);

finally {

System.out.println("Cleaning up resources...This is the end of this function");


}

return -1;

public static void main(String[] args) {

int k = greet();

System.out.println(k);

int a = 7;

int b = 9;

while(true){

try{

System.out.println(a/b);

catch (Exception e){

System.out.println(e);

break;

finally{

System.out.println("I am finally for value of b = " + b);

b--;

try{

System.out.println(50/3);
}

finally {

System.out.println("Yes this is finally");

 Write a java program to demonstrate syntax, logical 2 runtime errors.

2) Write a java program that prints "HaHa" during Arithmetic exception and
"HeHe" during an Illegal argument exception.

3) Write a program that allows you to given. If max retries exceed 5 print
"errors".

4) Modify program in Q3 to throw a custom Exception if max retries are


reached. 

5) Wrap the program in Q3 inside a method which throws your custom


Exception.
package com.company;

import java.util.Scanner;

public class cwh_86_ps14 {

public static void main(String[] args) {

// Problem 1

// Syntax Error - int a = 7

int age = 78;

int year_born = 2000-78; // Logical error

// System.out.println(6/0);
// Problem 2

try{

int a = 666/0;

catch (IllegalArgumentException e){

System.out.println("HeHe");

catch (ArithmeticException e){

System.out.println("Haha");

// Problem 3

boolean flag = true;

int [] marks = new int[3];

marks[0] = 7;

marks[1] = 56;

marks[2] = 6;

Scanner Sc = new Scanner(System.in);

int index;

int i = 0;

while(flag && i<5){

try {

System.out.println("Enter the value of index");

index = Sc.nextInt();
System.out.println("The value of marks[index] is " + marks[index]);

break;

catch (Exception e) {

System.out.println("Invalid Index");

i++;

if(i>=5){

System.out.println("Error");

ou have to create a custom calculator with following operations:


1. + -> Addition
2. - -> Subtraction
3. * -> Multiplication
4. / -> Division
which throws the following exceptions:
1. Invalid input Exception ex: 8 & 9
2. Cannot divide by 0 Exception
3. Max Input Exception if any of the inputs is greater than 100000
4. Max Multiplier Reached Exception - Don't allow any multiplication input
to be greater than 7000
package com.company;
public class cwh_87_ex6 {

public static void main(String[] args) {

/*

Exercise 6: You have to create a custom calculator with following operations:

1. + -> Addition

2. - -> Subtraction

3. * -> Multiplication

4. / -> Division

which throws the following exceptions:

1. Invalid input Exception ex: 8 & 9

2. Cannot divide by 0 Exception

3. Max Input Exception if any of the inputs is greater than 100000

4. Max Multiplier Reached Exception - Don't allow any multiplication input to be greater than
7000

*/

Java Collections Framework

package com.company;

import java.util.ArrayList;

import java.util.Set;

import java.util.TreeSet;

public class cwh_89_collections {

public static void main(String[] args) {


// ArrayList

// Set

// TreeSet

Collections Hierarchy in Java

How are collections available

Collections in java are available as class and interfaces Folling are few
commonly used collections in java :

 ArrayList -> For variables size collections 


 Set -> For distinct collection
 Stack-> A LIFO data structure 
 HashMap -> For strong key - value pairs

Collections class is available in java util package collection class also


provides static methods for sorting , searching etc.
package com.company;

import java.util.ArrayList;

import java.util.Set;

import java.util.TreeSet;

public class cwh_89_collections {

public static void main(String[] args) {

// ArrayList

// Set

// TreeSet
}

ArrayList in Java: Demo & Methods

package com.company;

import java.lang.reflect.Array;

import java.util.*;

public class cwh_91_arraylist {

public static void main(String[] args) {

ArrayList<Integer> l1 = new ArrayList<>();

ArrayList<Integer> l2 = new ArrayList<>(5);

l2.add(15);

l2.add(18);

l2.add(19);

l1.add(6);

l1.add(7);

l1.add(4);

l1.add(6);

l1.add(0, 5);

l1.add(0, 1);

l1.addAll(0, l2);

System.out.println(l1.contains(27));

System.out.println(l1.indexOf(6));
System.out.println(l1.lastIndexOf(6));

//l1.clear();

l1.set(1, 566);

for(int i=0; i<l1.size(); i++){

System.out.print(l1.get(i));

System.out.print(", ");

LinkedList in Java: Demo & Methods

package com.company;

import java.util.*;

public class cwh_92_linkedlist {

public static void main(String[] args) {

LinkedList<Integer> l1 = new LinkedList<>();

LinkedList<Integer> l2 = new LinkedList<>();

l2.add(15);

l2.add(18);

l2.add(19);

l1.add(6);

l1.add(7);

l1.add(4);
l1.add(6);

l1.add(0, 5);

l1.add(0, 1);

l1.addAll(0, l2);

l1.addLast(676);

l1.addFirst(788);

System.out.println(l1.contains(27));

System.out.println(l1.indexOf(6));

System.out.println(l1.lastIndexOf(6));

//l1.clear();

l1.set(1, 566);

for(int i=0; i<l1.size(); i++){

System.out.print(l1.get(i));

System.out.print(", ");

ArrayDeque in Java

package com.company;

import java.util.ArrayDeque;

public class cwh_93_arraydeque {

public static void main(String[] args) {

ArrayDeque<Integer> ad1 = new ArrayDeque<>();


ad1.add(6);

ad1.add(56);

ad1.add(9);

ad1.addFirst(5);

System.out.println(ad1.getFirst());

System.out.println(ad1.getLast());

HashSet in Java

package com.company;

import java.util.HashSet;

public class cwh_95_set {

public static void main(String[] args) {

HashSet<Integer> myHashSet = new HashSet<>(6, 0.5f);

myHashSet.add(6);

myHashSet.add(8);

myHashSet.add(3);

myHashSet.add(11);

myHashSet.add(11);

System.out.println(myHashSet);

Date and Time in Java


Date & Time in Java

java time -> package for date & time in java from java onwards

Before java 8, java util package used to hold the date time class now these
classes are deprecated 

How java stores a Date?

Date in java is stored in the form of a long numer. This long number holds
the number of miliseconds passed since 1 jan 1970

Java assumes that 1900 is the start year which means it calculates years
passed since 1900 whenever We ask it for years passed 

System current Time Millis () returns no of sound passed Once no. of ms


are calculated, we can calculate minutes, seconds & years passed 

Quick quiz : Is it save to store the no. of ms in a variable of type long? 


package com.company;

public class cwh_96_date {

public static void main(String[] args) {

System.out.println(System.currentTimeMillis()/1000/3600/24/365);

The Date Class in Java

package com.company;

import java.util.Date;

public class cwh_97_date_class {

public static void main(String[] args) {


// System.out.println(Long.MAX_VALUE);

// System.out.println(System.currentTimeMillis());

Date d = new Date();

System.out.println(d);

System.out.println(d.getTime());

System.out.println(d.getDate());

System.out.println(d.getSeconds());

System.out.println(d.getYear());

GregorianCalendar class & TimeZone in java

package com.company;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.TimeZone;

public class cwh_99_gregorian {

public static void main(String[] args) {

Calendar c = Calendar.getInstance();

System.out.println(c.getTime());

System.out.println(c.get(Calendar.DATE));

System.out.println(c.get(Calendar.SECOND));

System.out.println(c.get(Calendar.HOUR));

System.out.println(c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" +


c.get(Calendar.SECOND));
GregorianCalendar cal = new GregorianCalendar();

System.out.println(cal.isLeapYear(2018));

System.out.println(TimeZone.getAvailableIDs()[0]);

System.out.println(TimeZone.getAvailableIDs()[1]);

System.out.println(TimeZone.getAvailableIDs()[2]);

java.time API - Classes & Methods

package com.company;

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.time.LocalTime;

public class cwh_100_java_time {

public static void main(String[] args) {

LocalDate d = LocalDate.now();

System.out.println(d);

LocalTime t = LocalTime.now();

System.out.println(t);

LocalDateTime dt = LocalDateTime.now();

System.out.println(dt);

}
}

DateTimeFormatter in Java

package com.company;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

public class cwh_101_datetimeformatter {

public static void main(String[] args) {

LocalDateTime dt = LocalDateTime.now(); // This is the date

System.out.println(dt);

DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MM/yyyy -- E H:m a"); // This is the


format

DateTimeFormatter df2 = DateTimeFormatter.ISO_LOCAL_DATE;

String myDate = dt.format(df); // Creating date string using date and format

System.out.println(myDate);

Advanced Java Practice Set

package com.company;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import java.util.*;

public class cwh_102_ps {

public static void main(String[] args) {

// PS Q1

ArrayList ar = new ArrayList();

ar.add("Student 1");

ar.add("Student 2");

ar.add("Student 3");

ar.add("Student 4");

ar.add("Student 5");

ar.add("Student 6");

ar.add("Student 7");

ar.add("Student 8");

ar.add("Student 9");

ar.add("Student 10");

for(Object o: ar){

System.out.println(o);

// PS Q2

Date d = new Date();

System.out.println(d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());


// PS Q3

Calendar c = Calendar.getInstance();

System.out.println(c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" +


c.get(Calendar.SECOND));

// PS Q4

LocalDateTime dt = LocalDateTime.now(); // This is the date

DateTimeFormatter df = DateTimeFormatter.ofPattern("H:m:s"); // This is the format

String myDate = dt.format(df); // Creating date string using date and format

System.out.println(myDate);

// PS Q5

HashSet<Integer> s = new HashSet();

s.add(5);

s.add(6);

s.add(46);

s.add(60);

s.add(9);

s.add(6);

System.out.println(s);

Java Exercise 6: Solution | Custom Calculator

package com.company;

class InvalidInputException extends Exception{


@Override

public String toString() {

return "Cannot add 8 and 9";

@Override

public String getMessage() {

return "I am getMessage()";

class MaxInputException extends Exception{

@Override

public String toString() {

return "Input cant be greater than 100000";

@Override

public String getMessage() {

return "I am getMessage()";

class CannotDivideByZeroException extends Exception{

@Override

public String toString() {


return "Cannot divide by 0";

@Override

public String getMessage() {

return "I am getMessage()";

class MaxMultiplyInputException extends Exception{

@Override

public String toString() {

return "Input cant be greater than 7000 while multiplying";

@Override

public String getMessage() {

return "I am getMessage()";

class CustomCalculator {

double add(double a, double b) throws InvalidInputException, MaxInputException{

if(a>100000 || b>100000){

throw new MaxInputException();

}
if(a==8 || b==9) {

throw new InvalidInputException();

return a + b;

double subtract(double a, double b) throws MaxInputException{

if(a>100000 || b>100000){

throw new MaxInputException();

return a - b;

double multiply(double a, double b)throws MaxInputException, MaxMultiplyInputException{

if(a>100000 || b>100000){

throw new MaxInputException();

else if(a>7000 || b>7000){

throw new MaxMultiplyInputException();

return a * b;

double divide(double a, double b) throws CannotDivideByZeroException, MaxInputException{

if(a>100000 || b>100000){

throw new MaxInputException();

if(b==0){
throw new CannotDivideByZeroException();

return a / b;

public class cwh_103_ex6sol {

public static void main(String[] args) throws InvalidInputException,

CannotDivideByZeroException, MaxInputException, MaxMultiplyInputException {

CustomCalculator c = new CustomCalculator();

// c.add(8, 9);

// c.divide(6, 0);

// c.divide(600000000, 40);

c.multiply(5, 9888);

/*

Exercise 6: You have to create a custom calculator with following operations:

1. + -> Addition

2. - -> Subtraction

3. * -> Multiplication

4. / -> Division

which throws the following exceptions:

1. Invalid input Exception ex: 8 & 9

2. Cannot divide by 0 Exception

3. Max Input Exception if any of the inputs is greater than 100000

4. Max Multiplier Reached Exception - Don't allow any multiplication input to be greater than
7000
*/

Java Exercise 7: Library Management System in Java

package com.company;

public class cwh_104_ex7 {

public static void main(String[] args) {

/*

Create a library management system which is capable of issuing books to the students.

Book should have info like:

1. Book name

2. Book Author

3. Issued to

4. Issued on

User should be able to add books, return issued books, issue books

Assume that all the users are registered with their names in the central database

*/

Generating our own JavaDocs for our Package

package com.company;

public class cwh_105_javadoc {


static void foo(){

System.out.println("Good Morning bro!");

static void foo(int a){

System.out.println("Good morning " + a + " bro!");

static void foo(int a, int b){

System.out.println("Good morning " + a + " bro!");

System.out.println("Good morning " + b + " bro!");

static void foo(int a, int b, int c){

System.out.println("Good morning " + a + " bro!");

System.out.println("Good morning " + b + " bro!");

static void change(int a){

a = 98;

static void change2(int [] arr){

arr[0] = 98;

}
static void tellJoke(){

System.out.println("I invented a new word!\n" +

"Plagiarism!");

public static void main(String[] args) {

// tellJoke();

// Case 1: Changing the Integer

//int x = 45;

//change(x);

//System.out.println("The value of x after running change is: " + x);

// Case 1: Changing the Array

// int [] marks = {52, 73, 77, 89, 98, 94};

// change2(marks);

// System.out.println("The value of x after running change is: " + marks[0]);

// Method Overloading

foo();

foo(3000);

foo(3000, 4000);

// Arguments are actual!


}

Javadocs: Tags for Documenting Classes

package com.company;

/**

* This class is to demonstrate what javadoc is and how it is used in the java industry

* This is <i>italic</i> word<p>this is a new paragraph</p>

* @author Harry (CodeWithHarry)

* @version 0.1

* @since 2002

* @see <a href="https://docs.oracle.com/en/java/javase/14/docs/api/index.html"


target="_blank">Java Docs</a>

*/

public class cwh_106_javadoc {

public void add(int a, int b){

System.out.println("The sum is: " + a+b);

public static void main(String[] args) {

System.out.println("This is my main method");

}
}

Javadocs: Method Tags For Generating java Documentation

package com.company;

/**

* This is a good class

*/

public class cwh_107_method_tags {

/**

* @param args These are arguments supplied to the command line

*/

public static void main(String[] args) {

System.out.println("I am main method");

/**

* Hello this is a method and this is the most beautiful method of this class

* @param i This is the first number to add

* @param j This is the second number to add

* @return Sum of two numbers as an integer

* @throws Exception if i is 0

* @deprecated This method is deprecated please use + Operator

*/

public int add(int i, int j) throws Exception{


if(i==0){

throw new Exception();

int c;

c= i+ j;

return c;

Annotations in Java

package com.company;

@FunctionalInterface

interface myFunctionalInteface{

void thisMethod();

// void thisMethod2();

class NewPhone extends Phone{

@Override

public void showTime(){

System.out.println("Time is 8PM");

@Deprecated

public int sum(int a, int b){

return a+b;
}

public class cwh_108_java_annotations {

@SuppressWarnings("deprecation")

public static void main(String[] args) {

NewPhone phone = new NewPhone();

phone.showTime();

phone.sum(5, 6);

Java Generics

package com.company;

import java.util.ArrayList;

import java.util.Scanner;

class MyGeneric<T1, T2>{

int val;

private T1 t1;

private T2 t2;

public MyGeneric(int val, T1 t1, T2 t2) {

this.val = val;

this.t1 = t1;
this.t2= t2;

public T2 getT2() {

return t2;

public void setT2(T2 t2) {

this.t2 = t2;

public int getVal() {

return val;

public void setVal(int val) {

this.val = val;

public T1 getT1() {

return t1;

public void setT1(T1 t1) {

this.t1 = t1;
}

public class cwh_110_generics {

public static void main(String[] args) {

ArrayList<Integer> arrayList = new ArrayList();

// ArrayList<int> arrayList = new ArrayList(); -- this will produce an error

// arrayList.add("str1");

arrayList.add(54);

arrayList.add(643);

// arrayList.add(new Scanner(System.in));

int a = (int) arrayList.get(0);

// System.out.println(a);

MyGeneric<String, Integer> g1 = new MyGeneric(23, "MyString is my string ", 45);

String str = g1.getT1();

Integer int1 = g1.getT2();

System.out.println(str + int1);

File Handling in Java

package com.company;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;
import java.io.IOException;

import java.util.Scanner;

public class cwh_111_file {

public static void main(String[] args) {

// Code to create a new file

/*

File myFile = new File("cwh111file.txt");

try {

myFile.createNewFile();

} catch (IOException e) {

System.out.println("Unable to create this file");

e.printStackTrace();

// Code to write to a file

try {

FileWriter fileWriter = new FileWriter("cwh111file.txt");

fileWriter.write("This is our first file from this java course\nOkay now bye");

fileWriter.close();

} catch (IOException e) {

e.printStackTrace();

}
// Reading a file

File myFile = new File("cwh111file.txt");

try {

Scanner sc = new Scanner(myFile);

while(sc.hasNextLine()){

String line = sc.nextLine();

System.out.println(line);

sc.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

*/

// Deleting a file

File myFile = new File("cwh111file.txt");

if(myFile.delete()){

System.out.println("I have deleted: " + myFile.getName());

else{

System.out.println("Some problem occurred while deleting the file");

}
Advanced Java 2 - Practice Set

package com.company;

import java.io.FileWriter;

import java.io.IOException;

class MyDeprecated{

@Deprecated

void meth1(){

System.out.println("I am method 1");

interface MyInt{

void display();

public class cwh_112 {

public static void main(String[] args) {

// MyDeprecated d = new MyDeprecated();

// d.meth1();

// MyInt i = () -> System.out.println("I am display");

int i = 19;
String table = "";

for (int j = 0; j < 10; j++) {

table += i + "X"+(j+1) + "=" + i*(j+1);

table += "\n";

try {

FileWriter fileWriter = new FileWriter("MultiplicationTable.txt");

fileWriter.write(table);

fileWriter.close();

} catch (IOException e) {

e.printStackTrace();

Exercise 7: Solutions + Shoutouts

package com.company;

import java.util.ArrayList;

/*

Create a library management system which is capable of issuing books to the students.

Book should have info like:

1. Book name

2. Book Author

3. Issued to
4. Issued on

User should be able to add books, return issued books, issue books

Assume that all the users are registered with their names in the central database

*/

class Book{

public String name, author;

public Book(String name, String author) {

this.name = name;

this.author = author;

@Override

public String toString() {

return "Book{" +

"name='" + name + '\'' +

", author='" + author + '\'' +

'}';

class MyLibrary{

public ArrayList<Book> books;

public MyLibrary(ArrayList<Book> books) {

this.books = books;
}

public void addBook(Book book){

System.out.println("The book has been added to the library");

this.books.add(book);

public void issueBook(Book book, String issued_to){

System.out.println("The book has been issued from the library to " + issued_to);

this.books.remove(book);

public void returnBook(Book b){

System.out.println("The book has been returned");

this.books.add(b);

public class cwh_113_ex7sol {

public static void main(String[] args) {

// Exercise 7 Solution

ArrayList<Book> bk = new ArrayList<>();

Book b1 = new Book("Algorithms", "CLRS");

bk.add(b1);

Book b2 = new Book("Algorithms2", "CLRS2");

bk.add(b2);
Book b3 = new Book("Algorithms3", "CLRS3");

bk.add(b3);

Book b4 = new Book("Algorithms4", "CLRS4");

bk.add(b4);

MyLibrary l = new MyLibrary(bk);

l.addBook(new Book("algo4", "myAuthor"));

System.out.println(l.books);

l.issueBook(b3, "Harry");

System.out.println(l.books);

Exercise 7: Solutions + Shoutouts

package com.company;

import java.util.ArrayList;

/*

Create a library management system which is capable of issuing books to the students.

Book should have info like:

1. Book name

2. Book Author

3. Issued to

4. Issued on
User should be able to add books, return issued books, issue books

Assume that all the users are registered with their names in the central database

*/

class Book{

public String name, author;

public Book(String name, String author) {

this.name = name;

this.author = author;

@Override

public String toString() {

return "Book{" +

"name='" + name + '\'' +

", author='" + author + '\'' +

'}';

class MyLibrary{

public ArrayList<Book> books;

public MyLibrary(ArrayList<Book> books) {

this.books = books;

}
public void addBook(Book book){

System.out.println("The book has been added to the library");

this.books.add(book);

public void issueBook(Book book, String issued_to){

System.out.println("The book has been issued from the library to " + issued_to);

this.books.remove(book);

public void returnBook(Book b){

System.out.println("The book has been returned");

this.books.add(b);

public class cwh_113_ex7sol {

public static void main(String[] args) {

// Exercise 7 Solution

ArrayList<Book> bk = new ArrayList<>();

Book b1 = new Book("Algorithms", "CLRS");

bk.add(b1);

Book b2 = new Book("Algorithms2", "CLRS2");

bk.add(b2);

Book b3 = new Book("Algorithms3", "CLRS3");


bk.add(b3);

Book b4 = new Book("Algorithms4", "CLRS4");

bk.add(b4);

MyLibrary l = new MyLibrary(bk);

l.addBook(new Book("algo4", "myAuthor"));

System.out.println(l.books);

l.issueBook(b3, "Harry");

System.out.println(l.books);

You might also like