Vous êtes sur la page 1sur 12

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 + 1 / i;
i++;
}
System.out.println("\n\nSum of Harmonic Series is "+rst);
}
}
Q.2 Write a program to check whether the given year is leap year or not.
[20M]
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
int y;
Scanner in=new Scanner(System.in);
System.out.println("Enter the year");
y = in.nextInt();
if(y%100==0)
{
if(y%400==0)
System.out.println("Leap year");
else
System.out.println("Not Leap year");
}
else
{
if(y%4==0)
System.out.println("Leap year");
else
System.out.println("Not Leap year");
}
}
}

Q.3 Write a program to accept a string and count number of vowels, digits tabs a
nd
blank spaces in a string .
[20M]
import java.io.*;
class JavaApplication14
{
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(Sys
tem.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' || c
h == '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
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
for (int i = 0; i <= n; i++) {
System.out.print(fibonacci(i) + " ");
}
}
public static int fibonacci(int n) {

[20M]

if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}

Q.5 Write Program to sort an Array A using Bubble Sort [30M]


Where A[8]={ 17,6,15,2,3,14,20,1 }
public class Bubblesort {
public static void main(String[] args) {
int a[]={ 17,6,15,2,3,14,20,1 };
int i,j,t;
for(j=0; j<=6; j++)
{
for(i=0; i<=6-j; i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}
}
for(i=0; i<=7; i++)
System.out.println(a[i]);
}
}
Q.6 Create a
BANK ACCOUNT
application using switch (e.g. Deposit Money , Withdr
aw Money ,
Check Balance etc.).
[30M]
import java.util.*;
public class Bankacc {
public static void main(String[] args)throws Exception {
int ch,amt=0;
Scanner in = new Scanner(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");


ch=in.nextInt();
switch(ch)
{
case 1:
int dpamt;
System.out.println("Enter amount to deposit-");
dpamt=in.nextInt();
amt=amt+dpamt;
break;
case 2:
int wdamt;
System.out.println("Enter amount to withdraw-");
wdamt=in.nextInt();
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.
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);
}
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 n
umbers
from 1 to 10 and other thread prints non-prime numbers from 1 to 10 (use Thread
class ).
Note :- Each thread has a delay of 500 millisecond after printing one number [3
0M]
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.9 Write Program to perform Arithmetic Operations using Interface.

[30M]

interface arithmetic{
void add();
void sub();
void mul();
void div();
}
public staitc class A6 implements arithmetic{
int a=25,b=10,c;
public void add()
{
c=a+b;
System.out.println(+c);
}
public void sub(){
c=a-b;
System.out.println(+c);
}
public void mul(){
c=a*b;
System.out.println(+c);
}
public void div(){
c=a/b;
System.out.println(+c);
}
public static void main(String args[]){
A6 obj = new A6();
obj.add();
obj.sub();
obj.mul();
obj.div();
}
}
Q.10 Write a program to accept a number from the user , if number is zero then
[20M]
Number is 0
otherwise check whether no is prime or
throw user defined exception
not .
import java.util.Scanner;
class NumberZeroException extends Exception
{
public String toString()
{
return("Number is 0");
}

}
class PrimeNumber
{
int a;
Scanner in = new Scanner(System.in);
{
try
{
System.out.println("Enter any integer to check prime ");
a=in.nextInt();
if(a==0)
throw new NumberZeroException();
}
catch(IOException ex1)
{
System.out.println("Enter proper number");
}
catch(NumberZeroException ex)
{
System.out.println(ex);
}

}
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
class Account
{
String cust_name;
int acc_no;
Account(String a, int b)
{

[30M]

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 Multilevel
{
public static void main(String args[])
{
Acct_Details A = new Acct_Details("Pa.one",666,1000,5000,500,900
0);
A.display();
}
}
Q.12 Create a billing form where the flavor of ice-cream are displayed .
On the selection of a flavor and the quantity entered , the total amount
is calculated and displayed
[30M]
import java.util.Scanner;
public class IcecreamBill {
public static void main(String[] args)throws Exception {

String str;
int ch,qty,bill;
Scanner in = new Scanner(System.in);
System.out.println("1.Strawberry Flavour");
System.out.println("2.Mango Flavour");
System.out.println("3.Vanilla Flavour");
System.out.println("4.Dark Chocolate");
System.out.println("Enter your choice");
ch= in.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter Quantity");
qty=in.nextInt();
System.out.println("Cost 150");
bill=qty*150;
System.out.println("Total Bill" +bill);
break;
case 2:
System.out.println("Enter Quantity");
qty=in.nextInt();
System.out.println("Cost 150");
bill=qty*150;
System.out.println("Total Bill"+bill);
break;
case 3:
System.out.println("Enter Quantity");
qty=in.nextInt();
System.out.println("Cost 130");
bill=qty*130;
System.out.println("Total Bill"+bill);
break;
case 4:
System.out.println("Enter Quantity");
qty=in.nextInt();
System.out.println("Cost 180");
bill=qty*180;
System.out.println("Total Bill"+bill);
break;
case 5:
break;
default:
System.out.println("Wrong Choice");
}
}
}
`
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 th
e
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.io.*;
class program
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(Sys
tem.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 Osborne s 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 h
aving 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