c. alice and the cake
时间: 2023-04-22 19:02:52 浏览: 71
c. Alice和蛋糕
Alice是一个小女孩,她非常喜欢吃甜食,特别是蛋糕。有一天,她看到了一块非常美味的蛋糕,就想去尝一尝。但是,这块蛋糕被放在了一个很高的架子上,她够不着。
Alice不甘心,她想出了一个办法。她找来了一只小猫,让它帮忙把蛋糕拿下来。小猫很聪明,它跳上了架子,把蛋糕拿了下来,然后把它递给了Alice。
Alice高兴极了,她吃了一口蛋糕,觉得非常好吃。她决定把剩下的蛋糕分给小猫和其他的小朋友们一起分享。
大家一起吃蛋糕,度过了一个愉快的下午。从此以后,Alice和小猫成为了好朋友,他们经常一起玩耍,分享美食。
相关问题
/** 小明正在分析一本小说中的人物相关性。 他想知道在小说中 Alice 和 Bob有多少次同时出现。 更准确的说,小明定义 Alice 和 Bob“同时出现”的意思是: 在小说文本中 Alice 和 Bob 之间不超过 K 个字符。 例如以下文本: This is a story about Alice and Bob. Alice wants to send a private message to Bob. 假设 K = 20,则 Alice 和 Bob 同时出现了 2 次, 分别是“Alice and Bob”和“Bob. Alice”。 前者 Alice 和 Bob 之间有 5 个字符,后者有 2 个字符。 注意: 1. Alice 和 Bob 是大小写敏感的,alice 或 bob 等并不计算在内。 2. Alice 和 Bob 应为单独的单词,前后可以有标点符号和空格,但是不能有字母。 例如 Bobbi 並不算出现了 Bob。
*//**
* 统计小说中 Alice 和 Bob 同时出现的次数
* @param {string} novel - 小说文本
* @param {number} k - 字符间隔数
* @returns {number} - Alice 和 Bob 同时出现的次数
*/
function countAliceAndBob(novel, k) {
const alice = 'Alice';
const bob = 'Bob';
let count = 0;
// 将小说中的标点符号和空格替换为空格
novel = novel.replace(/[^\w]/g, ' ');
// 将小说文本转换成单词数组
const words = novel.split(' ');
for (let i = 0; i < words.length; i++) {
if (words[i] === alice || words[i] === bob) {
// 如果当前单词是 Alice 或 Bob,则查找与其距离不超过 k 的单词中是否有另一个人名
for (let j = i + 1; j < words.length && j <= i + k; j++) {
if (words[j] === alice || words[j] === bob) {
// 如果找到了,则计数器加一,并退出内层循环
count++;
break;
}
}
}
}
return count;
}
class Program { static void Main(string[ ] args) { Person David = new Person(); Person Alice = new Person(); // Initialize David David.age = 21; David.name = "David"; David.weight = 185.4; David.height = 72.3; // Initialize Alice Alice.age = 18; Alice.name = "Alice"; Alice.weight = 125.7; Alice.height = 67.1; // print some values Console.WriteLine("David’s age = {0}; David’s weight = {1}",David.age, David.weight); Console.WriteLine("Alice’s age = {0}; Alice’s weight = {1}", Alice.age, Alice.weight); } } Add this code to Program.cs. Compile and run it. Modify this code to print all of the instance variables of David and Alice. Run and understand the code. Add comments to record your understanding. Encapsulation in C#: In the previous example, we accessed the characteristics of the Person Class directly because the attributes (e.g. name, age, etc.) are defined as ‘public’. This is not a good practice and actually should be avoided in order to have ‘encapsulation’ – one of three most important features of object oriented design. The better option and practice is to define the attributes ‘private’ and then design some public methods to access them, normally ‘getXXX’ and ‘setXXX’. Amend the codes above accordingly. (Task 1.2)
class Person
{
private int age;
private string name;
private double weight;
private double height;
public int GetAge()
{
return age;
}
public void SetAge(int newAge)
{
age = newAge;
}
public string GetName()
{
return name;
}
public void SetName(string newName)
{
name = newName;
}
public double GetWeight()
{
return weight;
}
public void SetWeight(double newWeight)
{
weight = newWeight;
}
public double GetHeight()
{
return height;
}
public void SetHeight(double newHeight)
{
height = newHeight;
}
}
class Program
{
static void Main(string[] args)
{
Person David = new Person();
Person Alice = new Person();
// Initialize David
David.SetAge(21);
David.SetName("David");
David.SetWeight(185.4);
David.SetHeight(72.3);
// Initialize Alice
Alice.SetAge(18);
Alice.SetName("Alice");
Alice.SetWeight(125.7);
Alice.SetHeight(67.1);
// Print all instance variables of David
Console.WriteLine("David: age = {0}, name = {1}, weight = {2}, height = {3}", David.GetAge(), David.GetName(), David.GetWeight(), David.GetHeight());
// Print all instance variables of Alice
Console.WriteLine("Alice: age = {0}, name = {1}, weight = {2}, height = {3}", Alice.GetAge(), Alice.GetName(), Alice.GetWeight(), Alice.GetHeight());
}
}
In the modified code, the attributes (age, name, weight, height) of the Person class are defined as private. We have added public methods (getters and setters) to access and modify these attributes. By encapsulating the attributes, we achieve data hiding and provide controlled access to them.
The getters (e.g., GetAge, GetName, etc.) are used to retrieve the values of the private attributes, and the setters (e.g., SetAge, SetName, etc.) are used to set new values for the attributes.
In the Main method, we now use the setters to initialize the attributes for David and Alice. When printing the values, we use the getters to retrieve the attribute values.
Encapsulation is a fundamental concept in object-oriented programming that promotes data integrity and helps maintain code flexibility and reusability. By encapsulating the attributes, we can control access to them and enforce data validation or business logic if needed.