1.
Write a menu driven program to accept a choice and perform the following:
Choice
= 1, accept a number and check whether it is a PRIME NUMBER.
Choice
=2, accept a number and check whether it is a AUTOMORPHIC NUMBER
Else
display WRONG CHOICE
Answer:
import
java.io.*;
class
Q2
{
public
static void main(String[]args)throws IOException
{
BufferedReader
s=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please
Enter Your Choice");
System.out.println("1.Prime
Number");
System.out.println("2.Automorphic
Number");
String
input=s.readLine();
int
ch=Integer.parseInt(input);
switch(ch)
{
case
1:
System.out.println("Enter
a Number");
String
a=s.readLine();
int
num=Integer.parseInt(a);
int i;
for(i=2;i<num/2;i++){
}
if(num%i==0)
System.out.println("The
number you have entered is not a PRIME NUMBER");
else
System.out.println("The
number you have entered is a PRIME NUMBER");
break;
case
2:
System.out.println("Enter
a Number");
String
a1=s.readLine();
int
num1=Integer.parseInt(a1);
int
q=(num1*num1)%100;
if(q==num1)
System.out.println("The
number you have entered is an AUTOMORPHIC NUMBER");
else
System.out.println("The
number you have entered is not an AUTOMORPHIC NUMBER");
break;
default:
System.out.println("WRONG
CHOICE !!");
break;
} } }
2.Sasha
travels pvt ltd gives the following discount to it’s customers:
Ticket Amount
|
Discount
|
Above
Rs.70000
|
18%
|
Rs.55001
to Rs.70000
|
16%
|
Rs.35001
to Rs.55000
|
12%
|
Rs.25001
to Rs.35000
|
10%
|
Less
than Rs.25001
|
2%
|
Write
a program to INPUT the NAME and TICKET AMOUNT for the customer and calculate
the discount amount and net amount to be paid in the following format.
Name
Ticket Charges Discount Net Amount
Answer:
class
Discount
{
static
String name;
static
double amnt=0,net_amnt=0,disc=0;
public
static void input(String name1,double amt1)
{
name=name1;
amnt=amt1;
}
public
static void cal()
{
if(amnt<25001)
{
net_amnt=amnt*2/100;
disc=2;
}
else
if(amnt>=25001&&amnt<=35000)
{
net_amnt=amnt*10/100;
disc=10;
}
else
if(amnt>=35001&&amnt<=55000)
{
net_amnt=amnt*12/100;
disc=12;
}
else
if(amnt>+55001&&amnt<=70000)
{
net_amnt=amnt*16/100;
disc=16;
}
else
{
net_amnt=amnt*18/100;
disc=18;
}
}
public
static void display()
{
System.out.println("Name
\t Ticket Charges \t Discount \t Net Amount ");
System.out.println(name+"
\t "+amnt+" \t "+disc+" % \t
"+net_amnt);
}
public
static void main(String[]args)
{
Discount
obj=new Discount();
obj.input("QWERTY",3654);
cal();
obj.display();
}
}
3.Write
a class to represent a RIGHT ANGLED TRIANGLE. A CONSTRUCTOR should take the
length of two shorter sides and calculate the length of the hypotenuse .Include
a METHOD to calculate the area of the triangle.
Answer:
class
Triangle
{
double
ab,bc,ac,area;
Triangle(double
ab1,double bc1)
{
ab=ab1;
bc=bc1;
ac=Math.sqrt((ab*ab)+(bc*bc));
System.out.println("The
length of Hypotenuse is "+ac+"
"+"units");
}
public
void calculate(double ab2,double bc2)
{
ab=ab2;
bc=bc2;
double
ac1=Math.sqrt((ab*ab)+(bc*bc));
double
s=(ac+ab+bc)/2;
area=Math.sqrt(s*(s-ab)*(s-bc)*(s-ac1));
System.out.println("The
Area of triangle is "+area+"
"+"sq units");
}
public
static void main(String[]args)
{
Triangle
obj=new Triangle(3.0,4.0);
obj.calculate(4.0,1.0);
Triangle
obj1=new Triangle(50.0,25.0);
obj1.calculate(10.0,99.0);
}
}
4.Write
a program to generate the following series:
(1/1!)+(2/2!)+(3/3!)+(4/4!)+………………………..(n/n!)
Answer:
import
java.io.*;
public
class nested {
public
float factorial(int n)throws IOException
{
float prod=1;
for(int i=1;i<=n;i++)
prod=prod*i;
return
prod;
}
public
static void main(String[] args)throws IOException
{
BufferedReader
s=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
the number");
String
q=s.readLine();
nested
obj = new nested();
int
o=Integer.parseInt(q);
float
sum=0;
for(int
i=1;i<=o;i++)
{
float
prod = obj.factorial(i);
sum
= sum+(i/prod);
}
System.out.println("The
required output : "+sum);
}
}
5.Write
a program to accept a number and check whether it is PERFECT NUMBER .
Answer:
import
java.io.*;
class
Q010
{
public
static void cal()throws IOException
{
BufferedReader
s=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please
Enter a number : ");
String
q=s.readLine();
long
num=Long.parseLong(q);
long
n=2,lim=num/2,sum=1;
for(
n=2;n<=lim;n++)
{
if(num%n==0)
sum=sum+n;
}
if(sum==num)
System.out.println("The
number you have entered is a PERFECT NUMBER ");
else
System.out.println("The
number you have entered is not a PERFECT NUMBER ");
}
}
6.Write
a menu driven program to choose from various categories of cinema hall tickets.
After the user selects a category, accept the number of tickets the user wishes
to buy.
Display
the total cost of the tickets, by calculating as follows:
1.
|
Lower
Stall
|
Rs.50/-
|
2.
|
Upper
Stall
|
Rs.70/-
|
3.
|
Balcony
|
Rs.90/-
|
4.
|
Box
|
Rs.120/-
|
Answer:
import
java.io.*;
class
Q12
{
public
static void main(String[]args)throws IOException
{
BufferedReader
s=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\t?Enter
your choice please?");
System.out.println("\t1.Lower
Stall \t Rs.50/-");
System.out.println("\t2.Upper
Stall \t Rs.70/-");
System.out.println("\t3.Balcony \t Rs.90/-");
System.out.println("\t4.Box \t Rs.120/-");
String
input=s.readLine();
int
ch=Integer.parseInt(input);
switch(ch)
{
case
1:
System.out.println("Please
enter the number of tickets");
String
d=s.readLine();
int
n=Integer.parseInt(d);
int
rs=50*n;
System.out.println("Total
Amount :Rs. "+rs+"/-");
break;
case
2:
System.out.println("Please
enter the number of tickets");
String
d1=s.readLine();
int
n1=Integer.parseInt(d1);
int
rs1=70*n1;
System.out.println("Total
Amount :Rs. "+rs1+"/-");
break;
case
3:
System.out.println("Please
enter the number of tickets");
String
d2=s.readLine();
int
n2=Integer.parseInt(d2);
int
rs2=90*n2;
System.out.println("Total
Amount :Rs. "+rs2+"/-");
break;
case
4:
System.out.println("Please
enter the number of tickets");
String
d3=s.readLine();
int
n3=Integer.parseInt(d3);
int
rs3=120*n3;
System.out.println("Total
Amount :Rs. "+rs3+"/-");
break;
default:
System.out.println("Wrong
Choice !!");
break;
}
}
}
7.Write
a program to create a class BANK, which have the following:
Member
variables:
Account
Number
|
Account
holder’s name
|
Balance
Amount
|
The
class should have the following member functions:
A
parameterized and a non parameterized constructor.
|
A
function to update the balance amount on deposit of amount.
|
A
function to update the balance amount on deposit of amount.
|
A
function to display all the values.
|
Call
all the above functions from the main() function.
Answer:
class
Bank
{
String
name;
long
accno; String accType;
double
bal; public Bank()
{
name="";
accno=0;
accType="";
bal=0;
}
public
Bank(String name1,long accno1,String accType1,double bal1)
{
name=name1;
accno=accno1;
accType=accType1;
bal=bal1;
}
public
void initialize(String name1,long accno1,String accType1,double bal1)
{
name=name1;
accno=accno1;
accType=accType1;
bal=bal1;
}
public
void deposit(double amnt)
{
bal=
bal+amnt;
}
public
void withdraw(double amnt)
{
if(amnt<=bal)
bal=bal-amnt;
}
public
void display()
{
System.out.println("NAME "+name);
System.out.println("ACCOUNT
NUMBER "+accno);
System.out.println("ACCOUNNT
TYPE "+accType);
System.out.println("BALANCE "+bal);
}
public
static void main()
{
Bank
obj=new Bank("Bennington ",654165465,"Savings",10);
obj.deposit(200);
obj.withdraw(10);
obj.display();
obj.initialize("Albert
Einstein",654165465,"Savingssdf",200); obj.deposit(100);
obj.withdraw(200);
obj.display();
}
}
8.Write
a program to input any given String. Calculate the total number of digits,
vowels and other characters present in the String.
Answer:
import
java.io.*;
class
string_parser
{
public
static void main(String[] args)throws IOException
{
BufferedReader
s=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please
Enter a String value ");
String
a=s.readLine();
int
q=a.length();
a.toLowerCase();
int
vowel=0,consonant=0,digit=0, other=0;
for(int i=0;i<q;i++){
if(a.charAt(i)==
'a'||a.charAt(i)== 'e'||a.charAt(i)== 'i'||a.charAt(i)== 'o'|| a.charAt(i)==
'u' )
vowel++;
else
if(a.charAt(i)== '0'||a.charAt(i)== '1'||a.charAt(i)== '2'||a.charAt(i)== '3'||
a.charAt(i)== '4'||a.charAt(i)== '5'|| a.charAt(i)== '6'||a.charAt(i)== '7'||
a.charAt(i)== '8' ||a.charAt(i)== '9')
digit++;
else
if(a.charAt(i)!=' ')
consonant++;
else
other++;
}
System.out.println("Number of Vowels:
"+vowel);
System.out.println("Number of Consonants:
"+consonant);
System.out.println("Number of digits:
"+digit);
System.out.println("Number of other
characters: "+other);
}
}
9.Define
a class STUDENT described as below:
Data
members:
Name,
Age, m1, m2, m3 (marks in 3 subjects) max, avg.
Member
methods:
a.
A parameterized constructor to initialize the data members.
|
b.
To accept the details of a student.
|
c.
To compute the average and the maximum out of 3 marks.
|
d.
To display the name, age, marks in 3 subjects, max and avg.
|
Answer:
import
java.io.*;
class
Student
{
String
Name;
int
age;
double
m1,m2,m3,max=9.0,avg=7.0;
public
Student(String Name1,int age1,double ma1,double ma2,double ma3)
{
Name=Name1;
age=age1;
m1=ma1;m2=ma2;m3=ma3;max=0.0;avg=0.0;
cal();
}
public
void details(String Name2,int age2,double maa1,double maa2,double maa3)
{
Name=Name2;
age=age2;
m1=maa1;m2=maa2;m3=maa3;
}
public
void cal()
{
avg=(m1+m2+m3)/3;
max=Math.max(m1,(Math.max(m2,m3)));
System.out.println("A:"+max);
System.out.println("B:"+avg);
}
public
void display()
{
System.out.println("Name : "+Name);
System.out.println("Age : "+age);
System.out.println("Marks
1 : "+m1);
System.out.println("Marks
2 : "+m2);
System.out.println("Marks
3 : "+m3);
System.out.println("Maximum
marks : "+max);
System.out.println("Average : "+avg+"\n");
}
public
static void main(String[]args)
{
Student
obj=new Student("Eminem ",15,95,80,79);
obj.display();
obj.details("Marshall
Mathers ",15,99,98,100);
obj.cal();
obj.display();
}
}
Comments
Post a Comment