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

Program 1,2,3,4

Uploaded by

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

Program 1,2,3,4

Uploaded by

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

Program 1.

Develop a JAVA program to add TWO matrices of suitable order N (The


value of N should be read from command line arguments).

import java.util.Scanner;

public class MatrixAddition {


public static void main(String[] args) {
// Check if N is provided in command line arguments
if (args.length != 1) {
System.out.println("Please provide the value of N as a command line
argument.");
return;
}

// Read N from command line argument


int N = Integer.parseInt(args[0]);

// Create two matrices of order N x N


int[][] matrix1 = new int[N][N];
int[][] matrix2 = new int[N][N];
int[][] resultMatrix = new int[N][N];

// Scanner for user input


Scanner scanner = new Scanner(System.in);

// Input for first matrix


System.out.println("Enter the elements of the first matrix:");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix1[i][j] = scanner.nextInt();
}
}

// Input for second matrix


System.out.println("Enter the elements of the second matrix:");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix2[i][j] = scanner.nextInt();
}
}

// Add the two matrices


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the result


System.out.println("Resultant Matrix after Addition:");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}

scanner.close();
}
}

Program 2 : Develop a stack class to hold a maximum of 10 integers with suitable


methods. Develop a JAVA main
method to illustrate Stack operations.

package demo_1;
import java.util.Scanner;

//stack class (ss1)


class ss1 {
private int top = -1;
private int max_size = 10;
private int[] stack = new int[max_size];

Scanner input = new Scanner(System.in);

// push method to add an element to the stack


public void push() {
if (top == (max_size - 1))
System.out.println("Stack overflow");
//single statement for IF(), so no opening and closing braces { }
else {
System.out.println("Enter the item to be pushed:");
int item = input.nextInt();
stack[++top] = item;
}
}

// pop method to remove and print the top element


public void pop() {
if (top == -1)
System.out.println("Stack underflow");
else {
int item = stack[top--];
System.out.println("Popped element: " + item);
}
}

// display method to show all elements in the stack


public void display() {
if (top == -1)
System.out.println("Stack is empty ");
else {
System.out.println("elements are: ");
for (int i = top; i >= 0; i--) {
System.out.printf("%d ", stack[i]);
}
System.out.println(); // for
a new line after the stack elements
}
}
}

public class opt {

public static void main(String[] args) {


ss1 stack1 = new ss1();
// Create an instance of the stack class
Scanner input = new Scanner(System.in);
int choice;

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! ");
}

public void erase() {


System.out.println("erasing a shape! ");
}

public double area() {


return 0;
}
}

class Circle extends Shape {


private double radius;
// Constructor
public Circle(double radius) {
this.radius = radius;
}

public void draw() {


System.out.println("drawing a circle! ");
}

public void erase() {


System.out.println("erasing a circle! ");
}

public double area() {


return Math.PI * radius * radius; // area = πr²
}
}

//triangle class that extends Shape


class Triangle extends Shape {
private double base;
private double height;

// constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}

public void draw() {


System.out.println("drawing a Triangle! ");
}

public void erase() {


System.out.println("erasing a Triangle! ");
}

public double area() {


return 0.5 * base * height; // area = 0.5 *
base * height
}
}

//square class that extends Shape


class Square extends Shape {
private double side;

// constructor
public Square(double side) {
this.side = side;
}

public void draw() {


System.out.println("drawing a Square! ");
}

public void erase() {


System.out.println("erasing a Square! ");
}
public double area() {
return side * side; // area = side²
}
}

public class ShapeTest {


public static void main(String[] args) {
Shape[] shapes = new Shape[3]; // array
of Shape references to 3 objects

// Creating instances(objects) of each shape


shapes[0] = new Circle(5); //
Circle with radius 5
shapes[1] = new Triangle(4, 3); //
Triangle with base 4 and height 3
shapes[2] = new Square(2); //
Square with side length 2

// 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();
}
}
}

2nd method: of Program 3 Using User input( by Using Scanner Class)

package demo_3_A;

import java.util.Scanner;

//base class
class Shape {

public void draw() {


//Draw method
System.out.println("drawing a shape:");
}

public void erase() {


//Erase method
System.out.println("erasing a shape:");
}

public double area() {


//Area method
return 0;
}
}

//Subclass Circle that extends shape


class Circle extends Shape {
private double radius;

// constructor
public Circle(double radius) {
this.radius = radius;
}

public void draw() {


System.out.println("drawing a circle! ");
}

public void erase() {


System.out.println("erasing a circle! ");
}

public double area() {


return Math.PI * radius * radius; // area
= πr²
}
}

//Subclass Triangle that extends shape


class Triangle extends Shape {
private double base;
private double height;

// constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}

public void draw() {


System.out.println("drawing a triangle! ");
}

public void erase() {


System.out.println("erasing a triangle! ");
}

public double area() {


return 0.5 * base * height;
// area = 0.5 * base * height
}
}

//Subclass Square that extends shape


class Square extends Shape {
private double side;

// constructor
public Square(double side) {
this.side = side;
}

public void draw() {


System.out.println("drawing a square! ");
}
public void erase() {
System.out.println("erasing a square! ");
}

public double area() {


return side * side; // area
= side²
}
}

//main class demonstrating polymorphism


public class ShapeDemo {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// user input for circle


System.out.println("enter the radius of the circle: ");
double radius = scanner.nextDouble();

Shape circle = new Circle(radius);


// Creating an object "circle"

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

System.out.println("enter the base of the triangle: ");


double base = scanner.nextDouble();
// user input

System.out.println("enter the height of the triangle: ");


double height = scanner.nextDouble();
// user input

Shape triangle = new Triangle(base, height);


//Creating an object "triangle"

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

System.out.println("enter the side of the square: ");


double side = scanner.nextDouble();
// user input

Shape square = new Square(side);


//Creating an object "square"

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.

// Abstract class Shape


abstract class Shape {

abstract double area();


// Abstract methods
abstract double perimeter();
}

// Subclass Circle extends Shape


class Circle extends Shape {
private double radius;
//Instance variable radius

// Constructor to initialize radius


public Circle(double radius) {
this.radius = radius;
}

double area() {
//Area calculation
return Math.PI * radius * radius;
}

double perimeter()
{ //Perimeter calculation
return 2 * Math.PI * radius;
}
}

// Subclass Triangle extends Shape


class Triangle extends Shape {
private double a, b,
c; // Instance variables
sides and height of the triangle
private double height;

// Constructor to initialize sides and height


public Triangle(double a, double b, double c, double height) {
this.a = a;
this.b = b;
this.c = c;
this.height = height;
}
double area() {
//Area calculation
return (b * height) / 2;
}

double perimeter() {
//Perimeter calculation
return a + b + c;
}
}

// Main class

public class Main {


public static void main(String[] args) {

Shape circle = new Circle(5.0);


//Creating an object "circle" with radius 5.0

System.out.println("area of a Circle : " + circle.area());


System.out.println(" perimeter of a circle : " + circle.perimeter());

Shape triangle = new Triangle(2.0, 4.0, 3.0, 5.0);


//Creating an object "circle" with sides 2.0, 4.0, 3.0 and height 5.0

System.out.println("area of a triangle : " + triangle.area());


System.out.println("perimeter of a triangle : " + triangle.perimeter());
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy