ESERCIZIO 1
Per iniziare…
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
Me.Button1.Text = "Media"
Me.Button1.ForeColor = Color.Coral
Me.Button2.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left
Me.Button2.Text = "Istruzioni"
Me.Button2.ForeColor = Color.BlueViolet
Me.RichTextBox1.Anchor = AnchorStyles.Top
Me.RichTextBox1.ForeColor = Color.Fuchsia
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.RichTextBox1.Text = "29.4"
End Sub
Private Sub RichTextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.Click
Me.RichTextBox1.Text = "esami sostenuti: 5"
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("clicca sulla finestra per sapere il numero di esami sostenuti; clicca su media per conoscere il voto medio")
End Sub
End Class
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 Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Dario As New studente
Dario.nome = "Dario"
Dario.eta = 29
Dario.votoMedio = 30
Dario.indirizzo = "via cicci ciacci"
Dario.statura = statura.bassa
Dim Martina As New studente
Martina.nome = "Martina"
Martina.eta = 25
Martina.votoMedio = 29.4
Martina.indirizzo = "via ciccini"
Martina.statura = statura.alta
Dim Sara As New studente
With Sara
.nome = "Sara"
.eta = 24
.votoMedio = 30
.indirizzo = "via ciccinizza"
.statura = statura.alta
End With
Dim listaStudente As New List(Of studente)
listaStudente.Add(Dario)
listaStudente.Add(Martina)
listaStudente.Add(Sara)
For Each stu As studente In listaStudente
Me.RichTextBox1.AppendText(Environment.NewLine & stu.nome)
Next
For i As Integer = 0 To listaStudente.Count - 1
Dim stu As studente = listaStudente(i)
Me.RichTextBox1.AppendText(Environment.NewLine & stu.nome & " " & statura.GetName(GetType(statura), stu.statura))
Next
End Sub
End Class
· Con enumerazione che è un modulo:
Public Module enumerazioni
Public Enum statura
alta
media
bassa
End Enum
End Module
· E studente che è una classe:
Public Class studente
Public nome As String
Public eta As Integer
Public votoMedio As Double
Public indirizzo As String
Public statura As statura
End Class
ESERCIZIO 3
Ho creato un programma che mi consente di caricare dati di testo e conoscere il numero di osservazioni
Public Class esdati
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 Object, ByVal e As System.EventArgs) Handles Button1.Click
‘ci permette di aprire la finestra e scegliere il percorso'
'Dim o As New OpenFileDialog
'o.ShowDialog()
'If String.IsNullOrEmpty(o.FileName) Then Exit Sub
'Dim percorsoFile As String = o.FileName
‘il percorso lo imposto io’
Dim PercorsoFile As String = "I:\data mining\ES 200912 GLOBEX_2009-10-13.txt"
Dim SerieStorica As String = My.Computer.FileSystem.ReadAllText(percorsoFile)
Dim righe() As String = SerieStorica.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
'creazione singole osservazioni'
Dim osservazioni As New List(Of osservazione)
For Each riga As String In righe
Dim valori() As String = riga.Split("".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
'acqisizione dati'
Dim ore As Integer = CInt(valori(0))
Dim min As Integer = CInt(valori(1))
Dim sec As Integer = CInt(valori(2))
Dim ns As Integer = CInt(valori(3))
Dim tipop As Integer = CInt(valori(4))
Dim prezzo As Decimal = CDec(valori(5))
'creazione oggetti'
Dim osservazione As New osservazione
With osservazione
.istante = New Date(2009, 10, 13, ore, min, sec, ns)
.prezzo = prezzo
If tipop = 1 Then
.tipoP = WindowsApplication1.tipoPrezzo.bid
Else
.tipoP = WindowsApplication1.tipoPrezzo.ask
End If
End With
osservazioni.Add(osservazione)
Next
MsgBox(osservazioni.Count & "osservazioni lette da file")
End Sub
End Class
· Con enumerazione che è un modulo:
Public Module enumerazione
Public Enum tipoPrezzo
bid
ask
End Enum
End Module
· E osservazione che è una classe:
Public Class osservazione
Public tipoP As Integer
Public prezzo As Decimal
Public istante As Date
End Class
Genera una sequenza di numeri casuali per i prezzi bid e ask al variare del tempo. Riporto il risultato su un grafico ottenendo una serie storica dei prezzi nel tempo.
Public Class Form1
'dichiarazione dell'oggetto random
Public R As New Random
Public b As Bitmap
Public g As Graphics
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.b = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
Me.g = Graphics.FromImage(Me.b)
End Sub
Public minimoX As Double = Double.MaxValue
Public massimoX As Double = Double.MinValue
Public minimoY As Double = Double.MaxValue
Public massimoY As Double = Double.MinValue
Dim PuntiNonTrasformati_BID As New List(Of PointF)
Dim PuntiNonTrasformati_ASK As New List(Of PointF)
Dim PrezziCasuali_bid As New List(Of OsservazionePrezzo)
Dim PrezziCasuali_ask As New List(Of OsservazionePrezzo)
'generazione di un random walk
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'si impostano la data di inizio (precisamente un anno fa) e il prezzo iniziale
Dim TempoOrigine As Date = New Date(Now.Year - 1, Now.Month, Now.Day, 0, 0, 0)
Dim PrezzoIniziale As Decimal = 500
'variazione dei prezzi random
Dim Tick As Decimal = 0.5
Dim PrezzoPrecedente As Decimal = PrezzoIniziale
Dim TempoPrecedente As Date = TempoOrigine
Dim PrezzoCorrente As Decimal
Dim TempoCorrente As Date
Dim ossBid As New OsservazionePrezzo
Dim ossAsk As New OsservazionePrezzo
For i As Integer = 1 To 1000
If Me.R.NextDouble < 0.5 Then
PrezzoCorrente = PrezzoPrecedente - Tick
Else
PrezzoCorrente = PrezzoPrecedente + Tick
End If
TempoCorrente = TempoPrecedente.AddSeconds(Me.R.Next(5, 61))
'crea lista bid
Dim oss_bid As New OsservazionePrezzo
With oss_bid
.prezzo = PrezzoCorrente
.data = TempoCorrente
End With
PrezziCasuali_bid.Add(oss_bid)
'crea lista ask
Dim oss_ask As New OsservazionePrezzo
With oss_ask
.prezzo = PrezzoCorrente + Tick
.data = TempoCorrente
End With
PrezziCasuali_ask.Add(oss_ask)
Dim x_bid As Single = CSng((oss_bid.data - TempoOrigine).TotalSeconds)
Dim y_bid As Single = oss_bid.prezzo
Dim x_ask As Single = CSng((oss_ask.data - TempoOrigine).TotalSeconds)
Dim y_ask As Single = oss_ask.prezzo
'si aggiungono a queste due liste i punti per bid e ask
PuntiNonTrasformati_BID.Add(New PointF(x_bid, y_bid))
PuntiNonTrasformati_ASK.Add(New PointF(x_ask, y_ask))
If minimoX > x_bid Then minimoX = x_bid
If massimoX < x_bid Then massimoX = x_bid
If minimoY > y_ask Then minimoY = y_ask
If massimoY < y_ask Then massimoY = y_ask
TempoPrecedente = TempoCorrente
PrezzoPrecedente = PrezzoCorrente
Next
Dim ListaMedieMobili As New List(Of OsservazionePrezzo)
Dim listasigmapos As New List(Of OsservazionePrezzo)
Dim listasigmaneg As New List(Of OsservazionePrezzo)
Me.disegnaGrafico()
Me.CancellaTutteLeListe()
End Sub
Sub disegnaGrafico()
'se questa lista è vuota non viene creato nulla
If Me.PuntiNonTrasformati_BID Is Nothing Then Exit Sub
'Costruzione delle liste dei punti che vengono trasformati
Dim ListaPuntiTrasformati_bid As List(Of Point) = Me.CalcolaPuntiTrasformati(minimoX, massimoX, minimoY, massimoY, PuntiNonTrasformati_BID)
Dim ListaPuntiTrasformati_ask As List(Of Point) = Me.CalcolaPuntiTrasformati(minimoX, massimoX, minimoY, massimoY, PuntiNonTrasformati_ASK)
Me.b = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
Me.g = Graphics.FromImage(b)
'costruzione del rettangolo
Me.g.FillRectangle(Brushes.White, 0, 0, Me.PictureBox1.Width, Me.PictureBox1.Height)
'costruzione degli assi cartesiani
Me.g.DrawLine(Pens.Black, 0, Me.PictureBox1.Height - 1, Me.PictureBox1.Width, Me.PictureBox1.Height - 1)
Me.g.DrawLine(Pens.Black, 0, Me.PictureBox1.Height, 0, 0)
'scrivere sul grafico i nomi degli assi e titolo
Dim drawFont As New Font("Arial", 10)
Me.g.DrawString("TEMPO", drawFont, Brushes.Black, Me.PictureBox1.Width - 67, Me.PictureBox1.Height - 20)
Dim drawFont2 As New Font("Arial", 12, FontStyle.Bold)
Me.g.DrawString("Serie storica dei prezzi", drawFont2, Brushes.Purple, 380, 10)
Me.g.DrawString("PREZZO", drawFont, Brushes.Black, 10, 10)
'costruzione delle linee
Me.g.DrawLines(Pens.Fuchsia, ListaPuntiTrasformati_bid.ToArray)
Me.g.DrawLines(Pens.Blue, ListaPuntiTrasformati_ask.ToArray)
'legenda'
Dim drawfont3 As New Font("arial", 8)
Me.g.FillRectangle(Brushes.Fuchsia, Me.PictureBox1.Width - 570, Me.PictureBox1.Height - 90, 5, 5)
Me.g.DrawString("Bid", drawfont3, Brushes.Black, Me.PictureBox1.Width - 600, Me.PictureBox1.Height - 92)
Me.g.FillRectangle(Brushes.Blue, Me.PictureBox1.Width - 570, Me.PictureBox1.Height - 75, 5, 5)
Me.g.DrawString("Ask", drawfont3, Brushes.Black, Me.PictureBox1.Width - 600, Me.PictureBox1.Height - 77)
ListaPuntiTrasformati_bid.Clear()
ListaPuntiTrasformati_ask.Clear()
Me.PictureBox1.Image = Me.b
End Sub
'funzione per trasformare i punti
Function CalcolaPuntiTrasformati(ByVal minimoX As Double, ByVal massimoX As Double, ByVal minimoY As Double, ByVal massimoY As Double, _
ByVal PuntiNonTrasformati As List(Of PointF)) As List(Of Point)
Dim PuntiTrasformati As New List(Of Point)
For Each PuntoNonTrasformato As PointF In PuntiNonTrasformati
Dim x As Integer = CInt(b.Width * (PuntoNonTrasformato.X - minimoX) / (massimoX - minimoX))
Dim y As Integer = b.Height - CInt(b.Height * (PuntoNonTrasformato.Y - minimoY) / (massimoY - minimoY))
Dim point As New Point(x, y)
PuntiTrasformati.Add(point)
Next
Return PuntiTrasformati
End Function
Sub CancellaTutteLeListe()
PrezziCasuali_bid.Clear()
PrezziCasuali_ask.Clear()
PuntiNonTrasformati_bid.Clear()
PuntiNonTrasformati_ASK.Clear()
End Sub
Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Me.disegnaGrafico()
End Sub
End Class
ESERCIZIO 5
È stato realizzato un programma che genera un rettangolo (blu). Questo rettangolo può essere spostato, con il pulsante destro del mouse e, ridimensionato, con il tasto sinistro del mouse, a piacere dell’utente.
È stato realizzato un programma che genera un rettangolo (blu). Questo rettangolo può essere spostato, con il pulsante destro del mouse e, ridimensionato, con il tasto sinistro del mouse, a piacere dell’utente.
Durante le operazioni il rettangolo è di colore fuschia, con contorni tratteggiati in rosso, ad operazione finita il rettangolo sarà di colore verde.
Public Class Form1
Public b As Bitmap
Public g As Graphics
Public Rect As Rectangle
Public XinizialeMouse As Integer
Public YinizialeMouse As Integer
Public XinizialeRect As Integer
Public YinizialeRect As Integer
Public WinizialeRect As Integer
Public HinizialeRect As Integer
Public MouseEstPromuto As Boolean
Dim Pen_solida As Pen
Dim Pen_dotted As Pen
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
'creazione di un rettangolo
Me.b = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
Me.g = Graphics.FromImage(b)
Me.Pen_solida = New Pen(Color.Black, 1)
Me.Pen_dotted = New Pen(Color.Red, 1)
Me.Pen_dotted.DashStyle = Drawing2D.DashStyle.Dot
Me.Rect = New Rectangle(20, 20, 300, 250)
Me.g.Clear(Color.White)
Me.g.FillRectangle(Brushes.Blue, Me.Rect)
Me.g.DrawRectangle(Me.Pen_solida, Me.Rect)
'display
Me.PictureBox1.Image = b
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'salviamo la posizione corrente del mouse e del rettangolo
If Not Me.Rect.Contains(e.X, e.Y) Then Exit Sub
If e.Button = Windows.Forms.MouseButtons.Left Then
Me.Cursor = Cursors.Hand
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
Me.Cursor = Cursors.SizeNWSE
End If
XinizialeMouse = e.X
YinizialeMouse = e.Y
XinizialeRect = Me.Rect.X
YinizialeRect = Me.Rect.Y
WinizialeRect = Me.Rect.Width
HinizialeRect = Me.Rect.Height
Me.MouseEstPromuto = True
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Not Me.MouseEstPromuto Then Exit Sub
'prendiamo le nuove coordinate e vedere di quando si sono spostate rispetto alle coordinate iniziali e vogliamo dare quello stesso movimento al rettangolo
Dim DX As Integer = e.X - XinizialeMouse
Dim DY As Integer = e.Y - YinizialeMouse
If e.Button = Windows.Forms.MouseButtons.Left Then
'lo applico al nuovo vertice del rettangolo
Me.Rect.X = XinizialeRect + DX
Me.Rect.Y = YinizialeRect + DY
Else
Me.Rect.Width = WinizialeRect + DX
Me.Rect.Height = HinizialeRect + DY
End If
'ora devo ridisegnare il rettangolo
Me.g.FillRectangle(Brushes.White, Rectangle.Truncate(Me.b.GetBounds(GraphicsUnit.Pixel)))
Me.g.FillRectangle(Brushes.Fuchsia, Me.Rect)
'che è piu veloce del clear
Me.g.DrawRectangle(Me.Pen_dotted, Me.Rect)
Me.PictureBox1.Image = b
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Me.MouseEstPromuto = False
Me.Cursor = Cursors.Default
Me.g.Clear(Color.White)
Me.g.FillRectangle(Brushes.GreenYellow, Me.Rect)
Me.g.DrawRectangle(Me.Pen_solida, Me.Rect)
Me.PictureBox1.Image = b
End Sub
End Class
ESERCIZIO 6
Esempi visti in classe:
·
Costruttore
di una classe
Public Class Form1
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
Dim o As New Osservazione(2324, Now)
End Sub
End Class
Public Class Osservazione
Sub New(ByVal p As Decimal, ByVal i As Date) 'i parametri
assegnati ai campi dell'utente vengono assegnati ai campi della classe
Me.prezzo
= p
Me.istante
= i
End Sub
Public
prezzo As Decimal
Public
istante As Date
End Class
·
iterface
e comparer.
Public Class Form1
Private Sub
Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim o As New Osservazione(2324,
Now)
Dim s As New StudenteUniversitario
Dim c As New ContribuenteLaureato
Dim OggettoGenerale As
Studente
OggettoGenerale = s
OggettoGenerale = c
Dim NomiRagazzi As New List(Of String)
NomiRagazzi.Add("Tizio")
NomiRagazzi.Add("Pippo")
NomiRagazzi.Sort()
Dim Oss As New List(Of Osservazione)
Oss.Add(New Osservazione(123,
Now.AddHours(1)))
Oss.Add(New Osservazione(234,
Now)
Oss.Sort(New ComparaPrezzi)
Oss.Sort(New ComparaDate)
End Sub
End Class
Public Class ComparaPrezzi
Implements IComparer(Of Osservazione)
Public Function
Compare(ByVal x As
Osservazione, ByVal
y As Osservazione)
As Integer Implements System.Collections.Generic.IComparer(Of Osservazione).Compare
If x.prezzo < y.prezzo Then
Return -1
ElseIf x.prezzo > y.prezzo Then
Return 1
Else
Return 0
End If
End Function
End Class
Public Class ComparaDate
Implements IComparer(Of Osservazione)
Public Function
Compare(ByVal x As
Osservazione, ByVal
y As Osservazione)
As Integer Implements System.Collections.Generic.IComparer(Of Osservazione).Compare
If x.istante < y.istante Then
Return -1
ElseIf x.istante > y.istante Then
Return 1
Else
Return 0
End If
End Function
End Class
Public Class StudenteUniversitario
Implements Studente
Public Sub
CalcoloCodiceFiscale() Implements Studente.CalcoloCodiceFiscale
End Sub
Public Function
Votomedio() As Double
Implements Studente.Votomedio
End Function
End Class
Public Class ContribuenteLaureato
Implements Studente
Public Sub
CalcoloCodiceFiscale() Implements Studente.CalcoloCodiceFiscale
End Sub
Public Function
Votomedio() As Double
Implements Studente.Votomedio
End Function
End Class
Interface Studente
Function Votomedio() As
Double
Sub CalcoloCodiceFiscale()
End Interface
Public Class Osservazione
Sub New(ByVal p As Decimal, ByVal i As Date) 'i parametri assegnati ai campi dell'utente vengono
assegnati ai campi della classe
Me.prezzo = p
Me.istante = i
End Sub
Public prezzo As Decimal
Public istante As Date
End Class
Nessun commento:
Posta un commento