0% found this document useful (0 votes)
6 views9 pages

Java Shape Program

The document contains Java code for a shape program that defines an abstract class 'Shape' and its subclasses: 'Rectangle', 'Square', 'Circle', and 'Triangle', each implementing methods to calculate area and perimeter. It also includes a 'Main' class for user input to create shapes and display their area and perimeter. Additionally, there are several C++ code snippets for different functionalities, such as input/output operations and class definitions.

Uploaded by

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

Java Shape Program

The document contains Java code for a shape program that defines an abstract class 'Shape' and its subclasses: 'Rectangle', 'Square', 'Circle', and 'Triangle', each implementing methods to calculate area and perimeter. It also includes a 'Main' class for user input to create shapes and display their area and perimeter. Additionally, there are several C++ code snippets for different functionalities, such as input/output operations and class definitions.

Uploaded by

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

Java Shape Program

Shape.java

abstract class Shape {


double length, width, radius;
double side1, side2, side3;

abstract double calculateArea();


abstract double calculatePerimeter();

void setLength(double l) { length = l; }


void setWidth(double w) { width = w; }
void setRadius(double r) { radius = r; }

void setSides(double a, double b, double c) {


side1 = a;
side2 = b;
side3 = c;
}
}

Rectangle.java

class Rectangle extends Shape {


Rectangle(double l, double w) {
setLength(l);
setWidth(w);
}

double calculateArea() {
return length * width;
}

double calculatePerimeter() {
return 2 * (length + width);
}
}
Square.java

class Square extends Shape {


Square(double side) {
setLength(side);
}

double calculateArea() {
return length * length;
}

double calculatePerimeter() {
return 4 * length;
}
}

Circle.java

class Circle extends Shape {


Circle(double r) {
setRadius(r);
}

double calculateArea() {
return Math.PI * radius * radius;
}

double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}

Triangle.java

class Triangle extends Shape {


Triangle(double a, double b, double c) {
setSides(a, b, c);
}

double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}

double calculatePerimeter() {
return side1 + side2 + side3;
}
}

Main.java

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char shapeType = sc.next().charAt(0);
Shape shape;

switch (shapeType) {
case 'S':
double s = sc.nextDouble();
shape = new Square(s);
break;
case 'R':
double l = sc.nextDouble();
double w = sc.nextDouble();
shape = new Rectangle(l, w);
break;
case 'C':
double r = sc.nextDouble();
shape = new Circle(r);
break;
case 'T':
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
shape = new Triangle(a, b, c);
break;
default:
System.out.println("Invalid Shape type.");
sc.close();
return;
}
double perimeter = shape.calculatePerimeter();
double area = shape.calculateArea();

if (shapeType == 'C') {
System.out.printf("Circumference : %.2f\n", perimeter);
} else {
System.out.printf("Perimeter : %.2f\n", perimeter);
}

System.out.printf("Area : %.2f\n", area);


sc.close();
}
}

// You are using GCC

//A company maintains

#include <bits/stdc++.h>

using namespace std;

int main() {

int a;

double b;

cin >> a >> b;

cout << a << "\n";

if (floor(b) == b) {

cout << fixed << setprecision(1) << b << "\n";

} else {
cout << b << "\n";

if (b <= 100)

cout << 2;

else

cout << 1;

return 0;

// You are using GCC

//Karthick

#include <iostream>

using namespace std;

int main() {

int a;

int ans = 0;

cin >> a;

while (a > 0) {

ans += (a % 10) * (a % 10);

a /= 10;

}
cout << ans << "\n";

return 0;

// You are using GCC

//Given a Book

#include <bits/stdc++.h>

using namespace std;

class Book {

string title;

string author;

int price;

public:

Book(string t, string a, int p) : title(t), author(a), price(p) {}

void display() {

cout << "Title: " << title << "\n";

cout << "Author: " << author << "\n";

cout << "Price: " << price << "\n";

};
int main() {

string s, a;

int p;

cin >> s >> a >> p;

Book b = Book(s, a, p);

b.display();

return 0;

// You are using GCC

//Bicycle

#include <iostream>

using namespace std;

int main() {

int a, b, c;

cin >> a >> b >> c;

cout << "Number of gears: " << a << "\n";

cout << "Speed of the Bicycle: " << b << "\n";

cout << "Seat height: " << c << "\n";

return 0;

}
// You are using GCC

//Dr. Nila

#include <bits/stdc++.h>

using namespace std;

int main() {

string s;

cin >> s;

int n = s.length();

vector<vector<bool>> dp(n, vector<bool>(n, false));

int i_ = 0;

int ans = 1;

for (int i = 0; i < n; ++i) dp[i][i] = true;

for (int len = 2; len <= n; ++len) {

for (int i = 0, j = i + len - 1; j < n; ++i, ++j) {

if (s[i] == s[j] && (len == 2 || dp[i + 1][j - 1] == true)) {

dp[i][j] = true;

if (ans < len) {

ans = len;

i_ = i; // i_ holds start index

}
}

// << i_ << " " << ans << "\n";

string res = "";

for (int i{0}; i <ans; ++i) {

res += s[i+i_];

cout << res;

return 0;

You might also like