ABSTRACT CLASS
Create an abstract class Shape with abstract method area(). Inherit Shape into Circle. Write a
Java program to calculate and display area of circle. (Use final keyword)
Answer:
abstract class Shape {
abstract void area();
class Circle extends Shape {
final double PI = 3.14;
double r;
Circle(double r) {
this.r = r;
void area() {
double a = PI * r * r;
[Link]("Area of Circle = " + a);
class Main {
public static void main(String args[]) {
Circle c = new Circle(5);
[Link]();
}
Define an abstract class Shape with abstract method area(). Write a Java program to calculate
area of Triangle.
Answer:
abstract class Shape {
abstract double area();
class Triangle extends Shape {
double base, height;
Triangle(double b, double h) {
base = b;
height = h;
double area() {
return 0.5 * base * height;
class Main {
public static void main(String args[]) {
Triangle t = new Triangle(10, 5);
[Link]("Area of Triangle = " + [Link]());
Define an interface Shape with abstract method area(). Inherit interface Shape into the class
Triangle. Write a Java Program to calculate area of Triangle.
Answer:
interface Shape {
double area();
class Triangle implements Shape {
double base, height;
Triangle(double b, double h) {
base = b;
height = h;
public double area() {
return 0.5 * base * height;
}
class Main {
public static void main(String args[]) {
Triangle t = new Triangle(10, 5);
[Link]("Area of Triangle = " + [Link]());
Define abstract class shape with abstract method area (). Write a java program to calculate area
of circle.
abstract class Shape {
abstract double area();
class Circle extends Shape {
double radius;
Circle(double r) {
radius = r;
double area() {
return 3.14 * radius * radius;
}
class Main {
public static void main(String args[]) {
Circle c = new Circle(5);
[Link]("Area of Circle = " + [Link]());
Define an interface Shape with abstract method area(). Write a Java program to calculate area
of Rectangle.
interface Shape {
double area();
class Rectangle implements Shape {
double length, breadth;
Rectangle(double l, double b) {
length = l;
breadth = b;
public double area() {
return length * breadth;
}
class Main {
public static void main(String args[]) {
Rectangle r = new Rectangle(10, 5);
[Link]("Area of Rectangle = " + [Link]());
REPLACE
Accept a string, reverse the case of all characters, replace all digits with ‘*’
import [Link].*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
String s = [Link]();
String result = "";
for(int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if([Link](ch))
result += [Link](ch);
else if([Link](ch))
result += [Link](ch);
else if([Link](ch))
result += "*";
else
result += ch;
[Link]("Target String: " + result);
COPY FILE CONTENT
import [Link].*;
class Main {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("[Link]"));
BufferedWriter bw = new BufferedWriter(new FileWriter("[Link]"));
int ch;
while ((ch = [Link]()) != -1) {
char c = (char) ch;
if ([Link](c))
[Link]('*');
else
[Link](c);
[Link]();
[Link]();
[Link]("File copied successfully.");
Write a Java program to accept n numbers from user and store only prime numbers into array
and display that array.
import [Link].*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int a[] = new int[n];
int p[] = new int[n];
int k = 0;
for (int i = 0; i < n; i++)
a[i] = [Link]();
for (int i = 0; i < n; i++) {
if (isPrime(a[i])) {
p[k] = a[i];
k++;
}
}
for (int i = 0; i < k; i++)
[Link](p[i] + " ");
static boolean isPrime(int x) {
if (x < 2) return false;
for (int i = 2; i <= x/2; i++)
if (x % i == 0)
return false;
return true;
Write a Java program to accept a number from user. If it is zero then throw user defined
exception "Number is zero". Otherwise calculate its factorial.
import [Link].*;
class ZeroNumberException extends Exception {
ZeroNumberException(String msg) {
super(msg);
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
try {
if (n == 0)
throw new ZeroNumberException("Number is zero");
int fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
[Link]("Factorial: " + fact);
} catch (ZeroNumberException e) {
[Link]([Link]());
}
import [Link].*;
import [Link].*;
import [Link].*;
class EmpForm extends JFrame implements ActionListener {
JTextField t1, t2, t3;
JButton b1, b2;
EmpForm() {
setTitle("Employee Info");
setLayout(new FlowLayout());
add(new JLabel("ENo"));
t1 = new JTextField(15);
add(t1);
add(new JLabel("EName"));
t2 = new JTextField(15);
add(t2);
add(new JLabel("Salary"));
t3 = new JTextField(15);
add(t3);
b1 = new JButton("Display");
b2 = new JButton("Clear");
add(b1);
add(b2);
[Link](this);
[Link](this);
setSize(250, 250);
setVisible(true);
public void actionPerformed(ActionEvent ae) {
if ([Link]() == b1) {
[Link]("Employee No: " + [Link]());
[Link]("Employee Name: " + [Link]());
[Link]("Salary: " + [Link]());
else if ([Link]() == b2) {
[Link]("");
[Link]("");
[Link]("");
public static void main(String args[]) {
new EmpForm();
}