Oops Text
Oops Text
import java.util.*;
// METHOD
// A method is a block of code which only runs when it is called.
// You can pass data, known as parameters, into a method.
// Methods are used to perform certain actions, and they are also known as
functions.
// Why use methods? To reuse code: define the code once, and use it many times.
// METHOD OVERLOADING
// Multiple methods can have the same name with different parameters
//public class oops{
// static int plusMethod(int x, int y) {
// return x + y;
// }
//
// static String plusMethod(String x, String y) {
// return (x + y);
// }
//
// public static void main(String[] args) {
// int myNum1 = plusMethod(8, 5);
// String myNum2 = plusMethod("Hello ", "World");
// System.out.println("int: " + myNum1);
// System.out.println("double: " + myNum2);
// System.out.println(12 +67 +"34" +45+ 78);
// }
//}
// JAVA RECURSION
// Recursion is the technique of making a function call itself
//sum(10, 20)
//20 + sum(10, 19)
//20 + ( 19 + sum(10,18) )
//20 + ( 19 + ( 18 + sum(10, 17) ) )
//20 + ( 19 + ( 18 + ( 17 + sum(10, 16) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + sum(10, 15) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + sum(10, 14) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + sum(10, 13) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + sum(10, 12) ) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + ( 12 + sum(10,
11) ) ) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + ( 12 + ( 11 + sum(10, 10) )
) ) ) ) ) ) ) )
//20 + ( 19 + ( 18 + ( 17 + ( 16 + ( 15 + ( 14 + ( 13 + ( 12 + ( 11 + ( Conditon
false return 10 ) ) ) ) ) ) ) ) ) ) )
//Everything in Java is associated with classes and objects, along with its
attributes and methods.
//For example: in real life, a car is an object. The car has attributes, such as
weight and color,
//and methods, such as drive and brake.
//A Class is like an object constructor, or a "blueprint" for creating objects.
//class A{
//// private int a; // Doesn't Work
// int a;
//}
//public class oops{
// public static void main(String agrs[]) {
// A ob = new A();
// ob.a = 10;
// System.out.println(ob.a);
// }
//}
//class A{
// void get() {
// System.out.println("Beant Technology");
// }
// void put() {
// System.out.println("Welcome to Besant Terchnology");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// ob.get();
// ob.put();
// }
//}
//
17-10-2024
// CONSTRUCTOR
// Constructor is a special method this used to initialize objects.
// The constructor is called when an object of a class is created.
// Class name and Method name should be same in constructor and it cannot have a
return type
// Constructor types:
// No-Arg Constructor - a constructor that does not accept any arguments
// Parameterized constructor - a constructor that accepts arguments
// Default Constructor - a constructor that is automatically created by the Java
compiler
// No-Arg Constructor
//class A {
// A(){
// System.out.println("Constructor Called");
// }
//}
//class B{
// B(){
// System.out.println("Hello");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A myobj = new A();
// B myobj1 = new B();
// }
//}
// Parameterized constructor
//class A {
// A(String a){
// System.out.println("Constructor Called " + a);
// }
//}
//class B{
// B(int a, int b){
// if(a > b) {
// System.out.println("A is Greater than B");
// }
// else {
// System.out.println("A is Less than B");
// }
// }
//}
//public class oops{
// public static void main(String args[]) {
// A myobj = new A("Successfully");
// B myobj1 = new B(20, 30);
// }
//}
//class A {
// A(String a){
// System.out.println("Constructor Called " + a);
// }
//}
//class B{
// B(int a, int b){
// if(a > b) {
// System.out.println("A is Greater than B");
// }
// else {
// System.out.println("A is Less than B");
// }
// }
//}
//public class oops{
// public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// System.out.println("Sucessfully / Not");
// String a = in.next();
// A myobj = new A(a);
// System.out.println("Num1 :");
// int b = in.nextInt();
// System.out.println("Num2 :");
// int c = in.nextInt();
// B myobj1 = new B(b,c);
// }
//}
// Default Constructor
//class oops {
//
// String language;
//
// // constructor with no parameter
// oops() {
// this.language = "Java";
// }
//
// // constructor with a single parameter
// oops(String language) {
// this.language = language;
// }
//
// public void getName() {
// System.out.println("Programming Language: " + this.language);
// }
//
// public static void main(String[] args) {
//
// // call constructor with no parameter
// oops obj1 = new oops();
//
// // call constructor with a single parameter
// oops obj2 = new oops("Python");
//
// obj1.getName();
// obj2.getName();
// }
//}
//
21-10-2024
// INHERITANCE
// OOP that allows us to create a new class from an existing class.
// The new class that is created is known as subclass (child or derived class)
// and the existing class from where the child class is derived is known as
superclass (parent or base class).
// The extends keyword is used to perform inheritance in Java.
//class A{ // Super (or) Parent (or) Base Class
// void put() {
// System.out.println("Hello");
// }
//}
//
//class B extends A{ // Sub (or) Child (or) Derived Class
// void get() {
// System.out.println("Hii..");
// }
//}
//public class oops{
// public static void main(String args[]) {
// B obj = new B();
// obj.put();
// obj.get();
// }
//}
////Base class
//class Person {
// String name = "John";
// int age =17;
// String city = "Delhi";
// public void show()
// {
// System.out.println("Student inheriting properties from Person:\n");
// }
//}
////child class
//class Student extends Person {
// // defining additional properties to child class
// int marks = 78;
// String tutorial = "Besant Technology, Velachery";
//}
//
//public class oops{
// public static void main(String args[]) {
// Student obj = new Student();
// obj.show();
//
// System.out.println("Name of the student is: " + obj.name);
// System.out.println("Age of the student is: " + obj.age);
// System.out.println("Student lives in: " + obj.city);
// System.out.println("Student learns from: " + obj.tutorial);
// System.out.println("Marks obtained by the student is: " + obj.marks);
// }
//}
//
// 1.Single Inheritance
// In single inheritance, there is a single child class that inherits properties
from one parent class.
// Super Class : Which class gives the properties to other class
// Sub Class : Which class get the properties from other class
////Base class
//class Person {
// String name="John";
// public void show(){
// System.out.println("Student inheriting properties from Person");
// }
//}
////child class
//class Student extends Person {
// // defining additional properties to child class
// String course = "Java Course";
// public void show1() {
// System.out.println("I am a Student who belongs to Person class");
// }
//}
//public class oops{
// public static void main(String args[]){
// Student obj = new Student();
// obj.show();
// obj.show1();
// System.out.println("Name of student: " +obj.name);
// System.out.println("Course opted by the student: " +obj.course);
// }
//}
////Base class
//class Person{
// public void show(){
// System.out.println("Student inheriting properties from Person");
// }
//}
//class Student extends Person{
// public void show1(){
// System.out.println("I am a Student who belongs to Person class");
// }
//}
////child class
//class EngineeringStudent extends Student
//{
// // defining additional properties to the child class
// public void show2(){
// System.out.println("Engineering Student inheriting properties from
Student");
// }
//}
//public class oops{
// public static void main(String args[]){
// EngineeringStudent obj = new EngineeringStudent();
// obj.show();
// obj.show1();
// obj.show2();
// }
//}
//Method OverRiding
// SUPER CLASS AND SUB CLASS HAVE SAME METHOD NAME AND PARAMETER
//class Base{
// void Str() {
// System.out.println("Hello");
// }
//}
//class Derived extends Base{
// void str() {
// System.out.println("Hii.");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Derived ob = new Derived();
// ob.str();
// }
//}
//class Base{
// void Str() {
// System.out.println("Hello");
// }
//}
//class Derived extends Base{
// void str() {
// super.Str();
// System.out.println("Hii.");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Derived ob = new Derived();
// ob.str();
// }
//}
// 3.Hierarchical Inheritance
// In Hierarchical Inheritance, one class acts as a superclass (base class) for
more than one subclass.
// More than one subclass can inherit the features of a base class.
////Base class
//class Person {
// public void show() {
// System.out.println("I am a Person");
// }
//}
//
////child class1
//class Student extends Person {
// public void show1() {
// System.out.println("I am a Student who is Person ");
// }
//}
//
////child class2
//class Teacher extends Person {
//// defining additional properties to the child class
// public void show2() {
// System.out.println("I am a Teacher who is a Person");
// }
//}
////child class3
//class Doctor extends Person {
//// defining additional properties to the child class
// public void show3() {
// System.out.println("I am a Doctor who is a Person");
// }
//}
//public class oops {
// public static void main(String args[]) {
// Teacher teacher = new Teacher();
// Student student = new Student();
// Doctor doctor = new Doctor();
// student.show();
// student.show1();
// teacher.show();
// teacher.show2();
// doctor.show();
// doctor.show3();
// }
//}
////
22-10-2024
//We have already discussed that Java does not support multiple inheritances with
classes.
//We can achieve multiple inheritances only with the help of Interfaces.
// 5. Which class implements interface that can define the interface method
//class B implements A{
// public void get(){
// System.out.println("Hello");
// }
//}
// interface FirstInterface {
// public void myMethod(); // interface method
// }
//
// interface SecondInterface {
// public void myOtherMethod(); // interface method
// }
//
// class DemoClass implements FirstInterface, SecondInterface {
// public void myMethod() {
// System.out.println("Some text..");
// }
// public void myOtherMethod() {
// System.out.println("Some other text...");
// }
// }
//
// public class oops {
// public static void main(String[] args) {
// DemoClass myObj = new DemoClass();
// myObj.myMethod();
// myObj.myOtherMethod();
// }
// }
//// EXAMPLES
////A simple interface
//interface In1 {
//
// // public, static and final
// final int a = 10;
//
// // public and abstract
// void display();
//}
//
////A class that implements the interface.
//class TestClass implements In1 {
//
// // Implementing the capabilities of
// // interface.
// public void display(){
// System.out.println("Hello");
// }
//}
//public class oops{
// // Driver Code
// public static void main(String[] args)
// {
// TestClass t = new TestClass();
// t.display();
// System.out.println(t.a);
// }
//}
//interface Vehicle {
//
// // all are the abstract methods.
// void changeGear(int a);
// void speedUp(int a);
// void applyBrakes(int a);
//}
//
//class Bicycle implements Vehicle{
// int speed;
// int gear;
//
// // to change gear
// public void changeGear(int newGear){
// gear = newGear;
// }
//
// // to increase speed
// public void speedUp(int increment){
// speed = speed + increment;
// }
//
// // to decrease speed
// public void applyBrakes(int decrement){
// speed = speed - decrement;
// }
//
// public void printStates() {
// System.out.println("speed: " + speed + " gear: " + gear);
// }
//}
//
//class Bike implements Vehicle {
// int speed;
// int gear;
//
// // to change gear
// public void changeGear(int newGear){
// gear = newGear;
// }
//
// // to increase speed
// public void speedUp(int increment) {
// speed = speed + increment;
// }
//
// // to decrease speed
// public void applyBrakes(int decrement){
// speed = speed - decrement;
// }
//
// public void printStates() {
// System.out.println("speed: " + speed + " gear: " + gear);
// }
//}
//public class oops {
//
// public static void main (String[] args) {
//
// // creating an instance of Bicycle
// // doing some operations
// Bicycle bicycle = new Bicycle();
// bicycle.changeGear(2);
// bicycle.speedUp(3);
// bicycle.applyBrakes(1);
//
// System.out.println("Bicycle present state :");
// bicycle.printStates();
//
// // creating instance of the bike.
// Bike bike = new Bike();
// bike.changeGear(1);
// bike.speedUp(4);
// bike.applyBrakes(3);
//
// System.out.println("Bike present state :");
// bike.printStates();
// }
//}
////Interface 1
//interface API {
// // Default method
// default void show(){
// // Print statement
// System.out.println("Default API");
// }
//}
//
////Interface 2
////Extending the above interface
//interface Interface1 extends API {
// // Abstract method
// void display();
//}
//
////Interface 3
////Extending the above interface
//interface Interface2 extends API {
// // Abstract method
// void print();
//}
//
////Main class
////Implementation class code
//class TestClass implements Interface1, Interface2 {
// // Overriding the abstract method from Interface1
// public void display(){
// System.out.println("Display from Interface1");
// }
// // Overriding the abstract method from Interface2
// public void print(){
// System.out.println("Print from Interface2");
// }
//}
//public class oops{
// // Main driver method
// public static void main(String args[]) {
// // Creating object of this class
// // in main() method
// TestClass d = new TestClass();
//
// // Now calling the methods from both the interfaces
// d.show(); // Default method from API
// d.display(); // Overridden method from Interface1
// d.print(); // Overridden method from Interface2
// }
//}
//interface A {
// void method1();
// void method2();
//}
//// B now includes method1 and method2
//interface B {
// void method3();
//}
//// the class must implement all method of A and B.
//class gfg implements B , A{
// public void method1(){
// System.out.println("Method 1");
// }
// public void method2(){
// System.out.println("Method 2");
// }
// public void method3(){
// System.out.println("Method 3");
// }
//}
//public class oops{
// public static void main(String args[]) {
// gfg ob = new gfg();
// ob.method1();
// ob.method2();
// ob.method3();
// }
//}
//interface Student {
// public void data();
//}
//class avi implements Student {
// public void data () {
// String name="avinash";
// int rollno=68;
// System.out.println(name);
// System.out.println(rollno);
// }
//}
//public class oops {
// public static void main (String args []) {
// avi h= new avi();
// h.data();
// }
//}
////base interface1
//interface Moveable {
// public void run();
//}
//
////base interface2
//interface Speakable {
// public void speak();
//}
//
////child interface inheriting two base interfaces
//interface Ability extends Moveable, Speakable {
// public void show();
//}
//
//class Person implements Ability {
// public void run() {
// System.out.println("I can run !!");
// }
// public void speak(){
// System.out.println("I can speak !!");
// }
// public void show() {
// System.out.println("I am a person, I can speak and run !!");
// }
//}
//
//public class oops {
// public static void main(String[] args) {
// Person obj = new Person();
// obj.run();
// obj.speak();
// obj.show();
// }
//}
// Static Variable
// Static Method
//class A{
// static void get() {
// System.out.println("hai");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A.get();
// }
//}
// Anonymus Object
//class A{
// A(){
// System.out.println("Hai");
// }
//}
//public class oops{
// oops(String a){
// System.out.println("Hi " + a);
// }
// public static void main(String args[]) {
//// oops ob = new oops("Vijay");
// new oops("vijay"); // Anonymus Object
// }
//}
//class A{
// A(){
// System.out.println("Hai Building");
// }
// A(String a){
// System.out.println(a);
// }
//}
//class B extends A{
// B(){
// System.out.println("Hai Building");
// }
// B(String a){
// System.out.println(a);
// }
//}
//public class oops{
// public static void main(String args[]) {
// B ob = new B("Besant");
// }
//}
//
23-10-2024
// Final Keyword
// we don't reassign the value in final variable
//class a{
// final void get() {
// System.out.println("hai");
// }
//}
//class b extends a{
//// void get() { // does't Override
//// System.out.println("hello");
//// }
//}
//public class oops{
// public static void main(String args[]) {
//// final int a = 10; // FINAL VARIABLE
//// a = 30;
//// System.out.println(a);
//
// b ob = new b();;
// ob.get();
// }
//}
//It will shows error because every method should be in same return type.
//class Base{
// void get(String a) {
// System.out.println("hai");
// }
//}
//class Derived extends Base{
// int get(String a) {
// System.out.println("hello");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Derived ob = new Derived();
// ob.get();
// }
//}
//doesn't print the put() method because the object is created only the 'a' class
//class Base{
// void get() {
// System.out.println("hai");
// }
//}
//class Derived extends Base{
// void put() { // does't Override
// System.out.println("hello");
// }
//}
//public class oops{
// public static void main(String args[]) {
// Base ob = new Derived();;
// ob.get();
//// ob.put();
// }
//}
//class A{
// // Marker class
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
//
// }
//}
// Incase we use return type in any class we should print in the method only in the
main method
//class A{
// int get() {
// return 1;
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// System.out.println(ob.get());
// }
//}
// We create a object in class and call the method and parameter have any integer
or string or double
// incase we dont have the parameter type it takes the object method
//class A{
// void get(int a) {
// System.out.println("Integer Print");
// }
// void get(Object a) {
// System.out.println("Object Print");
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// ob.get(123);
// }
//}
//class A{
// void get(Object a, Object b) {
//// System.out.println(a + b); // Doesn't print
// }
//}
//public class oops{
// public static void main(String args[]) {
// A ob = new A();
// ob.get(10, 20);
// }
//}
// TYPE OF EXCEPTION
// 1.Arithmetic Exception
// This exception occurs when an illegal arithmetic operation is performed, such as
dividing by zero
// 2.ArrayIndexOutOfBoundsException
// This exception is thrown when trying to access an array with an invalid index,
// either negative or greater than or equal to the size of the array.
// 3.NegativeArraySizeExcepption
// NegativeArraySizeException is a runtime exception in Java that occurs when an
application tries to create an array with a negative size.
// 4.StringIndexOutOfBoundException
// It is thrown by String class methods to indicate that an index is either
negative or greater than the size of the string
// 5.InputMismatchException
// This exception often arises when using Scanner to read input. For example, if
you expect an integer but the user inputs a string or a float.
// 6.NumberFormatException
// NumberFormatException occurs when trying to convert a string to a number and the
string is not in the correct format.
// 7.NullPointerException
// EXAMPLES IN EXCEPTION
// 1.
public class oops{
static void divideNumbers(int num, int den) {
int result = 0;
try {
result = num / den;
System.out.println("Program Ended , Result : " + result);
if(result == 0) {
throw new ArithmeticException("Denominator should be
greater than Zero");
}
}
catch(ArithmeticException e) {
System.err.println("Cannot Divide By Zero");
}
}
public static void main(String args[]) {
divideNumbers(10,0);
divideNumbers(10,2);
Scanner in = new Scanner(System.in);
// oops ob = new oops();
int n1 = in.nextInt();
int n2 = in.nextInt();
divideNumbers(n1, n2);
System.err.println("Cannot Divide By Zero");
System.err.println("Cannot Divide By Zero");
System.err.println("Cannot Divide By Zero");
}
}
// 2.
//class invalidAgeException extends Exception{
// invalidAgeException(String s){
// super(s);
// }
//}
//class AgeValidator{
// static void CheckAge(int age) {
// try {
// if(age < 0 ||age >150) {
// throw new invalidAgeException("Age should not be less than
0 and greater than 150");
// }
// else {
// System.out.println("Age is valid");
// }
// }
// catch(invalidAgeException e){
// System.err.println(e);
// }
// }
//}
//public class oops{
// public static void main(String args[]) {
// Scanner in = new Scanner(System.in);
// int n1 = in.nextInt();
// AgeValidator ob = new AgeValidator();
// ob.CheckAge(n1);
// }
//}
// 3.
//class oops {
// static void fun()
// {
// try {
// throw new NullPointerException("demo");
// }
// catch (Exception e) {
// System.out.println("Caught inside fun()."+ e);
// throw e; // rethrowing the exception
// }
// }
//
// public static void main(String args[])
// {
// try {
// fun();
// }
// catch (Exception e) {
// System.out.println("Caught in main. "+e);
// }
// }
// }