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

task 3

The document contains multiple Java programs demonstrating different object-oriented programming concepts. It includes a program for class mechanisms with methods for arithmetic operations, method overloading for summing integers, constructor implementation with default values, and constructor overloading for an Employee class. Each program is structured with a main method that invokes the respective functionalities.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

task 3

The document contains multiple Java programs demonstrating different object-oriented programming concepts. It includes a program for class mechanisms with methods for arithmetic operations, method overloading for summing integers, constructor implementation with default values, and constructor overloading for an Employee class. Each program is structured with a main method that invokes the respective functionalities.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

a) Write a JAVA program to implement class mechanism.

Create a class,
methods and invoke them inside main method.

import java.u l.*;


public class Fun
{
int n1,n2;
int add,sub,mul ,div,rem;
public sta c void main(String args[])
{
Fun ref =new Fun();
ref.input();
ref.process();
ref.output();
}
void input()
{
Scanner r =new Scanner(System.in);
System.out.println("Enter Two numbers: ");
n1=r.nextInt();
n2=r.nextInt();
}
void process()
{
add =n1+n2;
sub =n1-n2;
mul =n1*n2;
div =n1/n2;
rem =n1%n2;
}
void output()
{
System.out.println("Sum of two numbers: "+add);
System.out.println("Sub of two numbers: "+sub);
System.out.println("Mul of two numbers: "+mul );
System.out.println("Div of two numbers: "+div);
System.out.println("Rem of two numbers: "+rem);
}}
b. Write a JAVA program implement method overloading.

public class Overloadex


{
public void Sum(int a,int b)
{
System.out.println(a+b);
}
public void Sum(int a, int b, int c)
{
System.out.println(a+b+c);
}
public sta c void main(String args[])
{
Overloadex oe = new Overloadex();
oe.Sum(10,30);
oe.Sum(51,2,4);
}
}

c. Write a JAVA program to implement constructor.

class A
{
int a;
String name;
A()
{
a=0; name=null;
}
void show()
{
System.out.print(a+" "+name);
}
}
class B
{
public sta c void main(String args[])
{
A ref =new A();
ref.show();
}
}

d. Write a JAVA program to implement constructor overloading.

class Employee
{
int id;
String name;
int age;
Employee(int x,String n)
{
id =x;
name =n;
}
Employee(int x, String n, int y)
{
id =x;
name =n;
age =y;
}
public void display()
{
System.out.println("id is: "+id);
System.out.println("name is: "+name);
System.out.println("age is: "+age);
}
public sta c void main(String args[])
{
Employee e1 =new Employee(1234, "Kishore");
Employee e2 =new Employee(1285,"Sravani",28);
e1.display();
e2.display();
}
}

You might also like