Java Prac File Updated
Java Prac File Updated
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";
}
}
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;
}
}
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));
}
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 ...");
}
}
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() {
float per;
Result(String n,int rn,int m1,int m2) {
super(n,rn,m1,m2);
}
void display() {
show();
System.out.println("Percentage = "+per);
}
}
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("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{
validate(sc.next());
}catch(Exception m){System.out.println("Exception occured: "+m);}
System.out.println("");
}
}