0% found this document useful (0 votes)
15 views

Java Unit 1 Pratical

java practial file

Uploaded by

dabhi16855002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Unit 1 Pratical

java practial file

Uploaded by

dabhi16855002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT 1 = STUDY PROGRAMS, PRACTICAL SYLLABUS PROGRAMS

 /* program to implement methods with no arg, one arg and mul ple arguments
it implements method overloading...(polymorphism) */
class demo1
{
// func on prototypes
sta c void area() // method declara on and defini on
{
System.out.println("Welcome to area calcula on..");
}
sta c void area(int l)
{
System.out.println("Area of square = " + l*l);
}
sta c void area(double r)
{
System.out.println("Area of circle = " + (3.14* (r*r)) );
}
sta c void area(int l, int b)
{
System.out.println("Area of rectangle = " + (l*b) );
}
public sta c void main(String args[])
{
// method calling
area(); // no arg... welcome only
area(10); // square
area(3.78); // circle
area(55,60); // rectangle
} //main over
} // class over

 // program to implement class and its methods


class emp
{
private int empid;
private String empname;
public void setdata()
{
empid = 113;
empname="Rohit";
System.out.println("Employee details are set");
}
public void showdata()
{
System.out.println("Employee details are as follows:");
System.out.println("Employee id = " + empid + "\nEmployee Name = " + empname);
}
} // class emp over

class demo2
{
public sta c void main(String args[])
{
emp e1 = new emp();
//e1.empid=101;
e1.setdata();
e1.showdata();
} //main over
} // class over

 //* first java program


public class hello
{
public sta c void main(String[] args)
{
System.out.println("Welcome to all");
System.out.println("hello " + args[0]);
} // main over
}

 /* program to input two numbers from command line and display maximum number using
ternary operator.
*/
class max
{
public sta c void main(String args[])
{
int a, b,c;
a = Integer.parseInt( args[0] );
b = Integer.parseInt( args[1] );
if ( a == b)
{
System.out.println("both numbers are equal");
}
else
{
c = (a>b) ? a : b ;
System.out.println("maximum = " + c );
}
} // main over
} // class over
 //*overload_demo
public class Overload_demo
{
sta c void calc_area()
{
int a,l,b;
l=10;
b= 5;
a = 2*(l*b);
System.out.println("Area is : " + a);
}
sta c void calc_area(int l)
{
int a = l*l;
System.out.println("Area of square is : " + a);
}
sta c void calc_area(int l, int b)
{
int a = 2*l*b;
System.out.println("Area of rectangle is : " + a);
}
sta c void calc_area(float l, int b)
{
float a = 2*(l*b);
System.out.println("Area of rectangle with float is : " + a);
}

sta c void calc_area(float r)


{
float a = 3.14f*r*r;
System.out.println("Area of circle is : " + a);

public sta c void main(String args[])


{
calc_area();
calc_area(5);
calc_area(5,3);
calc_area(2.7f);
calc_area(3.68f,9);
}
} //class over

 // program to use constructors in java


class product
{
private int pid,pqty;
private String pname;
private float prate;
// default constructor
product()
{
pid = 101;
pname="Refrigerator";
prate = 7999.99f;
pqty=27;
}
//parameterized constructor
product(int pid, String nm, float rate, int qty)
{
this.pid = pid;
pname=nm;
prate = rate;
pqty=qty;
}

product(product p) // copy constructor


{
pid = p.pid;
pname=p.pname;
prate = p.prate;
pqty=p.pqty;
}

public void display() // member method


{
System.out.println("\n\nProduct Details.......");
System.out.println("Id Name Rate Qty");
System.out.println("--------------------------------");
System.out.println(pid + " " + pname + " " + prate + " " + pqty);
}
} // class product over

class productdemo
{
public sta c void main(String args[])
{
product p1 = new product(); // default constructor
p1.display();

product p2 = new product(113,"Shampoo",345.35f,500);


// parameterized constructor
p2.display();

product p3 = new product(p1); // class as an argument


p3.display();

}//main over
}//class over
 // use of sta c method and sta c variable
// and sta c block

class sta cdemo


{
sta c int x=100, y=22;
sta c void findmax()
{
// int x=10, y=20; local variables allowed
if(x > y)
System.out.println(x + " is max");
else
System.out.println(y + " is max");
}
// sta c block is executed independently without main
sta c
{
System.out.println("Hello all to find max");
}
public sta c void main(String args[])
{
findmax();
} //main over
} // class over

 // sta c variable of class shared by


// various objects

class emp
{
private int empid;
public sta c int count=0;
public emp(int empid)
{ this.empid=empid; //solves ambiguity (confusion) between same name of
//class variable and func on argument variable
// this refers to current object
count++;
System.out.println(count + " object created");
}
void display()
{
System.out.println("\n Empd id = " + empid);
}
}
class sta cdemo1
{
public sta c void main(String args[])
{
emp e1 = new emp(101);
e1.display();
emp e2 = new emp(233);
e2.display();

emp e3 = new emp(158);


e3.display();
} //main over
} // class over

 // prac cal syllabus u1p2


/*
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 u1p2
{
public sta c void main(String args[])
{
int amt, years;
float rate, total, si, emi;
amt = Integer.parseInt(args[0]);
rate = 12.50f;
years = 5;
si = (amt*rate*years)/100;
total = amt + si;
emi = total/60;
System.out.println("Loan of " + amt + " at the rate of " + rate + "%" );
System.out.println("simple interest = " + si);
System.out.println("Total amount = " + total);
System.out.println("EMI for "+ years + " years = " +
String.format("%.2f",emi) );
}//main over
}//class over

 /* Java Prac cal unit 1 program 3


A car accessories shop assigns code 1 to seat covers, 2 to steering wheel covers ,
3 to car ligh ng 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 ligh ng, 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.u l.*;
class u1p3
{
public sta c void main(String args[])
{
int ch, amt;
double tax, totamt;
Scanner sc = new Scanner(System.in);
amt = 0;
tax=0.0;
totamt=0.0;
do
{
System.out.println("code product price tax");
System.out.println("----------------------------------- ");
System.out.println("1. Seat Cover\t\t4000\t2%");
System.out.println("2. Steering Wheel Cover\t1200\t3%");
System.out.println("3. Car Ligh ng\t\t3000\t4% ");
System.out.println("4. Air Purifiers\t800\t2.5%");
System.out.println("5. Other Items\t\t2000\t1.2%");
System.out.println("6. Exit from menu");
System.out.print("Enter your choice : ");
ch = sc.nextInt();
switch(ch)
{
case 1:
{
amt = amt + 4000;
tax = tax + (4000 * 2/100);
break;
}
case 2:
{
amt = amt + 1200;
tax = tax + (1200 * 3/100);
break;
}
case 3:
{
amt = amt + 3000;
tax = tax + (3000 * 4/100);
break;
}
case 4:
{
amt = amt + 800;
tax = tax + (800 * 2.5/100);
break;
}
case 5:
{
amt = amt + 2000;
tax = tax + (2000 * 1.2/100);
break;
}
case 6:
{
break;
}
}//switch over
} while (ch <=5);
totamt = amt + tax;
System.out.println ("\nYour Bill details......");
System.out.println ("\nAmount = " + amt);
System.out.println ("\nTax = " + tax);
System.out.println ("\nTotal Amount = " + totamt);
System.out.println ("\nThank you");
}//main over
}// class over

 // prac cal syllabus u1p4


/*
Write a java program to scan 3 integer values
from the command line argument
and display the maximum number
using condi onal operator.
*/

class u1p4
{
public sta c void main(String args[])
{
int a,b,c, max;
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
c = Integer.parseInt(args[2]);
if ( a == b && a == c && b == c)
System.out.println("All are euqal");
else
{
max = (a >= b && a >= c) ? a :
(b >= a && b >= c)? b : c;
System.out.println("max = " + max);
}
}//main over
}//class over

 /* prac cal syllabus unit 1 program 6


Write a program to calculate the area of square and rectangle by overloading the area method.
*/
class u1p6
{
sta c void area(int l){
System.out.println("\nArea of square with length " + l + " = " + (l*l) );
}
sta c void area(int l, int b){
System.out.println("\nArea of rectangle with length " + l + " and breadth " + b + " = " + (l*b) );
}
public sta c void main(String args[])
{
area(Integer.parseInt(args[0]) );
area(Integer.parseInt(args[0]), Integer.parseInt(args[1]) );
}//main over
}// class over

 /* prac cal syllabus unit 1 program 7


Create a complex number class.
The class should have a constructor and
methods to add, subtract and mul ply two complex numbers
and to return the real and imaginary parts.

Note:
compelx number is like
a+bi
where,
a = real part (integer)
b = imagenary part (integer)
i = posi ve (non-nega ve) integer
e.g.
c1 = 2+3i
c2 = 45+13i
c3 = 47+16i // c3 = c1 + c2

(-12) + (-5)i
*/

class complex
{
int a,b;
complex()
{
a = 10;
b = 10;
//System.out.println("object created with default values");
}
complex(int x, int y)
{
a = x; b =y;
System.out.println("object created with argument values");
}
void disp()
{
System.out.println(a + " + " + b + " i");
}
complex add(complex c2)
{
complex dummy = new complex();
dummy.a = a + c2.a;
dummy.b = b + c2.b;
return dummy;
}

complex sub(complex c2)


{
complex dummy = new complex();
dummy.a = a - c2.a;
dummy.b = b - c2.b;
return dummy;
}

complex mul(complex c2)


{
complex dummy = new complex();
dummy.a = a * c2.a;
dummy.b = b * c2.b;
return dummy;
}
} // class complex over

class u1p7
{
public sta c void main(String args[])
{
complex c1 = new complex(2,3);
c1.disp();
complex c2 = new complex(45,13);
c2.disp();
complex c3 = new complex();
c3 = c1.add(c2); // c3 = dummy
c3.disp();
complex c4 = new complex();
c4 = c1.sub(c2);
c4.disp();
complex c5 = new complex();
c5 = c1.mul(c2);
c5.disp();

} // main over
} // class u1p7 over

/*
data types
primi ve derived
int array, structure,
char linked list, stack, queue
long class, typedef, enum
float
double
boolean
*/

 // prac cal syllabus u1p9


 /*
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 a er the end of 5 years
for a deposit of Rs.5000 for compound interest.
Create necessary methods and constructors too.
*/
class deposit
{
int p, n;
float r, i, amt;
deposit()
{
p = 5000;
r = 6.5f;
n = 5;
}
void intcalc()
{
amt = p;
for (int j=1; j<=n; j++)
{
i = (amt * r * 1 ) / 100;
amt = amt + i; // new prin amt for next year
}
}
void disp()
{
System.out.println(p + " + " + (amt-p) + " = " + amt );
}
}
class u1p9
{
public sta c void main(String args[])
{
deposit d1 = new deposit();
d1.intcalc();
d1.disp();
}//main over
}//class over

 /* prac cal syllabus unit 1 program 10


Write a java program to display powers of 2 i.e. 2,4,8,16 etc up to 1024 using bitwise operators.
*/
class u1p10
{
public sta c void main(String args[])
{ // 8421
int n = 2; // 00000000 00000000 00000000 00000010 n=2
for(int i=1; i<=10; i++)
{
System.out.println("2 power " + i + " = " + n);
// bitwise le shi operator << will be used as follows
n = n << 1; // 0000000 00000000 00000000 000000100 n=4
}
// 1024 8421
// 0000000 00000000 00000010 000000000 n=1024
}//main over
}//class over

 // program to display welcome message


class welcome
{
public sta c void main(String[] args)
{
System.out.println( "welcome " + args[0] + " to java. Best wishes to " + args[1] + " and " + args[2] );
} // main over
} // class over

 // input user name inside program and display welcome message.


import java.u l.*;
class welcome2
{
public sta c void main(String args[])
{
String user_name;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name : ");
user_name = sc.next();
System.out.println("welcome " + user_name + " to the world of java");
} //main over
} // class over

You might also like