Professional Documents
Culture Documents
Nmero 2 - Clases
Resolucin de ejercicios 1 al 9
Programacin Orientada a Objetos. UAI Cede Centro.
Alejandro Freccero
10/05/2010
CONTENIDO
Ejercicio 1 ..................................................................................................................................................... 3
Form1.vb .................................................................................................................................................. 3
Circunferencia.vb ..................................................................................................................................... 3
rectangulo.vb ........................................................................................................................................... 3
Class Diagram ........................................................................................................................................... 4
Print Screen .............................................................................................................................................. 4
Ejercicio 2 ..................................................................................................................................................... 5
Form1.vb .................................................................................................................................................. 5
Triangulo.vb ............................................................................................................................................. 5
Class Diagram ........................................................................................................................................... 6
Print Screen .............................................................................................................................................. 6
Ejercicio 3 ..................................................................................................................................................... 6
Form1.vb .................................................................................................................................................. 6
Figura.vb................................................................................................................................................... 6
Circulo.vb ................................................................................................................................................. 7
Rectangulo.vb .......................................................................................................................................... 8
Class Diagram ........................................................................................................................................... 9
Print Screen .............................................................................................................................................. 9
Ejercicio 4 ..................................................................................................................................................... 9
Form1.vb .................................................................................................................................................. 9
Motor.vb ................................................................................................................................................ 11
Rueda.vb ................................................................................................................................................ 11
Class Diagram ......................................................................................................................................... 11
Print Screen ............................................................................................................................................ 11
Ejercicio 5 ................................................................................................................................................... 12
Form1.vb ................................................................................................................................................ 12
Cliente.vb ............................................................................................................................................... 12
Proveedor.vb .......................................................................................................................................... 13
Persona.vb ............................................................................................................................................. 13
Class Diagram ......................................................................................................................................... 14
Print Screen ............................................................................................................................................ 14
Ejercicio 6 ................................................................................................................................................... 14
Form1.vb ................................................................................................................................................ 14
VehiculoEstandard.vb ............................................................................................................................ 15
VehiculoDeportivo.vb ............................................................................................................................ 15
Vehiculo.vb............................................................................................................................................. 15
Class Diagram ......................................................................................................................................... 17
Print Screen ............................................................................................................................................ 17
Ejercicio 7 ................................................................................................................................................... 17
Hoja 1
Alejandro Freccero
Form1.vb ................................................................................................................................................ 17
Vehiculo.vb............................................................................................................................................. 18
Auto.vb ................................................................................................................................................... 18
Moto.vb .................................................................................................................................................. 18
Colectivo.vb ............................................................................................................................................ 19
Class Diagram ......................................................................................................................................... 19
Print Screen ............................................................................................................................................ 19
Ejercicio 8 (Falta Terminar)......................................................................................................................... 19
Form1.vb ................................................................................................................................................ 19
Persona.vb ............................................................................................................................................. 21
Articulo.vb .............................................................................................................................................. 21
Class Diagram ......................................................................................................................................... 22
Print Screen ............................................................................................................................................ 23
Ejercicio 9 ................................................................................................................................................... 23
Form1.vb ................................................................................................................................................ 23
Figura.vb................................................................................................................................................. 24
Circulo.vb ............................................................................................................................................... 24
Rectangulo.vb ........................................................................................................................................ 24
Class Diagram ......................................................................................................................................... 25
Print Screen ............................................................................................................................................ 26
Hoja 2
Alejandro Freccero
EJERCICIO 1
FORM1.VB
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim c As Rectangulo
c = New Rectangulo
c.Lado1 = Convert.ToDouble(TextBox1.Text)
c.Lado2 = Convert.ToDouble(TextBox2.Text)
Label4.Text = c.Superficie
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Dim circulo As Circunferencia = New Circunferencia
circulo.Radio = Convert.ToDouble(TextBox3.Text)
Label6.Text = "Superficie = " + circulo.Superficie().ToString
End Sub
End Class
CIRCUNFERENCIA.VB
Public Class Circunferencia
Private _radio As Double
Public Property Radio()
Get
Return _radio
End Get
Set(ByVal value)
_radio = value
End Set
End Property
Public Function Superficie() As Double
Dim s As Double
Const PI = 3.14159265358979
s = PI * (_radio ^ 2)
Return s
End Function
End Class
RECTANGULO.VB
Public Class Rectangulo
Private _lado1 As Double
Private _lado2 As Double
Public Property Lado1() As Double
Get
Return _lado1
End Get
Set(ByVal value As Double)
_lado1 = value
Hoja 3
Alejandro Freccero
End Set
End Property
Public Property Lado2() As Double
Get
Return _lado2
End Get
Set(ByVal value As Double)
_lado2 = value
End Set
End Property
Public Function Superficie() As Double
Dim Sup As Double
Sup = _lado2 * _lado1
Return Sup
End Function
End Class
CLASS DIAGRAM
PRINT SCREEN
Hoja 4
Alejandro Freccero
EJERCICIO 2
FORM1.VB
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim t As Triangulo = New Triangulo
t.Altura = Convert.ToDouble(TextBox1.Text)
t.Base = Convert.ToDouble(TextBox2.Text)
Label3.Text = "Superficie = " + t.Superficie.ToString
'Aqui demuestro la sobrecarga del consturctor NEW de la clase
Triangulo con
'un msgbox.
'Dim t2 As Triangulo
't2 = New Triangulo()
'MsgBox(t2.Altura)
End Sub
End Class
TRIANGULO.VB
Public Class Triangulo
Private _altura As Double
Private _base As Double
Property Altura()
Get
Return _altura
End Get
Set(ByVal value)
_altura = value
End Set
End Property
Property Base()
Get
Return _base
End Get
Set(ByVal value)
_base = value
End Set
End Property
Public Function Superficie() As Double
Dim sup As Double
sup = (_base * _altura) / 2
Return sup
End Function
Public Sub New()
_altura = 4
_base = 2
End Sub
End Class
Hoja 5
Alejandro Freccero
CLASS DIAGRAM
PRINT SCREEN
EJERCICIO 3
FORM1.VB
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Dim A As Figura = New Figura(TextBox3.Text)
Label6.Text = "Superficie = " + Convert.ToString(A.Superficie)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim C As Figura = New Figura(TextBox1.Text, TextBox2.Text)
Label5.Text = "Superficie = " + Convert.ToString(C.Superficie)
End Sub
End Class
FIGURA.VB
Public Class Figura
Dim x As Integer
Private _circulo As Circulo
Public Property Circulo() As Circulo
Get
Hoja 6
Alejandro Freccero
Return _circulo
End Get
Set(ByVal value As Circulo)
_circulo = value
End Set
End Property
Private _rectangulo As Rectangulo
Public Property rectangulo() As Rectangulo
Get
Return _rectangulo
End Get
Set(ByVal value As Rectangulo)
_rectangulo = value
End Set
End Property
Public Sub New(ByVal sRadio As Double) 'este lo usaria si es un
Circulo
Circulo = New Circulo
Circulo.Radio = sRadio
x = 1
End Sub
Public Sub New(ByVal sBase As Double, ByVal sAltura As Double)
'este lo usaria si es un Rectangulo.
rectangulo = New Rectangulo
rectangulo.altura = sAltura
rectangulo.base = sBase
x = 2
End Sub
Function Superficie() As Double
Dim resultado As Double
If x = 1 Then
resultado = Convert.ToDouble(Circulo.Superficie)
Return resultado
Else
If x = 2 Then
resultado = Convert.ToDouble(rectangulo.Superficie)
Return resultado
End If
End If
End Function
End Class
CIRCULO.VB
Public Class Circulo
Private _radio As Double
Private Const PI As Double = 3.14159265358979
Public Property Radio() As Double
Get
Return _radio
End Get
Set(ByVal value As Double)
_radio = value
Hoja 7
Alejandro Freccero
End Set
End Property
Public Function Superficie() As Double
Dim Sup As Double
Sup = PI * (_radio ^ 2)
Return Sup
End Function
End Class
RECTANGULO.VB
Public Class Rectangulo
Private _base As Double
Private _altura As Double
Property base()
Get
Return _base
End Get
Set(ByVal value)
_base = value
End Set
End Property
Property altura()
Get
Return _altura
End Get
Set(ByVal value)
_altura = value
End Set
End Property
Public Function Superficie() As Double
Dim Sup As Double
Sup = _base * _altura
Return Sup
End Function
End Class
Hoja 8
Alejandro Freccero
CLASS DIAGRAM
PRINT SCREEN
EJERCICIO 4
FORM1.VB
Public Class Auto
Dim mot As Boolean = False
Dim rue1 As Boolean = False
Dim rue2 As Boolean = False
Dim rue3 As Boolean = False
Dim rue4 As Boolean = False
Private _motor As Motor
Public Property Motor() As Motor
Get
Return _motor
Hoja 9
Alejandro Freccero
End Get
Set(ByVal value As Motor)
mot = True
_motor = value
End Set
End Property
Private _rueda1 As Rueda
Public Property Rueda1() As Rueda
Get
Return _rueda1
End Get
Set(ByVal value As Rueda)
rue1 = True
_rueda1 = value
End Set
End Property
Private _rueda2 As Rueda
Public Property Rueda2() As Rueda
Get
Return _rueda2
End Get
Set(ByVal value As Rueda)
rue2 = True
_rueda2 = value
End Set
End Property
Private _rueda3 As Rueda
Public Property Rueda3() As Rueda
Get
Return _rueda3
End Get
Set(ByVal value As Rueda)
rue3 = True
_rueda3 = value
End Set
End Property
Private _rueda4 As Rueda
Public Property Rueda4() As Rueda
Get
Return _rueda4
End Get
Set(ByVal value As Rueda)
rue4 = True
_rueda4 = value
End Set
End Property
Public Function AutoCompleto() As Boolean
If mot = True And rue1 = True And rue2 = True And rue3 = True
And rue4 = True Then
Return True
Else
Return False
End If
End Function
Hoja 10
Alejandro Freccero
End Class
MOTOR.VB
Public Class Motor
Private _motor As Boolean
Public Property Motor() As Boolean
Get
Return _motor
End Get
Set(ByVal value As Boolean)
_motor = value
End Set
End Property
End Class
RUEDA.VB
Public Class Rueda
Private _rueda As Boolean
Public Property rueda() As Boolean
Get
Return _rueda
End Get
Set(ByVal value As Boolean)
_rueda = value
End Set
End Property
End Class
CLASS DIAGRAM
PRINT SCREEN
Hoja 11
Alejandro Freccero
EJERCICIO 5
FORM1.VB
Public Class Form1
Dim cli As Cliente = New Cliente
Dim proveedor As Proveedor = New Proveedor
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
MsgBox(cli.Saludar())
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
cli.Comprar()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click
MsgBox(cli.CantidadCompras)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button4.Click
MsgBox(proveedor.Saludar())
End Sub
End Class
CLIENTE.VB
Public Class Cliente
Inherits Persona
Sub New()
MyBase.New("Martin", "Gonzalez")
End Sub
Private _cantidadCompras As Integer
Public ReadOnly Property CantidadCompras() As Integer
Get
Return _cantidadCompras
End Get
End Property
Hoja 12
Alejandro Freccero
PROVEEDOR.VB
Public Class Proveedor
Inherits Persona
Sub New()
MyBase.New("Alejandro", "Freccero")
End Sub
Private _codigo As Integer
Public Property Codigo() As Integer
Get
Return _codigo
End Get
Set(ByVal value As Integer)
_codigo = value
End Set
End Property
End Class
PERSONA.VB
Public Class Persona
Private _apellido As String
Public Property Apellido() As String
Get
Return _apellido
End Get
Set(ByVal value As String)
_apellido = value
End Set
End Property
Private _nombre As String
Public Property Nombre() As String
Get
Return _nombre
End Get
Set(ByVal value As String)
_nombre = value
End Set
End Property
Sub New(ByVal nomb As String, ByVal apell As String)
Nombre = nomb
Apellido = apell
End Sub
Function Saludar() As String
Dim saludo As String
saludo = Nombre + " " + Apellido
Return saludo
End Function
Hoja 13
Alejandro Freccero
End Class
CLASS DIAGRAM
PRINT SCREEN
EJERCICIO 6
FORM1.VB
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim AutoDeportivo As VehiculoDeportivo = New VehiculoDeportivo
Dim AutoComun As VehiculoEstandard = New VehiculoEstandard
If ComboBox1.Text = "Deportivo" Then
AutoDeportivo.Codigo = TextBox1.Text.ToString
AutoDeportivo.GPS = CheckBox1.Checked
AutoDeportivo.Puertas = Convert.ToInt16(TextBox2.Text)
ListBox1.Items.Add(AutoDeportivo.ToString)
Else
Hoja 14
Alejandro Freccero
VEHICULOESTANDARD.VB
Public Class VehiculoEstandard
Inherits Vehiculo
Public Overrides Function Precio() As Decimal
Dim valor As Integer = 1000
If Puertas >= 4 Then
valor = 500 + valor
End If
If GPS = True Then
valor = valor + 500
End If
Return valor
End Function
End Class
VEHICULODEPORTIVO.VB
Public Class VehiculoDeportivo
Inherits Vehiculo
Public Overrides Function Precio() As Decimal
Dim valor As Integer = 1500
If Puertas >= 4 Then
valor = valor + 600
End If
If GPS = True Then
valor = valor + 600
End If
Return valor
End Function
End Class
VEHICULO.VB
Public MustInherit Class Vehiculo
Private _codigo As String
Public Property Codigo() As String
Get
Return _codigo
End Get
Hoja 15
Alejandro Freccero
End Class
Hoja 16
Alejandro Freccero
CLASS DIAGRAM
PRINT SCREEN
EJERCICIO 7
FORM1.VB
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = "Auto" Then
Dim a As Auto = New Auto
Label5.Text = "$ " + Convert.ToString(a.Basico)
Label6.Text = "$ " + Convert.ToString(a.CalcularImporte())
Else
Hoja 17
Alejandro Freccero
VEHICULO.VB
Public MustInherit Class Vehiculo
Private _basico As Decimal
ReadOnly Property Basico() As Decimal
Get
Dim ValorKm As Decimal = 1.2
Dim KMs As Decimal = 3.4
_basico = ValorKm * KMs
Return _basico
End Get
End Property
MustOverride Function CalcularImporte() As Decimal
End Class
AUTO.VB
Public Class Auto
Inherits Vehiculo
Public Overrides Function CalcularImporte() As Decimal
Dim importe As Decimal = 0
importe = Basico * 1.5
Return importe
End Function
End Class
MOTO.VB
Public Class Moto
Inherits Vehiculo
Public Overrides Function CalcularImporte() As Decimal
Dim importe As Decimal = 0
importe = Basico
Return importe
End Function
End Class
Hoja 18
Alejandro Freccero
COLECTIVO.VB
Public Class Colectivo
Inherits Vehiculo
Public Overrides Function CalcularImporte() As Decimal
Dim importe As Decimal = 0
importe = Basico * 4
Return importe
End Function
End Class
CLASS DIAGRAM
PRINT SCREEN
Alejandro Freccero
Alejandro Freccero
CheckedListBox1.Items.Add(articulo3.ToString)
CheckedListBox1.Items.Add(articulo4.ToString)
End Sub
End Class
PERSONA.VB
Public Class Persona
Private _codigo As Integer
Public Property Codigo() As Integer
Get
Return _codigo
End Get
Set(ByVal value As Integer)
_codigo = value
End Set
End Property
Private _descuento As Boolean
Public Property Descuento() As Boolean
Get
Return _descuento
End Get
Set(ByVal value As Boolean)
_descuento = value
End Set
End Property
Private _nombre As String
Public Property Nombre() As String
Get
Return _nombre
End Get
Set(ByVal value As String)
_nombre = value
End Set
End Property
Public Overrides Function ToString() As String
Dim linea As String = String.Empty
linea = Convert.ToString(Codigo) + " " + Nombre + " "
If Descuento = True Then
linea = linea + "Cliente c/desc."
Else
linea = linea + "Cliente s/desc."
End If
Return linea
End Function
End Class
ARTICULO.VB
Public Class Articulo
Sub New(ByVal art As Integer, ByVal nom As String, ByVal valor As
Decimal)
Codigo = art
Nombre = nom
Precio = valor
End Sub
Private _codigo As Integer
Public Property Codigo() As Integer
Hoja 21
Alejandro Freccero
Get
Return _codigo
End Get
Set(ByVal value As Integer)
_codigo = value
End Set
End Property
Private _nombre As String
Public Property Nombre() As String
Get
Return _nombre
End Get
Set(ByVal value As String)
_nombre = value
End Set
End Property
Private _precio As Decimal
Public Property Precio() As Decimal
Get
Return _precio
End Get
Set(ByVal value As Decimal)
_precio = value
End Set
End Property
Public Overrides Function ToString() As String
Dim linea As String = String.Empty
linea = Convert.ToString(Codigo) + " " + Nombre + " $" +
Convert.ToString(Precio)
Return linea
End Function
End Class
CLASS DIAGRAM
Hoja 22
Alejandro Freccero
PRINT SCREEN
EJERCICIO 9
FORM1.VB
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = "Circulo" Then
Dim radio As Integer
If TextBox1.Text <> String.Empty Then
radio = Convert.ToDecimal(TextBox1.Text)
Dim c As Circulo = New Circulo(radio)
ListBox1.Items.Add(c.ToString)
Else
MsgBox("El radio no puede ser nulo!")
End If
End If
If ComboBox1.Text = "Rectangulo" Then
Dim Lado1 As Integer = 0
Dim Lado2 As Integer = 0
If (TextBox2.Text <> String.Empty) And (TextBox3.Text <>
String.Empty) Then
Lado1 = Convert.ToDecimal(TextBox2.Text)
Lado2 = Convert.ToDecimal(TextBox3.Text)
Dim rec As Rectangulo = New Rectangulo(Lado1, Lado2)
ListBox1.Items.Add(rec.ToString)
Else
MsgBox("No esta cargado alguno de sus lados!")
End If
Hoja 23
Alejandro Freccero
End If
End Sub
End Class
FIGURA.VB
Public MustInherit Class Figura
MustOverride Function Superficie() As Decimal
End Class
CIRCULO.VB
Public Class Circulo
Inherits Figura
Private _radio As Integer
Public Property Radio() As Integer
Get
Return _radio
End Get
Set(ByVal value As Integer)
_radio = value
End Set
End Property
Public Sub New(ByVal r As Integer)
Radio = r
End Sub
Public Overrides Function Superficie() As Decimal
Dim sup As Integer = 0
Const PI As Decimal = 3.141516
sup = PI * (Radio ^ 2)
Return sup
End Function
Public Overrides Function ToString() As String
Dim Linea As String = String.Empty
Linea = "Circulo" + vbTab + vbTab + "Radio: " + Radio.ToString
+ vbTab + vbTab + vbTab + "Superficie: " + Superficie.ToString
Return Linea
End Function
End Class
RECTANGULO.VB
Public Class Rectangulo
Inherits Figura
Private _lado1 As Integer
Public Property Lado1() As Integer
Get
Return _lado1
End Get
Set(ByVal value As Integer)
_lado1 = value
End Set
End Property
Hoja 24
Alejandro Freccero
CLASS DIAGRAM
Hoja 25
Alejandro Freccero
PRINT SCREEN
Hoja 26
Alejandro Freccero