0% found this document useful (0 votes)
8 views4 pages

Programacion Orientada A Objetos Ejemplo de Clases

The document describes classes for different shapes (triangle, square, rectangle, circle) that inherit from a base "Figura" class. It establishes attributes like base/height/side length for each shape and methods to calculate and return their areas. The main method creates instances of each shape class, sets their attributes, calculates areas, and prints out the results.
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)
8 views4 pages

Programacion Orientada A Objetos Ejemplo de Clases

The document describes classes for different shapes (triangle, square, rectangle, circle) that inherit from a base "Figura" class. It establishes attributes like base/height/side length for each shape and methods to calculate and return their areas. The main method creates instances of each shape class, sets their attributes, calculates areas, and prints out the results.
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/ 4

Ramrez Rosas Ricardo

09/04/14

Programacion Orientada a Objetos


Ejemplo de Clases
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

class Figura
{
protected String nomFigura;
void establecerNomFigura(String nom)
{
nomFigura=nom;
}
String obtenerNomFigura()
{
return nomFigura;
}
}
class Triangulo extends Figura
{
double baseTria;
double alturaTria;
double areaTria;
void establecerBaseyAltura(double base,double altura)
{
baseTria=base;
alturaTria=altura;
}
void calcularArea()
{
areaTria=(baseTria*alturaTria)/2;
}
double obtenerAreaTria()
{
return areaTria;
}
}
class Cuadrado extends Figura
{
double ladoCua;
double areaCua;
void establecerLado(double lado)
{

Ramrez Rosas Ricardo


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

ladoCua=lado;
}
void calcularArea()
{
areaCua=ladoCua*ladoCua;
}
double obtenerAreaCua()
{
return areaCua;
}
}
class Rectangulo extends Figura
{
double baseRect;
double alturaRect;
double areaRect;
void establecerBaseyAltura(double base,double altura)
{
baseRect=base;
alturaRect=altura;
}
void calcularArea()
{
areaRect=(baseRect*alturaRect);
}
double obtenerAreaRect()
{
return areaRect;
}
}
class Circulo extends Figura
{
double radioCir;
double areaCir;
void establecerRadio(double radio)
{
radioCir=radio;
}
void calcularArea()

09/04/14

Ramrez Rosas Ricardo

09/04/14

106 {
107
areaCir=3.1416*radioCir*radioCir;
108 }
109
110 double obtenerAreaCir()
111 {
112
return areaCir;
113 }
114
115 }
116
117 public class EjecutaFigura {
118
119
public static void main(String[] args)
120
{
121
int opcion;
122
float base;
123
float altura;
124
float lado;
125
float radio;
126
127
base=3;
128
altura=2;
129
lado=2;
130
radio=2;
131
132
for(opcion=1;opcion<=4;opcion++)
133
{
134
135
switch(opcion){
136
case 1:
137
{
138
Triangulo t1 = new Triangulo();
139
140
t1.establecerNomFigura("Triangulo 1");
141
t1.establecerBaseyAltura(base,altura);
142
t1.calcularArea();
143
System.out.println("El nombre de la figura es " +
t1.obtenerNomFigura());
144
System.out.println("Su area es " + t1.obtenerAreaTria());
145
break;
146
}
147
148
case 2:
149
{
150
Cuadrado t1 = new Cuadrado();
151
152
t1.establecerNomFigura("Cuadrado 1");
153
t1.establecerLado(lado);
154
t1.calcularArea();

Ramrez Rosas Ricardo

09/04/14

155
System.out.println("El nombre de la figura es " +
t1.obtenerNomFigura());
156
System.out.println("Su area es " + t1.obtenerAreaCua());
157
break;
158
}
159
160
case 3:
161
{
162
Rectangulo t1 = new Rectangulo();
163
164
t1.establecerNomFigura("Rectangulo 1");
165
t1.establecerBaseyAltura(base,altura);
166
t1.calcularArea();
167
System.out.println("El nombre de la figura es " +
t1.obtenerNomFigura());
168
System.out.println("Su area es " + t1.obtenerAreaRect());
169
break;
170
}
171
172
case 4:
173
{
174
Circulo t1 = new Circulo();
175
176
t1.establecerNomFigura("Circulo 1");
177
t1.establecerRadio(radio);
178
t1.calcularArea();
179
System.out.println("El nombre de la figura es " +
t1.obtenerNomFigura());
180
System.out.println("Su area es de " + t1.obtenerAreaCir());
181
break;
182
}
183
}
184
}
185
186
}

You might also like