Создать класс, два полиморфных метода#

Модераторы: Hawk, Romeo, Absurd, DeeJayC, WinMain

Ответить
ЛевикТютькин

21 янв 2017, 00:53

Создать класс, два полиморфных метода#
ЛевикТютькин

21 янв 2017, 00:58

ЗАДАНИЕ 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
class Class
{
int[] a;

public Class(int n)
{
a = new int[n];
Random r = new Random();
for (int i = 0; i < a.Length; i++)
{
a = r.Next(10);
}
}
//возвращает строку с четными элементами
public string method()
{
string str = "";
for (int i = 0; i < a.Length; i++)
{
//if (a % 2 == 0)
//{
str = str + a + " ";
//}
}
return str;
}
//возвращает строку с нечетными
public string method(int n)
{
string str = "";
for (int i = 0; i < a.Length; i++)
{
//if (a % 2 == 1)
//{
str = str + a + " ";
//}
}
return str;
}
}
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PraktikaTP
{
public partial class Form1 : Form
{
Class arr;
public Form1()
{
InitializeComponent();
}

private void buttonCreateObj_Click(object sender, EventArgs e)
{
int n=0;
while (n < 10)
{
try
{
n = Convert.ToInt32(textBoxRazmer.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (n < 10)
{
MessageBox.Show("Введите размер массива больше десяти!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
MessageBox.Show("Ok", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

arr = new Class(n);
}

private void buttonMethod1_Click(object sender, EventArgs e)
{
richTextBoxRes.Text = arr.method();
}

private void buttonMethod2_Click(object sender, EventArgs e)
{
richTextBoxRes.Text = arr.method(1);
}
}
}
ЛевикТютькин

21 янв 2017, 01:01

namespace PraktikaTP
{
partial class Form1
{
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Код, автоматически созданный конструктором форм Windows

/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
private void InitializeComponent()
{
this.labelRasmer = new System.Windows.Forms.Label();
this.textBoxRazmer = new System.Windows.Forms.TextBox();
this.buttonCreateObj = new System.Windows.Forms.Button();
this.buttonMethod1 = new System.Windows.Forms.Button();
this.buttonMethod2 = new System.Windows.Forms.Button();
this.richTextBoxRes = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// labelRasmer
//
this.labelRasmer.AutoSize = true;
this.labelRasmer.Location = new System.Drawing.Point(23, 18);
this.labelRasmer.Name = "labelRasmer";
this.labelRasmer.Size = new System.Drawing.Size(96, 13);
this.labelRasmer.TabIndex = 0;
this.labelRasmer.Text = "Размер массива:";
//
// textBoxRazmer
//
this.textBoxRazmer.Location = new System.Drawing.Point(125, 15);
this.textBoxRazmer.Name = "textBoxRazmer";
this.textBoxRazmer.Size = new System.Drawing.Size(100, 20);
this.textBoxRazmer.TabIndex = 1;
ЛевикТютькин

21 янв 2017, 01:02

//
// buttonCreateObj
//
this.buttonCreateObj.Location = new System.Drawing.Point(125, 45);
this.buttonCreateObj.Name = "buttonCreateObj";
this.buttonCreateObj.Size = new System.Drawing.Size(100, 27);
this.buttonCreateObj.TabIndex = 2;
this.buttonCreateObj.Text = "Создать массив";
this.buttonCreateObj.UseVisualStyleBackColor = true;
this.buttonCreateObj.Click += new System.EventHandler(this.buttonCreateObj_Click);
//
// buttonMethod1
//
this.buttonMethod1.Location = new System.Drawing.Point(24, 45);
this.buttonMethod1.Name = "buttonMethod1";
this.buttonMethod1.Size = new System.Drawing.Size(95, 27);
this.buttonMethod1.TabIndex = 3;
this.buttonMethod1.Text = "Метод \"1\"";
this.buttonMethod1.UseVisualStyleBackColor = true;
this.buttonMethod1.Click += new System.EventHandler(this.buttonMethod1_Click);
//
// buttonMethod2
//
this.buttonMethod2.Location = new System.Drawing.Point(24, 78);
this.buttonMethod2.Name = "buttonMethod2";
this.buttonMethod2.Size = new System.Drawing.Size(95, 27);
this.buttonMethod2.TabIndex = 4;
this.buttonMethod2.Text = "Метож \"2\"";
this.buttonMethod2.UseVisualStyleBackColor = true;
this.buttonMethod2.Click += new System.EventHandler(this.buttonMethod2_Click);
//
// richTextBoxRes
//
this.richTextBoxRes.Location = new System.Drawing.Point(231, 15);
this.richTextBoxRes.Name = "richTextBoxRes";
this.richTextBoxRes.Size = new System.Drawing.Size(312, 90);
this.richTextBoxRes.TabIndex = 5;
this.richTextBoxRes.Text = "Результат";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(553, 141);
this.Controls.Add(this.richTextBoxRes);
this.Controls.Add(this.buttonMethod2);
this.Controls.Add(this.buttonMethod1);
this.Controls.Add(this.buttonCreateObj);
this.Controls.Add(this.textBoxRazmer);
this.Controls.Add(this.labelRasmer);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label labelRasmer;
private System.Windows.Forms.TextBox textBoxRazmer;
private System.Windows.Forms.Button buttonCreateObj;
private System.Windows.Forms.Button buttonMethod1;
private System.Windows.Forms.Button buttonMethod2;
private System.Windows.Forms.RichTextBox richTextBoxRes;
}
}
ЛевикТютькин

21 янв 2017, 01:05

ЗАДАНИЕ 2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
class Class : Class1
{
int[] a;

//возвращает строку с четными элементами
public override string method()
{
string str = "";
for (int i = 0; i < a.Length; i++)
{
if (a % 2 == 0)
{
str += a + " ";
}
}
return str;
}
public Class(int n)
{
Random r = new Random();
a = new int[r.Next(10) + 10];
for (int i = 0; i < a.Length; i++)
{
a = r.Next(10) + n;
}
}
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
abstract class Class1
{
public abstract string method();
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
class Class2 : Class1
{

int[] a;

//возвращает строку с нечетными элементами
public override string method()
{
string str = "";
for (int i = 0; i < a.Length; i++)
{
if (a % 2 == 1)
{
str += a + " ";
}
}
return str;
}

public Class2(int n)
{
Random r = new Random();
a = new int[r.Next(10) + 10];
for (int i = 0; i < a.Length; i++)
{
a = r.Next(10) + n;
}
}

}
}
ЛевикТютькин

21 янв 2017, 01:07

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PraktikaTP
{
public partial class Form1 : Form
{
List<Class1> list;
int currentNumber;

public Form1()
{
InitializeComponent();
//пришлось запилить абстрактный класс, чтобы в список можно было добавлять объекты всех его наследников
//хз как сделать по-другому
list = new List<Class1>();
Random r = new Random();
int n = r.Next(5) + 5;

//добавление экземпляров первого класса
for (currentNumber = 0; currentNumber < n; currentNumber++)
{
list.Add(new Class(currentNumber));
}
//добавление экземпляров первого класса
n += r.Next(5) + 5;
for (; currentNumber < n; currentNumber++)
{
list.Add(new Class2(currentNumber));
}
//заполнение listBox
for (int i = 0; i < list.Count; i++)
{
listBox1.Items.Add("" + i);
}
currentNumber = 0;
listBox1.SelectedIndex = currentNumber;
}

private void buttonNext_Click(object sender, EventArgs e)
{
if (currentNumber+1 < list.Count){
currentNumber++;
listBox1.SelectedIndex = currentNumber;
}

}

private void buttonPrev_Click(object sender, EventArgs e)
{
if (currentNumber - 1 > -1)
{
currentNumber--;
listBox1.SelectedIndex = currentNumber;
}
}

private void buttonMeth_Click(object sender, EventArgs e)
{
richTextBoxRes.Text = list[currentNumber].method();
}


private void button1_Click(object sender, EventArgs e)
{
richTextBoxRes.Text = null;
for (int i = 0; i < list.Count; i++){
richTextBoxRes.Text += i+". "+list.method()+"\n";
}

}

}
}
ЛевикТютькин

21 янв 2017, 01:09

namespace PraktikaTP
{
partial class Form1
{
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Код, автоматически созданный конструктором форм Windows

/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
private void InitializeComponent()
{
this.richTextBoxRes = new System.Windows.Forms.RichTextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.buttonNext = new System.Windows.Forms.Button();
this.buttonPrev = new System.Windows.Forms.Button();
this.buttonMeth = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBoxRes
//
this.richTextBoxRes.Location = new System.Drawing.Point(12, 12);
this.richTextBoxRes.Name = "richTextBoxRes";
this.richTextBoxRes.Size = new System.Drawing.Size(207, 264);
this.richTextBoxRes.TabIndex = 5;
this.richTextBoxRes.Text = "Результат";
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(243, 12);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(227, 264);
this.listBox1.TabIndex = 6;
//
// buttonNext
//
this.buttonNext.Location = new System.Drawing.Point(395, 282);
this.buttonNext.Name = "buttonNext";
this.buttonNext.Size = new System.Drawing.Size(75, 36);
this.buttonNext.TabIndex = 7;
this.buttonNext.Text = ">>";
this.buttonNext.UseVisualStyleBackColor = true;
this.buttonNext.Click += new System.EventHandler(this.buttonNext_Click);
ЛевикТютькин

21 янв 2017, 01:10

//
// buttonPrev
//
this.buttonPrev.Location = new System.Drawing.Point(243, 282);
this.buttonPrev.Name = "buttonPrev";
this.buttonPrev.Size = new System.Drawing.Size(75, 36);
this.buttonPrev.TabIndex = 8;
this.buttonPrev.Text = "<<";
this.buttonPrev.UseVisualStyleBackColor = true;
this.buttonPrev.Click += new System.EventHandler(this.buttonPrev_Click);
//
// buttonMeth
//
this.buttonMeth.Location = new System.Drawing.Point(12, 281);
this.buttonMeth.Name = "buttonMeth";
this.buttonMeth.Size = new System.Drawing.Size(92, 37);
this.buttonMeth.TabIndex = 9;
this.buttonMeth.Text = "Применить к одному";
this.buttonMeth.UseVisualStyleBackColor = true;
this.buttonMeth.Click += new System.EventHandler(this.buttonMeth_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(127, 281);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(92, 37);
this.button1.TabIndex = 10;
this.button1.Text = "Применить ко всем";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(482, 325);
this.Controls.Add(this.button1);
this.Controls.Add(this.buttonMeth);
this.Controls.Add(this.buttonPrev);
this.Controls.Add(this.buttonNext);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.richTextBoxRes);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.RichTextBox richTextBoxRes;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button buttonNext;
private System.Windows.Forms.Button buttonPrev;
private System.Windows.Forms.Button buttonMeth;
private System.Windows.Forms.Button button1;

}
}
Ответить