0% found this document useful (0 votes)
3 views10 pages

Java Lab Part A

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Java Lab Part A

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA LAB PROGRAMS (Part A)

Part A - Program 1: Class and Object


Program Code:

class Student

{
String name;
int age;

void display()

{
[Link]("Name: " + name + ", Age: " + age);
}
}
public class StudentDetail

{
public static void main(String[] args)

{
Student s1 = new Student();
[Link] = "Rohan";
[Link] = 20;
[Link]();
}
}

Output:

 javac [Link]
 java StudentDetail

Name: Rohan , Age: 20


Part A - Program 2: Biggest of Two Numbers
Program Code:

import [Link];
class Biggest

{
public static void main(String[] args)

{
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
int a = [Link]();
int b = [Link]();
int big = (a > b) ? a : b;
[Link]("Biggest = " + big);
}
}

Output:

 javac [Link]
 java Biggest

Enter two numbers: 12 45


Biggest = 45
Part A - Program 3: Factorial using While Loop
Program Code:

class Factorial

{
public static void main(String[] args)

{
int i = 1;
while (i <= 10)

{
int fact = 1, j = i;
while (j > 0)

{
fact *= j;
j--;
}
[Link]("Factorial of " + i + " = " + fact);
i++;
}
}
}

Output:

 javac [Link]
 java Factorial

Factorial of 1 = 1
Factorial of 2 = 2
...
Factorial of 10 = 3628800
Part A - Program 4: Area and Circumference of Circle
Program Code:

import [Link];
class Area

{
public static void main(String[] args)

{
Scanner sc = new Scanner([Link]);
[Link]("Enter radius: ");
double r = [Link]();
double area = [Link] * r * r;
double circum = 2 * [Link] * r;
[Link]("Area = " + area);
[Link]("Circumference = " + circum);
}
}

Output:

 javac [Link]
 java Area

Enter radius: 5
Area = 78.5398
Circumference = 31.4159
Part A - Program 5: Method Overloading
Program Code:

class Calculator

{
int add(int a, int b)

return a + b;

double add(double a, double b)

return a + b;

}
}

class Overload

{
public static void main(String[] args)

{
Calculator c = new Calculator();
[Link]("Sum (int): " + [Link](10, 20));
[Link]("Sum (double): " + [Link](5.5, 6.5));
}
}

Output:

 javac [Link]
 java Overload

Sum (int): 30
Sum (double): 12.0
Part A - Program 6: Static Members
Program Code:

class Counter

{
static int count = 0;
Counter()

count++;

}
}

class StaticExample

{
public static void main(String[] args)

{
new Counter();
new Counter();
[Link]("Number of objects created: " + [Link]);
}
}

Output:

 javac [Link]

 java StaticExample

Number of objects created: 2


Part A - Program 7: Method Overriding
Program Code:

class Animal

{
void sound()

[Link]("Animal makes sound");

}
}

class Dog extends Animal

{
void sound()

[Link]("Dog barks");

}
}

public class Override

{
public static void main(String[] args)

{
Animal a = new Dog();
[Link]();
}
}

Output:

 javac [Link]

 java Override

Dog barks
Part A - Program 8: Dynamic Method Dispatch
Program Code:

class Add1
{
int add(int a, int b)
{
return a + b; // parent method
}
}

class Add2 extends Add1


{
int add(int a, int b)
{
return a + b + 5; // child overrides method
}
}

public class Dynamic


{
public static void main(String[] args)
{
Add1 ref;
ref = new Add1();
[Link]("Parent Class Addition: " + [Link](10, 20));
ref = new Add2();
[Link]("Child Class Addition: " + [Link](10, 20));
}
}

Output:
 javac [Link]
 java Dynamic
Parent Class Addition: 30
Child Class Addition: 35
Part A - Program 9: Abstract Class
Program Code:

abstract class Shape

{
abstract void draw();
}

class Circle extends Shape

{
void draw()

[Link]("Drawing Circle");

}
}

public class AbstractDemo

{
public static void main(String[] args)

{
Shape s = new Circle();
[Link]();
}
}

Output:
 javac [Link]
 java AbstractDemo

Drawing Circle
Part A - Program 10: Package Example
Program Code:

// Save as [Link]
package Rect; // package name
public class Rectangle
{
double length, breadth;
public Rectangle(double l, double b)
{
length = l;
breadth = b;
}
public double area()
{
return length * breadth;
}
}

// Save as [Link]
import [Link]; // import the package
public class RectArea
{
public static void main(String[] args)
{
Rectangle r = new Rectangle(10, 5);
[Link]("Area of Rectangle = " + [Link]());
}
}
Output:
 javac [Link]
 java RectArea
Area of Rectangle = 50.0

You might also like