Maximum Profit Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

VB Data Validation Unit 4. Input Validation Validating user input before it is written can improve the quality of the data stored. Good validation schemes.
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Customizing Reports. Custom Reports A report is a formatted hardcopy of the contents of one or more tables from a database. Although you can format and.
Program Design and Development
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Mr C Johnston ICT Teacher BTEC IT Unit 06 - Lesson 05 Learning To Program.
InvEasy (Project1) Please use speaker notes for additional information!
Break Processing Please use speaker notes for additional information!
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Visual Basic Games: Prepare for Hangman
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Array - adding to array at run time Please see speaker notes for additional information!
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Tutorial 6 The Repetition Structure
30/10/ Iteration Loops Do While (condition is true) … Loop.
Random Files Please see speaker notes for additional information!
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
1 Advanced Computer Programming Lab Calculator Project.
Introduction to ASP.NET Please use speaker notes for additional information!
Two Forms Please use speaker notes for additional information!
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Pay Example (PFirst98) Please use speaker notes for additional information!
CECS 5020 Computers in Education Visual Basic Variables and Constants.
31/01/ Selection If selection construct.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Using a Database Access97 Please use speaker notes for additional information!
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
I am using Visual Basic 6 for this class. If you want to use a different version, please contact me. Thanks!
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Computer Science Up Down Controls, Decisions and Random Numbers.
Visual Basic - Break Processing
Programming Right from the Start with Visual Basic .NET 1/e
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Department Array in Visual Basic
Please use speaker notes for additional information!
Please use speaker notes for additional information!
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Introduction to Problem Solving and Control Statements
If statements (Inven1, Inven2, Inven2a, Inven3, Inven3a)
Please use speaker notes for additional information!
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Maximum Profit Please use speaker notes for additional information!

Maximum Profit The problem was presented as you have a maximum budget of $1800 and the hours allocated for the work is 500 hours. Tables cost $60 and take 15 hours to make and have a profit of $50. Chairs cost $25 and take 10 hours to make and have a profit of $30. Benches cost $4 and take 2 hours to make and have a profit of $6. Figure out what combination of tables, chairs and benches to make to maximize profit.

Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = t * 60 hours = t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Tablelbl.Caption = Tablelbl.Caption & savet(0) & ", " & savet(1) & ", " & savet(2) & ", " & savet(3) & ", " & savet(4) Chairlbl.Caption = Chairlbl.Caption & savec(0) & ", " & savec(1) & ", " & savec(2) & ", " & savec(3) & ", " & savec(4) Benchlbl.Caption = Benchlbl.Caption & saveb(0) & ", " & saveb(1) & ", " & saveb(2) & ", " & saveb(3) & ", " & saveb(4) Countlbl.Caption = Countlbl.Caption & cnt End Sub Dim t As Integer Dim c As Integer Dim b As Integer Dim budget As Integer Dim budgetx As Integer Dim hours As Integer Dim hoursx As Integer Dim maxchairs As Integer Dim profit As Integer Dim MaxProfit As Integer Dim savet(5) As Integer Dim savec(5) As Integer Dim saveb(5) As Integer Dim cnt As Integer The Dim statements are located in General/Declarations but in fact there is only one routine so it does not matter. When the for loops are complete, I print out the contents of the arrays with the maximum profit combinations.

Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = t * 60 hours = t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Maximum Profit With a maximum budget of $1800 and maximum hours of 500, I did the math to figure out the maximum number of tables I could make. It costs $60 to make a table so, 1800/60 = 30 It takes 15 hours to make a table so, 500/15 = 33.3 but that would be over budget so I know the maximum number of tables is 30. This results in the FOR t = 0 to 30 loop. This means that cost is the dominant number in making tables.

Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = t * 60 hours = t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Maximum Profit Whatever t (number of tables) is, we are figuring out how much budget and how many hours are left after we deduct t times cost and hours. Now we need to determine the maximum number of chairs. To do that we need to find out whether hours or budget is the deciding factor in chairs. We will now do the For loop varying c (number of chairs) from 0 to maxchairs. Whatever c is, we are figuring out how much budget and how many hours are left after we deduct c times cost and hours.

Private Sub Form_Load() MaxProfit = 0 cnt = 0 For t = 0 To 30 budget = t * 60 hours = t * 15 If budget / 25 < hours / 10 Then maxchairs = budget / 25 Else maxchairs = hours / 10 End If For c = 0 To maxchairs budgetx = budget - c * 25 hoursx = hours - c * 10 If budgetx / 4 < hoursx / 2 Then b = budgetx / 4 Else b = hoursx / 2 End If profit = (t * 50) + (c * 30) + (b * 6) If profit = 1630 Then savet(cnt) = t savec(cnt) = c saveb(cnt) = b cnt = cnt + 1 End If If profit > MaxProfit Then MaxProfit = profit End If Next c Next t Profitbox.Text = MaxProfit Maximum Profit We now need to determine (after subtracting from the budget for tables and chairs) what is going to control benches b - the budget or the hours. We then use that figure to establish the value of b (number of benches). We can now calculate profit based on what we have been told the profit is for each of these. Tables has a 50 profit, chairs has a 30 profit and benches has a 6 profit. I have already figured out that the maximum profit with these particular figures is If that profit is achieved then I move the combination that achieved it to the save areas. I also set MaxProfit to that amount. That is really not needed, I could have preset it.

Maximum Profit - variable numbers The second view shows the scroll bar positioned at the bottom.

Maximum Profit - variable numbers Private Sub txtBenchPrice_LostFocus() Call EditRtn(txtBenchPrice) End Sub Private Sub txtBenchTime_LostFocus() Call EditRtn(txtBenchTime) End Sub Private Sub txtBudgtDol_LostFocus() Call EditRtn(txtBudgtDol) End Sub Private Sub txtBudgtHrs_LostFocus() Call EditRtn(txtBudgtHrs) End Sub Private Sub txtChairCost_LostFocus() Call EditRtn(txtChairCost) End Sub Private Sub txtChairPrice_LostFocus() Call EditRtn(txtChairPrice) End Sub Private Sub txtChairTime_LostFocus() Call EditRtn(txtChairTime) End Sub Private Sub txtTableCost_LostFocus() Call EditRtn(txtTableCost) End Sub Private Sub txtTablePrice_LostFocus() Call EditRtn(txtTablePrice) End Sub Private Sub txtTableTime_LostFocus() Call EditRtn(txtTableTime) End Sub

Maximum Profit - variable numbers “Object variables are stored as 32-bit (4-byte) addresses that refer to objects. Using the Set statement, a variable declared as an Object can have any object reference assigned to it.” Visual Basic Help Private Sub txtTableCost_LostFocus() Call EditRtn(txtTableCost) End Sub Private Sub txtTablePrice_LostFocus() Call EditRtn(txtTablePrice) End Sub Private Sub txtTableTime_LostFocus() Call EditRtn(txtTableTime) End Sub Private Sub EditRtn(CurrCtl As Object) If CurrCtl.Text <> "" Then If Not (IsNumeric(CurrCtl.Text)) Or _ Val(CurrCtl.Text) < 1 Or _ Int(Val(CurrCtl.Text)) <> Val(CurrCtl.Text) Then Beep CurrCtl.Text = "" CurrCtl.SetFocus End If End Sub If the field contains data, I want to make sure that it is numeric, positive and a whole number. This code will beep, set the field to null and give it focus if any of those criteria are not met.

Maximum Profit - variable numbers This is the data entry screen when we start the program. The last thing to be filled in is Benches Cost. At that point, a routine is executed to show the next stage.

Private Sub txtBenchCost_LostFocus() Call EditRtn(txtBenchCost) If txtBenchCost.Text <> "" Then lblProfit.Visible = True lblChairProfit.Caption = txtChairPrice.Text - txtChairCost.Text lblChairProfit.Visible = True lblTableProfit.Caption = txtTablePrice.Text - txtTableCost.Text lblTableProfit.Visible = True lblBenchProfit.Caption = txtBenchPrice.Text - txtBenchCost.Text lblBenchProfit.Visible = True btnCalcMaxProfit.Visible = True btnCalcMaxProfit.SetFocus End If End Sub Maximum Profit - variable numbers After we loose focus on txtBenchCost, the profit label area are made visible and the calculate profit appears. The button to activate the next processing is also made visible and receives focus. After the next stage, this button will disappear.

Maximum Profit - variable numbers The next area shows the maximum profit, tells the number of combinations and shows them. Notice that the button that was pressed to cause the last stage to happen is no longer visible.

Private Sub btnCalcMaxProfit_Click() Dim MaxProfit As Integer, Profit As Integer Dim MaxChairs As Integer, MaxTables As Integer Dim t As Integer, c As Integer, b As Integer Dim cnt As Integer, I As Integer Dim Budget1 As Integer, Hours1 As Integer Dim Budget2 As Integer, Hours2 As Integer Dim UnitsArray() As Integer btnCalcMaxProfit.Visible = False MaxProfit = 0 cnt = 0 MaxTables = Int(txtBudgtDol.Text / txtTableCost.Text) If MaxTables > Int(txtBudgtHrs.Text / txtTableTime.Text) Then MaxTables = Int(txtBudgtHrs.Text / txtTableTime.Text) End If For t = 0 To MaxTables Budget1 = txtBudgtDol.Text - (t * txtTableCost.Text) Hours1 = txtBudgtHrs.Text - (t * txtTableTime.Text) If Budget1 / txtChairCost.Text < Hours1 / txtChairTime.Text Then MaxChairs = Int(Budget1 / txtChairCost.Text) Else MaxChairs = Int(Hours1 / txtChairTime.Text) End If Maximum Profit - variable numbers MaxTables which is the maximum number of tables that can be produced given the budgeted dollars and time constraints is first calculated by dividing the budget in dollars by the table cost. I then checked to see if that number was greater than the budgeted time divided by the time it takes to make a table. If it is I put the hours calculation into MaxTables. The For statement goes from 0 to MaxTables. MaxChairs is no determined in the same way.

For c = 0 To MaxChairs Budget2 = Budget1 - (c * txtChairCost.Text) Hours2 = Hours1 - (c * txtChairTime.Text) If Budget2 / txtBenchCost.Text < Hours2 / txtBenchTime.Text Then b = Int(Budget2 / txtBenchCost.Text) Else b = Int(Hours2 / txtBenchTime.Text) End If Profit = (t * lblTableProfit.Caption) _ + (c * lblChairProfit.Caption) _ + (b * lblBenchProfit.Caption) If Profit > MaxProfit Then MaxProfit = Profit cnt = 0 ReDim UnitsArray(3, 0) End If If Profit = MaxProfit Then ReDim Preserve UnitsArray(3, cnt + 1) UnitsArray(1, cnt) = c UnitsArray(2, cnt) = t UnitsArray(3, cnt) = b cnt = cnt + 1 End If Next c Next t Maximum Profit - variable numbers If the maximum profit has changed, then a new value is moved to maximum profit and the array is re-dimensioned to hold a place for chairs, tables and benches (that is why there is a 3) and 0 elements meaning that it is reset to contain a blank row. Cnt is reset to 0 since there is nothing in the array and when entering we will add to 0th element. If the Profit is = to MaxProfit then the array is re-dimensioned to add room for one more set of chair, table and bench combinations but the integrity of the data in the array is preserved with the preserve.

lblMaxProfit.Caption = "The maximum profit is " & MaxProfit lblMaxProfit.Visible = True lblComboCnt.Caption = "of chairs, tables and benches " _ & "that will produce the maximum profit." If cnt > 0 Then If cnt > 1 Then lblComboCnt.Caption = "There are " & cnt & " combinations " & lblComboCnt.Caption & " They are:" Else lblComboCnt.Caption = "There is 1 combination " & lblComboCnt.Caption & " It is:" End If lblComboCnt.Visible = True If cnt > 0 Then lblTableHdr.Visible = True For I = 0 To cnt - 1 txtTableDisp.Text = txtTableDisp.Text _ & Space(7 - Len(Str(UnitsArray(1, I)))) & UnitsArray(1, I) _ & Space(15 - Len(Str(UnitsArray(2, I)))) & UnitsArray(2, I) _ & Space(14 - Len(Str(UnitsArray(3, I)))) & UnitsArray(3, I) _ & Chr(13) & Chr(10) Next I txtTableDisp.Visible = True End If End Sub Maximum Profit - variable numbers When the FOR loops are complete which they were in the last slide. The information about the MaxProfit is written out and the data from the array is displayed to show the combinations that were stored.

Maximum Profit - variable numbers If Profit > MaxProfit Then MaxProfit = Profit cnt = 0 ReDim UnitsArray(2, 0) End If If Profit = MaxProfit Then UnitsArray(0, cnt) = c UnitsArray(1, cnt) = t UnitsArray(2, cnt) = b cnt = cnt + 1 ReDim Preserve UnitsArray(2, cnt) End If 2,0 sets it up with essentially a 3 by 1 dimension which is exactly what I need to enter my first combination. Now the data goes into the 0th, 1st and 2nd. The first row will have cnt = 0. Then I added 1 to cnt and redimensioned the array in preparation for the next element. Note that in doing this I preserved what was already in the array.

Maximum Profit - variable numbers If cnt > 0 Then lblTableHdr.Visible = True For I = 0 To cnt - 1 txtTableDisp.Text = txtTableDisp.Text _ & Space(7 - Len(Str(UnitsArray(0, I)))) & UnitsArray(0, I) _ & Space(15 - Len(Str(UnitsArray(1, I)))) & UnitsArray(1, I) _ & Space(14 - Len(Str(UnitsArray(2, I)))) & UnitsArray(2, I) _ & Chr(13) & Chr(10) Next I txtTableDisp.Visible = True End If Because I changed the dimensions of the array I now change the 1, 2,3 to 0, 1, 2 in this code.

Maximum Profit - variable numbers Private Sub txtTableCost_Validate(KeepFocus As Boolean) Call EditRtn(txtTableCost, KeepFocus) End Sub Private Sub txtTablePrice_Validate(KeepFocus As Boolean) Call EditRtn(txtTablePrice, KeepFocus) End Sub Private Sub txtTableTime_Validate(KeepFocus As Boolean) Call EditRtn(txtTableTime, KeepFocus) End Sub Private Sub EditRtn(CurrCtl As Object, KeepFocus As Boolean) If CurrCtl.Text = "" Or _ CurrCtl.Text <> "" And _ (Not (IsNumeric(CurrCtl.Text)) Or _ Val(CurrCtl.Text) < 1 Or _ Int(Val(CurrCtl.Text)) <> Val(CurrCtl.Text)) Then Beep CurrCtl.Text = "" KeepFocus = True End If End Sub KeepFocus allows me to keep the focus on the field.

Properties continued Properties

lblMaxProfit.Caption = "The maximum profit is " & MaxProfit lblMaxProfit.Visible = True lblComboCnt.Caption = "of chairs, tables and benches " _ & "that will produce the maximum profit." If cnt > 0 Then If cnt > 1 Then lblComboCnt.Caption = "There are " & cnt & " combinations " & lblComboCnt.Caption & " They are:" Else lblComboCnt.Caption = "There is 1 combination " & lblComboCnt.Caption & " It is:" End If Output I put in initial captions in the properties to simply show what would go in these areas.