Vous êtes sur la page 1sur 11

J2EE PRACTICAL QUESTION BANK SOLUTION

Q.1 Write a program to generate Harmonic Series (1+1/2+1/3+1/4+1/5 = 2.28 )

[20M]

package harmonicseries;
import java.util.*;
public class HarmonicSeries
{
public static void main(String[] args)
{
int num, i=1;
double rst = 0.0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number for length of series");
num = in.nextInt();
while(i <= num)
{
System.out.print("1/"+i+" +");
rst = rst + (double) 1 / i;
i++;
}
System.out.println("\n\nSum of Harmonic Series is "+rst);
}
}

Q.3 Write a program to accept a string and count number of vowels, digits tabs and blank spaces
in a string .
[20M]
import java.io.*;
class vowels
{
public static void main(String args[]) throws IOException
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a String : ");
str = br.readLine();
for(int i = 0; i < str.length(); i ++)
{
ch = str.charAt(i);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i'
||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowels ++;

else if(Character.isDigit(ch))
digits ++;
else if(Character.isWhitespace(ch))
blanks ++;
}
System.out.println("Vowels : " + vowels);
System.out.println("Digits : " + digits);
System.out.println("Blanks : " + blanks);
}
}

Q.4 Write program to generate the Fibonacci series using recursion

[20M]

import java.util.Scanner;
public class FibonacciCalculator
{
public static void main(String args[])
{
System.out.println("Enter number upto which Fibonacci series to
print:");
int number = new Scanner(System.in).nextInt();
System.out.println("Fibonacci series upto " + number +" numbers :
");
for(int i=1; i<=number; i++)
{
System.out.print(fibonacci2(i) +" ");
}
}
public static int fibonacci(int number)
{
if(number == 1 || number == 2){ return 1;
}
return fibonacci(number-1) + fibonacci(number -2);
}
public static int fibonacci2(int number)
{
if(number == 1 || number == 2)
{
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++)
{
fibonacci = fibo1 + fibo2;
fibo1 = fibo2;
fibo2 = fibonacci;
}
return fibonacci;
}

Q.6 Create a BANK ACCOUNT application using switch (e.g. Deposit Money , Withdraw
Money , Check Balance etc.).
[30M]
package bankacc;
import java.io.*;
public class Bankacc {
public static void main(String[] args)throws IOException {
// TODO code application logic here
String str;
int ch,amt=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("1.Deposit Money");
System.out.println("2.Withdraw Money");
System.out.println("3.Check Balance");
System.out.println("4.Exit");
System.out.println("Enter your choice");
str=br.readLine();
ch=Integer.parseInt(str);
switch(ch)
{
case 1:
int dpamt;
System.out.println("Enter amount to deposit-");
str=br.readLine();
dpamt=Integer.parseInt(str);
amt=amt+dpamt;
break;
case 2:
int wdamt;
System.out.println("Enter amount to withdraw-");
str=br.readLine();
wdamt=Integer.parseInt(str);
amt=amt-wdamt;
break;
case 3:
System.out.println("Current Balance is "+amt+"\n");
break;
case 4:
break;
default:
System.out.println("Wrong Choice");
}
}
while(ch!=4);
}
}

Q.7 Write a program to define a class Fraction having data members numerator and denominator.
Initialize three objects using different constructors and display its fractional value.

import java.lang.*;
import java.io.*;
class Fraction
{
double num, deno, fraction;
Fraction (int a, double b)
{
num=a;
deno=b;
fraction=num/deno;
System.out.println ("Fraction1 = "+fraction);
}
Fraction (int x, int y)
{
num=x;
deno=y;
fraction=num/deno;
System.out.println ("Fraction2 = "+fraction);
}
Fraction(double m, double n)
{
num=m;
deno=n;
fraction=num/deno;
System.out.println ("Fraction3 = "+fraction);
}
}
class q3Fraction
{
public static void main(String args[])
{
Fraction f = new Fraction(12,12.3);
Fraction f2 = new Fraction(10,12);
Fraction f3 = new Fraction(10.5,13.5);
}
}

Q.8 Write a program to implement two threads such that one thread prints prime numbers from 1
to 10 and other thread prints non-prime numbers from 1 to 10 (use Thread class ).
[30M]
Note :- Each thread has a delay of 500 millisecond after printing one number
import java.lang.*;
import java.io.*;
import java.util.*;
class Prime extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
if(i==2||i==3||i==5||i==7)
{
System.out.println ("Prime No.= "+i);

}
Thread.sleep(500);
}
}
catch (Exception e){}
}
}
class notPrime extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
if(i==4||i==6||i==8||i==9||i==10)
{
System.out.println ("Non-Prime No.= "+i);
}
Thread.sleep(500);
}
}
catch (Exception e){}
}
}
class q13Thread
{
public static void main(String args[])
{
new Prime().start();
new notPrime().start();
}
}

Q.10 Write a program to accept a number from the user , if number is zero then throw user
defined exception Number is 0 otherwise check whether no is prime or not .
[20M]
import java.io.*;
class NumberZeroException extends Exception
{
public String toString()
{
return("Number is 0");
}
}
class PrimeNumber
{
int a;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrimeNumber()
{
try
{
System.out.println("Enter any integer to check prime ");

a=Integer.parseInt(br.readLine());
if(a==0)
throw new NumberZeroException();
}
catch(NumberZeroException ex)
{
System.out.println(ex);
}
catch(IOException ex1)
{
System.out.println("Enter proper number");
}
}
public void prime()
{
int cnt=0;
for(int i=2;i<=a/2;i++)
if(a%i==0)
{
cnt++;
break;
}
if(cnt==0)
System.out.println(a+" Number is prime");
else
System.out.println(a+" Number is not prime");
}
public static void main(String args[]) throws Exception
{
PrimeNumber pn=new PrimeNumber();
pn.prime();
}
}

Q.11 Write a program to implement the following Multi Level Inheritance


Account
Cust_name
Acc_no

Saving_Acc
Min_bal
Saving_bal

Acct_Details
Deposits
Withdrawals

[30M]

import java.lang.*;
import java.io.*;
class Account
{
String cust_name;
int acc_no;
Account(String a, int b)
{
cust_name=a;
acc_no=b;
}
void display()
{
System.out.println ("Customer Name: "+cust_name);
System.out.println ("Account No: "+acc_no);
}
}
class Saving_Acc extends Account
{
int min_bal,saving_bal;
Saving_Acc(String a, int b, int c, int d)
{
super(a,b);
min_bal=c;
saving_bal=d;
}
void display()
{
super.display();
System.out.println ("Minimum Balance: "+min_bal);
System.out.println ("Saving Balance: "+saving_bal);
}
}
class Acct_Details extends Saving_Acc
{
int deposits, withdrawals;
Acct_Details(String a, int b, int c, int d, int e, int f)
{
super(a,b,c,d);
deposits=e;
withdrawals=f;
}
void display()
{
super.display();
System.out.println ("Deposit: "+deposits);
System.out.println ("Withdrawals: "+withdrawals);
}
}
class q9Multilevel
{

public static void main(String args[])


{
Acct_Details A = new
Acct_Details("Pa.one",666,1000,5000,500,9000);
A.display();
}
}

Q.13 Write Program for simple Socket example. It opens a connection to a whois port(port
43) on the InterNIC Server , sends the command-line argument down the socket, and then prints
the data that is returned interNIC will try to look up the argument as a registered Internet domain
name, and then send back the IP address and contact information for the site.
[20M]
import java.net.*;
import java.io.*;
class appwhois
{
public static void main(String args[]) throws Exception
{
int c;
Socket s=new Socket("internet.net", 43);
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
String st=(args.length==0? "yahoo.com" : args[0]) + "\n";
while((c=is.read())!=-1)
{
System.out.print((char)c);
}
s.close();
}
}

Q.14 Write a program to Implement a program to accomplish the following task using string /
string Buffer class :
[30M]
i.
Accept a password from user.
ii.
Check if password is correct then display "Good"
iii. Else display "Incorrect password"
iv. Append the password with the string "Welcome to java!!!"
v.
Display the password in reverse order.
vi. Replace the character '!' in password with "*" character.
import java.lang.*;
import java.io.*;
class q6String
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String s1 = "!!sspp!!";
String s2;
System.out.println ("Enter Password");
s2 = br.readLine();
if(s1.compareTo(s2)==0)

{
System.out.println ("Good");
}
else
{
System.out.println ("Password is incorrect");
System.exit(0);
}
String s3 = "Welcome to Java!!!";
StringBuffer sb = new StringBuffer(s1);
sb.append(s3);
System.out.println ("Appended Password = "+sb);
StringBuffer s4 = new StringBuffer(s1);
s4=s4.reverse();
System.out.println ("String in Reverse Order = "+s4);
System.out.println ("Replaced '!' with '*' = "+s1.replace('!','*'));
}
}

Q.15 Create a URL to Osbornes download page and then examines its properties (URL :
http://www.osborne.com/downloads)
[20M]
package osborne;
import java.net.*;
import java.io.*;
import java.util.Date;
public class Osborne
{
public static void main(String args[]) throws Exception {
int c;
URL httpurl= new URL("http://www.osborne.com/downloads");
URLConnection httpurlCon= httpurl.openConnection();
long d= httpurlCon.getDate();
if(d==0) {
System.out.println("No date information");
}
else
System.out.println("Date: " +new Date(d));
System.out.println("Content-Type: " +httpurlCon.getContentType());
d= httpurlCon.getExpiration();
if(d==0) {
System.out.println("No expiration information");
}
else
System.out.println("Expires: " +new Date(d));
d= httpurlCon.getLastModified();
if(d==0) {
System.out.println("No last modified information");
}
else
System.out.println("Last modified: " +new Date(d));
int len= httpurlCon.getContentLength();
if(len==-1) {
System.out.println("Content length unavailable");

}
else {
System.out.println("Content length: " +len);
}
if(len!=0) {
System.out.println("==Content==");
InputStream input= httpurlCon.getInputStream();
int I= len;
while(((c=input.read())!=-1) && (--I>0)) {
System.out.print((char)c);
}
input.close();
}
else {
System.out.println("No content available");
}
}
}

Q.16 Write a program to accomplish following task


[20M]
A. Create a user defined package box which has a class definition for box having data
members an disp() method (Assume suitable data)
B. Source file imports above package and calculate the volume of box.
// Box.java:
import boxx.*;
public class Box
{
public static void main(String[] args)
{
boxx b=new boxx(4,6,8);
b.cal();
b.display();
}
}
//Boxx.java:
package boxx;
public class boxx {
double l,b,h,volume;
public boxx(double l, double b, double h){
this.l=l;
this.b=b;
this.h=h;
}
public void cal(){
volume=l*b*h;
}
public void display(){

System.out.println("Volume of Box= " +volume);


}
}

Vous aimerez peut-être aussi