Wednesday, August 3, 2011

Wednesday 8.3.11


Option Strict On
Imports System
Module Module1

Sub Main()
Dim x As Integer = 5
Dim y As Integer = 7

Dim andValue As Boolean
Dim orValue As Boolean
Dim xorValue As Boolean
Dim notValue As Boolean

andValue = x = 3 And y = 7
orValue = x = 3 Or y = 7
xorValue = x = 3 Xor y = 7
notValue = Not x = 3

Console.WriteLine("x = 3 and Y = 7. {0}", andValue)
Console.WriteLine("x = 3 or Y = 7. {0}", orValue)
Console.WriteLine("x = 3 Xor y = 7. {0}", xorValue)
Console.WriteLine("Not x = 3. {0}", notValue)

End Sub
End Module




Option Strict On
Imports System
Module Module1

Sub Main()
Dim minDrinkingCoffee As Integer = 5
Dim minReadingNewspaper As Integer = 10
Dim minArguing As Integer = 15
Dim minDawdling As Integer = 20

Dim numAdults As Integer = 2
Dim numChildren As Integer = 2

Dim wastedByEachAdult As Integer
Dim wastedByAllAdults As Integer
Dim wastedByEachKid As Integer
Dim wastedByAllKids As Integer
Dim wastedByFamily As Integer
Dim totalSeconds As Integer

wastedByEachAdult = minDrinkingCoffee + minReadingNewspaper
wastedByAllAdults = wastedByEachAdult * numAdults
wastedByEachKid = minDawdling + minArguing
wastedByAllKids = wastedByEachKid * numChildren
wastedByFamily = wastedByAllAdults + wastedByAllKids
totalSeconds = wastedByFamily * 60
Console.WriteLine("Each adult wastes {0} minutes", wastedByEachAdult)
Console.WriteLine("Each child wastes {0} minutes", wastedByEachKid)
Console.WriteLine("Total minutes wasted by entire family: {0}", wastedByFamily)
Console.WriteLine("Total wasted seconds: {0}", totalSeconds)


End Sub
End Module





Option Strict On
Imports System
Public Module Module1

Public Class Dog
Public weight As Integer
End Class



Public Class Tester

Public Sub Run()
'create an integer
Dim firstInt As Integer = 5
'create a second integer
Dim secondInt As Integer = firstInt
'display the two integers
Console.WriteLine("firstInt: {0} secondInt: {1}", firstInt, secondInt)

'modify the second integer
secondInt = 7
'display the two integers
Console.WriteLine("firstInt: {0} secondInt: {1}", firstInt, secondInt)

'create a dog
Dim milo As New Dog()
'assign a value to weight
milo.weight = 5
'create a second reference to teh dog
Dim Fido As Dog = milo
'dispaly their values
Console.WriteLine("Milo: {0}, fido: {1}, milo.weight, fido.weight")
'assign a new weight to the second reference
Fido.weight = 7
'display the two values
Console.WriteLine("Milo: {0}, fido: {1}", milo.weight, Fido.weight)

End Sub
End Class

Sub Main()
Dim testObject As New Tester()
testObject.Run()
End Sub

End Module




Option Strict On
Imports System
Public Class Time
'Private variables
Private Year As Integer
Private Month As Integer
Private tDate As Integer
Private Hour As Integer
Private Minute As Integer
Private Second As Integer
'Public methods
Public Sub DisplayCurrentTime()
Console.WriteLine("stub for DisplayCurrentTime")
End Sub 'Display Current Time
End Class 'Time

Module Module1
Sub Main()
Dim timeObject As New Time()
timeObject.DisplayCurrentTime()
End Sub
End Module




Option Strict On
Imports System
Public Class TestClass
Sub SomeMethod(ByVal firstParam As Integer, ByVal secondParam As Single)
Console.WriteLine("Here are the parameters received: {0}, {1}", firstParam, secondParam)
End Sub
End Class

Module Module1
Sub Main()
Dim howManyPeople As Integer = 5
Dim pi As Single = 3.14F

Dim tc As New TestClass()
tc.SomeMethod(howManyPeople, pi)
End Sub
End Module




Option Strict On
Imports System
Public Class Time
'Private variables
Private Year As Integer
Private Month As Integer
Private tDate As Integer
Private Hour As Integer
Private Minute As Integer
Private Second As Integer

'Public methods
Public Sub DisplayCurrentTime()
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, tDate, Year, Hour, Minute, Second)
End Sub

'Constructor
Public Sub New(ByVal theYear As Integer, ByVal theMonth As Integer, ByVal theDate As Integer, ByVal theHour As Integer, ByVal theMinute As Integer, ByVal theSecond As Integer)
Year = theYear
Month = theMonth
tDate = theDate
Hour = theHour
Minute = theMinute
Second = theSecond
End Sub
End Class 'Time

Module Module1
Sub Main()
Dim timeObject As New Time(2005, 3, 25, 8, 35, 20)
timeObject.DisplayCurrentTime()
End Sub
End Module




'copy constructor
Option Strict On
Imports System
Public Class Time
'Private Variables
Private Year As Integer
Private Month As Integer
Private tDate As Integer
Private Hour As Integer
Private Minute As Integer
Private Second As Integer = 30

'Public methods
Public Sub DisplayCurrentTime()
System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, tDate, Year, Hour, Minute, Second)
End Sub


Public Sub New(ByVal theYear As Integer, ByVal theMonth As Integer, ByVal theDate As Integer, ByVal theHour As Integer, ByVal theMinute As Integer)
Year = theYear
Month = theMonth
tDate = theDate
Hour = theHour
Minute = theMinute
End Sub

Public Sub New(ByVal existingObject As Time)
Year = existingObject.Year
Month = existingObject.Month
tDate = existingObject.tDate
Hour = existingObject.Hour
Minute = existingObject.Minute
End Sub

End Class

Module Module1
Sub Main()
Dim timeObject As New Time(2006, 3, 24, 10, 36)
Dim t2 As New Time(timeObject)
timeObject.DisplayCurrentTime()
t2.DisplayCurrentTime()
End Sub
End Module




'a common use of shared member variables, or fields, is to keep track of the number of instances/objects that currently exist
'for your class

Option Strict On
Imports System
Class Cat
'the cat class begins by defining a shared member variable, instances, that is initialized to 0
Private Shared instances As Integer = 0
Private weight As Integer
Private name As String

Public Sub New(ByVal nane As String, ByVal weight As Integer)
instances += 1
Me.name = name
Me.weight = weight
End Sub

Public Shared Sub HowManyCats()
Console.WriteLine("{0} cats adopted", instances)
End Sub

Public Sub TellWeight()
Console.WriteLine("{0} is {1} pounds", name, weight)
End Sub

End Class 'cat


Module Module1
Sub Main()
Cat.HowManyCats()
Dim frisky As New Cat("Frisky", 5)
frisky.TellWeight()
Cat.HowManyCats()
Dim whiskers As New Cat("Whiskers", 7)
whiskers.TellWeight() 'instance method
whiskers.HowManyCats() 'shared method through instance
Cat.HowManyCats() 'shared method through clas name
End Sub


End Module





Option Strict On
Imports System
Public Class Time
'private member variables
Private mYear As Integer
Private mMonth As Integer
Private mDate As Integer
Private mHour As Integer
Private mMinute As Integer
Private mSecond As Integer

Property Hour() As Integer
Get
Return mHour
End Get
Set(ByVal value As Integer)
mHour = value
End Set
End Property

Property Minute() As Integer
Get
Return mMinute
End Get
Set(ByVal value As Integer)
mMinute = value
End Set
End Property



'public accessor methods
Public Sub DisplayCurrentTime()
Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", mMonth, mDate, mYear, mHour, mMinute, mSecond)
End Sub 'displayCurrentTime

'Constructors
Public Sub New(ByVal dt As System.DateTime)
mYear = dt.Year
mMonth = dt.Month
mDate = dt.Day
mHour = dt.Hour
mMinute = dt.Minute
mSecond = dt.Second
End Sub 'New

Public Sub New(ByVal mYear As Integer, ByVal mMonth As Integer, ByVal mDate As Integer, ByVal mHour As Integer, ByVal mMinute As Integer, ByVal mSecond As Integer)
Me.mYear = mYear
Me.mMonth = mMonth
Me.mDate = mDate
Me.mHour = mHour
Me.mMinute = mMinute
Me.mSecond = mSecond
End Sub 'new

End Class 'time

Module Module1
Sub Main()
Dim currentTime As System.DateTime = System.DateTime.Now
Dim time1 As New Time(currentTime)
time1.DisplayCurrentTime()

'extract the hour to a local variable
Dim theHour As Integer = time1.Hour
'display the local variable
Console.WriteLine("Retrieved the hour: {0}", theHour)

'add one to the local variable
theHour += 1
'write the time back to the object
time1.Hour = theHour
'display the result
Console.WriteLine("Updated the hour: {0}", time1.Hour)

Dim theMinute As Integer = time1.Minute
Console.WriteLine("Retrieved the minute: {0}", theMinute)
theMinute += 1
time1.Minute = theMinute
Console.WriteLine("Updated the minute: {0}", time1.Minute)
End Sub
End Module




#include
int main(void)
{
int dogs;

printf("How many dogs do you have?\n");
scanf("%d", &dogs);
printf("So you have %d dog(s)!\n", dogs);

return 0;
}




#include
int main(void)
{
int num;
num = 1;

printf("I am a simple "); /* use the printf() function */
printf("computer.\n");
printf("My favorite number is %d because it is first.\n", num);

return 0;
}




//converts two fathoms to feet
#include
int main(void)
{
int feet, fathoms;
fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
printf("Yes, I said %d feet!\n", 6 * fathoms);

return 0;
}




#include
void butler(void);
int main(void)
{
printf("I will summon the butler function.\n");
butler();
printf("Yes, bring me some tea and crumpets.\n");
return 0;
}

void butler(void)
{
printf("You rang, sir?\n");
}




Option Strict On
Imports System
Public Class Cat
Private mWeight As Integer

Public Sub New(ByVal weight As Integer)
mWeight = weight
End Sub

Public Property Weight() As Integer
Get
Return mWeight
End Get
Set(ByVal value As Integer)
mWeight = value
End Set
End Property

Public Overrides Function ToString() As String
Return mWeight.ToString()
End Function

End Class ' Cat

Public Class Tester
Public Sub Run()
'declare a Cat and intialize it to 5
Dim theVariable As New Cat(5)
'display its value
Console.WriteLine("in Run. theVariable: {0}", theVariable)
'call a method and pass in the variable
Doubler(theVariable)
'return and display the value again
Console.WriteLine("back in run. the variable {0}", theVariable)
End Sub

Public Sub Doubler(ByVal param As Cat)
'display the value that was passed in
Console.WriteLine("In Method1. Received param: {0}", param)
'double the value
param.Weight = param.Weight * 2

'display the doubled value before returning
Console.WriteLine("Updated param. Returning new value: {0}", param)
End Sub
End Class 'tester

Module Module1
Sub Main()
Dim t As New Tester()
t.Run()
End Sub
End Module




SELECT ProductName, UnitPrice, UnitsInStock FROM Products
WHERE (ProductName BETWEEN 'A' AND 'D')

No comments: