Sunteți pe pagina 1din 17

Practical-1

1.Write a program in Java to find even and odd nos. ranging between 100-300.
public class EvenOdd { public static void main(String args[]) { int i,j=0,k=0,even[],odd[]; even=new int[111]; odd=new int[111]; for(i=100;i<=300;i++) { if(i%2==0) { j=j+1; even[j]=i; } else { k=k+1; odd[k]=i; } } System.out.println("even nos. are: "); for(i=1;i<=101;i++) { System.out.print(even[i] + " "); } System.out.println("\nodd nos. are: "); for(i=1;i<101;i++) { System.out.print(odd[i] + " "); } } }

even nos. are: 100 102 104 106 142 144 146 148 184 186 188 190 226 228 230 232 268 270 272 274 odd nos. are: 101 103 105 107 143 145 147 149 185 187 189 191 227 229 231 233 269 271 273 275

108 150 192 234 276 109 151 193 235 277

110 152 194 236 278 111 153 195 237 279

112 154 196 238 280 113 155 197 239 281

114 156 198 240 282 115 157 199 241 283

116 158 200 242 284 117 159 201 243 285

118 160 202 244 286 119 161 203 245 287

120 162 204 246 288 121 163 205 247 289

122 164 206 248 290 123 165 207 249 291

124 166 208 250 292 125 167 209 251 293

126 168 210 252 294 127 169 211 253 295

128 170 212 254 296 129 171 213 255 297

130 172 214 256 298 131 173 215 257 299

132 174 216 258 300 133 175 217 259

134 176 218 260

136 178 220 262

138 180 222 264

140 182 224 266

135 177 219 261

137 179 221 263

139 181 223 265

141 183 225 267

2.Write a program in Java to find largest of three numbers using a.Nested if else
import java.io.*; public class NestedIf { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int i,j,k; System.out.println("i: "); i = Integer.parseInt(br.readLine()); System.out.println("j: "); j = Integer.parseInt(br.readLine()); System.out.println("k: "); k = Integer.parseInt(br.readLine()); if(i>j) { if(i>k) { System.out.println("i } else { System.out.println("k } } else { if(j>k) { System.out.println("j } else { System.out.println("k } } } }

is largest no.");

is largest no.");

is largest no.");

is largest no.");

i: 10 j: 30 k: 50 k is largest no.

b.Turnary operator
import java.io.*; public class TurnaryOp { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int i,j,k,z; System.out.println("i: "); i = Integer.parseInt(br.readLine()); System.out.println("j: "); j = Integer.parseInt(br.readLine()); System.out.println("k: "); k = Integer.parseInt(br.readLine()); z=((i>j) ? ((i>k) ? i : k) : ((j>k) ? j : k)); System.out.println("z:" + z); } }

i: 10 j: 30 k: 50 z:50

3.Write a program in Java to find armstrong no. ranging between 0-500.


public class ArmstrongNo { public static void main(String args[]) { int z,x,y,i,sum=0; for(z=0;z<=500;z++) { i=z; sum=0; while(i!=0) { x=i%10; i=i/10; y=x*x*x; sum=sum+y; } if(z==sum) { System.out.println("Armstrong No.:" + z); } } } }

Armstrong Armstrong Armstrong Armstrong Armstrong Armstrong

No.:0 No.:1 No.:153 No.:370 No.:371 No.:407

4.Write a program in Java to implement simple calculator to perform following task: a.Addition b.Subtraction c.Multiplication d.Division
import java.io.*; public class Calculator { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cal,i,j; System.out.println("i: "); i = Integer.parseInt(br.readLine()); System.out.println("j: "); j = Integer.parseInt(br.readLine()); System.out.println("1.Addition"); System.out.println("2.Subtraction"); System.out.println("3.Multplication"); System.out.println("4.Division"); System.out.println("cal: "); cal = Integer.parseInt(br.readLine()); switch(cal) { case 1: System.out.println("Addition: " + (i+j)); break; case 2: System.out.println("Subtraction: " + (i-j)); break; case 3: System.out.println("Multiplication: " + (i*j)); break; case 4: System.out.println("Division: " + (i/j)); break; default: System.out.println("Enter the correct choice:"); } }

i: 90 j: 30 1.Addition 2.Subtraction 3.Multplication 4.Division cal: 1 Addition: 120

i: 90 j: 30 1.Addition 2.Subtraction 3.Multplication 4.Division cal: 2 Subtraction: 60

i: 90 j: 30 1.Addition 2.Subtraction 3.Multplication 4.Division cal: 3 Multiplication: 2700 i: 90 j: 30 1.Addition 2.Subtraction 3.Multplication 4.Division cal: 4 Division: 3

Practical-2 1. Design a class name triangle to represent triangle. The class contains: Three double data fields name side1, side2, side3 that specifies the three sides
of triangle. The default values are 1 for all the sides.

A No argument constructor that creates default triangle. A constructor that creates a triangle with specified sides. Get and set methods for all data fields. A method named get Area that returns the area of triangle. A method named get perimeter that returns perimeter.

Write a test program that creates two objects. Assign sides 4, 5, 6 to the first object and 1.5, 2.5, 3.5 to the second object. Display the properties of both objects and find their area and perimeter. Input:
class Triangle { double side1; double side2; double side3; public Triangle() { side1 = 1; side2 = 1; side3 = 1; System.out.println("Side 1 : "+side1); System.out.println("Side 2 : "+side2); System.out.println("Side 3 : "+side3); } public Triangle(double s1,double s2,double s3) { side1 = s1; side2 = s2; side3 = s3; } public void setside1(double s1) { side1 = s1; } public void setside2(double s2) { side2 = s2;

} public void setside3(double s3) { side3 = s3; } public double getSide1() { return(side1); } public double getSide2() { return(side2); } public double getSide3() { return(side3); } public void setside(double s1, double s2, double s3) { side1=s1; side2=s2; side3=s3; } public double getperimeter() { double perimeter = side1 + side2 + side3; return(perimeter); } public double getarea() { double s = (side1 + side2 + side3) / 2; double m = s * (s - side1) * (s - side2) * (s - side3); double a = java.lang.Math.sqrt(m); return(a); } } public class TestTriangle { public static void main(String args[]) { Triangle t = new Triangle(); double perimeter = t.getperimeter(); double a= t.getarea(); System.out.println("Perimeter: " +perimeter); System.out.println("Area: " +a); Triangle t1 = new Triangle(4,5,6); System.out.println();

double b1= t1.getSide1(); double b2= t1.getSide2(); double b3= t1.getSide3(); double p = t1.getperimeter(); double a1= t1.getarea(); System.out.println("Side-1: " +b1); System.out.println("Side-2: " +b2); System.out.println("Side-3: " +b3); System.out.println("Perimeter: " +p); System.out.println("Area: " +a1); System.out.println(); Triangle t2 = new Triangle(1.5,2.5,3.5); System.out.println(); double c1= t2.getSide1(); double c2= t2.getSide2(); double c3= t2.getSide3(); double p1 = t2.getperimeter(); double a2= t2.getarea(); System.out.println("Side1: " +c1); System.out.println("Side2: " +c2); System.out.println("Side3: " +c3); System.out.println("Perimeter: " +p1); System.out.println("Area: " +a2); System.out.println(); } }

Output:
Side 1 : 1.0 Side 2: 1.0 Side 3 : 1.0 Perimeter: 3.0 Area: 0.4330127018922193 Side-1: 4.0 Side-2: 5.0 Side-3: 6.0 Perimeter: 15.0 Area: 9.921567416492215

Side1: 1.5 Side2: 2.5 Side3: 3.5 Perimeter: 7.5 Area: 1.6237976320958225

2. Design a class named MyDateTime. The class contains: Data fields year,month,day,hour,minute,second that represent date and time.

A no argument constructor that creates MyDateTime object for current time. A constructor that constructs MyDateTime object with the specified year, month, day, hour, minute, second. A constructor that constructs MyDateTime object for another object of similar type. Get and set methods for all data fields. Implement the class and write a test program that creates two MyDateTime objects with the help of new operator and display their year, month, day, hour, second in appropriate format. Input:

import java.util.Date; class MyDateTime { double double double double double double min; hour; sec; month; year; day;

MyDateTime(double min,double hour,double sec,double month,double year,double day) { this.min = min; this.hour = hour; this.sec = sec; this.month = month; this.year = year; this.day = day; } MyDateTime() { Date d = new Date(); min = d.getMinutes(); hour = d.getHours(); sec = d.getSeconds(); month = d.getMonth(); year = d.getYear(); day = d.getDay(); } void print() { System.out.println("year: " +year+ "month: " +month+ "day: " +day+ "hour: " +hour+ "minute: " +min+ "second: " +sec); } }

public class TestMyDateTime { public static void main(String args[]) { MyDateTime m1 = new MyDateTime(31,9,50,9,2011,22); m1.print(); MyDateTime m2 = new MyDateTime(); m2.print(); } }

Output:
year: 2011.0month: 9.0day: 22.0hour: 9.0minute: 31.0second: 50.0 year: 111.0month: 8.0day: 1.0hour: 19.0minute: 17.0second: 8.0

3. Design a class named MyPoint that represents point in two dimensional space with xcoordinate and y-coordinate. The class contains: Two data fields x and y that represents coordinate with get methods. A No argument constructor that creates a point (0,0). A constructor that contructs a point with specified coordinates. Two get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point of MyPoint type. A method named distance that point returns the distance from this point to another point with specified x and y coordinates. Implement the class and write a test program that creates two points (0,0) and (10,30.5) and display the distance between them. Input:
package Pratical_2_3; public class MyPoint { double x,y; public MyPoint() { x=0; y=0; System.out.println("Default X= System.out.println("Default Y= } public MyPoint(double m, double n) { x=m; y=n; System.out.println("{Specified System.out.println("{Specified } public void setXY(double m, double

"+x); "+y);

by constructor} X= "+x); by constructor} Y= "+y); n)

{ x=m; y=n; } public double getX() { return x; } public double getY() { return y; } } package Pratical_2_3; public class TestMyPoint { public static double getDist(MyPoint m, MyPoint n) { double d1 = (m.x-n.x)*(m.x-n.x) + (m.y-n.y)*(m.y-n.y); double d2 = java.lang.Math.sqrt(d1); return d2; } public static void main(String args[]) { MyPoint mp = new MyPoint(); //mp.setXY(10,30); System.out.println("X: "+mp.getX()); System.out.println("Y: "+mp.getY()); MyPoint mp1 = new MyPoint(10,30.5); System.out.println("X: "+mp1.getX()); System.out.println("Y: "+mp1.getY());

System.out.println("The distance between mp and mp1 is: "+getDist(mp,mp1)); } }

Output:
Default X= 0.0 Default Y= 0.0 X: 0.0 Y: 0.0 {Specified by constructor} X= 10.0 {Specified by constructor} Y= 30.5 X: 10.0 Y: 30.5 The distance between mp and mp1 is: 32.09750769140807

Practical-3
Write a program which defines a base class employee having three data members namely name, employee no and gender and two methods namely show data and input data. Derive a class salary employed and add new data member salary. It also adds two methods increment and allowance. Allowance is calculated as if gender = female HRA = 0.1*salary or HRA = 0.09*salary and DA = 0.05*salary. Increment is calculated as 0.1*salary + salary. Display the gross salary in main class. Input:
import java.io.*; class Employee { String name; int emp_no; String gender; void Input_Data() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Name,Gender,Emp_no :"); name = br.readLine(); gender = br.readLine(); Integer.parseInt(br.readLine()); } void show_data() { System.out.println("Enter Name :" +name ); System.out.println("Emp_No :" +emp_no); System.out.println("Gender :" +gender); } } class SalariedEmployee extends Employee { double salary; double allowance() { double HRA,DA; if(gender.equalsIgnoreCase("female")) { HRA = 0.1 * salary; } else { HRA = 0.09 * salary; } DA = 0.05 * salary; return HRA + DA; } double increment() {

return 0.1 * salary + salary; } } public class TestEmployee { public static void main(String args[]) { SalariedEmployee s = new SalariedEmployee(); try { s.Input_Data(); } catch(Exception e) { } s.salary = 5000; s.show_data(); System.out.println("Gross Salary = " + (s.increment() + s.allowance())); } }

Output:
Enter Name,Gender,Emp_no : s female 123 Enter Name :d Emp_No :123 Gender :female Gross Salary = 6250.0 Enter Name,Gender,Emp_no : daksh male 58 Enter Name :daksh Emp_No :58 Gender :male Gross Salary = 6200.0

Practical-4 1. Create an interface shape2D which declare getArea method. Create an abstract class shape which abstract display method. Extend a class circle from shape class and implement shape2D. Exercise this class. Input:
interface Shape2D { } abstract class Shape { abstract void display(); } public class Circle extends Shape implements Shape2D { double rad; void display() { System.out.println("Radius = "+rad); } public void getArea() { System.out.println("Area of circle = "+(3.14*rad)); } public static void main(String args[]) { Circle c = new Circle(); c.rad = 2.0; c.display(); c.getArea(); } }

Output:
Radius = 2.0 Area of circle = 6.28

2. Create a package employee. This package declares two interface called payable declares two methods pay and get raise. Taxable defines increment rate and income tax method. Class employee represents these interface pay method defines salary of an employee and give raise method. Increment salary by given person. income tax method calculates tax as salary. Define main in same class.

Input:
package employee; public interface Payable { void pay(); void giveRaise(double p); } package employee; public interface Taxable { double INC_RATE = 0.1; double incometax(); } import employee.*; public class Employee1 implements Payable, Taxable { double salary; public void pay() { System.out.println("Salary: " +salary); } public void giveRaise(double p) { System.out.println("Raise in Salary:" +(salary*p)); } public double incometax() { return INC_RATE*salary; } public static void main(String args[]) { Employee1 e = new Employee1(); e.salary = 5000; e.pay(); e.giveRaise(0.05); e.incometax(); System.out.println("Income tax = "+e.incometax()); } }

Output:
Salary: 5000.0 Raise in Salary:250.0 Income tax = 500.0

Practical-5
1. Write a program to find average of n numbers stored in an array.

Input:
import java.io.*; public class Average1 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n; int i,s = 0,avg; int a[]; a=new int[20]; System.out.println("Enter the value of n: "); n = Integer.parseInt(br.readLine()); System.out.println("Enter the nos.:"); for(i=1;i<=n;i++) { a[i] = Integer.parseInt(br.readLine()); s = s + a[i]; } avg = s / n; System.out.println("Average = " +avg); } }

Output:
Enter the value of n: 5 Enter the nos.: 10 20 30 40 50 Average = 30

2. Write a program to replace substring by given substring in any string. Input:


public class StringReplace { public static void main(String args[]) { String org = "Daksh was an Indian.";

String search = "was";


String sub = "is"; String result = ""; int i; do { System.out.println(org); i = org.indexOf(search); if(i!=-1) { result = org.substring(0,i);

result = result + sub; result = result + org.substring(i + search.length()); org = result; } } while(i!=-1); } }

Output:
Daksh was an Indian. Daksh is an Indian.

S-ar putea să vă placă și