Нужно разобраться с кодами - C#
Формулировка задачи:
// Проанализируйте код и исправьте ошибки using System; class MyClass { private int alpha; // private access explicitly specified int beta; // private access by default public int gamma; // public access /* Methods to access alpha and beta. It is OK for a member of a class to access a private member of the same class. */ public void setAlpha(int a) { alpha = a; } public int getAlpha() { return alpha; } public void setBeta(int a) { beta = a; } public int getBeta() { return beta; } } class AccessDemo { public static void Main() { MyClass ob = new MyClass(); ob.setAlpha(-99); ob.setBeta(19); Console.WriteLine("ob.alpha is " + ob.getAlpha()); Console.WriteLine("ob.beta is " + ob.getBeta()); ob.alpha = 10; ob.beta = 9; Console.WriteLine("ob.alpha is " + ob.alpha()); Console.WriteLine("ob.beta is " + ob.beta()); ob.gamma = 99; Console.WriteLine("ob.gamma is " + ob.gamma()); } }// Проанализируйте код и объясните результаты using System; class MyClass { int alpha, beta; public MyClass(int i, int j) { alpha = i; beta = j; } public bool sameAs(MyClass ob) { if((ob.alpha == alpha) & (ob.beta == beta)) return true; else return false; } public void copy(MyClass ob) { alpha = ob.alpha; beta = ob.beta; } public void show() { Console.WriteLine("alpha: {0}, beta: {1}", alpha, beta); } } class PassOb { public static void Main() { MyClass ob1 = new MyClass(4, 5); MyClass ob2 = new MyClass(6, 7); Console.Write("ob1: "); ob1.show(); Console.Write("ob2: "); ob2.show(); if(ob1.sameAs(ob2)) Console.WriteLine("ob1 and ob2 have the same values."); else Console.WriteLine("ob1 and ob2 have different values."); Console.WriteLine(); ob1.copy(ob2); Console.Write("ob1 after copy: "); ob1.show(); if(ob1.sameAs(ob2)) Console.WriteLine("ob1 and ob2 have the same values."); else Console.WriteLine("ob1 and ob2 have different values."); } }  // Проанализируйте код и объясните результаты using System; class Test { /* This method causes no change to the arguments used in the call. */ public void noChange(int i, int j) { i = i + j; j = -j; Console.WriteLine("i and j: " + i + " " + j); } } class CallByValue { public static void Main() { Test ob = new Test(); int a = 15, b = 20; Console.WriteLine("a and b before call: " + a + " " + b); ob.noChange(a, b); Console.WriteLine("a and b after call: " + a + " " + b); } }// Проанализируйте код и объясните результаты using System; class Test { public int a, b; public Test(int i, int j) { a = i; b = j; } /* Pass an object. Now, ob.a and ob.b in object used in the call will be changed. */ public void change(Test ob) { ob.a = ob.a + ob.b; ob.b = -ob.b; } } class CallByRef { public static void Main() { Test ob = new Test(15, 20); Console.WriteLine("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.change(ob); Console.WriteLine("ob.a and ob.b after call: " + ob.a + " " + ob.b); } }// Проанализируйте код и объясните результаты using System; class RefTest { public void sqr(ref int i) { i = i * i; } } class RefDemo { public static void Main() { RefTest ob = new RefTest(); int a = 10; Console.WriteLine("a before call: " + a); ob.sqr(ref a); // notice the use of ref Console.WriteLine("a after call: " + a); } }// Проанализируйте код и объясните результаты using System; class Swap { public void swap(ref int a, ref int b) { int t; t = a; a = b; b = t; } } class SwapDemo { public static void Main() { Swap ob = new Swap(); int x = 10, y = 20; Console.WriteLine("x and y before call: " + x + " " + y); ob.swap(ref x, ref y); Console.WriteLine("x and y after call: " + x + " " + y); } }// Проанализируйте код и объясните результаты using System; class Decompose { public int parts(double n, out double frac) { int whole; whole = (int) n; frac = n - whole; // pass fractional part back through frac return whole; // return integer portion } } class UseOut { public static void Main() { Decompose ob = new Decompose(); int i; double f; i = ob.parts(10.125, out f); Console.WriteLine("Integer portion is " + i); Console.WriteLine("Fractional part is " + f); } }// Проанализируйте код и объясните результаты using System; class RefSwap { int a, b; public RefSwap(int i, int j) { a = i; b = j; } public void show() { Console.WriteLine("a: {0}, b: {1}", a, b); } public void swap(ref RefSwap ob1, ref RefSwap ob2) { RefSwap t; t = ob1; ob1 = ob2; ob2 = t; } } class RefSwapDemo { public static void Main() { RefSwap x = new RefSwap(1, 2); RefSwap y = new RefSwap(3, 4); Console.Write("x before call: "); x.show(); Console.Write("y before call: "); y.show(); Console.WriteLine(); // exchange the objects to which x and y refer x.swap(ref x, ref y); Console.Write("x after call: "); x.show(); Console.Write("y after call: "); y.show(); } }  // Проанализируйте код и объясните результаты using System; class MyClass { public int Val(params int[] nums) { int m; if(nums.Length == 0) { Console.WriteLine("Error: no arguments."); return 0; } m = nums[0]; for(int i=1; i < nums.Length; i++) if(nums[i] < m) m = nums[i]; return m; } } class ParamsDemo { public static void Main() { MyClass ob = new MyClass(); int result; int[] args = { 45, 67, 34, 9, 112, 8 }; result = ob.Val(args); Console.WriteLine("Result is " + result); } }// Проанализируйте код и объясните результаты using System; class Rect { int width; int height; public Rect(int w, int h) { width = w; height = h; } public int area() { return width * height; } public void show() { Console.WriteLine(width + " " + height); } /* Return a rectangle that is a specified factor larger than the invoking rectangle. */ public Rect enlarge(int factor) { return new Rect(width * factor, height * factor); } } class RetObj { public static void Main() { Rect r1 = new Rect(4, 5); Console.Write("Dimensions of r1: "); r1.show(); Console.WriteLine("Area of r1: " + r1.area()); Console.WriteLine(); // create a rectange that is twice as big as r1 Rect r2 = r1.enlarge(2); Console.Write("Dimensions of r2: "); r2.show(); Console.WriteLine("Area of r2 " + r2.area()); } }// Проанализируйте код, объясните результаты using System; class Rect { public int width; public int height; public Rect(int w, int h) { width = w; height = h; } public int area() { return width * height; } } class UseRect { public static void Main() { Rect r1 = new Rect(4, 5); Rect r2 = new Rect(7, 9); Console.WriteLine("Area of r1: " + r1.area()); Console.WriteLine("Area of r2: " + r2.area()); } }// Проанализируйте код и объясните результаты using System; class Overload { public void ovlDemo() { Console.WriteLine("No parameters"); } // Overload ovlDemo for one integer parameter. public void ovlDemo(int a) { Console.WriteLine("One parameter: " + a); } // Overload ovlDemo for two integer parameters. public int ovlDemo(int a, int b) { Console.WriteLine("Two parameters: " + a + " " + b); return a + b; } // Overload ovlDemo for two double parameters. public double ovlDemo(double a, double b) { Console.WriteLine("Two double parameters: " + a + " "+ b); return a + b; } } class OverloadDemo { public static void Main() { Overload ob = new Overload(); int resI; double resD; // call all versions of ovlDemo() ob.ovlDemo(); Console.WriteLine(); ob.ovlDemo(2); Console.WriteLine(); resI = ob.ovlDemo(4, 6); Console.WriteLine("Result of ob.ovlDemo(4, 6): " + resI); Console.WriteLine(); resD = ob.ovlDemo(1.1, 2.32); Console.WriteLine("Result of ob.ovlDemo(1.1, 2.2): " + resD); } } // Проанализируйте код и объясните результаты using System; class IfDemo { public static void Main() { int a, b, c; a = 2; b = 3; if (a < b) Console.WriteLine("a is less than b"); // this won't display anything if (a == b) Console.WriteLine("you won't see this"); Console.WriteLine(); c = a - b; // c contains -1 Console.WriteLine("c contains -1"); if (c >= 0) Console.WriteLine("c is non-negative"); if (c < 0) Console.WriteLine("c is negative"); Console.WriteLine(); c = b - a; // c now contains 1 Console.WriteLine("c contains 1"); if (c >= 0) Console.WriteLine("c is non-negative"); if (c < 0) Console.WriteLine("c is negative"); } }// Проанализируйте код и объясните результаты using System; class IfDemo { public static void Main() { int a, b, c; a = 2; b = 3; if (a < b) Console.WriteLine("a is less than b"); // this won't display anything if (a == b) Console.WriteLine("you won't see this"); Console.WriteLine(); c = a - b; // c contains -1 Console.WriteLine("c contains -1"); if (c >= 0) Console.WriteLine("c is non-negative"); if (c < 0) Console.WriteLine("c is negative"); Console.WriteLine(); c = b - a; // c now contains 1 Console.WriteLine("c contains 1"); if (c >= 0) Console.WriteLine("c is non-negative"); if (c < 0) Console.WriteLine("c is negative"); } }  // Проанализируйте код и объясните результаты using System; class ProdSum { static void Main() { int prod; int sum; int i; sum = 0; prod = 1; for (i = 1; i <= 5; i++) { sum = sum + i; prod = prod * i; } Console.WriteLine("Sum is " + sum); Console.WriteLine("Product is " + prod); } }// Проанализируйте код и объясните результаты using System; class PreDemo { public static void Main() { int x, y; int i; x = 1; Console.WriteLine("Series generated using y = x + ++x;"); for (i = 0; i < 10; i++) { y = x + ++x; // prefix ++ Console.WriteLine(y + " "); } Console.WriteLine(); } }// Проанализируйте код и объясните результаты using System; class PostDemo { public static void Main() { int x, y; int i; x = 1; Console.WriteLine("Series generated using y = x + x++;"); for (i = 0; i < 10; i++) { y = x + x++; // postfix ++ Console.WriteLine(y + " "); } Console.WriteLine(); } }// Проанализируйте код и объясните результаты // Create an implication operator in C#. using System; class Implication { public static void Main() { bool p = false, q = false; int i, j; for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { if (i == 0) p = true; if (i == 1) p = false; if (j == 0) q = true; if (j == 1) q = false; int b = (!p | q)?1:0; Console.WriteLine(i + " " + j + " " + b); Console.WriteLine(); } } } }// Проанализируйте код и объясните результаты using System; class LogicalFunction { public static void Main() { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int b = (i*j>0)?1:0; Console.WriteLine(i + " " + j + " " + b); Console.WriteLine(); } } } }// Проанализируйте код и объясните результаты using System; class LogicalFunction { public static void Main() { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int b = (i+j>0)?1:0; Console.WriteLine(i + " " + j + " " + b); Console.WriteLine(); } } } }// Проанализируйте код и объясните результаты using System; class Ladder { public static void Main() { int num; for (num = 2; num < 12; num++) { if ((num % 2) == 0) Console.WriteLine("Smallest factor of " + num + " is 2."); else if ((num % 3) == 0) Console.WriteLine("Smallest factor of " + num + " is 3."); else if ((num % 5) == 0) Console.WriteLine("Smallest factor of " + num + " is 5."); else if ((num % 7) == 0) Console.WriteLine("Smallest factor of " + num + " is 7."); else Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7."); } } }// Проанализируйте код, исправьте ошибки, и объясните результаты using System; class SwitchDemo { public static void Main() { int i; for (i = 0; i < 10; i++) switch (i) { case 0: Console.WriteLine("i is zero"); case 1: Console.WriteLine("i is one"); case 2: Console.WriteLine("i is two"); case 3: Console.WriteLine("i is three"); case 4: Console.WriteLine("i is four"); default: Console.WriteLine("i is five or more"); } } }// Проанализируйте код, исправьте ошибки, и объясните результаты using System; class SwitchDemo { public static void Main() { double i; for (i = 0; i < 10; i++) switch (i) { case 0: Console.WriteLine("i is zero"); break; case 1: Console.WriteLine("i is one"); break; case 2: Console.WriteLine("i is two"); break; case 3: Console.WriteLine("i is three"); break; case 4: Console.WriteLine("i is four"); break; default: Console.WriteLine("i is five or more"); } } }// Проанализируйте код, объясните результаты using System; class SwitchDemo2 { public static void Main() { char ch; for (ch = 'A'; ch <= 'z'; ch++) switch (ch) { case 'A': case 'a': Console.WriteLine("ch is A or a"); break; case 'B': case 'b': Console.WriteLine("ch is B or b"); break; case 'C': case 'c': Console.WriteLine("ch is C or c"); break; case 'D': case 'd': Console.WriteLine("ch is D or d"); break; case 'E': case 'e': Console.WriteLine("ch is E or e"); break; default: Console.WriteLine(ch+" "+(int)ch); break; } } }// Проанализируйте код, объясните результаты using System; class forDemo { public static void Main() { int i, j; bool done = false; for (i = 0, j = 100; !done; i++, j--) { if (i * i >= j) done = true; Console.WriteLine("i, j: " + i + " " + j); } } }// Проанализируйте код, объясните результаты using System; class ForVar { public static void Main() { int sum = 0; int fact = 1; for (int i = 1; i <= 5; i++) { sum += i; // i is known throughout the loop fact *= i; } Console.WriteLine("sum is " + sum); Console.WriteLine("fact is " + fact); } }// Проанализируйте код, объясните результаты using System; class WhileDemo { public static void Main() { int num; int mag; num = 435679; mag = 0; Console.WriteLine("Number: " + num); while (num > 0) { mag++; num = num / 10; }; Console.WriteLine("Magnitude: " + mag); } }// Проанализируйте код, объясните результаты using System; class Power { public static void Main() { int e; int result; for (int i = 0; i < 11; i++) { result = 1; e = i; while (e > 0) { result *= 2; e--; } Console.WriteLine("2 to the " + i + " power is " + result); } } }// Проанализируйте код, объясните результаты using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants // Display the area per person. public int areaPerPerson() { return area / occupants; } } // Use the return value from areaPerPerson(). class BuildingDemo { public static void Main() { Building house = new Building(); int areaPP; // area per person // assign values to fields in house house.occupants = 4; house.area = 75; house.floors = 2; // obtain area per person for house areaPP = house.areaPerPerson(); Console.WriteLine("house has:\n " + house.floors + " floors\n " + house.occupants + " occupants\n " + house.area + " total area\n " + areaPP + " area per person"); } }// Проанализируйте код, объясните результаты using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants // Display the area per person. public int areaPerPerson() { return area / occupants; } /* Return the maximum number of occupants if each is to have at least the specified minimum area. */ public int maxOccupant(int minArea) { return area / minArea; } } // Use maxOccupant(). class BuildingDemo { public static void Main() { Building house = new Building(); Building office = new Building(); // assign values to fields in house house.occupants = 4; house.area = 150; house.floors = 2; // assign values to fields in office office.occupants = 25; office.area = 420; office.floors = 2; Console.WriteLine("Maximum occupants for house if each has " + 20 + " square metre: " + house.maxOccupant(20)); Console.WriteLine("Maximum occupants for office if each has " + 10 + " square metre: " + office.maxOccupant(10)); } }// Проанализируйте код, объясните результаты using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants public Building(int f, int a, int o) { floors = f; area = a; occupants = o; } // Display the area per person. public int areaPerPerson() { return area / occupants; } /* Return the maximum number of occupants if each is to have at least the specified minum area. */ public int maxOccupant(int minArea) { return area / minArea; } } // Use the parameterized Building constructor. class BuildingDemo { public static void Main() { Building house = new Building(2, 2500, 4); Building office = new Building(3, 4200, 25); Console.WriteLine("Maximum occupants for house if each has " + 300 + " square feet: " + house.maxOccupant(300)); Console.WriteLine("Maximum occupants for office if each has " + 300 + " square feet: " + office.maxOccupant(300)); }
Решение задачи: «Нужно разобраться с кодами»
textual
Листинг программы
using System; class MyClass { private int alpha; // private access explicitly specified int beta; // private access by default public int gamma; // public access /* Methods to access alpha and beta. It is OK for a member of a class to access a private member of the same class. */ public void setAlpha(int a) { alpha = a; } public int getAlpha() { return alpha; } public void setBeta(int a) { beta = a; } public int getBeta() { return beta; } } class AccessDemo { public static void Main() { MyClass ob = new MyClass(); ob.setAlpha(-99); ob.setBeta(19); Console.WriteLine("ob.alpha is {0}", ob.getAlpha()); Console.WriteLine("ob.beta is {0}", ob.getBeta()); ob.SetAlpha(10); ob.SetBeta(9); Console.WriteLine("ob.alpha is {0}", ob.GetAlpha()); Console.WriteLine("ob.beta is {0}", ob.GetBeta()); ob.gamma = 99; Console.WriteLine("ob.gamma is {0}", ob.gamma); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д