100% found this document useful (1 vote)
371 views

Java Prac File Updated

The document contains 14 code snippets with solutions to programming problems involving Java concepts like loops, recursion, constructors, inheritance, polymorphism, exceptions, interfaces, and more. Specifically: 1) A program that prints odd numbers from 1 to 10 using a for loop. 2) A recursive program to calculate the factorial of a number. 3) A program that prints the Fibonacci series. 4) Examples demonstrating constructor overloading and counting object instances. 5) Examples showing call by value vs call by reference. 6) Examples implementing method overriding and overloading. 7) A Box class with subclasses demonstrating constructor overloading. 8) A program demonstrating runtime polymorphism. 9

Uploaded by

Shaurya Kapoor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
371 views

Java Prac File Updated

The document contains 14 code snippets with solutions to programming problems involving Java concepts like loops, recursion, constructors, inheritance, polymorphism, exceptions, interfaces, and more. Specifically: 1) A program that prints odd numbers from 1 to 10 using a for loop. 2) A recursive program to calculate the factorial of a number. 3) A program that prints the Fibonacci series. 4) Examples demonstrating constructor overloading and counting object instances. 5) Examples showing call by value vs call by reference. 6) Examples implementing method overriding and overloading. 7) A Box class with subclasses demonstrating constructor overloading. 8) A program demonstrating runtime polymorphism. 9

Uploaded by

Shaurya Kapoor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

1.Wap 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)
System.out.println(i);
}
}
}
2.Wap to find out Factorial of a number using Recurrsion.
Solution:
import java.util.Scanner;
class FactorialDemo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
int num = scanner.nextInt();
int factorial = fact(num);
System.out.println("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;
}
}
4.Wap to Print Fibonacci series.
Solution:
class Fibonacci{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.println(n1+" "+n2);

for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.println(" "+n3);
n1=n2;
n2=n3;
}
}
}
6.Wap to implement Constructor overloading.
Solution:
class Employee
{
int age;
String name;

Employee()
{
age =100;
name="Test1";
}

Employee(int age,String name)


{
this.age =age;
this.name=name;
}

public void disp()


{
System.out.println("Name : "+name+" Age : "+age);
}

}
public class ConstructorOverloading
{
public static void main(String args[])
{
Employee e1 = new Employee();
Employee e2 = new Employee(10,"Test2");

e1.disp();
e2.disp();
}
}
7.Wap to count the of Object created in a program.
Solution:
import java.io.*;
class abc
{
static int count;
abc()
{
count++;
System.out.println("objects"+count);
}
}
class mn
{
public static void main(String s[])
{
abc a1
=
new abc();
abc a2
=
new abc();
}
}
8.Wap to show call by value and call by reference.
Solution Call by Value:
import java.io.*;
class abc
{
static int count;
abc()
{
count++;
System.out.println("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();

System.out.println("before change "+op.data);


op.change(500);
System.out.println("after change "+op.data);

}
}
9.Wap to implement Method Overriding and Method Ovrloading.
Solution Method Overriding:
class A
{
void cal(double x)
{
System.out.println("square value="+(x*x));
}
}
class B extends A
{
void cal(double x)
{
System.out.println("square root="+Math.sqrt(x));
}
}
class PolymorphismOvrng
{
public static void main(String args[])
{
A a=new A();

a.cal(15);
}
}
Solution Method Overloading:
class Sum
{
void add(int a, int b)
{
System.out.println("Sum of two="+(a+b));
}

void add(int a, int b,int c)


{
System.out.println("Sum of three="+(a+b+c));
}
}
class Polymorphism
{
public static void main(String args[])
{
Sum s=new Sum();

s.add(10,15);
s.add(10,20,30);
}
}
10.Create 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=mybox1.volume();
System.out.println("Volume is: "+vol);
vol=mybox2.volume();
System.out.println("Volume is: "+vol);
vol=mybox3.volume();
System.out.println("Volume is: "+vol);
}
}
11.Wap to implement Runtime Polymorphism.
Solution:
class Vehicle{
public void drive(){
System.out.println("Driving vehicle ...");
}
}

class Car extends Vehicle{


@Override
public void drive(){
System.out.println("Driving car...");
}
}

class Truck extends Vehicle{


@Override
public void drive(){
System.out.println("Driving truck...");
}

public void load(){


System.out.println("Loading truck...");
}
}
public class RunTimePolymorphism {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.drive();

Vehicle carVehicle = new Car();


carVehicle.drive();

Vehicle truckVehicle = new Truck();


truckVehicle.drive();

Truck truck = new Truck();


truck.load();
}
}
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 java.util.*;
interface shapes
{
void area();
void perimeter();
}
class circle implements shapes
{
int r;
double pi=3.14,area,j;
Scanner s=new Scanner(System.in);
public void area()
{
System.out.println("Enter the radius of CIRCLE");
r=s.nextInt();
area=pi*r*r;
System.out.println("Area of CIRCLE is:"+area);
}
public void perimeter()
{
j=2*pi*r;
System.out.println("Perimeter of CIRCLE is:"+j);
}
}
class rectangle implements shapes
{
int l,b,a,p;
Scanner s=new Scanner(System.in);
Scanner t=new Scanner(System.in);
public void area()
{
System.out.println("Enter the length");
l=s.nextInt();
System.out.println("Enter the breadth");
b=t.nextInt();
a=l*b;
System.out.println("Area of RECTANGLE is:"+a);
}
public void perimeter()
{
p=2+(l*b);
System.out.println("Perimeter of RECTANGLE is:"+p);
}
}
class square implements shapes
{
int a,b,c;
Scanner s=new Scanner(System.in);
public void area()
{
System.out.println("Enter the side");
a=s.nextInt();
b=a*a;
System.out.println("Area of SQUARE is:"+b);
}
public void perimeter()
{
c=4*a;
System.out.println("Perimeter of SQUARE is:"+c);
}
}
class shape
{
public static void main(String args[])
{
circle c=new circle();
c.area();
c.perimeter();
rectangle r=new rectangle();
r.area();
r.perimeter();
square s=new square();
s.area();
s.perimeter();
}
}
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() {

System.out.println("Student Name : "+name);


System.out.println("Roll no : "+roll_no);
System.out.println("Marks1 : "+Marks1);
System.out.println("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();
System.out.println("Percentage = "+per);
}
}

public class StudentDetails {

public static void main (String[] args) {

Result r = new Result("Aashish",11,75,95);


r.Percent_cal();
r.display();
}
}
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;
System.out.println(num2);
System.out.println("Hey I'm at the end of try block");
}
catch (ArithmeticException e) {

System.out.println("You should not divide a number by zero");


}
catch (Exception e) {

System.out.println("Exception occurred");
}
System.out.println("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 java.util.*;
class accounts
{
Scanner s=new Scanner(System.in);
Scanner t=new Scanner(System.in);
Scanner p=new Scanner(System.in);
Scanner m=new Scanner(System.in);
int accno,ch;
String name,acname;
double bal,amt;
void withdrawal()
{
System.out.println("enter the amount to be withdrawn:");
amt=s.nextDouble();
bal=bal-amt;
System.out.println("updated balance is"+bal);
}
void deposit()
{
System.out.println("enter the amount to be deposit:");
amt=s.nextDouble();
bal=bal+amt;
System.out.println("updated balance is:"+bal);
}
void display()
{
System.out.println("enter your name:");
name=t.next();
System.out.println("enter account number:");
accno=p.nextInt();
System.out.println("enter the account name:");
acname=m.next();
System.out.println("enter the balance");
bal=s.nextDouble();
System.out.println("your name is:"+name);
System.out.println("your accont no is:"+accno);
System.out.println("your account name is:"+acname);
System.out.println("your balance is:"+bal);
System.out.println("press 1 for withdrawal or press 2 for deposit");
ch=p.nextInt();
switch(ch)
{
case 1:withdrawal();
break;
case 2:deposit();
break;
default:System.out.println("wrong choice entered");
}
}
}
class accountanother
{
public static void main(String args[])
{
accounts a=new accounts();
a.display();
}
}
15. Create a user defined exception named “NoMatchException” wich gets
triggered when someone enters a string which is not india.
import java.util.Scanner;
class NoMatchException extends Exception{
NoMatchException(String s){
super(s);
}
}
class testexception{

static void validate(String country)throws NoMatchException{


if(country=="india")
System.out.println("welcome indian.");
else

throw new NoMatchException("not valid");


}

public static void main(String args[]){


try{
Scanner sc=new Scanner(System.in);

System.out.println("Enter your country name");

validate(sc.next());
}catch(Exception m){System.out.println("Exception occured: "+m);}

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

You might also like