80 likes | 215 Views
Chapter 6 Methods Review. Find the error in the following method: public void SayHello (); { MessageBox.Show ("Hello"); } Should not have semicolon on the header line Following is a message header line: public void ShowValue ( int intMyNum )
E N D
Find the error in the following method: public void SayHello();{MessageBox.Show("Hello"); } Should not have semicolon on the header line Following is a message header line:public void ShowValue (intintMyNum) Find the error in the following call to the method:intintNum = 8;ShowValue(intintNum); Call to ShowValue should not contain variable data type (int) Slide 2
Find the error in the following method: public double TimesTwo(double dblNum){double dblResult = dblNum * 2; } Should have a return statement to return dblResult Find the error in the following method: public int Half(double dblNum){double dblResult = dblNum / 2.0; return dblResult; } Method header should have return type double, not int Slide 3
Given the code below in a click event, what will be displayed? int intNumA = 5, intNumB = 10; MessageBox.Show("NumA is " + intNumA + " and NumB is " + intNumB ); ChangeUs(intNumA, intNumB); MessageBox.Show("now NumA is " + intNumA + " and NumB is " + intNumB); void ChangeUs(int intFirst, int intSecond) { intFirst = 20; intSecond = 30; } NumA is 5 and NumB is 10 now NumA is 5 and NumB is 10 Slide 4
What must be changed in the code to allow the method to change the numbers? int intNumA = 5, intNumB = 10; MessageBox.Show("NumA is " + intNumA + " and NumB is " + intNumB ); ChangeUs(intNumA, intNumB); MessageBox.Show("now NumA is " + intNumA + " and NumB is " + intNumB); void ChangeUs(int intFirst, int intSecond) { intFirst = 20; intSecond = 30; } Change the call statement:ChangeUs(ref intNumA, ref intNumB); Change the method header:void ChangeUs(ref int intFirst, ref int intSecond) Slide 5
Write an example of a call statement given the following method header: public void DoSomething(intintNum) DoSomething(25); Given the method definition below, write a statement that passes the value 4 to the method and assigns a return value to intResult?public int Cube(intintNum) { return intNum * intNum * intNum; } intResult = Cube(4); Slide 6
A program contains the following method: public void Display ( int intX, double dblY, bool blnZ){ MessageBox.Show( “X is “ + intX + “ Y is “ + dblY + “ and Z is “ + blnZ);} Write a statement that calls this method and passes the following variables:bool blnCheck = false; int intAge = 25; double dblIncome = 50000.00; Display(intAge, dblIncome, blnCheck); Slide 7
Write a method named TimesTen. The method should accept a double argument and return a double value that is 10 times the value of the argument. public double TimesTen(double dblNum) { return dblNum * 10; } Slide 8