# 物件導向程式設計實習 ## 20240124課堂作業 ![image](https://hackmd.io/_uploads/S17ow70Fp.png) ![image](https://hackmd.io/_uploads/r1Y2vX0F6.png) ![image](https://hackmd.io/_uploads/HJhTvQ0Yp.png) ![image](https://hackmd.io/_uploads/rygw5Q0Kp.png) ### 心得 C#前面的部分跟C差不多,不過C#感覺是寫比較底層的,C#是以物件導向為主的,比較期待後面用C#做商務軟體開發。而且根據同學所說,C#跟Android開發的layout都可以用來做UI的,可以用來包裝後端系統,跟我目前在學的東西有一點點關聯 ## 20240221課堂作業 #### 註:vs中文套件好像有問題,打中文會出現"?",所以只能用英文 ### p1 ![image](https://hackmd.io/_uploads/BJvSt0zhT.png) ### p2 ![image](https://hackmd.io/_uploads/B1Pnumvna.png) ```csharp= //20240221 internal class Program { private static void Main(string[] args) { Console.Write("Your name? "); string? name = Console.ReadLine(); Console.Write("Amount of withdrawals:"); int money = Convert.ToInt32(Console.ReadLine()); Console.WriteLine($"Hi! {name},Amount:{money:C0}"); } } ``` ### p3 ![image](https://hackmd.io/_uploads/rkxei7PhT.png) ![image](https://hackmd.io/_uploads/HJNWoXwnp.png) ```csharp! namespace WinFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString(); } private void textBox1_TextChanged(object sender, EventArgs e) { Text = ""; } private void button2_Click(object sender, EventArgs e) { textBox1.Text = "Hello"; } } } ``` ## 20240306課堂作業 ### p1 ![image](https://hackmd.io/_uploads/H1GU_trT6.png) ```csharp= // See https://aka.ms/new-console-template for more information using static System.Console; Console.WriteLine("資訊一甲05江寬泓"); Console.WriteLine(""); int num1 = 1_23_4876; long num2 = 1_23_8009L; long max = Int64.MaxValue; long min = Int64.MinValue; int num3 = 0b1011_110; int num4 = 0b_11111_1011000; int num5 = 0x_FCB64; Console.WriteLine($"Number: {num1:n0},{num2:n0}"); Console.WriteLine($"二進轉十進:{num3:D5},{num4:d5},{num5:d5}"); WriteLine($".NET類別(num1): {num1.GetType()}"); WriteLine($".NET類別(num2): {num2.GetType()}"); WriteLine($"最大值:{max},最小值:{min}"); ``` ### p2 ![image](https://hackmd.io/_uploads/Byyt19rpp.png) ```csharp= // See https://aka.ms/new-console-template for more information using static System.Console; Console.WriteLine("資訊一甲05江寬泓"); Console.WriteLine(""); float num1 = 1.219618565189f; double num2 = 1.978465132798984456; decimal num3 = 1.48978465385463651846m; double x1 = 0.1; double x2 = 0.2; decimal y1 = 0.1m; Decimal y2 = 0.2m; WriteLine($"Float : {num1}"); WriteLine($"Double : {num2}"); WriteLine($"Decimal : {num3}"); WriteLine($"Float : {num1:f4}"); WriteLine($"double佔<{sizeof(double)}>位元組"); WriteLine($"Decimal佔<{sizeof(decimal)}>位元組"); ``` ### p3 ![image](https://hackmd.io/_uploads/r1ZW-qH6p.png) ```csharp= // See https://aka.ms/new-console-template for more information using static System.Console; Console.WriteLine("資訊一甲05江寬泓"); Console.WriteLine(""); const float square = 3.0579f; Write("坪數:"); float area = Convert.ToSingle(ReadLine()); WriteLine($"{area}坪 = {square*area}平方公尺"); const float pi = 3.14f; WriteLine($"圓面積 : {pi * 25.0}"); ``` ## 20240313課堂作業 ### p1 ```csharp= using System.CodeDom.Compiler; namespace WinFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { textBox2.ReadOnly = true; textBox1.MaxLength = 3; textBox1.TabIndex = 0; } private void label1_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { double temp = double.Parse(textBox1.Text); temp = temp * 9 / 5 + 32; textBox2.Text = temp.ToString("f1"); textBox1.Focus(); } catch { textBox2.Text = "請輸入溫度數值"; textBox1.Clear(); } } } } ``` ![image](https://hackmd.io/_uploads/Bk6ATFRTa.png) ### p2 ``` namespace WinFormsApp3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string username = Microsoft.VisualBasic.Interaction.InputBox("請輸入姓名", "輸入"); DialogResult dr = MessageBox.Show(username + "歡迎您!", "歡迎", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Text = username; } private void button1_Click(object sender, EventArgs e) { try { int mon = Int32.Parse(textBox2.Text); label3.Text = $"提款金額:{mon}"; } catch { MessageBox.Show("請輸入整數", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } } } ``` ![image](https://hackmd.io/_uploads/Hy8PpfZy0.png) ![image](https://hackmd.io/_uploads/SJbO6z-JA.png) ## 20240320課堂作業 ### p1 ```csharp= using System.Diagnostics.CodeAnalysis; int sum = 0; int counter = 0; for (; ; ) { counter++; Console.Write("請輸入值作家總"); int num = Int32.Parse(Console.ReadLine()); sum+= num; Console.Write("還要繼續嗎?(y繼續,n離開)"); string r = Console.ReadLine(); if (r == "n") { break; } else { continue; } } Console.WriteLine($"輸入{counter}個數值,合計{sum}"); ``` ![image](https://hackmd.io/_uploads/HJIpHf-10.png) ## 20240327課堂作業 ### p1 ```csharp= // See https://aka.ms/new-console-template for more information string[] name = { "Molly", "Eric", "Johseph", "Peter", "Iron", "Priyanka" }; int[] age = { 24, 26, 24, 26, 28, 25 }; Console.WriteLine("---年齡符合24歲---"); for(int i=0;i<age.Length; i++) { if (age[i] == 24) { Console.Write(name[i]+" "); } } Console.WriteLine(); Console.WriteLine(); Array.Sort(age); int pos = Array.IndexOf(age, 25); Console.Write($"找到年齡25! 位置:{pos}"); ``` ![image](https://hackmd.io/_uploads/ryHKixbyC.png) ### p2 ```csharp= Console.WriteLine(" Mary Tomas John"); Random rnd = new Random(Guid.NewGuid().GetHashCode()); long [] sum = { 0, 0, 0 }; for (int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { long randomNumber = rnd.Next(); sum[j] = sum[j] + randomNumber; Console.Write($" {randomNumber}"); } Console.WriteLine(); } Console.WriteLine("---------------------"); Console.Write("Sum: "); for(int i = 0;i < 3; i++) { Console.Write($"{sum[i]} "); } ``` ![image](https://hackmd.io/_uploads/S15T-bb1C.png) ### p3 ```csharp= Console.WriteLine("//Can't use Chinese"); int[,,] data = new int[2,2,3]{ { { 11, 13, 15}, {22, 24, 26} }, { { 33, 38, 41}, {44, 48, 52} } }; int findValue = 48; int table=0,row=0,col = 0; bool found = false; //IDE語言套件有問題,不能用中文 Console.WriteLine($"{data.GetLength(0) + 1} table ,{data.Rank - 1}D Array {data.GetLength(1) + 1}*{data.GetLength(2) + 1}"); for (int i = 0; i < data.GetLength(0); i++) { Console.WriteLine($"--- table {i+1} ---"); for(int j = 0; j < data.GetLength(1); j++) { for(int k = 0;k < data.GetLength(2); k++) { if(!found) { if (data[i,j,k] == findValue) { table = i + 1; row = k + 1; col = j + 1; found = true; } } Console.Write($"{data[i, j, k]} "); } Console.WriteLine(); } Console.WriteLine(); } Console.WriteLine($"element: {findValue} is in table {table},row {row},column{col}"); ``` ![image](https://hackmd.io/_uploads/ByiGoCwkC.png) ## 20240403課堂作業 ### p1 ```csharp= namespace ClassWork_0403_p1 { public partial class Form1 : Form { int n = 0; int sum = 0; public string[] j = new string[] { "A", "B", "C", "D", "E" }; public Form1() { InitializeComponent(); } public void label1_Click(object sender, EventArgs e) {} private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e) {} private void textBox1_TextChanged(object sender, EventArgs e) {} private void Form1_Load(object sender, EventArgs e) { label1.Text = $"{j[n]} score"; } private void button1_Click(object sender, EventArgs e) { int number; if (Int32.TryParse(textBox1.Text, out number)) { if(number<=100 && number >= 0) { listBox1.Items.Add($"{j[n]}: {number}"); if (n != 4) { n++; label1.Text = $"{j[n]} score"; } else { button1.Enabled = false; } sum += number; label2.Text = $"Average: {(double)sum / n:0.##}"; textBox1.Text = ""; } else { MessageBox.Show("score must between 0 to 100!","Warning",MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Pls Enter enteger!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); } textBox1.Text = ""; } } } ``` ![image](https://hackmd.io/_uploads/rk2E-PZxR.png) ### p2 ```csharp= using System; namespace ClassWork_0403_p2_version2 { public partial class Form1 : Form { public string[] song = new string[] { "Grievous Lady -nothing is but what is not-", "Stuck in Dreams", "The Arena", "Forest Shrine", "Ignotus", "Singularity", "Ether Strike", "Testify", "Regression", "Cyberangle" }; public string[] artist = new string[] { "Team Grimoire vs Laur", "Nightcall", "Lindsey Strling", "Kerusu", "ak+q", "Etia", "Akira Complex", "void(mournfinale)", "Gon", "Hanser" }; int[] rank = new int[10]; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { listView1.Items.Clear(); //rank int[] rank2 = new int[10]; rank.CopyTo(rank2, 0); Array.Sort(rank, song); Array.Sort(rank2, artist); for (int i = 0; i < rank2.Length; i++) { ListViewItem item = new ListViewItem($"{rank[i]}"); item.SubItems.Add(artist[i]); item.SubItems.Add(song[i]); listView1.Items.Add(item); } } private void Form1_Load(object sender, EventArgs e) { label1.Text = ""; listView1.Columns.Add("Rank", 50); listView1.Columns.Add("Artist", 150); listView1.Columns.Add("Song", 300); for (int i = 0; i < rank.Length; i++) { rank[i] = i + 1; } button1_Click(sender, e); } private void button2_Click(object sender, EventArgs e) { listView1.Items.Clear(); string[] song2 = new string[song.Length]; song.CopyTo(song2, 0); Array.Sort(song, rank); Array.Sort(song2, artist); for (int i = 0; i < song2.Length; i++) { ListViewItem item = new ListViewItem($"{rank[i]}"); item.SubItems.Add(artist[i]); item.SubItems.Add(song[i]); listView1.Items.Add(item); } } private void button3_Click(object sender, EventArgs e) { label1.Text = ""; listView1.Items.Clear(); string SearchArtist = textBox1.Text; string result = $"Can't find {SearchArtist}"; int index = Array.IndexOf(artist, SearchArtist); if (index != -1) { ListViewItem item = new ListViewItem($"{rank[index]}"); item.SubItems.Add(artist[index]); item.SubItems.Add(song[index]); listView1.Items.Add(item); } else { label1.Text = result; } } } } ``` ![image](https://hackmd.io/_uploads/SkB59nGlC.png) ![image](https://hackmd.io/_uploads/S1bj9hfgC.png) ![image](https://hackmd.io/_uploads/rJah5nMeR.png) ![image](https://hackmd.io/_uploads/BJ2SinGxA.png) ### p3 ```csharp= namespace ClassWork_0403_p3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { float height; if (radioButton1.Checked) { if (float.TryParse(textBox1.Text, out height)) { height = height / 100; } else { label2.Text = "error"; } } else { height = float.Parse(textBox1.Text); } float weight = float.Parse(textBox2.Text); label2.Text = $"BMI: {(weight/(height*height)).ToString()}"; } private void Form1_Load(object sender, EventArgs e) { label2.Text = ""; } } } ``` ![image](https://hackmd.io/_uploads/BkK0anMxA.png) ## 20240410課堂作業 ### p1 ```csharp= namespace homeWork_0410_p1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 初始化視窗元件 } // 視窗載入時的初始化設定 private void Form1_Load(object sender, EventArgs e) { // 設定圖片顯示模式為拉伸 PicShow.SizeMode = PictureBoxSizeMode.StretchImage; // 設定初始圖片高度與寬度 PicShow.Height = 90; PicShow.Width = 90; // 載入初始圖片 PicShow.Image = Image.FromFile("C:\\Users\\popst\\Pictures\\miyu_art\\pic1.png"); // 設定滑桿最大最小值與初始值 TkbPic.Maximum = 3; TkbPic.Minimum = 1; // 設定垂直滾動條的最大最小值與初始值 VsbHeight.Maximum = 180; VsbHeight.Minimum = 1; VsbHeight.LargeChange = 1; VsbHeight.Value = PicShow.Height; // 設定水平滾動條的最大最小值與初始值 HsbWidth.Maximum = 180; HsbWidth.Minimum = 1; HsbWidth.LargeChange = 1; HsbWidth.Value = PicShow.Width; // 設定工具提示內容 Ttip.SetToolTip(VsbHeight, $"{VsbHeight.Value}"); Ttip.SetToolTip(HsbWidth, $"{HsbWidth.Value}"); Ttip.SetToolTip(TkbPic, "pic1"); } // 垂直滾動條滾動事件 private void VsbHeight_Scroll(object sender, ScrollEventArgs e) { // 調整圖片高度並更新工具提示內容 PicShow.Height = VsbHeight.Value; Ttip.SetToolTip(VsbHeight, $"{VsbHeight.Value}"); } // 水平滾動條滾動事件 private void HsbWidth_Scroll(object sender, ScrollEventArgs e) { // 調整圖片寬度並更新工具提示內容 PicShow.Width = HsbWidth.Value; Ttip.SetToolTip(HsbWidth, $"{HsbWidth.Value}"); } // 圖片切換滑桿滾動事件 private void TkbPic_Scroll(object sender, EventArgs e) { // 切換圖片並更新工具提示內容 PicShow.Image = Image.FromFile($"C:\\Users\\popst\\Pictures\\miyu_art\\pic{TkbPic.Value}.png"); Ttip.SetToolTip(TkbPic, $"pic{TkbPic.Value}"); } } } ``` ![image](https://hackmd.io/_uploads/BJ7MgujeA.png) ![image](https://hackmd.io/_uploads/rkdQxuoeR.png) ### p2 ```csharp= namespace homeWork_0410_p2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { if(DateTime.Now >= dateTimePicker1.Value) { timer1.Enabled = false; MessageBox.Show(textBox1.Text,"warning"); System.Media.SystemSounds.Beep.Play(); } } private void Form1_Load(object sender, EventArgs e) { dateTimePicker1.CustomFormat = "HH:mm:ss"; dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.MinDate = DateTime.Now.AddMinutes(1); dateTimePicker1.MaxDate = DateTime.Now.AddMinutes(10); } private void button1_Click(object sender, EventArgs e) { timer1.Interval = 1000; timer1.Enabled = true; dateTimePicker1.Enabled = false; } } } ``` ![image](https://hackmd.io/_uploads/rJSoCy2x0.png) ## 20240417課堂作業 ### p1 ![image](https://hackmd.io/_uploads/ByyRjLRbC.png) ![image](https://hackmd.io/_uploads/rJ-g7DCWC.png) ![image](https://hackmd.io/_uploads/BkmxNDRZR.png) ![image](https://hackmd.io/_uploads/BJjbNwC-A.png) ![image](https://hackmd.io/_uploads/SkotVDRZR.png)