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

package teach;

//Define a simple class called 'Person'


class Person
{ // Instance variables (attributes)
String name;
int age;
void displayIfo()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

public class ClassAndObjectExample


{
public static void main(String[] args)
{
Person person1 = new Person();
person1.name = "Gayathri";
person1.age = 43;
System.out.println("Person 1:");
person1.displayInfo();
System.out.println();
}
}

package teach;
// Method overloading
class Helper
{
static int Multiply(int a, int b)
{ return a * b; }
static double Multiply(double a, double b)
{ return a * b; }
}
class Methodoverloading
{
public static void main(String[] args)
{
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}
___________________________________________________________________________
________
package teach;
//Java program to illustrate Constructor Overloading

class Boxx
{
double width, height, depth;

Boxx(double w, double h, double d)


{
width = w;
height = h;
depth = d;
}
Boxx() { width = height = depth = 0; }
Boxx(double len) { width = height = depth = len; }
double volume() { return width * height * depth; }
}
public class ConstructorOverloading
{
public static void main(String args[])
{
Boxx mybox1 = new Boxx(10, 20, 15);
Boxx mybox2 = new Boxx();
Boxx mycube = new Boxx(7);

double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);

vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);

vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}

package teach1;
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
int n, m, rev = 0, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
m = n;
while(n > 0)
{
x = n % 10;
rev = rev * 10 + x;
n = n / 10;
}
if(rev == m)
System.out.println(" "+m+" is a palindrome number");
else
System.out.println(" "+m+" is not a palindrome number");
}
}

package teach1;
import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String[] args)
{
int a,b = 0,temp,n,num;
Scanner sc = new Scanner(System. in );
System.out.println("Enter the value of n: ");
n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
num = i;
b = 0;
while (num > 0)
{
a = num%10;
b = b + (a * a * a);
num = num / 10;
}
if (i == b) System.out.println(i);
}
}
}

___________________________________________________________________________
______
package teach1;
import java.util.*;
public class Sunny
{
public static void main(String args[])
{
//declare an int variable and initialize with a static value
int inputNumber=8;
double next=inputNumber + 1;
double square_root = Math.sqrt(next);
//check whether the square root is a integer value or not
//if yes return true otherwise false
if(((square_root - Math.floor(square_root)) == 0))
System.out.println(inputNumber + " is a sunny number.");
else
System.out.println(inputNumber + " is not a sunny number.");
}
}

package teach1;
import java.util.*;
class Peterson
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int num=sc.nextInt();
int temp=num; int f=1,sum=0;
while(num!=0)
{
f=1;
int r=num%10;
for(int i=1;i<=r;i++)
{ f=f*i; }
sum=sum+f;
num=num/10;
}
if(sum==temp)
System.out.println("PETERSON NUMBER");
else
System.out.println("NOT PETERSON NUMBER");
}
}
__________________________________________________________________________
Java 29

package d2;
import java.util.Scanner;

public class ArraySort


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] arr = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++)
{
arr[i] = scanner.nextInt();
int n = arr.length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted array in ascending order:");
for (int element : arr)
System.out.print(element + " ");
System.out.println();
System.out.println("Sorted array in descending order:");
for (int i = n-1; i >=0; i--)
System.out.print(arr[i] + " ");
scanner.close();
}
}
}

package d2;

import java.util.Scanner;
public class CountArrayDuplicate
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int i,j;
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for ( i = 0; i < size; i++)
array[i] = scanner.nextInt();
// Count duplicates in the array
int count = 0;
for ( i = 0; i < array.length - 1; i++)
{
// Check for duplicates starting from the next element
for (j = i + 1; j < array.length; j++)
{
if (array[i] == array[j])
{
count++;
break; // Break to avoid counting the same duplicate multiple times
}
}
}
System.out.println("Number of duplicates in the array: " +count);
scanner.close();
}
}

package pgms29;

import java.util.Scanner;
public class CountEvenOdd
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++)
{
array[i] = scanner.nextInt();
}
int evenCount = 0;
int oddCount = 0;
for (int number : array)
{
if (number % 2 == 0)
evenCount++;
else
oddCount++;
}
System.out.println("Number of even numbers: " + evenCount);
System.out.println("Number of odd numbers: " + oddCount);
scanner.close();
}
}
__________________________________________________________________________

package pgms29;
//Print Kth element in an Array
import java.util.Scanner;

public class KthElementInAnArray


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++)
array[i] = scanner.nextInt();
System.out.print("Enter the value of K: ");
int k = scanner.nextInt();
if (k >= 0 && k <= n-1)
{
int kthElement = array[k-1];
System.out.println("The Kth element in the array i4s: " + kthElement);
}
else
System.out.println("Invalid value of K. Please enter a value bet 1 and " + n);
scanner.close();
}
}

package pgms29;
import java.util.Scanner;
public class MatrixSum
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
matrix[i][j] = scanner.nextInt();
}
}
// Calculate and display the sum of rows
System.out.println("Sum of rows:");
for (int i = 0; i < rows; i++)
{
int rowSum = 0;
for (int j = 0; j < columns; j++)
{
rowSum += matrix[i][j];
}
System.out.println("Row " + (i + 1) + ": " + rowSum);
}
// Calculate and display the sum of columns
System.out.println("Sum of columns:");
for (int j = 0; j < columns; j++)
{
int columnSum = 0;
for (int i = 0; i < rows; i++)
{
columnSum += matrix[i][j];
}
System.out.println("Column " + (j + 1) + ": " + columnSum);
}
scanner.close();
}
}

package pgms29;
import java.util.Scanner;

public class MatrixTranspose


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
matrix[i][j] = scanner.nextInt();
}
}
for (int i = 0; i<columns; i++)
{
for (int j = 0; j < rows; j++)
{
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
}
}

package pgms29;

public class MaxMin


{
public static void main(String[] args)
{
int[] array = {3, 7, 2, 8, 5, 1, 9, 4, 6};

int max = array[0], min = array[0];


for (int num : array)
{
if (num > max)
{
max = num;
}
else if (num < min)
{
min = num;
}
}
// Display the results
System.out.println("First Maximum: " + max);
System.out.println("First Minimum: " + min);
}
}

package pgms29;

import java.util.Scanner;

public class Odd1toN


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int N = scanner.nextInt();
// Print odd numbers from 1 to N
System.out.println("Odd numbers from 1 to " + N + ":");
for (int i = 1; i <= N; i += 2)
{
System.out.print(i + " ");
}
scanner.close();
}
}
___________________________________________________________________________
package pgms29;

import java.util.Scanner;
public class PalindromeCheck
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the string: ");
String str = scanner.nextLine();
int left = 0;
int right = str.length() - 1;
boolean flag=true;
while (left < right)
{
if (str.charAt(left) != str.charAt(right))
{
flag=false;
break;
}
left++;
right--;
}
if (flag)
System.out.println(str + " is a palindrome.");
else
System.out.println(str + " is not a palindrome.");
}
}

package pgms29;
import java.util.Scanner;
public class ReverseArray
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++)
{
array[i] = scanner.nextInt();
}
reverseArray(array);
System.out.println("Reversed array:");
for (int element : array)
System.out.print(element + " ");
scanner.close();
}
// Function to reverse the elements of an array
private static void reverseArray(int[] arr)
{
int start = 0;
int end = arr.length - 1;

while (start < end)


{
// Swap elements at start and end indices
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move towards the center of the array
start++;
end--;
}
}
}
package pgms29;

import java.util.Scanner;
public class SecondLargestArray
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++)
array[i] = scanner.nextInt();
// Find the second largest number
int firstLargest = 0;
int secondLargest = 0;
for (int number : array)
{
if (number > firstLargest)
{
secondLargest = firstLargest;
firstLargest = number;
}
else if (number > secondLargest && number != firstLargest)
{
secondLargest = number;
}
}
System.out.println("The second largest element is: " +secondLargest);
scanner.close();
}
}
__________________________________________________________________________
package pgms29;
import java.util.Scanner;

public class SquareRoot


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
double number = scanner.nextDouble();
if (number >= 0)
{
double sqRoot = Math.sqrt(number);
System.out.println("Square root of " + number + " is: " + sqRoot);
}
else
System.out.println("Cannot calc square root of a negative
number.");
scanner.close();
}
}

package pgms29;

import java.util.Scanner;
public class SymmetricMatrix
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the order of the square matrix: ");
int order = scanner.nextInt();
int[][] matrix = new int[order][order];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < order; i++)
{
for (int j = 0; j < order; j++)
matrix[i][j] = scanner.nextInt();
}
}
// Check if the matrix is symmetric
if (isSymmetric(matrix, order))
System.out.println("The matrix is symmetric.");
else
System.out.println("The matrix is not symmetric.");
scanner.close();
}

// Function to check if a matrix is symmetric


private static boolean isSymmetric(int[][] matrix, int order)
{
for (int i = 0; i < order; i++)
{
for (int j = 0; j < order; j++)
{
if (matrix[i][j] != matrix[j][i])
{
return false;
}
}
}
return true;
}
}
package inheritanceeg;

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

//Subclass (Child class) inheriting from Animal


class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

public class SingleInheritance {


public static void main(String[] args) {
Dog myDog = new Dog();

myDog.eat();

myDog.bark();
}
}
package inheritanceeg;

//Interface for the first functionality


interface FirstInterface {
void methodOne();
}

//Interface for the second functionality


interface SecondInterface {
void methodTwo();
}

//Class implementing both interfaces


class MyClass implements FirstInterface, SecondInterface {
@Override
public void methodOne() {
System.out.println("Implementing methodOne");
}

@Override
public void methodTwo() {
System.out.println("Implementing methodTwo");
}

// Additional methods specific to MyClass


public void myClassMethod() {
System.out.println("MyClass specific method");
}
}

public class MulInh {


public static void main(String[] args) {
// Create an instance of MyClass
MyClass myObject = new MyClass();

// Call methods from both interfaces


myObject.methodOne();
myObject.methodTwo();

// Call a method specific to MyClass


myObject.myClassMethod();
}
}
package inheritanceeg;

import java.io.*;
import java.lang.*;
import java.util.*;

class One {
public void print1()
{
System.out.println("One");
}
}

class Two extends One {


public void print2() { System.out.println("Two"); }
}

class Three extends Two {


public void print3()
{
System.out.println("Three");
}
}

public class Multilevel {


public static void main(String[] args)
{
Three g = new Three();
g.print3();
g.print2();
g.print1();
}
}
package inheritanceeg;

class A {
public void print_A() { System.out.println("Class A"); }
}

class B extends A {
public void print_B() { System.out.println("Class B"); }
}

class C extends A {
public void print_C() { System.out.println("Class C"); }
}

class D extends A {
public void print_D() { System.out.println("Class D"); }
}

//Driver Class
public class Hierarchial {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();


obj_C.print_A();
obj_C.print_C();

D obj_D = new D();


obj_D.print_A();
obj_D.print_D();
}
}
package inheritanceeg;

interface Shape {
void draw();
}

class Circle implements Shape {


@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
//Class extending another class
class Rectangle {
void drawRectangle() {
System.out.println("Drawing a rectangle");
}
}

//Class implementing Shape interface and extending Rectangle class


class Square extends Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a square");
}
}

//Class inheriting from Square and implementing another interface


class ColoredSquare extends Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing a colored square");
}

void applyColor() {
System.out.println("Applying color to the square");
}
}

public class HybridInheritance {


public static void main(String[] args) {

Circle circle = new Circle();


circle.draw();

Square square = new Square();


square.drawRectangle(); // Inherited from Rectangle
square.draw(); // Inherited from Square

ColoredSquare coloredSquare = new ColoredSquare();


coloredSquare.drawRectangle(); // Inherited from Rectangle
coloredSquare.draw(); // Inherited from ColoredSquare
coloredSquare.applyColor(); // Inherited from ColoredSquare
}
}
_____________________________________________________________________

You might also like