From bcb54d7fb2ac75b5f6bfee9e0e658864dc1312c7 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 11 Apr 2023 08:17:48 +0530 Subject: [PATCH 01/24] Create Module-2.md --- Module-2.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Module-2.md diff --git a/Module-2.md b/Module-2.md new file mode 100644 index 0000000..5f1a4d3 --- /dev/null +++ b/Module-2.md @@ -0,0 +1 @@ +# Java-Sample-Programs From 284e2f98af0f484995179704c5cee69352bd75f7 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 08:14:10 +0530 Subject: [PATCH 02/24] Update ArrayMinMax.java --- ArrayMinMax.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ArrayMinMax.java b/ArrayMinMax.java index 280b16b..0d2f84a 100644 --- a/ArrayMinMax.java +++ b/ArrayMinMax.java @@ -5,22 +5,22 @@ public class ArrayMinMax { public static void main(String[] args) { - int size = Integer. parseInt (args[0]); - System.out.print("\nSize of Array is: " + size); - int[] arr = new int[size]; // Declare an array variable Scanner reader = new Scanner(System.in); - System.out.print("\nEnter the elements of the array --------"); + System.out.print("\nSize of Array is: "); + int size = reader.nextInt(); + int[] arr = new int[size]; + System.out.print("\nEnter the elements of the array - "); for(int i = 0; i < size; i++ ) { System.out.print("\nEnter the "+ (i+1) + " element = "); arr[i] = reader.nextInt(); } - System.out.print("\nEntered array is ----------\n"); + System.out.print("\nEntered array is - \n"); for (int i=0; i < size ;i++) { System.out.print("\t"+arr[i]); } - // Code for finding Maximum and Minimum by appliyning linear search + int max = arr[0]; int min = arr[0]; for(int i = 1; i Date: Tue, 18 Apr 2023 08:15:05 +0530 Subject: [PATCH 03/24] Add files via upload --- Ascendingorder.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Ascendingorder.java diff --git a/Ascendingorder.java b/Ascendingorder.java new file mode 100644 index 0000000..e7a3646 --- /dev/null +++ b/Ascendingorder.java @@ -0,0 +1,38 @@ +//Program for sorting a given list of names in ascending order +import java.util.Scanner; + +public class Ascendingorder +{ + public static void main(String args[]) + { + Scanner reader = new Scanner(System.in); + System.out.print("\nEnter the numbers of names : "); + int n = reader.nextInt(); + String[] names = new String[n]; + System.out.print("\nEnter the names - "); + for (int i = 0; i < n; i++ ) + { + System.out.print("\nEnter the name "+(i+1) + " : "); + names[i] = new String(reader.next()); + } + + for (int i = 0; i 0) + { + String temp = names[i]; + names[i] = names[j]; + names[j] = temp; + } + } + } + + System.out.print("\nSorted names are ---"); + for (int i=0; i Date: Tue, 18 Apr 2023 08:16:42 +0530 Subject: [PATCH 04/24] Add files via upload --- IrregularArray.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 IrregularArray.java diff --git a/IrregularArray.java b/IrregularArray.java new file mode 100644 index 0000000..8f8db48 --- /dev/null +++ b/IrregularArray.java @@ -0,0 +1,40 @@ +//Program to read and print an array of size n rows with variable column size. + +import java.util.Scanner; +class IrregularArray +{ + public static void main(String[] args) + { + System.out.print("\nEnter the number of rows: "); + Scanner reader = new Scanner(System.in); + int r = reader.nextInt(); + + // Declaring 2-D array with r rows + int arr[][] = new int[r][]; + + // Creating a 2D array such that first row + // has 1 element, second row has two + // elements and so on. + int col [] = new int[r]; + for (int i = 0; i < arr.length; i++) + { + System.out.print("\nEnter the number of elements in row "+ (i+1 )); + col[i] = reader.nextInt(); + arr[i] = new int[col[i]]; + } + // Initializing array + int count = 0; + for (int i = 0; i < arr.length; i++) + for (int j = 0; j < arr[i].length; j++) + arr[i][j] = count++; + + // Displaying the values of 2D Jagged array + System.out.println("Contents of 2D Jagged Array"); + for (int i = 0; i < arr.length; i++) + { + for (int j = 0; j < arr[i].length; j++) + System.out.print(arr[i][j] + " "); + System.out.println(); + } + } +} From cdddb80395efc521b43ef28442bebf17b70b0af0 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 08:16:56 +0530 Subject: [PATCH 05/24] Add files via upload --- Replacevowels.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Replacevowels.java diff --git a/Replacevowels.java b/Replacevowels.java new file mode 100644 index 0000000..392cf9a --- /dev/null +++ b/Replacevowels.java @@ -0,0 +1,14 @@ +import java.util.*; + +class Replacevowels +{ + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a String : "); + String str = sc.nextLine(); + System.out.println("The string after replacing vowels : "); + str = str.replaceAll("[aeiou]","*"); + System.out.print(str); + } +} \ No newline at end of file From be1e539d74cb3f47234b18051d206106d05c1038 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 08:23:31 +0530 Subject: [PATCH 06/24] Add files via upload --- BankAccount.java | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 BankAccount.java diff --git a/BankAccount.java b/BankAccount.java new file mode 100644 index 0000000..af144ef --- /dev/null +++ b/BankAccount.java @@ -0,0 +1,81 @@ +//Program that creates a class Account that stores a variable balance. The class has methods to start account, to deposit money, to withdraw money and tell the current balance amount. + +import java.util.Scanner; +//import java.util.*; +class BankAccount +{ + String name; + double balance; + void CreateAccount(String n, double b) + { + name = n; + balance = b; + System.out.print("\nAccount Created:"); + System.out.println("\nName: " + name); + System.out.println("\nAmount: " + balance); + } + + void deposit(double d) + { + if(d > 0.0) + { + balance = balance + d; + } + System.out.print("\nBalance After Deposit: " + balance); + } + + void withdraw(double w) + { + if(w > 0.0 && w <= balance) + { + balance = balance - w; + } + System.out.print("\nBalance After Withdrawal: " + balance); + } + + double getBalance() + { + return balance; + } + + public static void main(String args[]) + { + BankAccount s1= new BankAccount(); + Scanner in = new Scanner(System.in); + System.out.print("\nEnter your name: "); + String x = in.nextLine(); + System.out.print("\nEnter Amount: "); + double y = in.nextDouble(); + s1.CreateAccount(x,y); + while(true) + { + System.out.print("\nChoose any method from the following Options ----"); + System.out.print("\nOption 1 :: To Deposit Money"); + System.out.print("\nOption 2 :: To Withdraw Money"); + System.out.print("\nOption 3 :: To Get Balance"); + System.out.print("\nOption 4 :: To Exit"); + System.out.print("\nSelect your choice: "); + int choice= in.nextInt(); + switch(choice) + { + case 1: // To deposit money + System.out.print("\nEnter the amount you would like to deposit: "); + double deposit = in.nextDouble(); + s1.deposit(deposit); + break; + case 2: // To withdraw money + System.out.print("\nEnter the amount you would like to withdraw: "); + double withdraw = in.nextDouble(); + s1.withdraw(withdraw); + break; + case 3: // To get balance + System.out.print(s1.getBalance()); + break; + case 4: + System.exit(0); + default: + System.out.print("\nInvalid choice! Please make avalid choice."); + } + } + } +} \ No newline at end of file From bedc6cc5effff920fcf9470eafddaaa90d250204 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:01:44 +0530 Subject: [PATCH 07/24] Add files via upload --- StaticVsNonStaticMethod.java | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 StaticVsNonStaticMethod.java diff --git a/StaticVsNonStaticMethod.java b/StaticVsNonStaticMethod.java new file mode 100644 index 0000000..fe4c997 --- /dev/null +++ b/StaticVsNonStaticMethod.java @@ -0,0 +1,44 @@ +//Program to differentiate between static and non-static methods. + +class StaticVsNonStaticMethod +{ + public static void main(String args[]) + { + // declaration of static and non-static variables + StaticNonStaticMethod ob1= new StaticNonStaticMethod(); + StaticNonStaticMethod.objectCount(); + ob1.increaseCount(); + ob1.increaseCount(); + ob1.increaseCount(); + ob1.printCount(); + StaticNonStaticMethod ob2= new StaticNonStaticMethod(); + StaticNonStaticMethod.objectCount(); + ob2.increaseCount(); + ob2.increaseCount(); + ob2.increaseCount(); + ob2.increaseCount(); + ob2.printCount(); + } +} + +class StaticNonStaticMethod +{ + int count=0; + static int obCount; + public static int objectCount() + { + // Returning number of object created + return ++obCount; + } + + public void increaseCount() + { + count++; + } + public void printCount() + { + System.out.println("\nThe number of object created = "+StaticNonStaticMethod.obCount ); + System.out.println("\nThe value of count = " +count ); + } +} + From f177cf67b7f08fabad360f779cda0ec80db9ec9e Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:04:21 +0530 Subject: [PATCH 08/24] Add files via upload --- ConstructorOverloading.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 ConstructorOverloading.java diff --git a/ConstructorOverloading.java b/ConstructorOverloading.java new file mode 100644 index 0000000..6eb649e --- /dev/null +++ b/ConstructorOverloading.java @@ -0,0 +1,21 @@ +//Program to show the overloading of constructures. + +class ConstructorOverloading +{ + // Non-Parameterized Constructor. + ConstructorOverloading() + { + System.out.println("Hello, I am a Constructor with no parameter."); + } + // Parameterized Constructor. + ConstructorOverloading(String name) + { + System.out.println("Hello, I am a Constructor with one parameter, name = " + name); + } + + public static void main(String[] args) + { + ConstructorOverloading p1 = new ConstructorOverloading(); // Non-parameterized constructor. + ConstructorOverloading p2 = new ConstructorOverloading("Pramod.H"); // One parameterized constructor. + } +} \ No newline at end of file From b3e189e10312f5e0449074ee9e7c392ba539b7dc Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:30:23 +0530 Subject: [PATCH 09/24] Add files via upload --- DynamicMethodDispatch.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 DynamicMethodDispatch.java diff --git a/DynamicMethodDispatch.java b/DynamicMethodDispatch.java new file mode 100644 index 0000000..69d81be --- /dev/null +++ b/DynamicMethodDispatch.java @@ -0,0 +1,28 @@ +//Program to implement the concept of dynamic method dispatch. +class Student + { + int maxRollNo = 100; + } +class SchoolStudent extends Student + { + int maxRollNo = 80; + } + +class CollegeStudent extends SchoolStudent + { + int maxRollNo = 700; + } + +public class DynamicMethodDispatch +{ + public static void main(String args[]) + { + //Super class can contain subclass object. + Student obj1 = new CollegeStudent(); + Student obj2 = new SchoolStudent(); + + //In both calls maxRollNo of super class will be printed. + System.out.println(obj1.maxRollNo); + System.out.println(obj2.maxRollNo); + } +} \ No newline at end of file From 5c4b72d8ab8ab53de5fc941044e5d8968c1b9dc3 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:37:52 +0530 Subject: [PATCH 10/24] Add files via upload --- ReverseString.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ReverseString.java diff --git a/ReverseString.java b/ReverseString.java new file mode 100644 index 0000000..802c555 --- /dev/null +++ b/ReverseString.java @@ -0,0 +1,22 @@ +//Program to reverse the words in a string. + +import java.util.Scanner; +class ReverseString +{ + public static void main(String args[]) + { + Scanner scanner = new Scanner(System.in); + System.out.print("\nEnter the string: "); + String originalStr = scanner.nextLine(); + scanner.close(); + //String words[] = originalStr.split("\\s"); + String reversedString = ""; + //Reverse each word’s position + for (int i = 0; i < originalStr.length(); i++) + { + reversedString = originalStr.charAt(i) + reversedString; + } + // Displaying the string after reverse + System.out.print("Reversed string : " + reversedString); + } +} \ No newline at end of file From 0f7ac0a34503567ba98682e623fc1085ab253eb6 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:44:16 +0530 Subject: [PATCH 11/24] Add files via upload From f436727b055e56cf308ba4f0a4324297878a17e0 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:47:45 +0530 Subject: [PATCH 12/24] Add files via upload --- SubStringCount.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 SubStringCount.java diff --git a/SubStringCount.java b/SubStringCount.java new file mode 100644 index 0000000..c2cce7f --- /dev/null +++ b/SubStringCount.java @@ -0,0 +1,27 @@ +//Program to count the number of occurrences of a search string in a given text string. + +import java.io.*; +import java.util.Scanner; +class SubStringCount +{ + static int countOccurrences(String str, String word) + { + String a[] = str.split(" "); + int count = 0; + for (int i = 0; i < a.length; i++) + { + if (word.equals(a[i])) + count++; + } + return count; + } + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + System.out.print("\nEnter a string:"); + String str = sc.nextLine(); + System.out.println("\nEnter a word to check for occurences:"); + String word = sc.nextLine(); + System.out.print(countOccurrences(str, word)); + } +} \ No newline at end of file From 5b45d943184135d08870435946a3ffaeb5a665c0 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 25 Apr 2023 08:47:21 +0530 Subject: [PATCH 13/24] Add files via upload --- VolumeOfShape.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 VolumeOfShape.java diff --git a/VolumeOfShape.java b/VolumeOfShape.java new file mode 100644 index 0000000..93da84f --- /dev/null +++ b/VolumeOfShape.java @@ -0,0 +1,58 @@ +import java.util.Scanner; +class Shape +{ + double length, breadth; + Shape(double l, double b){ + length = l; + breadth= b; + } + Shape(double len){ + length = breadth = len; + } + double calculate(){ // To calculate the area of a shape object + return length * breadth ; + } + } + class Test1 extends Shape{ + //Create a derived class constructor which can call the one parametrized constructor of the base class + double height; + Test1(double l,double h) + { + super(l); + this.length = l; + this.height = h; + } +//Create a derived class constructor which can call the two parametrized constructor of the base class +Test1(double l,double b,double h) +{ + super(l,b); + this.length=l; + this.breadth=b; + this.height=h; +} +//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape +double calculate() +{ + return length*breadth*height; +} +} + +class VolumeOfShape +{ + public static void main(String[] args) + { + System.out.println("Enter the length, breadth and height\n"); + Scanner sc = new Scanner(System.in);//Create an object to read input + double l=sc.nextDouble(); //Read length + double b=sc.nextDouble(); //Read breadth + double h=sc.nextDouble(); //Read height + Test1 myshape1 = new Test1(l,h); + Test1 myshape2 = new Test1(l,b,h); + double volume1; + double volume2; + volume1 = myshape1.calculate(); + volume2=myshape2.calculate(); + System.out.println("The volume using length and height is : "+volume1); + System.out.println("The volume using all 3 parameters is : "+volume2); + } +} From f280a9fcb1a5ee8d64c61723ccaa0ceea592dccd Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 25 Apr 2023 08:56:37 +0530 Subject: [PATCH 14/24] Delete VolumeOfShape.java --- VolumeOfShape.java | 58 ---------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 VolumeOfShape.java diff --git a/VolumeOfShape.java b/VolumeOfShape.java deleted file mode 100644 index 93da84f..0000000 --- a/VolumeOfShape.java +++ /dev/null @@ -1,58 +0,0 @@ -import java.util.Scanner; -class Shape -{ - double length, breadth; - Shape(double l, double b){ - length = l; - breadth= b; - } - Shape(double len){ - length = breadth = len; - } - double calculate(){ // To calculate the area of a shape object - return length * breadth ; - } - } - class Test1 extends Shape{ - //Create a derived class constructor which can call the one parametrized constructor of the base class - double height; - Test1(double l,double h) - { - super(l); - this.length = l; - this.height = h; - } -//Create a derived class constructor which can call the two parametrized constructor of the base class -Test1(double l,double b,double h) -{ - super(l,b); - this.length=l; - this.breadth=b; - this.height=h; -} -//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape -double calculate() -{ - return length*breadth*height; -} -} - -class VolumeOfShape -{ - public static void main(String[] args) - { - System.out.println("Enter the length, breadth and height\n"); - Scanner sc = new Scanner(System.in);//Create an object to read input - double l=sc.nextDouble(); //Read length - double b=sc.nextDouble(); //Read breadth - double h=sc.nextDouble(); //Read height - Test1 myshape1 = new Test1(l,h); - Test1 myshape2 = new Test1(l,b,h); - double volume1; - double volume2; - volume1 = myshape1.calculate(); - volume2=myshape2.calculate(); - System.out.println("The volume using length and height is : "+volume1); - System.out.println("The volume using all 3 parameters is : "+volume2); - } -} From 812942bcab62c66637c135694d69401c0100c1ea Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Tue, 25 Apr 2023 08:56:55 +0530 Subject: [PATCH 15/24] Add files via upload --- VolumeOfShape.java | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 VolumeOfShape.java diff --git a/VolumeOfShape.java b/VolumeOfShape.java new file mode 100644 index 0000000..2023d91 --- /dev/null +++ b/VolumeOfShape.java @@ -0,0 +1,66 @@ +import java.util.Scanner; +class Shape +{ + double length, breadth; + Shape(double l, double b) + { + length = l; + breadth= b; + } + Shape(double len) + { + length = breadth = len; + } + // To calculate the area of a shape object + double calculate() + { + return length * breadth ; + } +} + class Test1 extends Shape + { + + //Create a derived class constructor which can call the one parametrized constructor of the base class + double height; + Test1(double l,double h) + { + super(l); + this.length = l; + this.height = h; + } + + //Create a derived class constructor which can call the two parametrized constructor of the base class + Test1(double l,double b,double h) + { + super(l,b); + this.length=l; + this.breadth=b; + this.height=h; + } + + //Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape + double calculate() + { + return length*breadth*height; + } +} + +class VolumeOfShape +{ + public static void main(String[] args) + { + System.out.println("Enter the length, breadth and height\n"); + Scanner sc = new Scanner(System.in);//Create an object to read input + double l=sc.nextDouble(); //Read length + double b=sc.nextDouble(); //Read breadth + double h=sc.nextDouble(); //Read height + Test1 myshape1 = new Test1(l,h); + Test1 myshape2 = new Test1(l,b,h); + double volume1; + double volume2; + volume1 = myshape1.calculate(); + volume2=myshape2.calculate(); + System.out.println("The volume using length and height is : "+volume1); + System.out.println("The volume using all 3 parameters is : "+volume2); + } +} From a4e0de4b534e630e796c2849393a427e257a000a Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:09:22 +0530 Subject: [PATCH 16/24] Add files via upload --- Distance2Point.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Distance2Point.java diff --git a/Distance2Point.java b/Distance2Point.java new file mode 100644 index 0000000..0da7758 --- /dev/null +++ b/Distance2Point.java @@ -0,0 +1,40 @@ +//Program to define a class Point with two fields x and y each of type double. also, a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double. (Hint: use Math.sqrt( ) ) +import java.util.Scanner; + +class Point +{ + double x; + double y; + double distance(Point p1,Point p2) + { + double res; + return Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y)); + } +} + +class Distance2Point +{ + public static void main(String args[]) + { + // creating one object of Point class + Point p1=new Point(); + // creating another object of Point class + Point p2=new Point(); + Scanner reader = new Scanner(System.in); + + System.out.print("\nEnter the first point -----"); + System.out.print("\nx1 = "); + p1.x = reader.nextDouble(); + System.out.print("\ny1 = "); + p1.y = reader.nextDouble(); + + System.out.print("\nEnter the second point -----"); + System.out.print("\nx2 = "); + p2.x = reader.nextDouble(); + System.out.print("\ny2 = "); + p2.y = reader.nextDouble(); + + // calling distance method + System.out.println("The distance between two points:" + p1.distance(p1,p2)); + } +} \ No newline at end of file From 1df260099473d6074fdb5e5091a8a2bee72c8691 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:10:21 +0530 Subject: [PATCH 17/24] Add files via upload --- EmployeeInheritence.java | 76 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 EmployeeInheritence.java diff --git a/EmployeeInheritence.java b/EmployeeInheritence.java new file mode 100644 index 0000000..41f4e85 --- /dev/null +++ b/EmployeeInheritence.java @@ -0,0 +1,76 @@ +//Program to create a class named 'Member' having the following members: Name, Age, Phone number, Address, Salary. It also has a method named printSalary which prints the salary of the members. Two classes Employee and Manager inherits the Member class. The Employee and Manager' classes have data members specialization and department respectively. Now, assign name, age, phone number, address and salary to an employee and a manager by making an object of both of these classes and print the same. + +class Member +{ + String name; + int age; + long ph_num; + String address; + double salary; + + Member(String n,int a,long p,String ad,double s) // Constructor + { + this.name=n; + this.age=a; + this.ph_num=p; + this.address=ad; + this.salary=s; + } + + void print_details() + { + System.out.print("\nEmployee Details are as:"); + System.out.print("\nName: " + name); + System.out.print("\nAge: " + age); + System.out.print("\nAddress: " + address); + System.out.print("\nPhone number: " + ph_num); + } + + void print_Salary() + { + System.out.print("\nSalary: " + this.salary); + } +} + +class Employee extends Member +{ + String specialization="Market Research"; + String department="Marketing and Sales"; + + Employee() //Constructor + { + super("Pramod",24,9876767876l,"XYZ",95140.5); //Calling Constructor of Parent Class + } +} + +class Manager extends Member +{ + String specialization="Market Research"; + String department="Marketing and Sales"; + + Manager() // Consructor + { + super("Revanth",25,9876447876l,"ABC",96140.5); //Calling Constructor of Parent Class + } +} + +class EmployeeInheritence +{ + public static void main(String args[]) + { + Employee e = new Employee(); + Manager m = new Manager(); + + System.out.print("\n----Employee details----\n"); + e.print_details(); + System.out.print("\nSpecialization: "+e.specialization); + System.out.print("\nDepartment: "+e.department); + e.print_Salary(); + + System.out.print("\n----Manager details----\n"); + m.print_details(); + System.out.print("\nSpecialization: "+m.specialization); + System.out.print("\nDepartment: "+m.department); + m.print_Salary(); + } +} \ No newline at end of file From 4d711aea7d8096ed2376986cf680b62f73eeb621 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:12:08 +0530 Subject: [PATCH 18/24] Add files via upload --- ShapeOverloading1.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ShapeOverloading1.java diff --git a/ShapeOverloading1.java b/ShapeOverloading1.java new file mode 100644 index 0000000..c7d2f07 --- /dev/null +++ b/ShapeOverloading1.java @@ -0,0 +1,42 @@ +//Program to create a class named 'Shape' with a method to print This is This is shape. Then create two other classes named Rectangle, Circle inheriting the Shape class, both having a method to print This is rectangular shape and This is circular shape respectively. Create a subclass Square of Rectangle having a method to print Square is a rectangle. Now call the method of Shape and Rectangle class by the object of Square class. +class Shape +{ + public void print_shape() + { + System.out.print("\nThis is shape."); + } +} + +class Rectangle extends Shape // Rectangle class is subclass of Shape class +{ + public void print_rect() + { + System.out.print("\nThis is rectangular shape."); + } +} + +class Circle extends Shape // Circle class is subclass of Shape class +{ + public void print_circle() + { + System.out.print("\nThis is circular shape."); + } +} + +class Square extends Rectangle +{ + public void print_square() // Square class is subclass of Rectangle class + { + System.out.print("\nSquare is a rectangle."); + } +} + +class ShapeOverloading1 +{ + public static void main(String[] args) + { + Square sq = new Square(); //Creating object of Square class + sq.print_shape(); //Object of Square class calling function of Shape class + sq.print_rect(); //Object of Square class calling function of Rectangle class + } +} \ No newline at end of file From be77f2ebff6337e25729eb8f73016f2d9969b33b Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:13:54 +0530 Subject: [PATCH 19/24] Add files via upload --- Inheritance1.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Inheritance1.java diff --git a/Inheritance1.java b/Inheritance1.java new file mode 100644 index 0000000..7824bcb --- /dev/null +++ b/Inheritance1.java @@ -0,0 +1,33 @@ +//Program to create a class with a method that prints This is parent class and its subclass with another method that prints This is child class. Now, create an object for each of the class and call + //method of parent class by object of parent class + //method of child class by object of child class + //method of parent class by object of child class + +class Parent +{ + void display1() + { + System.out.print("\nThis is parent class."); + } +} + +class Child extends Parent +{ + void display2() + { + System.out.print("\nThis is child class."); + } +} + +class Inheritance1 +{ + public static void main(String args[]) + { + Parent p=new Parent(); // Creating an object of Parent class + Child c=new Child(); // Creating an object of Child class + + p.display1(); // Calling Parent class method using Parent class object + c.display2(); // Calling Child class method using Child class object + c.display1(); // Calling Parent class method using Child class object + } +} \ No newline at end of file From 3899f0c4dd1d0a06e4a3e350378a9cefb00da8bf Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:15:52 +0530 Subject: [PATCH 20/24] Add files via upload --- PolymorphismExample.java | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 PolymorphismExample.java diff --git a/PolymorphismExample.java b/PolymorphismExample.java new file mode 100644 index 0000000..60858fe --- /dev/null +++ b/PolymorphismExample.java @@ -0,0 +1,65 @@ +//Program to create a class telephone with ( ) , lift ( ) and disconnected ( ) methods as abstract methods create another class smart telephone and demonstrate polymorphism + +interface telephone2 +{ + void pickCall(); + void recieveCall(); +} + +interface phone2 +{ + void call(); + void with(); + void lift(); + void disconnected(); +} + +class CellPhone +{ + void recording_audio() + { + System.out.print("\nHello hello babya baye .."); + } +} + +class Smartphone extends CellPhone implements telephone2, phone2 +{ + public void call() + { + System.out.print("\nHello is it ....care"); + } + + public void with() + { + System.out.println("\nI am on a call with my friend."); + } + + public void lift() + { + System.out.print("\nHello your lift is good now."); + } + + public void disconnected() + { + System.out.print("\nCall is disconnected."); + } + + public void pickCall() + { + System.out.print("\nHello, It's Hello"); + } + + public void recieveCall() + { + System.out.println("Bye ..."); + } +} + +class PolymorphismExample { + public static void main(String args[]) + { + telephone2 tl = new Smartphone(); + tl.pickCall(); + tl.recieveCall(); + } +} \ No newline at end of file From c9629e68a1afe5f86629ec61429da9eded6110a8 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:19:28 +0530 Subject: [PATCH 21/24] Add files via upload --- PolymorphismProgram.java | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 PolymorphismProgram.java diff --git a/PolymorphismProgram.java b/PolymorphismProgram.java new file mode 100644 index 0000000..347af4f --- /dev/null +++ b/PolymorphismProgram.java @@ -0,0 +1,83 @@ +//Program to design a vehicle class hierarchy in Java, and develop a program to demonstrate Polymorphism. +class Vehicle +{ + String regno; + int model; + Vehicle(String r, int m) + { + regno = r; + model = m; + } + + void display() + { + System.out.print("\nRegistration no: "+regno); + System.out.print("\nModel no: "+model); + } +} + +class Twowheeler extends Vehicle +{ + int noofwheels; + Twowheeler(String r, int m, int n) + { + super(r,m); + noofwheels = n; + } + + void display() + { + System.out.print("\nTwo wheeler tvs"); + super.display(); + System.out.print("\nNo. of wheels: " + noofwheels); + } +} + +class Threewheeler extends Vehicle +{ + int noofwheels; + + Threewheeler(String r,int m,int n) + { + super(r,m); + noofwheels = n; + } + + void display() + { + System.out.print("\nThree wheeler auto"); + super.display(); + System.out.print("\nNo. of wheels: " +noofwheels); + } +} + +class Fourwheeler extends Vehicle +{ + int noofwheels; + + Fourwheeler(String r,int m,int n) + { + super(r,m); + noofwheels=n; + } + + void display() + { + System.out.print("\nFour wheeler car"); + super.display(); + System.out.print("\nNo. of wheels: " + noofwheels); + } +} + +class PolymorphismProgram +{ + public static void main(String arg[]) + { + Twowheeler t1 = new Twowheeler("KA74 12345", 1,2); + Threewheeler th1 = new Threewheeler("KA74 54321", 4,3); + Fourwheeler f1 = new Fourwheeler("KA34 45677",5,4); + t1.display(); + th1.display(); + f1.display(); + } +} \ No newline at end of file From 4c982bccb480cbfebfc2d8eddd9377e1253cb021 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:21:51 +0530 Subject: [PATCH 22/24] Add files via upload --- FibonacciSeries.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 FibonacciSeries.java diff --git a/FibonacciSeries.java b/FibonacciSeries.java new file mode 100644 index 0000000..1c0e276 --- /dev/null +++ b/FibonacciSeries.java @@ -0,0 +1,35 @@ +//Program to generate Fibonacci Series by using Constructor to initialize the Data Members. +import java.util.Scanner; + +class FibonacciSeries +{ + int n1,n2; + FibonacciSeries() // Constructor + { + n1 = 0; + n2 = 1; + } + + void series(int n) + { + int i, next; + System.out.print(n1 + "\t" + n2); + for(i=1; i <= n-2; i++) + { + next = n1 + n2; + System.out.print("\t " + next); + n1 = n2; + n2 = next; + } + } + + public static void main(String args[]) + { + FibonacciSeries fib = new FibonacciSeries(); + Scanner reader = new Scanner(System.in); + System.out.print("\nEnter value for n: "); + int n = reader.nextInt(); + System.out.println("\nThe Fibonacci series for " + n + " is -----\n"); + fib.series(n); +} +} \ No newline at end of file From 043796fc4e04dac1c1ff9768b503a2694eeb7267 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:23:45 +0530 Subject: [PATCH 23/24] Add files via upload From 0e7026fd0681fbc668f11b130942016a815db095 Mon Sep 17 00:00:00 2001 From: Pramod-1999 <127389766+Pramod-1999@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:25:14 +0530 Subject: [PATCH 24/24] Add files via upload --- MultipleInheritence.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 MultipleInheritence.java diff --git a/MultipleInheritence.java b/MultipleInheritence.java new file mode 100644 index 0000000..3d6ba63 --- /dev/null +++ b/MultipleInheritence.java @@ -0,0 +1,34 @@ +//Program to demonstrate multiple inheritance through interface. + +interface AnimalEat +{ + void eat(); +} + +interface AnimalTravel +{ + void travel(); +} + +class Animal implements AnimalEat, AnimalTravel +{ + public void eat() + { + System.out.print("\nAnimal is eating."); + } + + public void travel() + { + System.out.print("\nAnimal is travelling."); + } +} + +public class MultipleInheritence +{ + public static void main(String args[]) + { + Animal a = new Animal(); + a.eat(); + a.travel(); + } +} \ No newline at end of file