Java Lab
Java Lab
Java Lab
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);
}
}
}
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";
}
}
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.
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(){
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(){
}
}
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(){
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(){
}
}
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();
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();
}
}