Open Lyo001 opened 1 month ago
interface Animal { String sound(); String type(); }
class Dog implements Animal { public String sound() { return "Bark"; }
public String type() {
return "Domestic";
}
}
class Cat implements Animal { public String sound() { return "Meow"; }
public String type() {
return "Domestic";
}
}
class Cow implements Animal { public String sound() { return "Moo"; }
public String type() {
return "Farm";
}
}
public class MainAnimal { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); Cow cow = new Cow(); System.out.println("Dog: " + dog.sound() + ", " + dog.type()); System.out.println("Cat: " + cat.sound() + ", " + cat.type()); System.out.println("Cow: " + cow.sound() + ", " + cow.type()); } }
import java.util.Scanner;
interface Animal { String sound(); String type(); }
class Dog implements Animal { public String sound() { return "Bark"; }
public String type() {
return "Domestic";
}
}
class Cat implements Animal { public String sound() { return "Meow"; }
public String type() {
return "Domestic";
}
}
class Cow implements Animal { public String sound() { return "Moo"; }
public String type() {
return "Farm";
}
}
public class MainAnimal { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("Choose an animal (Dog, Cat, Cow): ");
String choice = scanner.nextLine();
Animal animal;
if (choice.equalsIgnoreCase("Dog")) {
animal = new Dog();
} else if (choice.equalsIgnoreCase("Cat")) {
animal = new Cat();
} else {
animal = new Cow();
}
System.out.println("Sound: " + animal.sound());
System.out.println("Type: " + animal.type());
scanner.close();
}
}
import java.util.Scanner;
interface Vehicle { void move(); }
interface FlyingVehicle extends Vehicle { void fly(); }
class Aeroplane implements FlyingVehicle { public void move() { System.out.println("Aeroplane is moving."); }
public void fly() {
System.out.println("Aeroplane is flying.");
}
}
public class MainVehicle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
Aeroplane aeroplane = new Aeroplane();
System.out.print("Enter action (move/fly): ");
String action = scanner.nextLine();
if (action.equalsIgnoreCase("move")) {
aeroplane.move();
} else if (action.equalsIgnoreCase("fly")) {
aeroplane.fly();
}
scanner.close();
}
}
import java.util.Scanner;
interface Calculator { int add(int a, int b); int subtract(int a, int b); int multiply(int a, int b);
default double divide(int a, int b) {
return b == 0 ? 0 : (double) a / b;
}
}
class BasicCalculator implements Calculator { public int add(int a, int b) { return a + b; }
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
}
public class MainCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
BasicCalculator calc = new BasicCalculator();
System.out.println("Add: " + calc.add(a, b));
System.out.println("Subtract: " + calc.subtract(a, b));
System.out.println("Multiply: " + calc.multiply(a, b));
System.out.println("Divide: " + calc.divide(a, b));
scanner.close();
}
}
import java.util.Scanner;
interface MathConstants { double PI = 3.14159; double E = 2.71828; }
class MathOperations implements MathConstants { public double circumference(double radius) { return 2 PI radius; } }
public class MainMathOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius of the circle: ");
double radius = scanner.nextDouble();
MathOperations mathOps = new MathOperations();
System.out.println("Circumference of Circle: " + mathOps.circumference(radius));
scanner.close();
}
}
import java.util.Scanner;
interface MathConstants { double PI = 3.141592653589793; double E = 2.718281828459045; }
class MathOperations implements MathConstants { public double calculateCircumference(double radius) { return 2 PI radius; }
public double calculateAreaOfCircle(double radius) {
return PI * radius * radius;
}
public double calculateExponential(double exponent) {
return Math.pow(E, exponent);
}
}
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); MathOperations mathOps = new MathOperations();
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
System.out.println("Circumference of the circle: " + mathOps.calculateCircumference(radius));
System.out.println("Area of the circle: " + mathOps.calculateAreaOfCircle(radius));
System.out.print("Enter the exponent for E: ");
double exponent = scanner.nextDouble();
System.out.println("E raised to the power of " + exponent + ": " + mathOps.calculateExponential(exponent));
scanner.close();
}
}
q5
interface Drawable { void draw(); default void msg() { System.out.println("Default method"); } }
class Rectangle implements Drawable { public void draw() { System.out.println("Drawing Rectangle"); } }
class Test { public static void main(String[] args) { Drawable d = new Rectangle(); d.draw(); d.msg(); } }
interface Drawable { void draw(); static int cube(int x) { return (x x x); } }
class Rectangle implements Drawable { public void draw() { System.out.println("Drawing Rectangle"); }
static long cube(int x) {
return (x * x * x * x);
}
}
class Test { public static void main(String[] args) { Drawable d = new Rectangle(); d.draw();
System.out.println(Drawable.cube(3)); // Calls static method from interface
System.out.println(Rectangle.cube(2)); // Calls static method from class
System.out.println(d.cube(5)); // This line might not work as static methods are not inherited
}
}
class XYZ { public static void main(String[] args) { // Integer and Boolean wrapper class examples Integer obj1 = Integer.valueOf(3); Double obj2 = Double.valueOf(5.5); Boolean obj3 = Boolean.valueOf(true);
int var1 = obj1;
double var2 = obj2;
boolean var3 = obj3;
System.out.println(var1 + var2 + (var3 ? 1 : 0));
}
}
interface Shape { double area(); double perimeter(); }
class Circle implements Shape { double radius;
}
class Rectangle implements Shape { double length, width;
}
public class Main { public static void main(String[] args) { Circle circle = new Circle(5.0); Rectangle rectangle = new Rectangle(4.0, 7.0); System.out.println("Circle Area: " + circle.area()); System.out.println("Circle Perimeter: " + circle.perimeter()); System.out.println("Rectangle Area: " + rectangle.area()); System.out.println("Rectangle Perimeter: " + rectangle.perimeter()); } }