Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 84

Divyangna

ECE 3A

22/12

1.Program to print hello world on consoleimport java.util.*;


public class Me
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}

Divyangna

ECE 3A

22/12

2.Program to print the area of circle, rectangle and


squareimport java.util.*;
public class Area
{
public static void main(String args[])
{
float d,a1,a2,a3,r,l,b;
System.out.println("Enter the radius of circle=");
Scanner c1=new Scanner(System.in);
r=c1.nextFloat();
a1=3.14f*r*r;
System.out.println("The area of circle is="+a1);
System.out.println("Enter the length of rectangle=");
Scanner c2=new Scanner(System.in);
l=c2.nextFloat();
System.out.println("Enter the breadth of rectangle ");
Scanner c3=new Scanner(System.in);
b=c3.nextFloat();
a2=l*b;
System.out.println("The area of rectangle is="+a2);
System.out.println("Enter side of square=");
Scanner c4=new Scanner(System.in);
d=c4.nextFloat();
a3=d*d;
System.out.println("The area of square is="+a3);
2

Divyangna
}
}

ECE 3A

22/12

Divyangna

ECE 3A

22/12

3.Program to print the circumference of circleimport java.util.*;


public class Circumference
{
public static void main(String args[])
{
float r,c;
System.out.println("Enter the radius of circle=");
Scanner c1=new Scanner(System.in);
r=c1.nextFloat();
c=2*3.14f*r;
System.out.println("The Circumference of circle="+c);
}
}

Divyangna

ECE 3A

22/12

4.Program to count the number of digits of a


numberimport java.util.*;
public class Count
{
public static void main(String a[]) //to count the number of digits
{
int no,t,i,j=0;
System.out.println("Enter any number");
Scanner c=new Scanner(System.in);
no=c.nextInt();
for(i=no;i>0;i=i/10)
{
t=i%1;
j++;}
5

Divyangna

ECE 3A

22/12

System.out.println("Number of digits in above number are= "+j);


}
}

5.Program to find whether the number is even or


oddimport java.util.*;
public class Even
{
public static void main(String args[]) // find number is even or odd
{
int no,i;
System.out.println("Enter any numbr= ");
Scanner c=new Scanner(System.in);
no=c.nextInt();
if(no%2==0)
{
System.out.println("Number is even ");
}
6

Divyangna
else
{
System.out.println("Odd numbr");
}
}
}

ECE 3A

22/12

Divyangna

ECE 3A

22/12

6.Program to find even number from 1- 100import java.util.*;


public class Even100
{
public static void main(String arp[])
{
int i,n; // find even number from 1 to 100
for(i=2;i<=50;i++)
{
if(i%2==0)
{
System.out.println(i);} }

Divyangna

ECE 3A

22/12

7.Program to find whether a year is leap year or


notimport java.util.*;
public class Leap
{
public static void main(String a[])
{
int no; // year is leap or not
System.out.println("Enter any year");
Scanner c=new Scanner(System.in);
no=c.nextInt();
9

Divyangna
if(no%4==0)
{
System.out.println("The year is leap year");
}
else
{
System.out.println("Not a leap year");
}
}
}

10

ECE 3A

22/12

Divyangna

8.// Prime not done

11

ECE 3A

22/12

Divyangna

ECE 3A

22/12

9.Program to swap 2 numbers using 3rd variableimport java.util.*;


public class Swap1
{
public static void main(String ar[])
{
int a,b,c;
System.out.println("nter 1st number a=");
Scanner c1=new Scanner(System.in);
a=c1.nextInt();
System.out.println("nter 2nd number b=");
Scanner c2=new Scanner(System.in);
b=c2.nextInt();
c=a;
a=b;
b=c;
System.out.println("After swapping a= "+a);
System.out.println("After swapping b= "+b);
}
}

12

Divyangna

13

ECE 3A

22/12

Divyangna

ECE 3A

22/12

10.Program to swap 2 number without using 3rd


variableimport java.util.*;
public class Swap
{
public static void main(String ar[])
{
int a,b,c;
System.out.println("nter 1st number a=");
Scanner c1=new Scanner(System.in);
a=c1.nextInt();
System.out.println("nter 2nd number b=");
Scanner c2=new Scanner(System.in);
b=c2.nextInt();

a=a+b;
b=a-b;
a=a-b;
System.out.println("After swapping a= "+a);
System.out.println("After swapping b= "+b);
}
} // Not DONE

14

// swap without 3rd variable

Divyangna

ECE 3A

22/12

10. Program to print the table of a numberimport java.util.*;


public class Table

//Prit table

{
public static void main(String ar[])
{
int t,i,n;
System.out.println("nter any number for table=");
Scanner c=new Scanner(System.in);
n=c.nextInt();
System.out.println("Table is=");
for(i=1;i<=10;i++)
{
t=i*n;
System.out.println(t);
}
}
}

15

Divyangna

ECE 3A

22/12

11.Program to find whether a number is positive,


negative or equal to zeroimport java.util.*;
public class ValidNumber
{
public static void main(String args[])
{
float n;
System.out.println("Enter the number=");
Scanner c1=new Scanner(System.in);
n=c1.nextFloat();
if(n>0)
{
16

Divyangna

ECE 3A

System.out.println("Entered number is positive");


}
else if(n<0)
{
System.out.println("Entered no is negative");
}
else{
System.out.println("Entered number is equal to zero");
}
}
}

17

22/12

Divyangna

ECE 3A

22/12

12.Program to dind whether a year is valid or notimport java.util.*;


public class ValidYear
{
public static void main(String a[])
{
int no;
System.out.println("Enter any year=");
Scanner c=new Scanner(System.in);
no=c.nextInt();
if((no>=1990)&&(no<=2050))
{
System.out.println("The year is valid");
}
else
{
18

Divyangna
System.out.println("Not valid year");
}
}
}

19

ECE 3A

22/12

Divyangna

ECE 3A

22/12

13.Program to display days of week i.e. Monday to


Sunday using switch-case statementimport java.util.*;
public class WeekDay
{
public static void main(String ar[])
{
int n;
System.out.println("Enter any number from 1-7=");
Scanner c=new Scanner(System.in);
n=c.nextInt();
switch(n)
{
case 1:
System.out.println("MONDAY");
break;
case 2:
System.out.println("TUESDAY");
20

Divyangna

ECE 3A

break;
case 3:
System.out.println("WEDNESDAY");
break;
case 4:
System.out.println("THURSDAY");
break;
case 5:
System.out.println("FRIDAY");
break;
case 6:
System.out.println("SATURDAY");
break;
case 7:
System.out.println("SUNDAY");
break;
default:
System.out.println("You have enterd a wrong number");
break;
}
}
}

21

22/12

Divyangna

ECE 3A

22/12

14.Program to print 1 to 10 naural numbrsimport java.util.*;


public class Whole
{
public static void main(String ars[])
{
int i;
System.out.println("1- 10 Natural numbers are");
for(i=1;i<=10;i++)
{
System.out.println(i);
}
}

22

Divyangna

ECE 3A

22/12

15.Program to print the factorial of numberimport java.util.*;


public class Factorial
{public static void main(String ar[])
{
int n,i,k=1;
System.out.println("Enter a number for factorial= ");
Scanner c=new Scanner(System.in);
n=c.nextInt();
for(i=n;i>=1;i--)
{
k=k*i;

23

Divyangna

ECE 3A

}
System.out.println("Factorial of number is ="+k);
}
}

24

22/12

Divyangna

ECE 3A

22/12

16. Program to print the squares of 1st 10 natural


numbersimport java.util.*;
public class Square
{
public static void main(String ar[])
{
int i,n,sum;
System.out.println("The squares of 1st 10 l number =");
for(i=1;i<=10;i++)
{
sum=i*i;
System.out.println(sum);
}
}
}

25

Divyangna

ECE 3A

22/12

17. Program to print series 10,9,8......1;


import java.util.*;
public class Series1
{
public static void main(String ar[])
{
int i;
for(i=10;i>=1;i--)
{
System.out.println(i);
}
}
}

26

Divyangna

ECE 3A

22/12

18.Program to print first N natural numbers and


their sumimport java.util.*;
public class Sum
{
public static void main(String ar[])
{
int i,sum=0,n;
System.out.println("Enter the number limit= ");
Scanner c=new Scanner(System.in);
n=c.nextInt();
for(i=1;i<=n;i++)
{
27

Divyangna

ECE 3A

22/12

sum=sum+i;
}
System.out.println("Sum of 1st n natural numbers= "+sum);

}}

19. Program to calculate Simple Interest for p, n &


rimport java.util.*;
public class Interest
{
public static void main(String ar[])
{
int i,n,p,r,ci;
System.out.println("Enter the principal amount= ");
Scanner c1=new Scanner(System.in);
p=c1.nextInt();
System.out.println("Enter the rate= ");
Scanner c2=new Scanner(System.in);
28

Divyangna

ECE 3A

r=c2.nextInt();
System.out.println("Enter time in years= ");
Scanner c3=new Scanner(System.in);
n=c1.nextInt();
ci=(p*n*r)/100;
System.out.println("The simple interest = "+ci);
}
}

29

22/12

Divyangna

ECE 3A

22/12

20. Program to find whether a person can vote or


notimport java.util.*;
public class Vote
{
public static void main(String ar[])
{
int n;
System.out.println("Enter the age = ");
Scanner c=new Scanner(System.in);
n=c.nextInt();
if(n>=18)
{
System.out.println(" person can vote ");
30

Divyangna
}
else
{
System.out.println(" person can notvote ");
}
}

31

ECE 3A

22/12

Divyangna

ECE 3A

22/12

21. Program to calculate the Gross Salary of an


employee.
Assume Gross Salary=Basic Salary+DA+HRA
DA=40% of Basic Salary
HRA=20% of Basic Salary------import java.util.*;
public class Salary
{
public static void main(String ar[])
{
float gs,bs,da,hra;
System.out.println("Enter the basic salary of employee= ");
Scanner c=new Scanner(System.in);
bs=c.nextFloat();
da=0.4f*bs;
hra=0.2f*bs;
gs=bs+da+hra;
System.out.println("Gross salary= "+gs);
}

32

Divyangna
}

33

ECE 3A

22/12

Divyangna

ECE 3A

22/12

22. /*Create a class called Invoice that a hardware store


might use to represent an invoice for an item sold at the
store. An Invoice should include four pieces of information
as instance variablesa part number (type String), a part
description (type String), a quantity of the item being
purchased (type int) and a price per item (double). Your
class should have a constructor that initializes the four
instance variables. Provide a set and a get method for
each instance variable. In addition, provide a method
named getInvoiceAmount that calculates the invoice
amount (i.e., multiplies the quantity by the price per
item), then returns the amount as a double value. If the
quantity is not positive, it should be set to 0. If the price
per item is not positive, it should be set to 0.0. Write a
test application named InvoiceTest that demonstrates
class Invoices capabilities.*/
class Invoice
{
String partno,partdesc;
int quantity;
double priceperitem;
public Invoice()
{
partno=" ";
34

Divyangna

ECE 3A

partdesc= " ";


quantity=0;
priceperitem=0;
}
public void setPart(String partno)
{
this.partno=partno;
}
public void setDescribe(String partdesc)
{
this.partdesc=partdesc;
}
public void setQuantity(int quantity)
{
this.quantity=quantity;
}
public void setPricePer(double priceperitem)
{
this.priceperitem=priceperitem;
}
public String getPart()
{
return partno;
}
public String getDescribe()
{
35

22/12

Divyangna

ECE 3A

22/12

return partdesc;
}
public int getQuantity()
{
return quantity;
}
public double getPriceper()
{
return priceperitem;
}}
class InvoiceTest1 extends Invoice
{
double inamount;
void getInAmount( int quantity,double priceperitem)
{
inamount=quantity*priceperitem;
System.out.println("The total amount invoice amount= "+inamount);
}
boolean isValidQ(int quantity)
{
if(quantity<0)
return false;
else
return true;
}
boolean isValidPrice(double priceperitem)
36

Divyangna

ECE 3A

{
if(priceperitem<0)
return false;
else
return true;
}
}

public class InvoiceTest


{
public static void main(String args[])
{
int q;
String p,d;
double r;
q=3;
p="PHONE";
d="MOBILE";
r=30000;
InvoiceTest1 n=new InvoiceTest1();
n.setPart(p);
n.setDescribe(d);
n.setQuantity(q);
n.setPricePer(r);
System.out.println("Part "+n.getPart());
System.out.println("Descrbtiob "+n.getDescribe());
37

22/12

Divyangna

ECE 3A

22/12

System.out.println("Quantity "+n.getQuantity());
System.out.println("Check= "+n.isValidQ(n.getQuantity()));
System.out.println("Price per item "+n.getPriceper());
System.out.println("Check= "+n.isValidPrice(n.getPriceper()));
n.getInAmount(n.getQuantity(),n.getPriceper());
}
}

38

Divyangna

ECE 3A

22/12

23.A company wants to transmit data over the


telephone but is concerned that its phones may be
tapped. It has asked you to write a program that
will encrypt the data so that it may be transmitted
more securely. All the data is transmitted as fourdigit integers. Your application should read a fourdigit integer entered by the user and encrypt it as
follows: Replace each digit with the result of
adding 7 to the digit and getting the remainder
after dividing the new value by 10. Then swap the
first digit with the third, and swap the second digit
with the fourth. Then print the encrypted integer.
Write a separate application that inputs an
encrypted four-digit integer and decrypts it to
form the original number.*/
class Transmit
{
int integer;
public int encrypt(int i)
{
int d1,d2,d3,d4;
d1=i%10;
i/=10;
39

Divyangna

ECE 3A

d1=(d1+7)/10;
d2=i%10;
i/=10;
d2=(d2+7)/10;
d3=i%10;
i/=10;
d3=(d3+7)/10;
i=(i+7)/10;
d4=d2*1000+d1*100+i*10+d3;
return d4;
}
public int decrypt(int i)
{
int d1,d2,d3,d4;
d1=i%10;
i/=10;
d1=(d1*10)-7;
d2=i%10;
i/=10;
d2=(d2*10)-7;
d3=i%10;
i/=10;
d3=(d3*10)-7;
i=(i*10)-7;
d4=d2*1000+d1*100+i*10+d3;
return d4;
40

22/12

Divyangna

ECE 3A

22/12

}
};
public class Code
{
public static void main(String args[])
{
int t1,t2;
Transmit t=new Transmit();
t1=t.encrypt(1234);
System.out.println("code is= "+t1);
t2=t.decrypt(t1);
System.out.println("entered integer is= "+t2);
}
}

41

Divyangna

ECE 3A

22/12

25. Develop a Java application that will determine


whether any of several department-store
customers has exceeded the credit limit on a
charge account. For each customer, the following
facts area available:
a) account number
b) balance at the beginning of the month
c) total of all items charged by the customer this
month
d) total of all credits applied to the customers
account this month
e) allowed credit limit.
The program should input all these facts as
integers, calculate the new balance (= beginning
balance+ charges credits), display the new
balance and determine whether the new balance
exceeds the customers credit limit. For those
customers whose credit limit is exceeded, the
program should display the message "Credit limit
exceeded". */
42

Divyangna

ECE 3A

class DeptStore
{
int accNo;
int balAbeg;
int totCharge;
int totCredit;
int allowCredit;
DeptStore()
{
accNo=0;
balAbeg=5000;
totCharge=20000;
totCredit=100;
allowCredit=500;
}
DeptStore(int a,int b,int tch,int tcr,int alc)
{
accNo=a;
balAbeg=b;
totCharge=tch;
totCredit=tcr;
allowCredit=alc;
}
public int getAccNo()
{
return accNo;
43

22/12

Divyangna

ECE 3A

22/12

}
public int getBalAbeg()
{
return balAbeg;
}
public int getTotCharge()
{
return totCharge;
}
public int getTotCredit()
{
return totCredit;
}
public int getAllowCredit()
{
return allowCredit;
}
public int checkClimit(int balAbeg,int totCharge,int totCredit,int allowCredit)
{
int newCharges;
if(totCredit>allowCredit)
{
System.out.println("credit limit exceded");
return 0;
}
else
44

Divyangna

ECE 3A

22/12

{
newCharges=balAbeg+totCharge-totCredit;
return newCharges;
}
}
}
public class Customer
{
public static void main(String args[])
{
int a,b,tch,tcr,ac,nc;
DeptStore ds=new DeptStore(9848,3000,10000,80,150);
a=ds.getAccNo();
System.out.println("Account number= "+a);
b=ds.getBalAbeg();
System.out.println("Balance at the beginning of the month= "+b);
tch=ds.getTotCharge();
System.out.println("Total charge= "+tch);
tcr=ds.getTotCredit();
System.out.println("otal of all credits applied to the customers account
this month= "+tcr);
ac=ds.getAllowCredit();
System.out.println("AC= "+ac);
nc=ds.checkClimit(b,tch,tcr,ac);
System.out.println("new charges="+nc);
}
}
45

Divyangna

46

ECE 3A

22/12

Divyangna

ECE 3A

22/12

26. /*Create a class called Employee that includes


three pieces of information as instance variables
a first name (type String), a last name (type
String) and a monthly salary (double). Yourclass
should have a constructor that initializes the three
instance variables. Provide a set and a ge tmethod
for each instance variable. If the monthly salary is
not positive, set it to 0.0. Write a test application
named EmployeeTest that demonstrates class
Employees capabilities. Create two Employee
objects and display each objects yearly salary.
Then give each Employee a 10% raise and display
each Employees yearly salary again */
class Employee
{
String firstName;
String lastName;
double monthlySalary;
Employee(String f,String l,double d)
{
firstName=f;
lastName=l;
monthlySalary=d;
}
public void setFirstName(String f)
{
firstName=f;
47

Divyangna

ECE 3A

}
public void setLastName(String l)
{
lastName=l;
}
public void setMonthlySalary(double d)
{
monthlySalary=d;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getMonthlySalary()
{
return monthlySalary;
}
public double Check(double d)
{
if(d>0)
return d;
else
48

22/12

Divyangna

ECE 3A

22/12

return 0.0;
}
public double RaisedSalary(double d)
{
double rs;
rs=(d*10)/100;
rs+=d;
return rs;
}
};
class EmployeeTest
{
public static void main(String args[])
{
String fn,ln;
double ms,cs;
Employee e=new Employee("juhi","girdhar",200000.0);
Employee d=new Employee("aman","khokhar",100000.0);
e.setFirstName("shilpi");
e.setLastName("sidana");
e.setMonthlySalary(50000.0);
fn=e.getFirstName();
ln=e.getLastName();
ms=e.getMonthlySalary();
cs=e.Check(ms);
ms=e.RaisedSalary(cs);
49

Divyangna

ECE 3A

22/12

System.out.println("First name of employee 1= "+fn);


System.out.println("last name of employee 1= "+ln);
System.out.println("monthly salary of employee 1= "+cs);
System.out.println("raised 10% in salary,raised salary = "+ms);
d.setFirstName("mani");
d.setLastName("bawa");
d.setMonthlySalary(70000.0);
fn=d.getFirstName();
ln=d.getLastName();
ms=d.getMonthlySalary();
cs=d.Check(ms);
ms=d.RaisedSalary(cs);
System.out.println("First name of employee 2="+fn);
System.out.println("last name of employee 2="+ln);
System.out.println("monthly salary of employee 2="+ms);
System.out.println("raised 10% in salary,raised salary ="+ms);
}

50

Divyangna
}

51

ECE 3A

22/12

Divyangna

ECE 3A

22/12

27. /*The greatest common divisor (GCD) of two


integers is the largest integer that evenly divides
each of the two numbers. Write a method gcd that
returns the greatest common divisor of two
integers */
import java.util.*;
class Number
{
int x,y;
Number()
{
x=1;
y=1;
}
public void Result(int x,int y)
{
int m,i;
if(x>y)
m=y;
else
m=x;

for(i=m;i>=1;i--)
{

52

Divyangna

ECE 3A

22/12

if(x%i==0 && y%i==0)


{
System.out.println("GCD of two number is "+i) ;
break;
}
}
}
};
class GCD
{
public static void main(String args[])
{
int n1,n2;
Number n=new Number();
Scanner s=new Scanner(System.in);
System.out.println("enter first number=");
n1=s.nextInt();
System.out.println("enter 2nd number=");
n2=s.nextInt();
n.Result(n1,n2);
}

53

Divyangna

54

ECE 3A

22/12

Divyangna

ECE 3A

22/12

28. Develop a Java application that will determine


the gross pay for each of three employees. The
company pays straight time for the first 40 hours
worked by each employee and time and a half for
all hours worked in excess of 40 hours. You are
given a list of the employees of the company, the
number of hours each employee worked last week
and the hourly rate of each employee. Your
program should input this information for each
employee and should determine and display the
employees gross pay. Use class Scanner to input
the data. */
import java.util.*;
class Employee
{
int hourlyPay;
int hours;
Employee()
{
hours=40;
hourlyPay=100;
}
Employee(int h,int hp)
{
hours=h;
hourlyPay=hp;
}
public void isValidHours(int t)
{
if(t==40)
System.out.println("employee works for 40 hours");
else
{
if(t<40)
System.out.println("employee works for less than 40
hours");
else
55

Divyangna

ECE 3A

22/12

System.out.println("employee works for more than 40


hours");
}
}
public int calPay(int t,int pay)
{
int totalPay;
if(t==40)
{
totalPay=40*pay;
return totalPay;
}
else
{
if(t>40)
{
t-=40;
totalPay=(t*pay/2)+(40*pay);
return totalPay;
}
else
{
totalPay=t*pay;
return totalPay;
}
}
}
};
public class GrossSalary
{
public static void main(String args[])
{
int tp;
Scanner s=new Scanner(System.in);
System.out.println("enter number of hours for employee 1=");
int h=s.nextInt();
System.out.println("enter hourlypay for employee 1=");
int hp=s.nextInt();
Employee e1=new Employee(h,hp);
e1.isValidHours(h);
tp=e1.calPay(h,hp);
System.out.println("gross salary of employee1="+tp);
System.out.println("enter number of hours for employee 2=");
h=s.nextInt();
56

Divyangna

ECE 3A

22/12

System.out.println("enter hourlypay for employee 2=");


hp=s.nextInt();
Employee e2=new Employee(h,hp);
e2.isValidHours(h);
tp=e2.calPay(h,hp);
System.out.println("gross salary of employee2="+tp);
System.out.println("enter number of hours for employee 3=");
h=s.nextInt();
System.out.println("enter hourlypay for employee 3=");
hp=s.nextInt();
Employee e3=new Employee(h,hp);
e3.isValidHours(h);
tp=e3.calPay(h,hp);
System.out.println("gross salary of employee3="+tp);
}
}

30. /*(Palindromes) A palindrome is a sequence of characters that reads the same


backward as forward.
For example, each of the following five-digit integers is a palindrome: 12321,
55555, 45554 and 11611.
Write an application that reads in a five-digit integer and determines whether it is a
palindrome.

57

Divyangna

ECE 3A

22/12

If the number is not five digits long, display an error message and allow the user to
enter a new value. */

import java.util.*;
class Palindrome
{
int number;
Palindrome()
{
number=12321;
}
public int checkNumber(int n)
{
int count=0,rc;
while(n!=0)
{
n/=10;
count++;
}
if(count==5)
{
System.out.println("entered number has five digit");
return n;
}
else
{
58

Divyangna

ECE 3A

22/12

System.out.println("invalid number");
System.out.println("enter another number=");
Scanner c=new Scanner(System.in);
n=c.nextInt();
rc=checkNumber(n);
return rc;
}
}
public void checkPalindrome(int n)
{
int p=0,q;
number=n;
while(n!=0)
{
q=n%10;
n/=10;
p=p*10+q;
}
if(p==number)
System.out.println("number is palindrome");
else
System.out.println("number is not palindrome");
}
};
public class Integer
{
59

Divyangna

ECE 3A

22/12

public static void main(String args[])


{
int r;
Scanner s=new Scanner(System.in);
System.out.println("enter any five digit number=");
int num=s.nextInt();
System.out.println("entered number is="+num);
Palindrome p=new Palindrome();
r=p.checkNumber(num);
p.checkPalindrome(r);
}
}

60

Divyangna

ECE 3A

22/12

31. /*An integer number is said to be a perfect


number if its factors, including 1 (but not the
number itself), sum to the number.For example, 6
is a perfect number, because 6 = 1 + 2 + 3. Write a
method perfect that determines whether
parameter number is a perfect number. Use this
method in an application that determines and
displays all the perfect numbers between 1 and
1000. Display the factors of each perfect number
to confirm that the number is indeed perfect.
Challenge the computing power of your computer
by testing numbers much larger than 1000. Display
the results. */
import java.util.*;
61

Divyangna

ECE 3A

22/12

class Perfect
{
int number;
public void checkPerfect(int n)
{
int i=1,digit=0;
number=n;
while(i<number)
{
if(number%i==0)
digit+=i;
i++;
}
if(digit==number)
System.out.println(n+"is a Perfect Number");
else
System.out.println(n+"is not a Perfect Number");
}
public void Display()
{
int i,j,digit;
for(i=1;i<=10000;i++)
{
j=1;
digit=0;
while(j<i)
62

Divyangna

ECE 3A

22/12

{
if(i%j==0)
digit+=j;
j++;
}
if(digit==i)
System.out.println(i+"is a Perfect Number");
else
continue;
}
}
};
class Number
{
public static void main(String args[])
{
int k=8128;
Perfect p=new Perfect();
p.checkPerfect(k);
p.Display();
}
}

63

Divyangna

ECE 3A

22/12

32.(Savings Account Class) Create class


SavingsAccount. Use a static variable
annualInterestRate to store the annual interest
rate for all account holders. Each object of the
class contains a private instance variable
savingsBalance indicating the amount thesaver
currently has on deposit. Provide method calculate
MonthlyInterest to calculate the monthly interest
by multiplying the savingsBalance by
64

Divyangna

ECE 3A

22/12

annualInterestRate divided by 12this interest


should be added to savings- Balance. Provide a
static method modifyInterestRate that sets the
annualInterestRate to a new value. Write a
program to test class SavingsAccount. Instantiate
two savingsAccount objects, saver1 and saver2,
with balances of $2000.00 and $3000.00,
respectively. Set annualInterestRate to 4%, then
calculate the monthly interest and print the new
balances for both savers. Then set the
annualInterestRate to 5%, calculate the next
months interest and print the new balances for
both savers. */
class SavingsAccount
{
static float annualInterestRate;
float savingsBalance;
SavingsAccount(float annualInterestRate,float savingsBalance)
{
this.annualInterestRate=annualInterestRate;
this.savingsBalance=savingsBalance;
}
public float calculateMonthlyInterest()
{
float sb;
float monthlyInterest;
monthlyInterest=(savingsBalance*annualInterestRate)/12;
sb=savingsBalance+monthlyInterest;
65

Divyangna
/*

ECE 3A

22/12

System.out.println("mi="+monthlyInterest);
System.out.println("sb="+savingsBalance);
System.out.println("air="+annualInterestRate);*/
return sb;

}
public static void modifyInterestRate(float air)
{
annualInterestRate=air;
}
};
public class Saver
{
public static void main(String args[])
{
float s1,s2;
SavingsAccount saver1=new SavingsAccount(0.04f,2000);
SavingsAccount saver2=new SavingsAccount(0.04f,3000);
s1=saver1.calculateMonthlyInterest();
System.out.println("monthly interest of saver 1="+s1);
s2=saver2.calculateMonthlyInterest();
System.out.println("monthly interest of saver 2="+s2);
saver1.modifyInterestRate(0.05f);
s1=saver1.calculateMonthlyInterest();
System.out.println(" new monthly interest of saver 1="+s1);
saver2.modifyInterestRate(0.05f);
s2=saver2.calculateMonthlyInterest();
66

Divyangna

ECE 3A

22/12

System.out.println("new monthly interest of saver 2="+s2);


}
}

33. /*Write a class distance with attributes feet


and distance? Validate the attribute inch? Use
parameterized constructor to initialize the
67

Divyangna

ECE 3A

22/12

attributes? Display the distance like 107?write a


method that transform the distance in feets and
inches to meters? */
class Distance
{
float distance;
int inches;
int feet;
Distance()
{
distance=0;
inches=0;
feet=0;
}
Distance(float d,int i,int f)
{
distance=d;
inches=i;
feet=f;
}
public void setDistance(float d)
{
distance=d;
}
public void setInches(int i)
{
68

Divyangna
inches=i;
}
public void setFeet(int f)
{
feet=f;
}
public float getDistance()
{
return distance;
}
public int getInches()
{
return inches;
}
public int getFeet()
{
return feet;
}
boolean isValidInches()
{
if(inches==7)
return true;
else
return false;
}
boolean isValidFeet()
69

ECE 3A

22/12

Divyangna
{
if(feet==10)
return true;
else
return false;
}
public float convertItoM(int i)
{
double f;
inches=i;
f=inches*(0.0254);
return (float)i;
}
public float convertDtoF(float d)
{
double f;
distance=d;
f=distance*(3.28);
return (float)f;
}
};
public class Transform
{
public static void main(String args[])
{
float d1,i2;
70

ECE 3A

22/12

Divyangna

ECE 3A

22/12

int f1,i1;
Distance s=new Distance();
s.setDistance(14000);
s.setInches(7);
s.setFeet(10);
s.isValidFeet();
s.isValidInches();
d1=s.getDistance();
f1=s.getInches();
i1=s.getFeet();
System.out.println("distance= "+d1);
System.out.println("feet= "+f1);
System.out.println("inches= "+i1);
i2=s.convertItoM(i1);
d1=s.convertDtoF(d1);
System.out.println("convertion of inches to meter= "+i2);
System.out.println("convertion of distance to feet= "+d1);
}
}

71

Divyangna

72

ECE 3A

22/12

Divyangna

ECE 3A

22/12

34. At an airport, traveler is allowed entry into flight


only if he clears following checks
Baggage check
Immigration check
Security check
Traveler has attributes travelid, baggageAmount,
expireYear, nocStatus. Use the appropriate Mutator
and Accessor methods to input all the values.
Check baggage amount between 0 and 40. Check
expire year is >=2001 and >=2025.
import java.util.*;
class Travel1
{
int travelid,bgam,exyear;
String nocstatus;
public Travel1()
{
travelid=0;
bgam=0;
exyear=0;
nocstatus="n";
}
public void setTravel(int id)
{
travelid=id;
}
public void setBaggage(int b)
{
73

Divyangna
bgam=b;
}
public void setExpire(int ex)
{
exyear=ex;
}
public void setStatus(String s)
{
nocstatus=s;
}
public int getTravel()
{
return travelid;
}
public int getBaggage()
{
return bgam;
}
public int getExpire()
{
return exyear;
}

public String getStatus()


{
return nocstatus;
74

ECE 3A

22/12

Divyangna

ECE 3A

}
boolean isValidExpire(int exyear)
{
if((exyear>=2001)&&(exyear<=2025))
{
return true;
}
else
{
return false;
}
}
}
public class Travel2
{
public static void main(String args[])
{
Travel1 T=new Travel1();
int t,b,y,h;
String s;
System.out.println("Enter Traveler id=");
Scanner c1=new Scanner(System.in);
t=c1.nextInt();
T.setTravel(t);
System.out.println("Enter Baggage amount=");
Scanner c2=new Scanner(System.in);
75

22/12

Divyangna

ECE 3A

22/12

b=c2.nextInt();
T.setBaggage(b);
System.out.println("Enter EXPIRE YEAR=");
Scanner c3=new Scanner(System.in);
y=c3.nextInt();
T.setExpire(y);
System.out.println("Enter NOC Status");
System.out.println("Enter 1 for YES");
System.out.println("Enter 2 for NO");
Scanner c4=new Scanner(System.in);
h=c1.nextInt();
if(h==1)
{
String o="YES";
T.setStatus("YES");
}
else
{
String l="NO";
T.setStatus("NO");
}
System.out.println("--------------------------------------------------");
System.out.println("Travelis "+T.getTravel());
System.out.println("Baggage amount "+T.getBaggage());
System.out.println("Expire Year "+T.getExpire());
System.out.println("Noc Status "+T.getStatus());
76

Divyangna

ECE 3A

22/12

}
}

35.Design a class to represent a bank


account.Include the following members:

77

Divyangna

ECE 3A

22/12

Data members:
Name of the depositor, aacount
number,type of account, balance amount in the
account;
Methods: 1.To assign initial values;
2.To deposite amount;
3.To withdraw amount;
4.To display the balance amount
import java.util.*;
class Account
{
double accnum,balance;
String name;
String type;
double newbalance;
Account()
{
accnum=0;
balance=0;
name=" ";
type=" ";
}
public void setNumber(double accnum)
{
this.accnum=accnum;
}
78

Divyangna
public void setBalance(double balance)
{
this.balance=balance;
}
public void setName(String name)
{
this.name=name;
}
public void setType(String type)
{
this.type=type;
}
public double getNumber()
{
return accnum;
}
public double getBalance()
{
return balance;
}
public String getName()
{
return name;
}
public String getType()
{
79

ECE 3A

22/12

Divyangna

ECE 3A

22/12

return type;
}
void Deposite()
{
double d,newbalance;
int j=0;
System.out.println("Enter amount to deposite= ");
Scanner c1=new Scanner(System.in);
d=c1.nextInt();
balance=balance+d;
System.out.println("Your current balance in account = "+balance);
System.out.println("Do you want to deposite more amount= ");
System.out.println("IF YES then press 1");
System.out.println("IF NO then press 2");
Scanner v=new Scanner(System.in);
j=v.nextInt();
if(j==1)
{
System.out.println("Enter amount to deposite= ");
Scanner c3=new Scanner(System.in);
d=c3.nextInt();
balance=balance+d;
System.out.println("Your current balance in account = "+balance);
System.out.println("Thank you");
}
else
80

Divyangna

ECE 3A

22/12

{
System.out.println("Thank you");
}
}
void Withdraw()
{
double w;
System.out.println("Your current balance in account = "+balance);
System.out.println("Enter amount to be withdrawn= ");
Scanner c2=new Scanner(System.in);
w=c2.nextInt();
newbalance=balance-w;
System.out.println(" Now Your current balance in account = "+newbalance);
}
void display()
{
System.out.println("Name= "+name);
System.out.println("Balance = "+newbalance);
}
}
public class BankAccount
{
public static void main(String ar[])
{
Account a=new Account();
a.setNumber(612374);
81

Divyangna

ECE 3A

22/12

a.setName("Prateek");
a.setType("Savings");
a.setBalance(50000);
System.out.println("Account number= "+a.getNumber());
System.out.println("Name = "+a.getName());
System.out.println("Type of account= "+a.getType());
System.out.println("Balance amount= "+a.getBalance());
System.out.println("------------------------------------");
a.Deposite();
System.out.println("------------------------------------");
a.Withdraw();
System.out.println("------------------------------------");
a.display();
System.out.println("------------------------------------");
}
}

82

Divyangna

83

ECE 3A

22/12

Divyangna
36.

84

ECE 3A

22/12

You might also like