214k views
1 vote
Test 7 string Test7(sbyte offset, string message) For this test you will end up writing your own class, called TextCodec, make an instance of it, call the Encode() method (described below), and return the result of that method. You can put the TextCodec class declaration either: in its own file called TextCodec.cs, or you can put it in the Submission.cs file with your Submission class within the same namespace scope. 5 COP1000 (PG1) - - PROGRAMMING I LAB7A To create your TextCodec class, you will add the following access modifier, fields, constructor and methods. Your TextCodec class must be declared as public. When you create your class called TextCodec, you must make the class itself public. That means, you must put the keyword public directly before the words class TextCodec. For example: public class TextCodec { // fields of this class // constructors of this class // methods of this class } Your TextCodec class must have 1 member variable (field), which is: • an sbyte, which holds a numeric offset You can name the field any name you want. Your TextCodec class must have an overloaded constructor, which takes the offset variable, that will be assigned to the field of the class: public TextCodec(sbyte offset) Your TextCodec class must have the following method: public string Encode(string message) The purpose of the Encode() method is take the message passed into the method and encode it into an unreadable string using the offset field stored in the class. To accomplish this, we will provide you with the following code to use as the body of your Encode method: StringBuilder sb = new StringBuilder(message); for (int i = 0; i < message.Length; i++) { sb[i] = (char)(sb[i] + mOffset); } return sb.ToString(); This method uses a StringBuilder class because it is more efficient to construct strings with a StringBuilder than simply adding strings together. The StringBuilder class is declared in the System.Text namespace. For this Test, when you have finished adding the above requested information to your TextCodec class, you will then make an instance object of type TextCodec, calling its overloaded constructor passing the offset variable given to you in this Test, and then you will call the Encode() method passing the message variable given to you in this Test, and return the result of the Encode() method. For example: TextCodec codec = new TextCodec(offset); // call the Encode() method passing message and return the result

User Domarm
by
6.9k points

1 Answer

2 votes

Final answer:

To answer the question, a public class named TextCodec with a sbyte offset field and a public Encode method is created. The Encode method modifies a given string by a specified offset to produce an encoded string.

Step-by-step explanation:

The question involves creating a TextCodec class in C# that includes a sbyte field for an offset and a public method named Encode (). The Encode method will use the field to alter the characters of a string according to the provided offset and return the encoded string. A constructor is also required to initialize the offset field. Here is a simplified example of how the TextCodec class might be implemented:

public class TextCodec

{

private sbyte Moff set.

public TextCodec(sbyte offset)

{

Moff set = offset.

}

public string Encode (string message)

{

StringBuilder sb = new StringBuilder(message).

for (int i = 0; i < message. Length; i++)

{

sb[i] = (char)(sb[i] + Moff set).

}

return sb.ToString().

}

}

To use this class:

Textmode codec = new Textmode(offset).

string encoded Message = condescended(message).

User Valentin Briand
by
7.9k points