Untitled

 avatar
unknown
plain_text
a month ago
7.2 kB
5
Indexable

1000114068.jpg
Solve this code

Which question do you want the code for?
The image contains 10 Java programs from “Assignment 3: Inheritance & Package”.

For example:

Shape & Circle using inheritance

Employee, Manager, Programmer salary

Abstract class Shape

Method overloading (comp())

Player class inheritance

Bank interface (SBI, PNB)

Java package program

Cube package program

Interface inheritance

String reverse & vowel count

Send the question number (like “solve 1” or “solve all”) and I’ll provide the full Java code.


Solve all

1. Shape and Circle using Inheritance
class Shape {
        void getPerimeter() {
                    System.out.println("Perimeter");
        }

            void getArea() {
                        System.out.println("Area");
            }
}

class Circle extends Shape {
        double radius;

            Circle(double r) {
                        radius = r;
            }

                void getPerimeter() {
                            double perimeter = 2 * 3.14 * radius;
                                    System.out.println("Perimeter of Circle = " + perimeter);
                }

                    void getArea() {
                                double area = 3.14 * radius * radius;
                                        System.out.println("Area of Circle = " + area);
                    }

                        public static void main(String[] args) {
                                    Circle c = new Circle(5);

                                            c.getPerimeter();
                                                    c.getArea();
                        }
}
2. Employee, Manager and Programmer
class Employee {
        double salary;

            double calculateSalary() {
                        return salary;
            }
}

class Manager extends Employee {

        Manager() {
                    salary = 60000;
        }

            double calculateSalary() {
                        return salary;
            }
}

class Programmer extends Employee {

        Programmer() {
                    salary = 50000;
        }

            double calculateSalary() {
                        return salary;
            }
}

public class Main {
        public static void main(String[] args) {

                    Manager m = new Manager();
                            Programmer p = new Programmer();

                                    System.out.println("Manager Salary = " + m.calculateSalary());
                                            System.out.println("Programmer Salary = " + p.calculateSalary());
        }
}
3. Abstract Class Shape
abstract class Shape {

        abstract void getArea();

            abstract void getPerimeter();
}

class Rectangle extends Shape {
        int length, breadth;

            Rectangle(int l, int b) {
                        length = l;
                                breadth = b;
            }

                void getArea() {
                            System.out.println("Area of Rectangle = " + (length * breadth));
                }

                    void getPerimeter() {
                                System.out.println("Perimeter of Rectangle = " + (2 * (length + breadth)));
                    }
}

class Circle extends Shape {
        double radius;

            Circle(double r) {
                        radius = r;
            }

                void getArea() {
                            System.out.println("Area of Circle = " + (3.14 * radius * radius));
                }

                    void getPerimeter() {
                                System.out.println("Perimeter of Circle = " + (2 * 3.14 * radius));
                    }
}

public class Main {
        public static void main(String[] args) {

                    Rectangle r = new Rectangle(4, 5);
                            r.getArea();
                                    r.getPerimeter();

                                            Circle c = new Circle(3);
                                                    c.getArea();
                                                            c.getPerimeter();
        }
}
4. Method Overloading (Compare)
class Compare {

        void comp(int a, int b) {
                    if (a > b)
                                System.out.println("Greater value = " + a);
                                        else
                                                    System.out.println("Greater value = " + b);
        }

            void comp(int a, int b, int c) {
                        int min = a;

                                if (b < min)
                                            min = b;

                                                    if (c < min)
                                                                min = c;

                                                                        System.out.println("Lowest value = " + min);
            }

                public static void main(String[] args) {

                            Compare obj = new Compare();

                                    obj.comp(10, 20);
                                            obj.comp(10, 5, 7);
                }
}
5. Player Class Inheritance
class Player {
        String name;
            int age;

                Player(String n, int a) {
                            name = n;
                                    age = a;
                }
}

class Cricket_Player extends Player {

        Cricket_Player(String n, int a) {
                    super(n, a);
        }

            void display() {
                        System.out.println("Cricket Player: " + name + ", Age: " + age);
            }
}

class Football_Player extends Player {

        Football_Player(String n, int a) {
                    super(n, a);
        }

            void display() {
                        System.out.println("Football Player: " + name + ", Age: " + age);
            }
}

class Hockey_Player extends Player {

        Hockey_Player(String n, int a) {
                    super(n, a);
        }

            void display() {
                        System.out.println("Hockey Player: " + name + ", Age: " + age);
            }
}

public class Main {
        public static void main(String[] args) {

                    Cricket_Player c = new Cricket_Player("Virat", 35);
                            Football_Player f = new Football_Player("Messi", 37);
                                    Hockey_Player h = new Hockey_Player("Raj", 28);

                                            c.display();
                                                    f.display();
                                                            h.display();
        }
}v
        }
}
            }
        }
}
            }
        }
}
            }
        }
}
                }
}
                }
            }
        }
}
        }
}
                    }
                }
            }
}
                    }
                }
            }
}
}
        }
}
            }
        }
}
            }
        }
}
            }
}
                        }
                    }
                }
            }
}
            }
        }
}
Editor is loading...
Leave a Comment