Programming | network
1. Write a console application to complete the following functions.
1 Create a class that outputs the class name of the class with a constructor that has no parameters.
2 Add an overloaded constructor with a string parameter that prints the string passed in this constructor.
3 creates an object in the main method that belongs to this class and does not pass arguments.
4 creates another object belonging to this class in the main method, passing a string "this is a string.".
5 declare an array of 5 objects of the type in the main method, but do not actually create an object that is assigned to the array.
6 write out the results that the running program should output.
Troubleshooting
Using System;
Class Test1
{
Public Test1 ()
{
Console.WriteLine (this);
}
Public Test1 (String str)
{
Console.WriteLine (str);
}
public static void Main ()
{
Test1 T1 = new Test1 ();
Test1 t2 = new Test1 ("This is a string.");
test1[] t3 = new TEST1[5];
}
}
Output results:
Test1
This is a string.
2. Write a console application that defines a class MyClass that contains public, private, and protected data members and methods. Then you define a class Mymain that inherits from the MyClass class, puts the main method in Mymain, creates an object in the main method for the MyClass class, and accesses the data members and methods in the class, respectively. Requirements indicate which statements produce compilation errors when attempting to access all class members.
Troubleshooting
Using System;
Class MyClass
{
public int i;
private int J;
protected int k;
public void Method1 ()
{
Console.WriteLine ("Public Method.");
}
private void Method2 ()
{
Console.WriteLine ("Private Method.");
}
protected void method3 ()
{
Console.WriteLine ("Protected Method.");
}
}
Class Mymain:myclass
{
public static void Main ()
{
MyClass t = new MyClass ();
Console.WriteLine ("I={0}", T.I);
Console.WriteLine ("J={0}", T.J); A compilation error occurs and private members are not allowed to access in other classes
Console.WriteLine ("K={0}", T.K); There is a compilation error, you should create the Mymain object,
Accessed through the Mymain object
T.method1 ();
T.METHOD2 (); A compilation error occurs, and the private method does not allow calls in other classes
T.method3 (); There is a compilation error, you should create the Mymain object, and then pass the Mymain
object to invoke the method
}
}
3. Create a class that contains protected data. Create a second class in the same file and manipulate the protected data in the first class in a single method.
Troubleshooting
Using System;
Class Class1
{
protected int i = 5;
protected void MyMethod ()
{
Console.WriteLine ("Protected Method.");
}
}
Class Class2:class1
{
private void Newmethod ()
{
Console.WriteLine (THIS.I);
THIS.I + 10;
Console.WriteLine (THIS.I);
}
public static void Main ()
{
Class2 t = new Class2 ();
T.newmethod ();
}
}
4. Write out the results of the following statements, respectively.
1) Console.WriteLine ("{0}--{0:p}good", 12.34F);
2) Console.WriteLine ("{0}--{0:####}good", 0);
3) Console.WriteLine ("{0}--{0:00000}good", 456);
Troubleshooting
12.34--1,234.00%good
0--good
456--00456good
5. Write a console application that calculates
Requires a precision of 10-8.
Troubleshooting
Using System;
Class Test5
{
public static void Main ()
{
int n = 50;
Double x = 3;
Double s = 0;
Double A = 1;
for (int i = 1; I <= n; i++)
{
a *= i;
S + + Math.pow ( -1, i + 1) * MATH.POW (x, i)/A;
}
Console.WriteLine ("n={0},s={1:0.00000000}", N, s);
}
}
6. Write a console application that receives a string longer than 3 and completes the following functions
1) The length of the output string.
2 The position of the first occurrence of the letter A in the output string.
3 Inserts the substring "Hello" after the 3rd character of the string, outputting the new string.
4 replaces the string "Hello" with "Me" and outputs a new string.
5 separates the string with the character "M" and outputs the separated string.
Troubleshooting
Using System;
Class Test6
{
public static void Main ()
{
String str = "";
while (str. Length <= 3)
{
Console.Write ("Please enter a string longer than 3:");
str = Console.ReadLine ();
}
(1)
Console.WriteLine ("Length of string: {0}", str.) Length);
(2)
int i = str. IndexOf (' a ');
if (i >-1)
{
Console.WriteLine ("The first occurrence of the letter A is: {0}", i);
}
Else
{
Console.WriteLine ("String" does not contain the letter A.) ");
}
(3)
String str1 = str. Insert (3, "Hello"); In the 3rd (initial sequence number is) character match either insert Hello
Console.WriteLine ("Insert Hello Result: {0}", str1);
(4)
String str2 = str1. Replace ("Hello", "Me");
Console.WriteLine ("Replace Hello with me with the result: {0}", str2);
(5)
string[] arr = str2. Split (' m ');
Console.WriteLine ("Separated by M-delimiter string has:");
for (int j = 0; J < arr. Length; J + +)
{
Console.WriteLine (Arr[j]);
}
}
}