0% found this document useful (0 votes)
14 views

Final Codes Wagera

The document contains multiple Java classes demonstrating various programming concepts including object-oriented programming, method overloading, inheritance, abstract classes, interfaces, exception handling, and threading. Key examples include calculations for area and perimeter of shapes, leap year determination, and custom exception handling. It also showcases the use of static methods, constructors, and the implementation of interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Final Codes Wagera

The document contains multiple Java classes demonstrating various programming concepts including object-oriented programming, method overloading, inheritance, abstract classes, interfaces, exception handling, and threading. Key examples include calculations for area and perimeter of shapes, leap year determination, and custom exception handling. It also showcases the use of static methods, constructors, and the implementation of interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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();
c.radius = 5.0;
r.length = 4;
r.breadth = 6;
System.out.println("Area of Circle: " + c.Area(c.radius));
System.out.println("Perimeter of Circle: " + c.Perimeter(c.radius));
System.out.println("Area of Rectangle: " + r.Area(r.length,
r.breadth));
System.out.println("Perimeter of Rectangle: " + r.Perimeter(r.length,
r.breadth));
}
}
//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() {
System.out.println("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();
obj.finalMethod();
System.out.println("Value of final variable a: " + obj.a);

}
}

import java.util.Scanner;

public class Concat {


public static void main(String args[]){
Scanner sc = new Scanner(System.in);

System.out.print("Enter first name:");


final String first = sc.nextLine();

System.out.print("Enter last name:");


final String last = sc.nextLine();

final String full = first + " " + last;


System.out.println(full);

}
}
public class CommandLine{
/* public static void main(String args[]) {
int i;
int sum = 0;
for (i = 0; i < args.length; i++) {
sum += Integer.parseInt(args[i]);
}
System.out.println("Sum of command line arguments: " + sum);
} */

/* public static void main(String args[]) {


String arg1 = args[0];
String arg2 = args[1];
String arg3 = args[2];
System.out.println(arg1 + " " + arg2 + " " + arg3);
}*/

public static void main(String args[]) {


String arg1 = args[0];
String arg2 = args[1];
int arg3 = Integer.parseInt(args[2]);
System.out.println(arg1 + " " + arg2 + " " + arg3);
}
}

import java.util.Scanner;
// 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(System.in);


System.out.print("Enter a year: ");
int year = sc.nextInt();
sc.close();

String result = ((year % 4 == 0 && year % 100 != 0) || (year % 400 ==


0)) ? "Leap Year" : "Not a Leap Year";
System.out.println(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;
System.out.println("Addition: " + calc.add(a, b));
System.out.println("Subtraction: " + calc.subtract(a, b));
System.out.println("Multiplication: " + calc.multiply(a, b));
System.out.println("Division: " + calc.divide(a, b));

}
}

import java.util.Scanner;

public class factorial {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Enter a number: ");
int num = sc.nextInt();
sc.close();
int result = 1;

for (int i = 1; i <= num; i++) {


result *= i;
}

System.out.println("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[numbers.length];

for (int i=0; i<numbers.length; i++){


rev[i] = numbers[numbers.length-i-1];
}

System.out.print("Reversed array: ");


for (int i=0; i<rev.length; i++){
System.out.print(rev[i] + " ");
}
System.out.println();
}
}

public class Palindrome{


public static void main(String args[]) {
String str = args[0];
String rev = "";

for (int i = str.length() - 1; i >= 0; i--) {


rev += str.charAt(i);
}

if (str.equals(rev)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}

class Bhaiya {
int age = 40;

Bhaiya() {
System.out.println("Bhaiya constructor called");
}

void about1() {
System.out.println("Papa hu main, pure duniya ka papa!");
}
}

class Munna extends Bhaiya {


int age = 20;

Munna() {
super(); // Calls Bhaiya's constructor
System.out.println("Munna constructor called");
}

@Override
void about1() {
System.out.println("Bhaiya Bacahaooo!!");
super.about1(); // Call Bhaiya's about1()
System.out.println("Bhaiya's age is: " + super.age); // Access
Bhaiya's variable
System.out.println("Munna's age is: " + this.age); // 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();
System.out.println(mo.mul(5, 6)); // prints 30
System.out.println(mo.mul(10,10,10));
}
}

class Bhaiya {
public void about1(){
System.out.println("Papa hu main, pure duniya ka papa!");
}
}

class Munna extends Bhaiya {


@Override

public void about1(){


System.out.println("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() {
System.out.println("Default Constructor");
}
ConstructorOverloading(int a) {
System.out.println("Parameterized Constructor with int: " + a);
}
ConstructorOverloading(String s) {
System.out.println("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";
System.out.println("Static block of Europe class executed");
}

}
class Test{
public static void main(String args[]){
Europe e1 = new Europe();

e1.country="Brazil";
e1.shirt=11;

Europe e2 = new Europe();


e2.country="Espana";
e2.shirt=19;

System.out.println("Country: " + e1.country + ", Shirt: " + e1.shirt +


", Club: " + Europe.Club);
System.out.println("Country: " + e2.country + ", Shirt: " + e2.shirt +
", Club: " + Europe.Club);
}
}

abstract class Animal{


int age;
String name;
public abstract void makenoise();
}

class Dog extends Animal{


public void makenoise(){
System.out.println("Bhaw Bhaw");
}
}

class Test{
public static void main(String[] args) {
Dog d = new Dog();
d.name="Tommy";
d.age=7;
d.makenoise();
System.out.println("Name:" + d.name + " " + "Age:" + d.age);

}
}
class Bhaiya {
public void about1(){
System.out.println("Papa hu main, pure duniya ka papa!");
}
}

class Munna extends Bhaiya {


public void about2(){
System.out.println("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() {


System.out.println("Hello from show method of interface A");
}
public void config() {
System.out.println("Hello from config method of interface A");
}
}
class Test {
public static void main(String args[]) {
B b = new B();
b.show();
b.config();
System.out.println("Value of num: " + b.num); // Accessing interface
variable
System.out.println("Value of s: " + b.s); // Accessing interface
variable
}
}

interface A{

void hola();
void bhola();
}

class B implements A{

public void hola(){


System.out.println("Hola");
}

public void bhola(){


System.out.println("bhola");
}
}

class C extends B{

@Override
public void hola(){
System.out.println("Hola from C");
}

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

C c = new C();
c.hola(); // Output: Hola from C
c.bhola(); // Output: bhola from B
}
}

abstract class Appliance{


abstract void turnOn();
}

class Fan extends Appliance{


void turnOn(){
System.out.println("Fan is turned on.");
}
}

class TV extends Appliance{


void turnOn(){
System.out.println("TV is turned on.");
}
}

class Main{
public static void main(String args[]){
Fan fan = new Fan();
TV tv = new TV();
fan.turnOn(); // Output: Fan is turned on.
tv.turnOn(); // 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) {


this.hoursWorked = hoursWorked;
this.hourlyRate = hourlyRate;
System.out.println("Credentials Acquired!");
}

void calculateSalary(int hoursWorked, int hourlyRate) {


int salary = hoursWorked * hourlyRate;
System.out.println("Full-time employee salary: " + salary);
}
}

class Main{
public static void main(String args[]) {
FullTimeEmployee emp = new FullTimeEmployee(40, 20);
emp.calculateSalary(emp.hoursWorked, emp.hourlyRate); // Output: Full-
time employee salary: 800
}
// Output: Credentials Acquired!
}

class Task extends Thread {


public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(getName() + " - Count: " + i);
}
}

public static void main(String[] args) {


Task t1 = new Task();
Task t2 = new Task();
t1.start();
t2.start();
}
}

// 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) {
System.out.println("Negative value exception caught: " +
e.getMessage());
}
finally {
System.out.println("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) {


this.hoursWorked = hoursWorked;
this.hourlyRate = hourlyRate;
System.out.println("Credentials Acquired!");
}

void calculateSalary(int hoursWorked, int hourlyRate) {


int salary = hoursWorked * hourlyRate;
System.out.println("Full-time employee salary: " + salary);
}
}

class Main{
public static void main(String args[]) {
FullTimeEmployee emp = new FullTimeEmployee(40, 20);
emp.calculateSalary(emp.hoursWorked, emp.hourlyRate); // Output: Full-
time employee salary: 800
}
// Output: Credentials Acquired!
}

class Box {

int value;
Box(int value) {
this.value = value;
}

Box doubleValue(Box b) {
return new Box(b.value * 2);
}
public static void main(String[] args) {
Box b1 = new Box(10);
Box b2 = b1.doubleValue(b1);
System.out.println("Doubled Value: " + b2.value);
}
}

You might also like