实验3 面向对象编程(1)
【实验目的】
1.掌握类和对象的创建。
2.掌握类成员访问修饰符的含义。
3.掌握类成员:字段、构造函数、属性、方法(参数、重载、重写与覆盖)的使用。
4.掌握方法中参数的4种参数传递方式。
5.掌握静态方法和实例方法。
【实验内容】
1、3486简单类及成员实例。定义了如下图所示类Student,根据下图和给出代码,补写缺失的代码。
using System;
namespace sample
{
class Student
{
public string studentid;//学号
public string studentname;//姓名
private string birthplace;//籍贯
private DateTime birthdate;//出生日期
/////////////////////////////////////////////////////////////////
//请填写代码,实现类的无参和有参构造函数、
//属性StudentId、StudentName、BirthPlace、BirthDate、Age
public Student(string StudentId, string StudentName) {
studentid = StudentId;
studentname = StudentName;
}
public DateTime BirthDate {
get { return birthdate; }
set { birthdate = value; }
}
public string BirthPlace {
get { return birthplace; }
set { birthplace = value; }
}
public string StudentName {
get { return studentname; }
}
public string StudentId {
get { return studentid; }
}
public string Age {
get {
DateTime now = new DateTime();
now = DateTime.Parse("2021-12-10");
TimeSpan age1 = now - birthdate;
int age = age1.Days / 365;
return age.ToString();
}
}
/////////////////////////////////////////////////////////////////
}
class Program
{
static void Main(string[] args)
{
Student zs = new Student("201753501234", "zs");
zs.BirthDate = DateTime.Parse("1988-12-10");
zs.BirthPlace = "jinan";
string s = "name:{0},no:{1},native:{2},age:{3}";
Console.WriteLine(s, zs.StudentName, zs.StudentId, zs.BirthPlace, zs.Age);
}
}
}
2、3454 假定已经获取题库中的试题号,并存放在数组arrayKT中。例如, int [] arrayKT={10,13,18,19,20,22,30,31}。定义一个静态成员方法,该方法实现从上述数组中随机抽出n(n=arrayKT.Length-1)道考题,并组成一个考题字符串。比如,随机从arrayKT中抽取n题组成考题字符串:“10,13,18,20,22,30,31”。要求,组成考题字符串中考题不重复,输出所有可能的字符串。
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace Program0
{
class Program
{
static void Main(string[] args)
{
int n;
string s1 = Console.ReadLine();
int.TryParse(s1, out n);
string s2 = Console.ReadLine();
string[] sp = s2.Split(' ');
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int p = n - i - 1;
if (j != p)
Console.Write(sp[j] + ' ');
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
3、3445 假设有一个GetNumber方法(参数为字符串strSource),编写一个静态方法可以用来统计字符串strSource中数字字符的个数。
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
namespace Program0
{
class Program
{
static void Main(string[] args)
{
string strSource = Console.ReadLine();
Console.WriteLine(GetNumber(strSource));
Console.ReadKey();
}
static int GetNumber(string strSource)
{
int sum=0;
for (int i = 0; i < strSource.Length; i++)
{
if (strSource[i] >= '0' && strSource[i] <= '9')
sum++;
}
return sum;
}
}
}
4、3451 编写一个实例方法Method01。该方法使用Random类随机产生n个3位数字(如636)的随机正整数,并把产生的随机数存入数组中并输出该数组int num= Convert.ToInt32(Console.ReadLine())。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num = Convert.ToInt32(Console.ReadLine());
Program.Method01(num);
Console.ReadKey();
}
////////////////////////
////////////////在此处填写代码
public static void Method01(int num){
Random rd = new Random();
int s;
for (int j = 0; j < num; j++)
{
s = rd.Next(100, 999);
Console.WriteLine(s.ToString());
}
}
/////////////////////
}
}
5、3446 编写一个实例方法getCountChar方法。该方法参数有两个,第一个参数可以是字符串s,第二个参数为字符c,方法返回值为第二个参数在第一个参数中出现次数。例如,getCountChar (“6221982”,‘2’)返回值为3。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
char ch = (char)Console.Read();
Program pro = new Program();
Console.WriteLine(pro.getCountChar(str, ch));
Console.ReadKey();
}
public int getCountChar(String s, char c){
int num = 0;
for (int i = 0; i < s.Length; i++) {
if(s[i]==c)
num+=1;
}
return num;
}
}
}