Final Codes Wagera
class Circle{
public double radius;
public double Area(double radius){
return (3.14 * radius * radius);
}
public double Perimeter(double radius){
return (2*3.14*radius);
}
}
class Rectangle{
public int length;
public int breadth;
public int Area(int length, int breadth){
return (length * breadth);
}
public int Perimeter(int length, int breadth){
return (2*(length + breadth));
}
}
public class PeriArea{
public static void main(String[] args) {
Circle c = new Circle();
Rectangle r = new Rectangle();
[Link] = 5.0;
[Link] = 4;
[Link] = 6;
[Link]("Area of Circle: " + [Link]([Link]));
[Link]("Perimeter of Circle: " + [Link]([Link]));
[Link]("Area of Rectangle: " + [Link]([Link],
[Link]));
[Link]("Perimeter of Rectangle: " + [Link]([Link],
[Link]));
}
}
//Final Class - No inheritance allowed
//Finalmethods - No overriding allowed
//Final variables - No reassigning allowed
final class FinalClass { // Final class, cannot be inherited
final void finalMethod() {
[Link]("This is a final method and cannot be
overridden.");
}
final int a = 10; // Final variable, cannot be reassigned
class Test{
public static void main(String[] args) {
FinalClass obj = new FinalClass();
[Link]();
[Link]("Value of final variable a: " + obj.a);
}
}
import [Link];
public class Concat {
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
[Link]("Enter first name:");
final String first = [Link]();
[Link]("Enter last name:");
final String last = [Link]();
final String full = first + " " + last;
[Link](full);
}
}
public class CommandLine{
/* public static void main(String args[]) {
int i;
int sum = 0;
for (i = 0; i < [Link]; i++) {
sum += [Link](args[i]);
}
[Link]("Sum of command line arguments: " + sum);
} */
/* public static void main(String args[]) {
String arg1 = args[0];
String arg2 = args[1];
String arg3 = args[2];
[Link](arg1 + " " + arg2 + " " + arg3);
}*/
public static void main(String args[]) {
String arg1 = args[0];
String arg2 = args[1];
int arg3 = [Link](args[2]);
[Link](arg1 + " " + arg2 + " " + arg3);
}
}
import [Link];
// This program checks if a given year is a leap year or not.
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
[Link]();
String result = ((year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0)) ? "Leap Year" : "Not a Leap Year";
[Link](year + " is " + result);
}
}
class Calc {
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 int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return a / b;
}
public class BasicCalc {
public static void main(String[] args) {
Calc calc = new Calc();
int a = 10;
int b = 5;
[Link]("Addition: " + [Link](a, b));
[Link]("Subtraction: " + [Link](a, b));
[Link]("Multiplication: " + [Link](a, b));
[Link]("Division: " + [Link](a, b));
}
}
import [Link];
public class factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link]();
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
[Link]("Factorial of " + num + " is: " + result);
}
}
public class array {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int[] rev = new int[[Link]];
for (int i=0; i<[Link]; i++){
rev[i] = numbers[[Link]-i-1];
}
[Link]("Reversed array: ");
for (int i=0; i<[Link]; i++){
[Link](rev[i] + " ");
}
[Link]();
}
}
public class Palindrome{
public static void main(String args[]) {
String str = args[0];
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
if ([Link](rev)) {
[Link](str + " is a palindrome.");
} else {
[Link](str + " is not a palindrome.");
}
}
}
class Bhaiya {
int age = 40;
Bhaiya() {
[Link]("Bhaiya constructor called");
}
void about1() {
[Link]("Papa hu main, pure duniya ka papa!");
}
}
class Munna extends Bhaiya {
int age = 20;
Munna() {
super(); // Calls Bhaiya's constructor
[Link]("Munna constructor called");
}
@Override
void about1() {
[Link]("Bhaiya Bacahaooo!!");
super.about1(); // Call Bhaiya's about1()
[Link]("Bhaiya's age is: " + [Link]); // Access
Bhaiya's variable
[Link]("Munna's age is: " + [Link]); // Access
Munna's variable
}
}
public class Super {
public static void main(String[] args) {
Munna m = new Munna(); // Constructor chain runs
m.about1(); // Method chain runs
}
}
public class MethodOverloading {
public int mul(int a,int b){
return a*b;
}
public int mul (int a,int b,int c){
return a*b*c;
}
}
class Test{
public static void main(String args[]){
MethodOverloading mo = new MethodOverloading();
[Link]([Link](5, 6)); // prints 30
[Link]([Link](10,10,10));
}
}
class Bhaiya {
public void about1(){
[Link]("Papa hu main, pure duniya ka papa!");
}
}
class Munna extends Bhaiya {
@Override
public void about1(){
[Link]("Bhaiya Bacahaooo!!");
super.about1(); //Forcefully calling the parent class method
}
class Test{
public static void main(String args[]){
Munna m = new Munna();
m.about1(); // prints "Bhaiya Bacahaooo!!"
}
}
public class ConstructorOverloading {
ConstructorOverloading() {
[Link]("Default Constructor");
}
ConstructorOverloading(int a) {
[Link]("Parameterized Constructor with int: " + a);
}
ConstructorOverloading(String s) {
[Link]("Parameterized Constructor with String: " + s);
}
}
class Test{
public static void main(String[] args) {
ConstructorOverloading obj1 = new ConstructorOverloading();
ConstructorOverloading obj2 = new ConstructorOverloading(10);
ConstructorOverloading obj3 = new ConstructorOverloading("Spandan
Certified Java Programmer");
}
}
//Basically, Static is a class that contains static methods and variables.
// Static methods and variables are shared among all instances of the class.
// Static methods and variables can be accessed without creating an instance
of the class.
class Europe{
static String Club;
int shirt;
String country;
static {
Club = "Barcelona";
[Link]("Static block of Europe class executed");
}
}
class Test{
public static void main(String args[]){
Europe e1 = new Europe();
[Link]="Brazil";
[Link]=11;
Europe e2 = new Europe();
[Link]="Espana";
[Link]=19;
[Link]("Country: " + [Link] + ", Shirt: " + [Link] +
", Club: " + [Link]);
[Link]("Country: " + [Link] + ", Shirt: " + [Link] +
", Club: " + [Link]);
}
}
abstract class Animal{
int age;
String name;
public abstract void makenoise();
}
class Dog extends Animal{
public void makenoise(){
[Link]("Bhaw Bhaw");
}
}
class Test{
public static void main(String[] args) {
Dog d = new Dog();
[Link]="Tommy";
[Link]=7;
[Link]();
[Link]("Name:" + [Link] + " " + "Age:" + [Link]);
}
}
class Bhaiya {
public void about1(){
[Link]("Papa hu main, pure duniya ka papa!");
}
}
class Munna extends Bhaiya {
public void about2(){
[Link]("Bhaiya Bacahaooo!!");
}
}
class Test{
public static void main(String args[]){
Munna m = new Munna();
m.about1();
m.about2();
}
}
interface A { // We acn use interaface if we have abstact class with
abstract methods
void show(); //By Default these are abstract methods
void config(); //We cannont instantiate interface
int num = 10;
String s = "Hello"; //We can declare variables in interface but they are by
default static and final
} //A a = new A(); // This is not allowed
class B implements A { //We can implement interface
public void show() {
[Link]("Hello from show method of interface A");
}
public void config() {
[Link]("Hello from config method of interface A");
}
}
class Test {
public static void main(String args[]) {
B b = new B();
[Link]();
[Link]();
[Link]("Value of num: " + [Link]); // Accessing interface
variable
[Link]("Value of s: " + b.s); // Accessing interface
variable
}
}
interface A{
void hola();
void bhola();
}
class B implements A{
public void hola(){
[Link]("Hola");
}
public void bhola(){
[Link]("bhola");
}
}
class C extends B{
@Override
public void hola(){
[Link]("Hola from C");
}
class Main{
public static void main(String[] args) {
C c = new C();
[Link](); // Output: Hola from C
[Link](); // Output: bhola from B
}
}
abstract class Appliance{
abstract void turnOn();
}
class Fan extends Appliance{
void turnOn(){
[Link]("Fan is turned on.");
}
}
class TV extends Appliance{
void turnOn(){
[Link]("TV is turned on.");
}
}
class Main{
public static void main(String args[]){
Fan fan = new Fan();
TV tv = new TV();
[Link](); // Output: Fan is turned on.
[Link](); // Output: TV is turned on.
}
}
abstract class Employee{
abstract void calculateSalary(int hoursWorked, int hourlyRate);
}
class FullTimeEmployee extends Employee {
int hoursWorked;
int hourlyRate;
FullTimeEmployee(int hoursWorked, int hourlyRate) {
[Link] = hoursWorked;
[Link] = hourlyRate;
[Link]("Credentials Acquired!");
}
void calculateSalary(int hoursWorked, int hourlyRate) {
int salary = hoursWorked * hourlyRate;
[Link]("Full-time employee salary: " + salary);
}
}
class Main{
public static void main(String args[]) {
FullTimeEmployee emp = new FullTimeEmployee(40, 20);
[Link]([Link], [Link]); // Output: Full-
time employee salary: 800
}
// Output: Credentials Acquired!
}
class Task extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](getName() + " - Count: " + i);
}
}
public static void main(String[] args) {
Task t1 = new Task();
Task t2 = new Task();
[Link]();
[Link]();
}
}
// Custom exception class
class NegativeValueException extends Exception {
NegativeValueException(String message) {
super(message);
}
}
public class exception1 {
public static void main(String[] args) {
try {
int a = -1;
if (a < 0) {
throw new NegativeValueException("Value cannot be negative!");
}
}
catch(NegativeValueException e) {
[Link]("Negative value exception caught: " +
[Link]());
}
finally {
[Link]("Finally block executed.");
}
}
}
abstract class Employee{
abstract void calculateSalary(int hoursWorked, int hourlyRate);
}
class FullTimeEmployee extends Employee {
int hoursWorked;
int hourlyRate;
FullTimeEmployee(int hoursWorked, int hourlyRate) {
[Link] = hoursWorked;
[Link] = hourlyRate;
[Link]("Credentials Acquired!");
}
void calculateSalary(int hoursWorked, int hourlyRate) {
int salary = hoursWorked * hourlyRate;
[Link]("Full-time employee salary: " + salary);
}
}
class Main{
public static void main(String args[]) {
FullTimeEmployee emp = new FullTimeEmployee(40, 20);
[Link]([Link], [Link]); // Output: Full-
time employee salary: 800
}
// Output: Credentials Acquired!
}
class Box {
int value;
Box(int value) {
[Link] = value;
}
Box doubleValue(Box b) {
return new Box([Link] * 2);
}
public static void main(String[] args) {
Box b1 = new Box(10);
Box b2 = [Link](b1);
[Link]("Doubled Value: " + [Link]);
}
}