Java Lab

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

/*Write a program to print Prime numbers up to a given

number*/
import java.util.*;
class Test {
int i,j;
void check(int num) {
System.out.println ("Prime numbers up to "+num+" are:");
for(i=2;i<num;i++){
for( j=2;j<i;j++) {
if(i%j==0){
break;
}
}
if(i==j){
System.out.println(i);
}
}
}

} //end of class Test


public class prime {
public static void main(String args[ ]) {
Test obj1=new Test();
Scanner input=new Scanner(System.in);
System.out.println("Enter the value of n:");
int n=input.nextInt();
obj1.check(n);
}
}
/*Write a program to print roots of a quadratic equation ax2+bx+c=0.*/
/*Write a program to print roots of a quadratic equation ax2+bx+c=0.*/
import java.io.*;
import java.util.*;
public class Quad{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
System.out.println("enter the values for a,b & c");
int a=s.nextInt();
int b=s.nextInt();
int c=s.nextInt();
double d,e;
d=((b*b)-(4*a*c));
if(d==0){
System.out.println("expression has real&equal roots");
e=(-b)/(2*a);
System.out.print("roots are:"+e+"\t"+e);
}
if(d>0){
System.out.println("expression has real&unreal roots");
e=(-b+Math.sqrt(d))/(2*a);
System.out.print("roots are:"+e+"\t");
e=(-b-Math.sqrt(d))/(2*a);
System.out.println(e);
}
if(d<0){
System.out.println("expression has imaginary rotts");
}
}
}
/*Write a program to print Fibonacci sequence up to a given number.*/
import java.io.*;
import java.util.Scanner;
public class Fib {
public static void main(String args[ ]) {
Scanner input=new Scanner(System.in);
int i,a=0,b=1,c=0,t;
System.out.println("Enter value of t:");
t=input.nextInt();
System.out.print(a);
System.out.print(" "+b);
for(i=0;i<t-2;i++) {
c=a+b;
a=b;
b=c;
System.out.print(" "+c);
}

System.out.println();
System.out.print(t+"th value of the series is: "+c);
}
}
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:
To assign instance variables (Constructors-Zero argument and parameterized)
1. To deposit an amount
2. To withdraw amount after checking the balance
3. To display name and address
Define ExecuteAccount class in which define main method to test above
class.
import java.util.*;
class BankAccount{

String name;
int accNo;
String type;
double balance;
double wbalance;
String addr;

BankAccount(){
name="abc";
type="saving";
accNo=12345;
balance=00.00;
addr="hyd";
}

Scanner s=new Scanner(System.in);


void deposit(){
System.out.println("enter the deposited amount");
balance=s.nextDouble();
}
void withdraw(){
if(balance>100){
System.out.println("enter the withdraw amount");
wbalance=s.nextDouble();
balance=balance-wbalance;

}
else
System.out.println("invalid amount");
}
void display(){
System.out.println("Account Holder Name: "+name);
System.out.println("Address of Account Holder is: "+addr);
System.out.println("Account Number: "+accNo);
System.out.println("Withdraw Money: "+wbalance);
System.out.println("Remaining Account Balance after withdrawl: "+balance);
}
}
public class ExecuteAccountA{
public static void main(String args[]){
BankAccount ba=new BankAccount();
ba.deposit();
ba.withdraw();
ba.display();
}
}
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.
C).In main method of ExecuteAccount class, define an array to handle five accounts.
D). In Account class constructor, demonstrate the use of “this” keyword.

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

F).Overload the method deposit() method (one with argument and another without
argument).
G). In Account class, define set and get methods for each instance variable.
Example:
For account no variable, define the methods
getAccountNo() and setAccountNo(int accno)
In each and every method of Account class, reading data from and writing data
to instance variables should be done through these variables.
• 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.
import java.util.*;
class BankAccount{
String name;
int accNo;
String type;
double balance,wbalance;
String addr;
int count,accNo1,accNo2,accNo3,accNo4,accNo5,accNo6;
BankAccount(){
name="abc";
type="saving";
balance=00.00;
addr="hyd“;
System.out.println("Enter Account Numbers:");
count=0;
accNo=s.nextInt();
count++;
accNo1=s.nextInt();
count++;
accNo2=s.nextInt();
count++;
accNo3=s.nextInt();
count++;
accNo4=s.nextInt();
count++;
accNo5=s.nextInt();
count++;
accNo6=s.nextInt();
count++;
System.out.println("Total Number of Account holders in the bank: "+count);
}
Scanner s=new Scanner(System.in);
void deposit(){
System.out.println("enter the total amount");
balance=s.nextDouble();
}
void withdraw(){
if(balance>100){
System.out.println("enter the withdraw amount");
wbalance=s.nextDouble();
balance=balance-wbalance;
}
}
void display(){

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


System.out.println("Address of Account Holder is:"+addr);
System.out.println("Account Number: "+accNo);
System.out.println("Withdraw Money: "+wbalance);
System.out.println("Account Balance: "+balance);
System.out.println("Display account numbers");

System.out.println(" Account Number1: "+accNo1);


System.out.println(" Account Number2: "+accNo2);
System.out.println(" Account Number3: "+accNo3);
System.out.println(" Account Number:4 "+accNo4);
System.out.println(" Account Number5: "+accNo5);
System.out.println(" Account Number6: "+accNo6);
}
}
public class ExecuteAccountB{
public static void main(String args[]){
BankAccount ba=new BankAccount();

ba.deposit();
ba.withdraw();
ba.display();

}
}
In main method of ExecuteAccount class, define an array to handle five accounts.
import java.util.*;
class BankAccount{
String name;
String accNo;
String type;
double balance;
double wbalance;
String addr;
BankAccount(){
name="abc";
type="saving";
accNo="12a345";
balance=00.00;
addr="hyd";
}
Scanner s=new Scanner(System.in);
void deposit(){
System.out.println("enter the total amount");
balance=s.nextDouble();
}
void withdraw(){
if(balance>0){
System.out.println("enter the withdraw amount");
wbalance=s.nextDouble();
balance=balance-wbalance;
}
}
void display(){

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


System.out.println("Address of Account Holder is: "+addr);
System.out.println("Account Number: "+accNo);
System.out.println("Withdraw Money: "+wbalance);
System.out.println("Account Balance: "+balance);
}
}
public class ExecuteAccountC{
public static void main(String args[]){
BankAccount ba=new BankAccount();
Scanner in=new Scanner(System.in);
long AcctNo[]=new long[5];
System.out.println("Enter Account Numbers:");
for(int i=0;i<5;i++){
AcctNo[i]=in.nextLong();
}
System.out.println("Total no.of Accounts:"+AcctNo.length);
System.out.println("Account Numbers:");
for(int i=0;i<AcctNo.length;i++){
System.out.println(AcctNo[i]);
}
ba.deposit();
ba.withdraw();
ba.display();

}
}
In Account class constructor, demonstrate the use of “this” keyword.
import java.util.*;
class BankAccount{
String name;
int accNo;
String type;
double balance;
double wbalance;
String addr;
BankAccount(String name,String type,int accNo,double balance,String addr){
this.name=name;
this.type=type;
this.accNo= accNo;
this.balance=balance;
this.addr=addr;
}
Scanner s=new Scanner(System.in);
void deposit(){
System.out.println("enter the total amount");
balance=s.nextDouble();
}
void withdraw(){
if(balance>0){
System.out.println("enter the withdraw amount");
wbalance=s.nextDouble();
balance=balance-wbalance;
}
}
void display(){
System.out.println("Account Holder Name: "+name);
System.out.println("Address of Account Holder is: "+addr);
System.out.println("Account Number: "+accNo);
System.out.println("Withdraw Money: "+wbalance);
System.out.println("Account Balance: "+balance);
}
}
public class ExecuteAccountD{
public static void main(String args[]){
BankAccount ba=new BankAccount("xyz","current",1562,15000.00,"hyd");

ba.deposit();
ba.withdraw();
ba.display();

}
}
Modify the constructor to read data from keyboard.
import java.util.*;
class BankAccount{
String name,type;
int AcctNo;
double balance;
double wbalance;
Scanner s=new Scanner(System.in);
public BankAccount(){
System.out.println("enter the account name");

name=s.next();
System.out.println("enter the account type");
type=s.next();
System.out.println("enter the account number");
AcctNo=s.nextInt();
}
void deposit(){
System.out.println("enter the account deposit amount");
balance=s.nextDouble();
}
void withdraw(){
if(balance>0){
System.out.println("enter the withdrawal amount");
wbalance=s.nextDouble();
balance=balance-wbalance;
System.out.println("remaining balance: "+balance);
}
}
void display(){

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


System.out.println("Account Number: "+AcctNo);
System.out.println("Account Balance: "+balance);
}
}
class ExecuteAccountE{
public static void main(String args[]){
BankAccount ba=new BankAccount();
ba.deposit();
ba.withdraw();
ba.display();
}
}
Overload the method deposit() method (one with argument and another without argument)

import java.util.*;
class BankAccount{
String name,type;
int AcctNo;
double balance,bal;
double wbalance;
public BankAccount(){
name="abc";
type="saving";
AcctNo=1234567890;
balance=00.00;
}
Scanner input=new Scanner(System.in);
void deposit(){
System.out.println("Enter Balance: ");
balance=input.nextDouble();
System.out.println("Balance from deposit 1: "+balance);
}
void deposit(double b){
bal=b;
System.out.println("Balance from deposit(arg) 2: "+bal);

}
void withdraw(){
System.out.println("enter the withdraw amount");
wbalance=input.nextDouble();
if(wbalance<balance){
balance=balance-wbalance;
}
else
{
bal=bal-wbalance;

}
}
void display(){

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


System.out.println("Account Number: "+AcctNo);
System.out.println("Withdraw Money: "+wbalance);
System.out.println("Account Balance in deposit 1: "+balance);
System.out.println("Account Balance in deposit 2: "+bal);
}
}
class ExecuteAccountF{
public static void main(String args[]){
BankAccount ba=new BankAccount();
ba.deposit();
ba.deposit(10000.00);
ba.withdraw();
ba.display();
}
}
In Account class, define set and get methods for each instance
variable.
Example:
For account no variable, define the methods
getAccountNo() and setAccountNo(int accno)
In each and every method of Account class, reading data from
and writing data to instance variables should be done through
these variables.
import java.util.*;
class BankAccount{
String name,type;
int AcctNo;
double balance,bal;
double wbalance;
public BankAccount(){
setAcctNo();
set();
}
void set(){
name="james";
type="saving";
balance=00.00;
}
void get(){
System.out.println("Account Holder Name: "+name);
System.out.println("type of Account : "+type);
System.out.println("balance of Account Holder : "+balance);
}
void setAcctNo(){
AcctNo=1562;
}
void getAcctNo(){
System.out.println("Acct Number is :"+AcctNo);
}
Scanner input=new Scanner(System.in);
void deposit(){
System.out.println("Enter Balance: ");
balance=input.nextDouble();
System.out.println("Balance from deposit 1: "+balance);
}
void withdraw(){
System.out.println("enter the withdraw amount");
wbalance=input.nextDouble();
if(balance>0){
balance=balance-wbalance;
}
}
void display(){
getAcctNo();
get();

}
}
class ExecuteAccountG{
public static void main(String args[]){
BankAccount ba=new BankAccount();
ba.deposit();
ba.withdraw();
ba.display();
}
}
Define Resister class in which we define the following members:
Instance variables:
resistance
Instance Methods:
giveData():To assign data to the resistance variable
displayData(): To display data in the resistance variable
constructors
Define subclasses for the Resistor class called SeriesCircuit and
ParallelCircuit in which define methods : calculateSeriesResistance( ) and
calculateParallelResistance() respectively.Both the methods should take
two Resistor objects as arguments and return Resistor object as result.In
main method , define another class called ResistorExecute to test the above
class.
import java.io.*;
class Resister{
double resistance;
void giveData()throws IOException{
DataInputStream dis=new DataInputStream(System.in);

resistance=Integer.parseInt(dis.readLine());
}
void displayData(){
System.out.println(resistance);
}
}
class SeriesCircuit extends Resister{
Resister calculateSeriesResistance(Resister R1,Resister R2){
Resister R3=new Resister();
R3.resistance=R1.resistance+R2.resistance;
return(R3);
}
}
class ParallelCircuit extends Resister{
Resister claculateParallelResistance(Resister R1,Resister R2){
Resister R3=new Resister();
R3.resistance=((R1.resistance+R2.resistance)/(R1.resistance*R2.resistance));
return(R3);

}
}
class ResistorExecute{
public static void main(String args[]){
SeriesCircuit s=new SeriesCircuit();
ParallelCircuit p=new ParallelCircuit();

Resister R1=new Resister();


Resister R2=new Resister();
try{
System.out.println("enter resistance1");
R1.giveData();
System.out.println("enter resistance2");
R2.giveData();
}
catch(Exception e){
}
Resister R3=s.calculateSeriesResistance(R1,R2);
System.out.println("series circuit resistance is ");
R3.displayData();
Resister R4=p.claculateParallelResistance(R1,R2);
System.out.println("parallel circuit resistance is ");
R4.displayData();
}
}
Write a program to demonstrate method overriding.

class A{
int i,j;
A(int a,int b){
i=a;
j=b;
}
void show(){
System.out.println("i and j :"+i+" "+j);
}}
class B extends A{
int k;
B(int a,int b,int c){
super(a,b);
k=c;
}
void show(){
System.out.println("k=:"+k);
}}
class Override{
public static void main(String args[]){
B subob=new B(3,4,5);
subob.show();
}
}
Write a program to demonstrate the uses of “super” keyword
class Box{
Box(){
System.out.println("inside default Constructor of Box");
}
Box(int a){
System.out.println("inside parameterized Constructor of Box"+a);
}
}
class BoxSmall extends Box{
BoxSmall(int b){
super();
System.out.println("inside parameterized Constructor of BoxSmall");
}
}
class TestSuper{
public static void main(String args[]){
BoxSmall s1=new BoxSmall(20);
}
}
class A {
int i;
}
// Create a subclass by extending class A.
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);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
class A{
int i,j;
class Override
A(int a,int b){
{
i=a;
public static void main(String args[])
j=b;
{
} B subob=new B(3,4,5);
void show(){ subob.show();
System.out.println(“i and j :”+i+” “+j); }
}} }
class B extends A{
int k;
B(int a,int b.int c){
super(a,b);
k=c;
}
void show(){
super.show(); // this calls A's show()
System.out.println(“k=:”+k);
}}
Write a program to demonstrate dynamic method dispatch (i.e .Dynamic
polymorphism).
class A
{
void callme()
{
System.out.println(“Inside A’s callme method”); class Dispatch
} {
} Public static void main(String args[])
class B extends A {
{ A a=new A();
B b=new B();
void callme()
C c=new C();
{ A r; // obtain a reference of type A
System.out.println(“Inside B’s callme method”);
} r=a; // r refers to an A object
}
class C extends A r.callme(); // calls A's version of callme
{
void callme() r=b; // r refers to an B object
{ r.callme(); // calls B's version of callme
System.out.println(“Inside C’s callme method”);
r=c; // r refers to an C object
}
r.callme(); // calls C's version of callme
}
}
}
Write a program to check whether the given string is palindrome or not.

import java.util.*;
import java.lang.*;
class Palindrome
{
public static void main(String a[])
{
System.out.println("Enter a String");
Scanner input = new Scanner(System.in);
String s = input.next();
System.out.println("The given string is "+s);
int n = s.length();
int p = 0;
for(int i=0;i<n;i++)
{if(s.charAt(i) != s.charAt(n-i-1))
{ p = 1;
}
}
if(p == 1)
{
System.out.println("The given String is not a palindrome");
}
else
{
System.out.println("The given String is a palindrome");
} } }
)Write a program for sorting a given list of names in ascending order.*/
import java.util.*;
import java.lang.*;
class SortString
{
public static void main(String a[])
{
String names[] = new String[5];
System.out.println("Enter 5 Strings");
Scanner input = new Scanner(System.in);
for(int i = 0;i<5;i++)
{ names[i] = input.next();
}
for(int i = 0;i<5;i++)
{ for(int j=i+1;j<5;j++)
{ if(names[i].compareTo(names[j])>0)
{ String temp = names[i];
names[i] = names[j];
names[j] = temp;
} }
}
System.out.println("Sorted List is :");
for(int i=0;i<5;i++)
{
System.out.println(" "+names[i]);
} } }
)Write a program to count number of words in a given text
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class WordsCount
{
public static void main(String arg[]) throws IOException
{
long nl=0,nw=0,nc=0;
String line;
BufferedReader br = new BufferedReader(new FileReader(arg[0]));
while((line = br.readLine())!= null)
{
nl++;
nc = nc +line.length();
StringTokenizer st = new StringTokenizer(line);
nw += st.countTokens();
}
System.out.println("Number of Characters: "+nc);
System.out.println("Number of Words: "+nw);
System.out.println("Number of Lines: "+nl);
}
}
Define an interface “GeometricShape” with methods area () and perimeter () (both
methods 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.
import java.io.*;
interface GeometricShape
{
void area();
void perimeter();
}
class Triangle implements GeometricShape
{
int a = 3,b = 6, c=9, h=10;
double ar,per;
public void area()
{
ar = 0.5*b*h;
System.out.println("Area of the Triangle is :"+ar);
}
public void perimeter()
{
per = a+b+c;
System.out.println("Perimeter of the Triangle is :"+per);
}
}
class Circle implements GeometricShape
{
int r=4;
double pi =3.14, ar,per;
public void area()
{
ar = pi*r*r;
System.out.println("Area of the Circle is :" +ar);
}
public void perimeter()
{
per = 2*pi*r;
System.out.println("Perimeter of the Circle is :"+per);
}
}
class Rectangle implements GeometricShape
{
int a = 7,b=5;
double ar,per;
public void area()
{
ar = a*b;
System.out.println("Area of the Rectangle is :" +ar);
}
public void perimeter()
{
per = 2*(a+b);
System.out.println("Perimeter of the Rectangle is :" +per);
}
}
class ExecuteMain
{
public static void main(String a[])
{
Circle c= new Circle();
c.area();
c.perimeter();
Rectangle r = new Rectangle();
r.area();
r.perimeter();
Triangle t = new Triangle();
t.area();
t.perimeter();
}
}
Define a package with name “sortapp” in which declare an interface “SortInterface” with
method sort () whose return type and parameter type should be void and empty.
Define “subsortapp” as sub package 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 “serachingimpl” 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
it’s methods).
*/
package sortapp;
public interface SortInterface
{
void sort();
}
package sortapp.subsortapp;
import sortapp.SortInterface;
public class SortImpl implements SortInterface
{
public void sort()
{
System.out.println("Linear Sort is used");
}
}
package searchingapp;
public interface SearchInterface
{
void search();
}
package searchimpl;
import searchingapp.*;
public class SearchImpl implements SearchInterface
{
public void search()
{
System.out.println("Linear Search is Used");
}
}
import searchimpl.SearchImpl;
import sortapp.subsortapp.SortImpl;
class ExecutePackage
{
public static void main(String a[])
{
SearchImpl si = new SearchImpl();
si.search();
SortImpl so = new SortImpl();
so.sort();
}
}

You might also like