rules

29

Upload: gustav

Post on 14-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Rules. Two Teams Questions worth 1-3 points Entire team can confer to answer the question Max of 2 minutes per question You can use a computer on any question except “what does this program output…” Teams switch when either Three questions have been asked or - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Rules
Page 2: Rules

Rules

• Two Teams• Questions worth 1-3 points

– Entire team can confer to answer the question– Max of 2 minutes per question– You can use a computer on any question except “what does this

program output…”• Teams switch when either

– Three questions have been asked or– A team answers incorrectly three times (three strikes)

• After three strikes, the opposing team can “steal” the points by answering the question correctly

• Team with the most points at the end wins

Page 3: Rules

Question 1 of 12• Name at least two ways a List is more flexible

than an array.

Page 4: Rules

Answer 1 (2 points)• Name at least two ways a List is more flexible

than an array.– Don’t have to specify the size of the List, can add

items to the end– Can remove items at a specified index– Built-in methods to search the list

Page 5: Rules

Question 2 (3 points)• What is the output of this code?

int[] a = { 0, 1, 1, 0, 2, 1 }; int[] h = new int[3]; h[0] = 0; h[1] = 0; h[2] = 0; for (int i = 0; i < a.Length; i++) { h[a[i]]++; } Console.WriteLine(h[0]); Console.WriteLine(h[1]); Console.WriteLine(h[2]);

Page 6: Rules

Answer 2• 2• 3• 1

Page 7: Rules

Question 3 (1 point)• If a method is declared to be virtual then what

can we do with that method in a derived class?

Page 8: Rules

Answer 3• Override it

Page 9: Rules

Question 4 (1 point)• Write code that creates an instance of the

Dude class using the constructor (send in any data you like that is valid)

class Dude { private string name; private int age; public Dude(string n, int a) { name = n; age = a; } }

Page 10: Rules

Answer 4• Dude dude = new Dude("Ben Curtis", 29);

Page 11: Rules

Question 5 (3 point)• Write equivalent code that uses two while

loops instead of two for loops

for (int i = 0; i < 10; i++) { for (int j = 0; j < i; j++) { int temp = ary[j]; ary[j] = ary[i]; ary[i] = temp; } }

Page 12: Rules

Answer 5• Code int i = 0;

while (i < 10) { int j = 0; while (j < i) { int temp = ary[j]; ary[j] = ary[i]; ary[i] = temp; j++; } i++; }

Page 13: Rules

Question 6 (2 points)• What is the difference between a reference

type and a primitive type?

Page 14: Rules

Answer 6• Primitive type stores the actual value directly

in the memory location for the variable• Reference type stores an address in memory,

or a reference, to another place in memory that stores the actual value of the data

Page 15: Rules

Question 7 (2 points)• Give the code to create a variable named

“nums” that is a List of doubles, and then add 3.5 and 10.4 to the list

Page 16: Rules

Answer 7• List<double> nums = new List<double>();• nums.Items.Add(3.5);• nums.Items.Add(10.4);

Page 17: Rules

Question 8 (1 point)• What is the difference between public,

private, and protected?

Page 18: Rules

Answer 8• Public: accessible from outside the class• Private: accessible only inside the class• Protected: accessible inside the class and in

any derived classes

Page 19: Rules

Question 9 (3 points)• Write a class named “AfricanGrey” that is

derived from Parrot and override description() to return “Grey with red tails”

class Parrot { public virtual string description() { return ("Talking Bird"); } }

Page 20: Rules

Answer 9• Code

class AfricanGrey : Parrot { public override string description() { return ("Grey with Red Tails"); } }

Page 21: Rules

Question 10 (1 point)• How is it that inheritance helps us eliminate

redundant code?

Page 22: Rules

Answer 10• Methods or variables can be defined in parent

classes and are “inherited” by any derived class, so we don’t have to write the inherited code again

Page 23: Rules

Question 11 (2 points)• Fill in the blanks so the method accepts an

array of strings and a target string, and also invokes the method

public bool search( __________ ary, string targetname){ for (int i = 0; i < ary.Length; i++) { if (ary[i].Equals(targetname)) return true; } return false;}

public void button_click(){ string[] ary = { "Joe", "Bob", "Ted" }; if (search( _______ , "Ted")) { MessageBox.Show ("Ted is in the array."); }}

Page 24: Rules

Answer 11• Code

public bool search( string[] ary, string targetname){ for (int i = 0; i < ary.Length; i++) { if (ary[i].Equals(targetname)) return true; } return false;}

public void button_click(){ string[] ary = { "Joe", "Bob", "Ted" };

if (search( ary , "Ted")) { MessageBox.Show ("Ted is in the array."); }}

Page 25: Rules

Question 12 (3 points)• Give the output

private void question12(string[] a, int count) { a[2] = ""; count--; }private void button6_Click(object sender, EventArgs e){ string[] ary = { "Joe", "Bob", "Ted" }; int count = 3;

question12(ary, count); Console.WriteLine("Count: " + count); for (int i = 0; i < count; i++) { Console.WriteLine(ary[i]); }}

Page 26: Rules

Answer 12• Count = 3• Joe• Bob• <blank>

Page 27: Rules

The End

Page 28: Rules

Runoff Question• Write the Money class so the following code

works:

Money dinero = new Money(10000, "pesos");Money cash = new Money(5, "dollars");

// Outputs 10000 pesosMessageBox.Show(dinero.Amount + " " + dinero.Currency);// Outputs 5 dollarsMessageBox.Show(cash.Amount + " " + cash.Currency);

Page 29: Rules

Answer class Money { private int amount; public int Amount { get { return amount; } set { amount = value; } } private string currency; public string Currency { get { return currency; } set { currency = value; } } public Money(int a, string c) { amount = a; currency = c; } }