You are responsible for all material covered through the end of Feb 28.
static int find(int[] arr, int val)
requires: val occurs exactly once in arr
effects: returns index i such that arr[i] = val
static int find(int[] arr, int val)
effects: returns largest index i such that
arr[i] = val, or -1 if no such i
static int find(int[] arr, int val)
requires: val occurs in arr
effects: returns largest index i such that arr[i] = val
static int find(int[] arr, int val)
requires: val occurs in arr
effects: returns index i such that arr[i] = val
// effects: returns largest index i such that
arr[i] = val, or -1 if no such i
static int find(int[] arr, int val) {
for (int i = arr.length - 1 ; i >= 0; i--) {
if (arr[i] == val) return i;
}
return -1;
}
True or False: The following input demonstrates that find does not satisfy the spec: [ 1, 2, 2 ], 2
True or False: The following input demonstrates that find does not satisfy the spec. [ 1, 2, 3 ], 2
True or False: The following input demonstrates that find does not satisfy the spec. [ 1, 2, 2 ], 4
True or False: find's implementation does satisfy the spec!
// Requires: tiles has length 7 & contains only uppercase letters.
// crossings contains only uppercase letters, without duplicates.
// Effects: Returns a list of strings
public static List<string> scrabble(string tiles, string crossings) {
if (tiles.Length != 7) { throw new Exception(); }
return new List<string>();
}
scrabble
?tiles
has only uppercase letterscrossings
has no duplicatesscrabble
takes two argumentsscrabble
returns a list of stringsscrabble
?tiles
is a string of uppercase letterscrossings
has no duplicatestiles.length() != 7
, scrabble throws an Exceptionscrabble
takes two argumentsDoes scrabble
's implementation satisfy the specification?
Is the following specification stronger or weaker than the original one?
// Requires: tiles has length 7 & contains only uppercase letters.
// crossings contains only uppercase letters, without duplicates.
// Effects: Returns a list of all possible words where each word can be
// made by taking
// letters from tiles and at most 1 letter from crossings. Returns
// empty list only if no words can be found.
public static List<string> scrabble(string tiles, string crossings)
// Requires: tiles has length 7.
// crossings contains letters, without duplicates.
// Effects: Returns a list of all possible words where each word can be
// made by taking
// letters from tiles and at most 1 letter from crossings. Returns
// empty list only if no words can be found.
public static List<string> scrabble(string tiles, string crossings)
// Alice writes
public static int gcd(int a, int b) {
if (a > b) {
return gcd(a-b, b);
} else if (b > a) {
return gcd(a, b-a);
}
return a;
}
// Bob writes
[TestMethod]
public void gcdTest() {
Assert.AreEqual(6, gcd(24, 54));
}
// requires: nothing
// effects: Returns a list containing lowercase strings from list.
static List<string> ToLowerCase(List<string> list)
{
for (int i = 0; i < list.Count; i++)
{
list[i] = list[i].ToLower();
}
return list;
}
List<string> result = ToLowerCase(null);
class Talker
{
public virtual void HandleMessage(string message)
{
Console.WriteLine(message);
}
}
class Counter : Talker
{
int _counter = 0;
public Counter()
{
}
public override void HandleMessage(string message)
{
Console.WriteLine(message + " : " + _counter++);
}
}
class Program
{
public static void Main()
{
Talker talker1 = new Counter();
Talker talker2 = new Talker();
talker1.HandleMessage("Yay!");
talker1.HandleMessage("Yay!");
talker2.HandleMessage("Yay!");
talker2.HandleMessage("Yay!");
}
}
class Fancy
{
public delegate void CallbackCb();
List<CallbackCb> _cbs = new List<CallbackCb>();
public Fancy()
{
}
public void AddCallback(CallbackCb cb)
{
_cbs.Add(cb);
}
public void InvokeCallbacks()
{
foreach (CallbackCb cb in _cbs)
{
cb();
}
}
}
class Program
{
public static void Function1()
{
Console.WriteLine("Foo #1!");
}
public static void Function2()
{
Console.WriteLine("Foo #2!");
}
public static void Function3()
{
Console.WriteLine("Foo #3!");
}
public static void Main()
{
Fancy fancy = new Fancy();
fancy.AddCallback(Function1);
fancy.AddCallback(Function3);
fancy.AddCallback(Function2);
fancy.InvokeCallbacks();
}
}
using Gtk;
class MysteryApp : Window
{
string text = @"All of creation in a math equation?";
public MysteryApp() : base("Modest Mouse")
{
BorderWidth = 8;
SetPosition(WindowPosition.Center);
DeleteEvent += delegate { Application.Quit(); };
Label lyrics = new Label(text);
Add(lyrics);
ShowAll();
}
public static void Main()
{
Application.Init();
new MysteryApp();
Application.Run();
}
How do event-based applications differ from non-event driven applications?
Suppose we wish to build an application that keeps track of how many miles a user walks every day. This application should allow the user to record and view their data using some kind of graphical interface (such as GTK). Describe a Model-View-Controller (MVC) design for this application.