Program 1,2,3,4
Program 1,2,3,4
import java.util.Scanner;
scanner.close();
}
}
package demo_1;
import java.util.Scanner;
while (true) {
System.out.println("1. Push 2. Pop 3. Display 4. Quit");
System.out.println("Enter your choice: ");
choice = input.nextInt();
switch (choice) {
case 1:
stack1.push();
break;
case 2:
stack1.pop();
break;
case 3:
stack1.display();
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
// Exit the program
default:
System.out.println("Wrong choice! ");
Program 3 : Develop a JAVA program to create a class named shape. Create three sub
classes namely: circle,
triangle and square, each class has two member functions named draw () and erase
(). Demonstrate
polymorphism concepts by developing suitable methods, defining member data and main
program.
package demo_3;
//base class
class Shape {
public void draw() {
System.out.println("drawing a shape! ");
}
// constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// constructor
public Square(double side) {
this.side = side;
}
// demonstrating polymorphism
for (Shape shape : shapes) {
shape.draw();
// Calling the draw method
System.out.println("area: " + shape.area()); // Calling the
area method
shape.erase();
// Calling the erase method
System.out.println();
}
}
}
package demo_3_A;
import java.util.Scanner;
//base class
class Shape {
// constructor
public Circle(double radius) {
this.radius = radius;
}
// constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// constructor
public Square(double side) {
this.side = side;
}
circle.draw();
// Calling the draw method
System.out.println("area of circle is " +
circle.area()); // Calling the area method
circle.erase();
// Calling the erase method
triangle.draw();
// Calling the draw method
System.out.println("area of triangle is " + triangle.area()); //
Calling the area method
triangle.erase();
// Calling the erase method
square.draw();
// Calling the draw method
System.out.println("area of square is " + square.area());
// Calling the area method
square.erase();
// Calling the erase method
scanner.close();
// closing the scanner
}
}
Program 4: Develop a JAVA program to create an abstract class Shape with abstract
methods calculate Area()
and calculate Perimeter(). Create subclasses Circle and Triangle that extend the
Shape class and
implement the respective methods to calculate the area and perimeter of each shape.
double area() {
//Area calculation
return Math.PI * radius * radius;
}
double perimeter()
{ //Perimeter calculation
return 2 * Math.PI * radius;
}
}
double perimeter() {
//Perimeter calculation
return a + b + c;
}
}
// Main class