Core java practical
Unit 1 java introduction
1 |
Write a program to evaluate simple
interest for a given principle, rate and time.
|
|
class prog1 { public static void main(String args[]) { int p=1000, time=5; float rate =4.5f, si; si=(p*time*rate)/100; System.out.println("Simple
interest is"+si); } } |
2 |
A motor cycle dealer sells two-wheelers to his
customer on loan, which is to be repaid in 5 years. The dealer charges simple
interest for the whole term on the day of giving the loan itself. The total amount
is then divided by 60(months) and is collected as equated monthly instalment
(EMI). Write a program to calculate the EMI for a loan of Rs. X, where X is given
from command line argument. Print the EMI value in
rupees. |
|
class prog2 { public static void main(String args[]) { int loanamt=Integer.parseInt(args[0]); float rate =
Float.parseFloat(args[1]); int time=5; float si=(loanamt*time*rate)/100; float totalamt = si+loanamt; float emi =totalamt/12; System.out.println("EMI
is"+emi); } } |
3 |
A car accessories shop assigns code 1 to seat
covers, 2 to steering wheel covers , 3 to car lighting and 4 for air purifiers. All
other items have code 5 or more. While selling the goods, a sales tax of 2% to
seat covers ,3% to steering wheel covers, 4% to car lighting, 2.5% to air
purifiers and 1.2% for all other items is charged. A list containing the product code and
price is given for making a bill. Write a java
program using switch statements to prepare a bill. |
|
import
java.util.*; class prog3 { public static void main(String args[]) { System.out.println("1 seat cover
-15000rs."); System.out.println("2 steering
wheel cover -1500rs."); System.out.println("3 car lighting
-7000rs."); System.out.println("4 air
purifiers -500rs."); System.out.println("5 other items-
enter amt."); System.out.println("\n Enter your
choice"); Scanner sc = new Scanner(System.in); int ch=sc.nextInt(); double amt; switch(ch) { case 1: amt=15000 + (15000*2)/100;
System.out.println("Seat cover total charges are: "+ amt); break; case 2: amt=1500 + (1500*3)/100; System.out.println("Steering
wheel cover total charges are: "+ amt); break; case 3: amt=7000 + (7000*4)/100;
System.out.println("Car lighting total charges are: "+ amt); break; case 4: amt=500 + (500*2.5)/100;
System.out.println("Seat cover total charges are: "+ amt); break; case 5: System.out.println("
Enter amt"); amt=sc.nextFloat();
System.out.println("Enter item"); String name= sc.next(); amt=amt + (amt*1.2)/100; System.out.println(name
+"total charges are: "+ amt); break; default:
System.out.println("Wrong choice entered"); } } } |
4 |
Write a java program to scan 3 integer values
from the command line argument and display the
maximum number using conditional operator. |
|
class prog4 { public static void main(String args[]) { int a,b,c,max; a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); c=Integer.parseInt(args[2]); max=(a>b)?
((a>c)?a:c):((b>c)?b:c); System.out.println("Max no
is"+max); } } |
5 |
Write a program to calculate the hypotenuse of
right angled triangle when other Sides of the triangle are given. (Hypotenuse =
square root (x*x + Y *Y)) |
|
import java.util.*; class prog5 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("\n Enter value
of x"); int x=sc.nextInt(); System.out.println("\n Enter value
of y"); int y=sc.nextInt(); double hyp =Math.sqrt((x*x)+(y*y)); System.out.println("Hypotenuse of
Right angled triangle is : "+ hyp); } } |
6 |
Write a program to calculate the area of square
and rectangle by overloading the area method. |
|
class prog6 { void area(int l) { System.out.println("Area of square
is"+(l*l)); } void area(int l, int b) { System.out.println("Area of
rectangle is"+(l*b)); } public static void main(String args[]) { prog6 p1= new prog6(); p1.area(5); p1.area(5,6); } |
7 |
Create a complex number class. The class should
have a constructor and methods to add, subtract and multiply two complex
numbers and to return the real and imaginary parts. |
|
class complex { int real, img; complex( ) { real=img=0; } complex(int x) { real=img=x; } complex(int x, int y) { real=x; img=y; } complex add(complex a, complex b) { complex temp= new complex(); temp.real=a.real+b.real; temp.img=a.img+b.img; return(temp); } complex
sub(complex a, complex b) { complex temp= new complex(); temp.real=a.real-b.real; temp.img=a.img-b.img; return(temp); } complex
multi(complex a, complex b) { complex temp= new complex(); temp.real=a.real*b.real; temp.img=a.img*b.img; return(temp); } void display() { System.out.println("Real"+real+"
Imag "+img); } } class complexdemo
{ public static void main(String args[]) { complex c1= new complex(5,6); complex c2= new complex(4,7); complex c3= new complex(); c3= c1.add(c1,c2); c3.display(); c3= c1.sub(c1,c2); c3.display(); c3= c1.multi(c1,c2); c3.display(); } } |
8 |
A shop during festival season offers a discount
10% for purchase made up to Rs.1,000, 12% for purchase value of Rs.1,000 or
more up to Rs 1,500 and 15% for purchase value of Rs.1,500 or more. Write a
program to implement the above scheme for a given sales and print out the
sales and print out the sales value, discount and net amount payable by a
customer. Create necessary methods and constructors. |
|
import
java.util.*; class calcost { int sv, disc; float netv; calcost() { sv=disc=0; netv=0; } void netpay(int sv) { if(sv<=1000) {
disc=10; } else if(sv>1000
&&sv<1500) { disc=12; } else { disc=15; } netv=sv -((disc*sv)/100); System.out.println("Netvalue for" +sv +"is"+netv); } } class u1_p8 { public static void main(String args []) { int n; Scanner sc = new Scanner (System.in); System.out.println("Enter purhase
value"); n=sc.nextInt(); calcost c1= new calcost(); c1.netpay(n); } } |
9 |
A bank gives 6.5% per annum interest on
deposits made in that bank. Write a program to calculate the total amount that a
person will receive after the end of 5 years for a deposit of Rs.5000 for compound
interest. Create necessary methods and constructors too. |
|
class calamount { double p,r,n; calamount(double p1,double r1, double n1) { p=p1; r=r1; n=n1; } void calint() { double ci=p*(Math.pow((1+(r/100)),n)); double netv=p+ci; System.out.println("Interest
is" +ci +"and amount recevied is"+netv); } } class u1_p9 { public static void main(String args []) { double p=5000, r=6.5,n=5; calamount c1= new calamount(p,r,n); c1.calint(); } } |
10 |
Write a java program to display powers of 2
i.e. 2,4,8,16 etc up to 1024 using bitwise operators. |
|
public class bitwisedemo { public static void
main(String args[]) { int a=1; for(int
i=1;i<11;i++) { System.out.println(a<<i);
} } } |