You are responsible for all material covered through the end of Week 3.
int numItems = '4';
int[] values = new int[4];
for (int i = 0; i < numItems; i++)
{
values[i] = 0;
}
Consider the errors above. Can they be found at compile time found at runtime?
The following program fails to compile. Why?
using System;
public static void Main()
{
Console.WriteLine("Hello, World");
}
Suppose we fix the above code. What commands can we use to compile and run it?
Consider the code in question 3. Suppose Sally builds hello.exe on one of the lab linux machines. Can Billy run the executable on his windows laptop? Why or why not?
The following class has bad encapsulation. Why?
class A
{
public int a = 0;
public A(int startVal)
{
a = startVal;
}
public int GetA()
{
return a;
}
}
How can we re-write the class in question 6 to have better encapsulation?
What is the output of the following program?
class A
{
protected int _value = 1;
public A()
{
}
public virtual int GetValue()
{
return _value;
}
}
class B : A
{
public B(int startVal)
{
_value = startVal;
}
public override int GetValue()
{
return _value * 2;
}
}
class MainClass
{
public static void Main()
{
A a1 = new A();
A a2 = new B(2);
Console.WriteLine("a1 has value: "+a1.GetValue());
Console.WriteLine("a2 has value: "+a2.GetValue());
}
}
public static void Solve(double v1, double v2, double v3)
{
if (v1 == 0) {
Console.WriteLine("The value is "+(-v3/v2));
}
else
{
v1 = v1 * 2;
double tmp = v2;
v2 = v2 * v2 - 2 * v1 * v3;
if (v2 > 0) {
v2 = Math.Sqrt(v2);
Console.WriteLine("The value is "+(-tmp + v2)/v1);
Console.WriteLine("The value is "+(-tmp - v2)/v1);
}
else if (v2 == 0)
{
v2 = Math.Sqrt(v2);
Console.WriteLine("The value is "+(-tmp)/v1);
}
}
}
// requires: startInterval <= endInterval
// effects: returns true if value is within the interval defined by [startInterval, endInterval]
// (inclusive!!) and false otherwise
bool inInterval(float value, float startInterval, float endInterval)