0% found this document useful (0 votes)
6 views43 pages

CHALANA CHITRAM FILM FEST

The document contains Java programs for various tasks including printing prime numbers, calculating roots of quadratic equations, generating Fibonacci sequences, and creating a bank account class with functionalities like deposit and withdrawal. Each section includes the program code, expected output, and context for a B.Tech OOP lab course at Sreenidhi Institute of Science and Technology. Additionally, it demonstrates concepts like constructors, static variables, and the use of the 'this' keyword in Java.

Uploaded by

fourmensquad488
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views43 pages

CHALANA CHITRAM FILM FEST

The document contains Java programs for various tasks including printing prime numbers, calculating roots of quadratic equations, generating Fibonacci sequences, and creating a bank account class with functionalities like deposit and withdrawal. Each section includes the program code, expected output, and context for a B.Tech OOP lab course at Sreenidhi Institute of Science and Technology. Additionally, it demonstrates concepts like constructors, static variables, and the use of the 'this' keyword in Java.

Uploaded by

fourmensquad488
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 43

Name of the Student: Rollno:

1(A). Write a program to print prime numbers up to a given number.


Program:
import java.util.Scanner;
class Prime
{
public static void main(String args[])
{
int i, j, n, count = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number");
n = s.nextInt ();
System.out.println("The prime numbers upto" + n + "are...");
for (j = 2; j <= n; j++)
{
for (i = 1, count = 0; i <= j; i++)
{
if (j % i == 0)
{
count++;
}
}
if (count == 2)
{
System.out.print ("\t" + j);
}
}
}
}
Ouput:
Enter the number: 7
The prime numbers upto 7 are...
2 3 5 7

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
1
Name of the Student: Rollno:

1(B). Write a program to print roots of a quadratic equation ax2+bx+c=0


Program:
import java.util.Scanner;
class Roots
{
public static void main(String args[])
{
int a, b, c;
double d,k1, k2;
Scanner s = new Scanner(System.in);
System.out.println("Enter the coefficients of x^2 , x and constant");
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
d = (b * b)-(4 * a * c);
if(d > 0)
{
System.out.println ("roots are real and unequal");
k1 = (-b) + Math.sqrt (d) / (2 * a);
k2 = (-b) - Math.sqrt (d) / (2 * a);
System.out.println ("roots are " + k1 + "\t" + k2);
}
else if(d == 0)
{
System.out.println ("roots are real and equal");
k1 = (-b) / (2 * a);
System.out.println ("roots are " + k1 + "\t" + k1);
}
else
{
System.out.println ("roots are imaginary");
}
}
}

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
2
Name of the Student: Rollno:

Ouput:
Enter the coefficients of x^2 , x and constant
261
roots are real and unequal
roots are -4.677124344467705 -7.322875655532295

1(C). Write a program to print Fibonacci sequence up to a given number.


Program:
import java.util.Scanner;
class Fibo
{
public static void main(String args[])
{
int a=0,b=1,c=1,n;
Scanner s=new Scanner(System.in);
System.out.print("enter the number upto which fibonacci series is to be
printed:");
n= s.nextInt();
System.out.print(a+"\t"+b);
do
{
System.out.print("\t"+c);
a=b;
b=c;
c=a+b;
}while(c<=n);
}
}
Ouput:
Enter the number upto which fibonacci series is to be printed: 13
Fibonacci series is...
0 1 1 2 3 5 8 13

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
3
Name of the Student: Rollno:

1(D). Write a program to print the following format.

* *

* * *

* * * *
Program:
class Star
{
public static void main(String args[])
{
for(int i=1;i<5;i++)
{
for(int j=3;j>=i;j--)
{
System.out.print(" ");
}
for(int n=1;n<=i;n++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Ouput:
*
**
***
****

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
4
Name of the Student: Rollno:

2(A). Define a class to represent a bank account and include the following
members Instance variables: (i)Name of depositor (ii)Account No
(iii)Type of account (iv)Balance amount in the account. Instance Methods:
(i)To assign instance variables(Constructors-Zero argument and
parameterized) (ii)To deposit an amount (iii)To withdraw amount after
checking the balance (iv)To display name and address.
Define ExecuteAccount class in which define main method to test above class.
Program:
import java.util.Scanner;
class BankAccount
{
String name;
long accno;
String acctype;
double balance;
BankAccount()
{
name="Krishna";
accno=987654;
acctype="Savings";
balance=2020125.50;
}
BankAccount(String n,long an,String at,double b)
{
name=n;
accno=an;
acctype=at;
balance=b;
}
void deposit(double amount)
{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
5
Name of the Student: Rollno:

{
if(amount>balance)
System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{
System.out.println("Account details are...");
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
}
class ExecuteAccountA
{
public static void main(String args[])
{
BankAccount acc1=new BankAccount();
System.out.println("Using default constructor");
acc1.display();
BankAccount acc2=new BankAccount("Sree",56786,"Current",12390.0);
System.out.println("Using Parameter constructor");
acc2.display();
Double amount;
Scanner s=new Scanner(System.in);
System.out.print("Enter the amount to be deposited:");
amount=s.nextDouble();
acc2.deposit(amount);
System.out.print("Enter the amount to be withdrawn:");
amount=s.nextDouble();
acc2.withdraw(amount);
acc2.display();
}

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
6
Name of the Student: Rollno:

}
Ouput:
Using default constructor
Account details are...
Krishna-987654-Savings-2020125.5
Using Parameter constructor
Account details are...
Sree-56786-Current-12390.0
Enter the amount to be deposited:1000
Balance is:13390.0
Enter the amount to be withdrawn:500
Balance is:12890.0
Account details are...
Sree-56786-Current-12890.0
2(B). In the above account class, maintain the total no. of account holders
present in the bank and also define a method to display it. Change the main
method appropriately.
Program:
class BankAccount
{
String name;
long accno;
String acctype;
double balance;
static int count;
BankAccount()
{
name="Krishna";
accno=987654;
acctype="savings";
balance=2020125.50;
count++;
}
BankAccount(String n,long an,String at,double b)
{

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
7
Name of the Student: Rollno:

name=n;
accno=an;
acctype=at;
balance=b;
count++;
}
void deposit(double amount)
{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)
{
if(amount>balance)
System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{
System.out.println("Account details are...");
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
void noAccountHolders()
{
System.out.println("Number of account holders are:"+count);
}
}
class ExecuteAccountB
{
public static void main(String args[])
{

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
8
Name of the Student: Rollno:

BankAccount acc1=new BankAccount();


acc1.noAccountHolders();
BankAccount acc2=new BankAccount("Sree",56786,"Current",12390.0);
acc2.noAccountHolders();
}
}
Ouput:
Number of account holders are: 1
Number of account holders are: 2

2(C). In main method of ExecuteAccount class , define an array to handle five


accounts
Program:
import java.util.Scanner;
class BankAccount
{
String name;
long accno;
String acctype;
double balance;
BankAccount()
{
name="Krishna";
accno=987654;
acctype="savings";
balance=2020125.50;
}
BankAccount(String n,long an,String at,double b)
{
name=n;
accno=an;
acctype=at;
balance=b;
}
void deposit(double amount)

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
9
Name of the Student: Rollno:

{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)
{
if(amount>balance)
System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
}
class ExecuteAccountC
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String name,type;
long no;
double bal;
BankAccount ba[]=new BankAccount[5];
for(int i=0;i<5;i++)
{
System.out.println("Enter Account name,no,type and balance");
name=s.next();
no=s.nextLong();
type=s.next();
bal=s.nextDouble();

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
10
Name of the Student: Rollno:

ba[i]=new BankAccount(name,no,type,bal);
}
System.out.println("Account holders details are");
for(int i=0;i<5;i++)
{
ba[i].display();
}
}
}
Ouput:
Enter Account name,no,type and balance
Ravi 456123 Savings 12390.90
Enter Account name,no,type and balance
Sumith 178923 Current 129056.56
Enter Account name,no,type and balance
Srinee 234156 Savings 13456.89
Enter Account name,no,type and balance
Ranvee 238976 Current 23467.012
Enter Account name,no,type and balance
Nidhi 167834 Savings 127890.98
Account holders details are
Ravi-456123-Savings-12390.9
Sumith-178923-Current-129056.56
Srinee-234156-Savings-13456.89
Ranvee-238976-Current-23467.012
Nidhi-167834-Savings-127890.98

2(D). In Account class constructor, demonstrate the use of "this" keyword


Program:
import java.util.Scanner;
class BankAccount
{
String name;
long accno;
String acctype;

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
11
Name of the Student: Rollno:

double balance;
BankAccount()
{
name="Krishna";
accno=987654;
acctype="savings";
balance=2020125.50;
}
BankAccount(String name,long accno,String acctype,double balance)
{
this.name=name;
this.accno=accno;
this.acctype=acctype;
this.balance=balance;
}
void deposit(double amount)
{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)
{
if(amount>balance)
System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{
System.out.println("Account details are...");
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
12
Name of the Student: Rollno:

}
class ExecuteAccountD
{
public static void main(String args[])
{
BankAccount acc=new BankAccount("Sree",56786,"Current",12390.0);
acc.display();
Double amount;
Scanner s=new Scanner(System.in);
System.out.print("Enter the amount to be deposited:");
amount=s.nextDouble();
acc.deposit(amount);
System.out.print("Enter the amount to be withdrawn:");
amount=s.nextDouble();
acc.withdraw(amount);
acc.display();
}
}
Ouput:
Account details are...
Sree-56786-Current-12390.0
Enter the amount to be deposited:1000
Balance is:13390.0
Enter the amount to be withdrawn:1400
Balance is:11990.0
Account details are...
Sree-56786-Current-11990.0

2(E). Modify the constructor to read data from keyboard


Program:
import java.util.Scanner;
class BankAccount
{
String name;
long accno;

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
13
Name of the Student: Rollno:

String acctype;
double balance;
BankAccount()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter name,accountno,accounttype and balance");
name = s.next();
accno=s.nextLong();
acctype=s.next();
balance=s.nextDouble();
}
BankAccount(String n,long an,String at,double b)
{
name=n;
accno=an;
acctype=at;
balance=b;
}
void deposit(double amount)
{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)
{
if(amount>balance)
System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
14
Name of the Student: Rollno:

System.out.println("Account details are...");


System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
}
class ExecuteAccountE
{
public static void main(String args[])
{
BankAccount acc=new BankAccount();
acc.display();
Double amount;
Scanner s=new Scanner(System.in);
System.out.print("Enter the amount to be deposited:");
amount=s.nextDouble();
acc.deposit(amount);
System.out.print("Enter the amount to be withdrawn:");
amount=s.nextDouble();
acc.withdraw(amount);
acc.display();
}
}
Ouput:
Enter name,accountno,accounttype and balance
Srinee 1678923 Current 129089.90
Account details are...
Srinee-1678923-Current-129089.9
Enter the amount to be deposited:2000
Balance is:131089.9
Enter the amount to be withdrawn:1400
Balance is:129689.9
Account details are...
Srinee-1678923-Current-129689.9

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
15
Name of the Student: Rollno:

2(F). Overload the method deposit() method (one with argument and another
without argument)
Program:
import java.util.Scanner;
class BankAccount
{
String name;
long accno;
String acctype;
double balance;
BankAccount()
{
name="Krishna";
accno=987654;
acctype="savings";
balance=2020125.50;
}
BankAccount(String n,long an,String at,double b)
{
name=n;
accno=an;
acctype=at;
balance=b;
}
void deposit()
{
balance=balance+5000;
System.out.println("Balance is:"+balance);
}
void deposit(double amount)
{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
16
Name of the Student: Rollno:

{
if(amount>balance)
System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{
System.out.println("Account details are...");
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
}
class ExecuteAccountF
{
public static void main(String args[])
{
BankAccount acc1=new BankAccount();
acc1.display();
acc1.deposit();
BankAccount acc2=new BankAccount();
acc2.display();
Double amount;
Scanner s=new Scanner(System.in);
System.out.print("Enter the amount to be deposited:");
amount=s.nextDouble();
acc2.deposit(amount);
}
}
Ouput:
Account details are...
Krishna-987654-savings-2020125.5
Balance is:2025125.5

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
17
Name of the Student: Rollno:

Account details are...


Krishna-987654-savings-2020125.5
Enter the amount to be deposited:1200
Balance is:2021325.5
2(G). In Account class, define set and get methods for each instance variable.
Example:For account no variable, define the methods
getAccountNo() and setAccountNo(long accno). In each and every method of
Account class, reading data from and writing data to instance variables
should be done through these variables.
Program:
class BankAccount
{
String name;
long accno;
String acctype;
double balance;
BankAccount()
{
name="Krishna";
accno=987654;
acctype="savings";
balance=2020125.50;
}
BankAccount(String n,long an,String at,double b)
{
name=n;
accno=an;
acctype=at;
balance=b;
}
void setName(String n)
{
name = n;
}
String getName()

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
18
Name of the Student: Rollno:

{
return name;
}
void setAccountNo(long an)
{
accno = an;
}
long getAccountNo()
{
return accno;
}
void setAccountType(String at)
{
acctype = at;
}
String getAccountType()
{
return acctype;
}
void setBalance(double ba)
{
balance = ba;
}
double getBalance()
{
return balance;
}
void deposit(double amount)
{
balance=balance+amount;
System.out.println("Balance is:"+balance);
}
void withdraw(double amount)
{
if(amount>balance)

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
19
Name of the Student: Rollno:

System.out.println("Insufficient funds...");
else
{
balance=balance-amount;
System.out.println("Balance is:"+balance);
}
}
void display()
{
System.out.println("Account details are...");
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
}
class ExecuteAccountG
{
public static void main(String args[])
{
BankAccount acc=new BankAccount();
acc.setName("Vignya");
acc.setAccountNo(123456);
acc.setAccountType("Current");
acc.setBalance(10000.90);
System.out.println("Account details are...");
System.out.println(acc.getName()+"-"+acc.getAccountNo()+"-"+acc.getAcco
untType()+"-"+acc.getBalance());
acc.deposit(1000);
acc.withdraw(550);
}
}
Ouput:
Account details are...
Vignya-123456-Current-10000.9
Balance is:11000.9
Balance is:10450.9

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
20
Name of the Student: Rollno:

3(A) Define Resistor class in which define the following Instance Variables:
resistance, Instance Methods: giveData(),displayData(). Define Subclasses
called SerieCircuit and ParallelCircuit in which define:
calculateSeriesResistance() and calculateParallelResistance() respectively.
Program:
class Resistor
{
double resistance;
Resistor()
{
resistance=2.35;
}
Resistor(double r)
{
resistance=r;
}
void giveData(double r)
{
resistance=r;
}
void displayData()
{
System.out.println("Resistance is:"+resistance);
}
}
class SeriesCircuit extends Resistor
{
Resistor calculateSeriesResistance(Resistor r1,Resistor r2)
{
Resistor r=new Resistor();
r.resistance =r1.resistance+r2.resistance;
return r;
}
}
class ParallelCircuit extends Resistor

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
21
Name of the Student: Rollno:

{
Resistor calculateParallelResistance(Resistor r1,Resistor r2)
{
Resistor r=new Resistor();
r.resistance =((r1.resistance*r2.resistance)/(r1.resistance+r2.resistance));
return r;
}
}
class ResistorExecuteA
{
public static void main(String args[])
{
Resistor r=new Resistor();
r.displayData();
Resistor r1=new Resistor(1.74);
r1.displayData();
Resistor r2=new Resistor();
r2.giveData(10.2);
r2.displayData();
SeriesCircuit s= new SeriesCircuit();
r=s.calculateSeriesResistance(r1,r2);
System.out.println("Series ");
r.displayData();
ParallelCircuit p=new ParallelCircuit();
r=p.calculateParallelResistance(r1,r2);
System.out.println("Parallel ");
r.displayData();
}
}
Output:
Resistance is:2.35
Resistance is:1.74
Resistance is:10.2
Series Resistance is:11.94
Parallel Resistance is:1.4864321608040199

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
22
Name of the Student: Rollno:

3(B) Modify the above two methods, which should accept array of Resistor objects as
argument and return Resistor Object as result.
Program:
import java.util.Scanner;
class Resistor
{
double resistance;
Resistor()
{
resistance=2.35;
}
Resistor(double r)
{
resistance=r;
}
void giveData(double r)
{
resistance=r;
}
void displayData()
{
System.out.println("Resistance is:"+resistance);
}
}
class SeriesCircuit extends Resistor
{
Resistor calculateSeriesResistance(Resistor r1[])
{
Resistor r=new Resistor();
r.resistance=0;
for(int i=0;i<5;i++)
{
r.resistance +=r1[i].resistance;
}
return r;

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
23
Name of the Student: Rollno:

}
}
class ParallelCircuit extends Resistor
{
Resistor calculateParallelResistance(Resistor r1[])
{
Resistor r=new Resistor();
r.resistance=0;
for(int i=0;i<5;i++)
{
r.resistance +=1/r1[i].resistance;
}
r.resistance=1/ r.resistance;
return r;
}
}
class ResistorExecuteB
{
public static void main(String args[])
{
Resistor r=new Resistor();
Scanner s=new Scanner(System.in);
Resistor resistancearray[]=new Resistor[5];
for(int i=0;i<5;i++)
{
resistancearray[i]=new Resistor();
System.out.println("Enter resistance:");
Double rs=s.nextDouble();
resistancearray[i].giveData(rs);
}
SeriesCircuit s= new SeriesCircuit();
r=s.calculateSeriesResistance(resistancearray);
System.out.println("Series ");
R.displayData();
ParallelCircuit p=new ParallelCircuit();

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
24
Name of the Student: Rollno:

r=p.calculateParallelResistance(resistancearray);
System.out.println("Parallel ");
r.displayData();
}
}
Output:
Enter resistance: 1.25
Enter resistance: 2.24
Enter resistance: 3.14
Enter resistance: 4.25
Enter resistance: 5.16
Series Resistance is: 16.04
Parallel Resistance is: 0.5015064058174367

3(C) Write a program to demonstrate method Overriding.


Program:
class Resistor
{
void displayData()
{
System.out.println("In Resistor class");
}
}
class SeriesCircuit extends Resistor
{
void displayData()
{
System.out.println("In SeriesCircuit class");
}
}
class ParallelCircuit extends Resistor
{
void displayData()
{
System.out.println("In ParallelCircuit class ");

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
25
Name of the Student: Rollno:

}
}
class Override
{
public static void main(String args[])
{
Resistor r=new Resistor();
r.displayData();
SeriesCircuit s= new SeriesCircuit();
s.displayData();
ParallelCircuit p=new ParallelCircuit();
p.displayData();
}
}
Output:
In Resistor class
In SeriesCircuit class
In ParallelCircuit class

3(D)Write a program to demonstrate the uses of “super” keyword.


//First Use: To call Super class constructor
class A
{
int i,j;
A(int ii,int jj)
{
i=ii;
j=jj;
}
}
class B extends A
{
int k;
B(int ii,int jj,int kk)
{

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
26
Name of the Student: Rollno:

super(ii,jj);
k=kk;
}
void sum()
{
System.out.println("Sum of i+j+k is "+(i+j+k));
}
}
class Super1
{
public static void main(String args[])
{
B subObj=new B(10,20,40);
subObj.sum();
}
}
Output:
Sum of i+j+k is 70

//Second use: Access super class members from the sub class
class A
{
int i;
}
class B extends A
{
int i; // this i hides the i in A
B(int a, int b)
{
super.i = a; // i in A
i = b; // i in B
}
void show()
{
System.out.println("i in superclass: " + super.i);

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
27
Name of the Student: Rollno:

System.out.println("i in subclass: " + i);


}
}
class Super2
{
public static void main(String args[])
{
B subOb = new B(10, 20);
subOb.show();
}
}
Output:
i in superclass: 10
i in subclass: 20
3(E) Write a program to demonstrate dynamic method dispatch (i.e. , Dynamic
polymorphism).
Program:
class Resistor
{
void displayData()
{
System.out.println("In Resistor class");
}
}
class SeriesCircuit extends Resistor
{
void displayData()
{
System.out.println("In SeriesCircuit class");
}
}
class ParallelCircuit extends Resistor
{
void displayData()
{

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
28
Name of the Student: Rollno:

System.out.println("In ParallelCircuit class ");


}
}
class DMD
{
public static void main(String args[])
{
Resistor r=new Resistor();
SeriesCircuit s= new SeriesCircuit();
ParallelCircuit p=new ParallelCircuit();
Resistor r1=new Resistor();
r=r1;
r.displayData();
r=s;
r.displayData();
r=p;
r.displayData();
}
}
Output:
In Resistor class
In SeriesCircuit class
In ParallelCircuit class
4(A) Write a program to check whether the given string is palindrome or not
Program:
import java.util.Scanner;
class Palindrome
{
public static void main(String args[])
{
String s,d="";
int l;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string:");
s=sc.next(); //s=sc.nextLine();

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
29
Name of the Student: Rollno:

l=s.length();
for(int i=l-1;i>=0;i--)
{
d=d+s.charAt(i);
}
if(s.equals(d))
System.out.println("String is palindrome");
else
System.out.println("String is not palindrome");
}
}
Output:
Enter the string: malayalam
String is palindrome
Enter the string: snist
String is not palindrome
4(B) Write a program for sorting a given list of names in ascending order
Program:
import java.util.Scanner;
class SortingNames
{
public static void main(String args[])
{
String s[]=new String[10];
String temp="";
int n,i,j;
Scanner sc=new Scanner(System.in);
System.out.print("How many strings u want to sort:");
n=sc.nextInt();
System.out.println("Enter "+n+" strings");
for(i=0;i<n;i++)
{
s[i]=sc.next();
}
for(i=0;i<5;i++)

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
30
Name of the Student: Rollno:

{
for(j=i+1;j<4;j++)
{
if(s[i].compareTo(s[j])>=0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
System.out.println("Sorted strings are...");
for(i=0;i<n;i++)
System.out.print(s[i]+"-");
}
}
Output:
How many strings u want to sort:4
Enter 4 strings
sree nidhi institute technology
Sorted strings are...
institute-nidhi-sree-technology
4(C) Write a program to count the no. of words in a given text
Program:
import java.util.Scanner;
class CountWords
{
public static void main(String args[])
{
String s;
int count=1,l;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
s=sc.nextLine();
l=s.length();

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
31
Name of the Student: Rollno:

for(int i=0;i<l;i++)
{
if(s.charAt(i)==' ')
count++;
}
System.out.println("Number of words:"+count);
}
}
Output:
Enter the string
sree nidhi institute of science and technology
Number of words:7
5(A) Define an interface “GeomtricShape” with methods area( ) and
perimeter( ) (Both method’s return type and parameter list should be void
and empty respectively. Define classes like Triangle, Rectangle and Circle
implementing the “GeometricShape” interface and also define “ExecuteMain”
class in which include main method to test the above class
Program:
interface GeometricShape
{
void area();
void perimeter();
}
class Triangle implements GeometricShape
{
int height,base,side1,side2,side3;
Triangle()
{
height=15;
base=7;
side1=5;
side2=10;
side3=6;
}
public void area()

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
32
Name of the Student: Rollno:

{
double area=0.5*height*base;
System.out.println("Area of the Triangle is:"+area);
}
public void perimeter()
{
int perimeter=side1+side2+side3;
System.out.println("Perimeter of the Triangle is:"+perimeter);
}
}
class Rectangle implements GeometricShape
{
int length,breadth;
Rectangle()
{
length=10;
breadth=20;
}
public void area()
{
double area=length*breadth;
System.out.println("Area of the Rectangle is:"+area);
}
public void perimeter()
{
int perimeter=2*(length+breadth);
System.out.println("Perimeter of the Rectangle is:"+perimeter);
}
}
class Circle implements GeometricShape
{
double radius;
Circle()
{
radius=1.2;

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
33
Name of the Student: Rollno:

}
public void area()
{
double area=3.14*radius*radius;
System.out.println("Area of the Circle is:"+area);
}
public void perimeter()
{
double perimeter=2*3.14*radius;
System.out.println("Perimeter of the Circle is:"+perimeter);
}
}
class ExecuteMain
{
public static void main(String args[])
{
Triangle t=new Triangle();
t.area();
t.perimeter();
Rectangle r=new Rectangle();
r.area();
r.perimeter();
Circle c=new Circle();
c.area();
c.perimeter();
}
}
Output:
Area of the Triangle is:52.5
Perimeter of the Triangle is:21
Area of the Rectangle is:200.0
Perimeter of the Rectangle is:60
Area of the Circle is:4.521599999999999
Perimeter of the Circle is:7.536

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
34
Name of the Student: Rollno:

5(B) Define a package with name “sortapp” in which declare an interface


“SortInterface” with method sort( ) whose return type and parameter list
should be void and empty.Define “subsortapp” as subpackage of “sortapp”
package in which define class “SortImpl” implementing “SortInterface” in
which sort() method should print a message linear sort is used.
Define a package “searchingapp” in which declare an interface
“SearchInterface” with search( ) method whose return type and parameter
list should be void and empty respectively.
Define “searchingimpl” package in which define a “SearchImpl” class
implementing “SearchInterface” defined in “searchingapp” package in which
define a search( ) method which should print a message linear search is used.
Define a class ExecutePackage with main method using the above
packages(classes and its methods).
Program:
package sortapp;
public interface SortInterface
{
void sort();
}
-----------------------
package sortapp.subsortapp;
import sortapp.*;
public class SortImpl implements SortInterface
{
public void sort()
{
System.out.println("Linear sort is used...");
}
}
-----------------------
package searchingapp;
public interface SearchInterface
{
void search();
}

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
35
Name of the Student: Rollno:

----------------------------
package searchingimpl;
import searchingapp.*;
public class SearchImpl implements SearchInterface
{
public void search()
{
System.out.println("Linear search is used...");
}
}
----------------------------
import sortapp.subsortapp.*;
import searchingimpl.*;
class ExecutePackage
{
public static void main(String args[])
{
SortImpl s=new SortImpl();
s.sort();
SearchImpl p=new SearchImpl();
p.search();
}
}
Output:
Linear sort is used...
Linear search is used...
(6) Modify the withdraw() method of Account class such that this method
should throw “InsufficientFundException” if the account holder tries to
withdraw an amount that leads to condition where current balance becomes
less than minimum balance otherwise allow the account holder to withdraw
and update the balance accordingly.
Program:
import java.util.Scanner;
class InsufficientFundException extends Exception
{

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
36
Name of the Student: Rollno:

private String msg;


InsufficientFundException(String m)
{
msg=m;
}
public String toString()
{
return "InsufficientFundException:" + msg;
}
public String getMessage() { return msg;}
}
class BankAccount
{
String name;
long accno;
String acctype;
double balance;
BankAccount()
{
name="Krishna";
accno=9876546;
acctype="Savings";
balance=20200.50;
}
void withdraw(double amount)
{
try
{
if(balance-amount>=1000) //minimum balance is Rs.1000
{
System.out.println("Balance before withdrawl:"+balance);
balance=balance-amount;
System.out.println("Balance after withdrawl:"+balance);
}
else

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
37
Name of the Student: Rollno:

throw new InsufficientFundException("Insufficient balance");


}
catch(InsufficientFundException e)
{
System.out.println("Caught "+e);
System.out.println("Exception Description:"+e.getMessage());
}
}
void display()
{
System.out.println("Account details are...");
System.out.println(name+"-"+accno+"-"+acctype+"-"+balance);
}
}
class AccountException
{
public static void main(String args[])
{
BankAccount a=new BankAccount();
a.display();
Scanner s=new Scanner(System.in);
System.out.print("Enter the amount to be withdrawn:");
double amount=s.nextDouble();
a.withdraw(amount);
}
}
Output:
Account details are...
Krishna-9876546-Savings-20200.5
Enter the amount to be withdrawn:20000
Caught InsufficientFundException:Insufficient balance
Exception Description:Insufficient balance
7(A) Define two threads such that one thread should print even numbers and
another thread should print even numbers.
Program:

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
38
Name of the Student: Rollno:

class MyThread extends Thread


{
public void run()
{
System.out.println(" \t\t\tOdd thread is running");
try
{
for(int i=1;i<=10;i=i+2)
{
Thread.sleep(1000);
System.out.println("\t\t\t\t"+i);
}
}
catch(InterruptedException e)
{System.out.println(e); }
System.out.println("\t\t\tExit from Odd Thread");
}
}
class EvenOddThread
{
public static void main(String args[ ]) throws InterruptedException
{
System.out.println("Event thread is running");
MyThread t = new MyThread();
t.start();
for(int i=2;i<=10;i=i+2)
{
Thread.sleep(1000);
System.out.println(i);
}
System.out.println("Exit from Event Thread");
}
}
Output:
Event thread is running

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
39
Name of the Student: Rollno:

Odd thread is running


2
1
4
3
6
5
8
7
10
Exit from Event Thread
9
Exit from Odd Thread
7(D) Write a program to implement thread priority.
Program:
class ThreadPriority
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
System.out.println("Current thread:"+t);
//change the thread priority
t.setPriority(Thread.MIN_PRIORITY);
System.out.println("After Priority change:"+t);
t.setPriority(Thread.NORM_PRIORITY);
System.out.println("After Priority change:"+t);
t.setPriority(Thread.MAX_PRIORITY);
System.out.println("After Priority change:"+t);
}
}
Output:
Current thread:Thread[main,5,main]
After Priority change:Thread[main,1,main]
After Priority change:Thread[main,5,main]
After Priority change:Thread[main,10,main]

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
40
Name of the Student: Rollno:

8) Design the user screen as follows and handle the events appropriately.
Add Window
First Number

Second Number

Result
ADD SUBTRACT

Program:
import java.awt.*;
import java.awt.event.*;
class ScreenDesign extends Frame implements ActionListener
{
TextField t1,t2,t3;
ScreenDesign()
{
super("Design Screen");
setLayout(new FlowLayout(FlowLayout.LEFT,20,5));
Label l1=new Label("First Number");
t1=new TextField(10);
Label l2=new Label("Second Number");
t2=new TextField(10);
Label l3=new Label("Result");
t3=new TextField(10);
Button a=new Button("ADD");
Button s=new Button("SUBTRACT");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(a);
add(s);

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
41
Name of the Student: Rollno:

a.addActionListener(this);
s.addActionListener(this);
setSize(250,250);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
int temp;
if(ae.getActionCommand().equals("ADD"))
temp=Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText());
else
temp=Integer.parseInt(t1.getText())-Integer.parseInt(t2.getText());
t3.setText(temp+" ");
}
public static void main(String args[])
{
new ScreenDesign();
}
}
Output:

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
42
Name of the Student: Rollno:

Client side

II B.Tech II Semester CSE(C) OOP Through Java Lab 2012-2013


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
43

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy