[Link] to add all Odd numbers Between 1 to10.
Solution:
public class odd
{
public static void main(String[] args)
{
int i;
for(i=1;i<=10;i++){
if(i%2!=0)
[Link](i);
}
}
}
[Link] to find out Factorial of a number using Recurrsion.
Solution:
import [Link];
class FactorialDemo{
public static void main(String args[]){
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number:");
int num = [Link]();
int factorial = fact(num);
[Link]("Factorial of entered number is: "+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
output = fact(n-1)* n;
return output;
}
}
[Link] to Print Fibonacci series.
Solution:
class Fibonacci{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
}
}
}
[Link] to implement Constructor overloading.
Solution:
class Employee
{
int age;
String name;
Employee()
{
age =100;
name="Test1";
}
Employee(int age,String name)
{
[Link] =age;
[Link]=name;
}
public void disp()
{
[Link]("Name : "+name+" Age : "+age);
}
}
public class ConstructorOverloading
{
public static void main(String args[])
{
Employee e1 = new Employee();
Employee e2 = new Employee(10,"Test2");
[Link]();
[Link]();
}
}
[Link] to count the of Object created in a program.
Solution:
import [Link].*;
class abc
{
static int count;
abc()
{
count++;
[Link]("objects"+count);
}
}
class mn
{
public static void main(String s[])
{
abc a1
=
new abc();
abc a2
=
new abc();
}
}
[Link] to show call by value and call by reference.
Solution Call by Value:
import [Link].*;
class abc
{
static int count;
abc()
{
count++;
[Link]("objects"+count);
}
}
class mn
{
public static void main(String s[])
{
abc a1
=
new abc();
abc a2
=
new abc();
}
}
Solution Call by Reference:
class Operation1{
int data=50;
void change(int data){
data=data+100;
}
public static void main(String args[]){
Operation1 op=new Operation1();
[Link]("before change "+[Link]);
[Link](500);
[Link]("after change "+[Link]);
}
}
[Link] to implement Method Overriding and Method Ovrloading.
Solution Method Overriding:
class A
{
void cal(double x)
{
[Link]("square value="+(x*x));
}
}
class B extends A
{
void cal(double x)
{
[Link]("square root="+[Link](x));
}
}
class PolymorphismOvrng
{
public static void main(String args[])
{
A a=new A();
[Link](15);
}
}
Solution Method Overloading:
class Sum
{
void add(int a, int b)
{
[Link]("Sum of two="+(a+b));
}
void add(int a, int b,int c)
{
[Link]("Sum of three="+(a+b+c));
}
}
class Polymorphism
{
public static void main(String args[])
{
Sum s=new Sum();
[Link](10,15);
[Link](10,20,30);
}
}
[Link] a class Box having height, weight, depth as the instance variable
&calculate its its volume. Implement constructor overloading in it. Create a
subclass named box_new that has weight as an instance variable. Use super in
the box_new class to initialize members of the base class.
Solution:
class Box
{
double width,height,depth;
Box()
{
width=height=depth=-1;
}
Box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
Box(double len)
{
width=height=depth=len;
}
double volume()
{return width*height*depth;
}
}
class overloadConstructor
{
public static void main(String args[])
{
Box mybox1=new Box();
Box mybox2=new Box(10,20,15);
Box mybox3=new Box(7);
double vol;
vol=[Link]();
[Link]("Volume is: "+vol);
vol=[Link]();
[Link]("Volume is: "+vol);
vol=[Link]();
[Link]("Volume is: "+vol);
}
}
[Link] to implement Runtime Polymorphism.
Solution:
class Vehicle{
public void drive(){
[Link]("Driving vehicle ...");
}
}
class Car extends Vehicle{
@Override
public void drive(){
[Link]("Driving car...");
}
}
class Truck extends Vehicle{
@Override
public void drive(){
[Link]("Driving truck...");
}
public void load(){
[Link]("Loading truck...");
}
}
public class RunTimePolymorphism {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
[Link]();
Vehicle carVehicle = new Car();
[Link]();
Vehicle truckVehicle = new Truck();
[Link]();
Truck truck = new Truck();
[Link]();
}
}
12. WAP to implement interface. Create an interface named Shape having area () &
perimeter () as its methods. Create three classes circle, rectangle & square that implement this
interface.
Solution:
import [Link].*;
interface shapes
{
void area();
void perimeter();
}
class circle implements shapes
{
int r;
double pi=3.14,area,j;
Scanner s=new Scanner([Link]);
public void area()
{
[Link]("Enter the radius of CIRCLE");
r=[Link]();
area=pi*r*r;
[Link]("Area of CIRCLE is:"+area);
}
public void perimeter()
{
j=2*pi*r;
[Link]("Perimeter of CIRCLE is:"+j);
}
}
class rectangle implements shapes
{
int l,b,a,p;
Scanner s=new Scanner([Link]);
Scanner t=new Scanner([Link]);
public void area()
{
[Link]("Enter the length");
l=[Link]();
[Link]("Enter the breadth");
b=[Link]();
a=l*b;
[Link]("Area of RECTANGLE is:"+a);
}
public void perimeter()
{
p=2+(l*b);
[Link]("Perimeter of RECTANGLE is:"+p);
}
}
class square implements shapes
{
int a,b,c;
Scanner s=new Scanner([Link]);
public void area()
{
[Link]("Enter the side");
a=[Link]();
b=a*a;
[Link]("Area of SQUARE is:"+b);
}
public void perimeter()
{
c=4*a;
[Link]("Perimeter of SQUARE is:"+c);
}
}
class shape
{
public static void main(String args[])
{
circle c=new circle();
[Link]();
[Link]();
rectangle r=new rectangle();
[Link]();
[Link]();
square s=new square();
[Link]();
[Link]();
}
}
13. Wap to show multiple inheritance.
Solution:
interface Exam {
void Percent_cal();
}
class Student {
String name;
int roll_no, Marks1, Marks2;
Student(String n, int rn, int m1, int m2) {
name = n;
roll_no = rn;
Marks1 = m1;
Marks2 = m2;
}
void show() {
[Link]("Student Name : "+name);
[Link]("Roll no : "+roll_no);
[Link]("Marks1 : "+Marks1);
[Link]("Marks2 : "+Marks2);
}
}
class Result extends Student implements Exam {
float per;
Result(String n,int rn,int m1,int m2) {
super(n,rn,m1,m2);
}
public void Percent_cal() {
int tot = Marks1 + Marks2;
per = (float)tot / 2;
}
void display() {
show();
[Link]("Percentage = "+per);
}
}
public class StudentDetails {
public static void main (String[] args) {
Result r = new Result("Aashish",11,75,95);
r.Percent_cal();
[Link]();
}
}
14. Wap to implement Exception Handling. Use Try, Catch & Finally.
Solution:
class TestExceptions {
public static void main(String args[]) {
int num1, num2;
try {
num1 = 0;
num2 = 62 / num1;
[Link](num2);
[Link]("Hey I'm at the end of try block");
}
catch (ArithmeticException e) {
[Link]("You should not divide a number by zero");
}
catch (Exception e) {
[Link]("Exception occurred");
}
[Link]("I'm out of try-catch block in Java.");
}
}
5. WAP that creates a class Accounts with following details:
Instance variables: ac_no., name, ac_name, balance
Methods: withdrawal (), deposit (), display ().Use constructors to
initialize members.
import [Link].*;
class accounts
{
Scanner s=new Scanner([Link]);
Scanner t=new Scanner([Link]);
Scanner p=new Scanner([Link]);
Scanner m=new Scanner([Link]);
int accno,ch;
String name,acname;
double bal,amt;
void withdrawal()
{
[Link]("enter the amount to be withdrawn:");
amt=[Link]();
bal=bal-amt;
[Link]("updated balance is"+bal);
}
void deposit()
{
[Link]("enter the amount to be deposit:");
amt=[Link]();
bal=bal+amt;
[Link]("updated balance is:"+bal);
}
void display()
{
[Link]("enter your name:");
name=[Link]();
[Link]("enter account number:");
accno=[Link]();
[Link]("enter the account name:");
acname=[Link]();
[Link]("enter the balance");
bal=[Link]();
[Link]("your name is:"+name);
[Link]("your accont no is:"+accno);
[Link]("your account name is:"+acname);
[Link]("your balance is:"+bal);
[Link]("press 1 for withdrawal or press 2 for deposit");
ch=[Link]();
switch(ch)
{
case 1:withdrawal();
break;
case 2:deposit();
break;
default:[Link]("wrong choice entered");
}
}
}
class accountanother
{
public static void main(String args[])
{
accounts a=new accounts();
[Link]();
}
}
15. Create a user defined exception named “NoMatchException” wich gets
triggered when someone enters a string which is not india.
import [Link];
class NoMatchException extends Exception{
NoMatchException(String s){
super(s);
}
}
class testexception{
static void validate(String country)throws NoMatchException{
if(country=="india")
[Link]("welcome indian.");
else
throw new NoMatchException("not valid");
}
public static void main(String args[]){
try{
Scanner sc=new Scanner([Link]);
[Link]("Enter your country name");
validate([Link]());
}catch(Exception m){[Link]("Exception occured: "+m);}
[Link]("");
}
}