הוראת פעולות במציאות החדשה

Download Report

Transcript הוראת פעולות במציאות החדשה

‫הוראת פעולות במציאות‬
‫החדשה‬
‫מגיש‪ :‬בונימוביץ' לביא‬
‫מעבר בין תכנות פרוצדורלית לתמ"ע‬
‫הבעיה הגדולה היא‪:‬‬
‫גישה שונה לתכנות‬
‫גישה פרוצדורלית‬
‫לזהות נתונים ופעולות‬
‫להגדיר מבני נתונים ופעולות‬
‫לממש פעולות‬
‫בעזרת הפעולות לממש את התרגיל‬
‫גישה מונחית עצמים‬
‫לזהות עצמים‬
‫להגדיר התנהגות עצמים‬
‫להגדיר תכונות ולממש פעולות‬
‫בעזרת המחלקות שנבנו לממש את הפתרון‬
‫תכנית הלימודים‬
‫"פרק ‪ :6‬טיפוסים ומבוא למבני‪-‬נתונים (‪ 10‬שעות)‬
‫מטרות הפרק‬
‫להכיר את המושג 'טיפוס נתונים'‬
‫להכיר את מושג המחלקה הבסיסי‬
‫לבנות מבנים מורכבים‬
‫פירוט התכנים‬
‫המושג טיפוס‪ :‬הצהרת טיפוס‪ ,‬המחלקה כמגדירה טיפוס‪.‬‬
‫מבני נתונים מורכבים‪ :‬מערכים‪ ,‬מחלקות בעלות תכונות פומביות‬
‫בלבד‪ ,‬הגדרת מחלקה‪ ,‬תכונות של מחלקה‪ ,‬גישה ישירה לתכונה‬
‫של מחלקה‪ ,‬קליטת ערך לתכונה‪"...‬‬
‫כיצד לשרוד?‬
‫דוגמא לפתרון בשתי גישות‬
‫תרגיל ‪ ,10‬תשס"ג‬
‫לקראת תחרות ארצית במדעי המחשב‪ ,‬נערכה בחינת מיון ל‪1750 -‬‬
‫תלמידים‪.‬‬
‫לתחרות הארצית יתקבלו תלמידים שציוניהם בבחינת המיון גבוה‬
‫מהציון הממוצע של כל הנבחנים בבחינה זו‪.‬‬
‫פתח אלגוריתם שיקלוט לכל מועמד רשומה הכוללת את הנתונים‬
‫האלה‪ :‬שם‪ ,‬כתובת‪ ,‬מספר תעודת זהות‪ ,‬שפת התכנות המועדפת עליו‬
‫(פסקל או ‪ )C‬וציון בבחינת המיון‪.‬‬
‫האלגוריתם יציג כפלט שתי רשימות המכילות את שמות התלמידים‬
‫שיתקבלו לתחרות הארצית‪ ,‬כתובותיהם ומספרי תעודות הזהות‬
‫שלהם‪.‬‬
‫הרשימה הראשונה תכלול את פרטי התלמידים ששפת התכנות שלהם‬
‫היא פסקל‪ ,‬והרשימה השנייה תכלול את פרטי התלמידים ששפת‬
‫התכנות שלהם היא ‪.C‬‬
)‫ (פתרון של ראמי גבאלי‬Student ‫ מחלקה‬,‫פתרון תמ"ע‬
class Student
{
private string name;
private string address;
private int id;
private int grade;
private string proglang;
public Student(string name, string address, int grade, string proglang, int id)
{
this.name = name;
this.address = address;
this.grade = grade;
this.proglang = proglang;
this.id = id;
}
public string GetName(){return this.name;}
public string GetAddress(){return this.address;}
public int GetGrade(){return this.grade;}
public int GetID(){return this.id;}
public string GetProgLang(){return this.proglang;}
)‫ (פתרון של ראמי גבאלי‬Student ‫ מחלקה‬,‫פתרון תמ"ע‬
public void SetAddress(string address)
{
this.address = address;
}
public void SetGrade(int grade)
{
this.grade = grade;
}
public void SetProgLang(string proglang)
{
this.proglang = proglang;
}
public void Print()
{
Console.WriteLine("Name : {0}", this.name);
Console.WriteLine("Address : {0}", this.address);
Console.WriteLine("Id
:{0} ", this.id);
Console.WriteLine("-------------------------------");
}
}
)‫ (פתרון של ראמי גבאלי‬Contest ‫ מחלקה‬,‫פתרון תמ"ע‬
class Contest
{
private Student[] arr;
private int counter;
public Contest ()
{
arr = new Student[1750];
counter = 0;
}
public void AddStudent(string name, string address, int grade,
string proglang, int id)
{
arr[counter++] = new Student(name, address, grade, proglang, id);
}
public void AddStudent(Student s)
{
arr[counter++] = new Student(s.GetName(), s.GetAddress(), s.GetGrade(),
s.GetProgLang(), s.GetID());
}
)‫ (פתרון של ראמי גבאלי‬Contest ‫ מחלקה‬,‫פתרון תמ"ע‬
public double GetAverage()
{
int sum = 0;
double avg;
for (int i = 0; i < this.counter; i++)
sum += arr[i].GetGrade();
avg = (double)sum / this.counter;
return avg;
}
public void print(double avg, string proglang)
{
for (int i = 0; i < this.counter; i++)
if (this.arr[i].GetGrade() > avg &&
this.arr[i].GetProgLang() == proglang)
this.arr[i].print();
}
}
)‫ (פתרון של ראמי גבאלי‬Program ‫ מחלקה‬,‫פתרון תמ"ע‬
class Program
{
static void Main(string[] args)
{
Contest all = new Contest ();
for (int i = 0; i < 1750; i++)
{
Console.WriteLine("Enter Name");
string name = Console.ReadLine();
Console.WriteLine("Enter Address");
string address = Console.ReadLine();
Console.WriteLine("Enter Grade");
int grade = int.Parse(Console.ReadLine());
Console.WriteLine("Enter C or Pascal");
string proglang = Console.ReadLine();
Console.WriteLine("Enter Id");
int id = int.Parse(Console.ReadLine());
all.AddStudent(name, address, grade, proglang, id);
}
double avg = all.GetAverage();
all.Print(avg, ”C");
all.Print(avg, ”Pascal");
‫יתרונות‪/‬חסרונות של פתרון תמ"ע‬
‫חסרונות‬
‫דורש זמן רב (הרבה קוד)‬
‫זיהוי עצמים (‪ )OOD‬מהווה משימה קשה מדי‬
‫יתרונות‬
‫מלמד גישה תמ"ע נכונה‪ :‬חלוקה למחלקות‪ ,‬כימוס‬
)‫פתרון פרוצדורלי (פתרון של ראמי גבאלי‬
class Student
{
public string name;
public string address;
public int id;
public string proglang;
public int grade;
}
class Program
{
static void InputData(Student[] arr)
//‫קליטת פרטי תלמידים למערך‬
{
for (int i = 0; i < arr.Length; i++)
{
arr[i]=new Student();
Console.WriteLine("Enter Name, Address, Id num ,Language,Grade
from Student ");
arr[i].name = Console.ReadLine();
arr[i].address = Console.ReadLine();
arr[i].id = int.Parse(Console.ReadLine());
arr[i].proglang = Console.ReadLine();
arr[i].grade = int.Parse(Console.ReadLine());
}
}
)‫פתרון פרוצדורלי (פתרון של ראמי גבאלי‬
static double Average(Student[] arr)
//‫חישוב ממוצע הציונים‬
{
int sum = 0;
double avg;
for (int i = 0; i < arr.Length; i++)
sum += arr[i].grade;
avg = (double)sum / arr.Length;
return avg;
}
static void Print(Student[] arr, double avg, string language)
// ‫הדפסת שתי הרשימות‬
{
for(int i=0; i<arr.Length; i++)
if (arr[i].grade > avg && arr[i].proglang == language)
{
Console.WriteLine("Name : {0}", arr[i].name);
Console.WriteLine("Address: {0}", arr[i].address);
Console.WriteLine("Id Num : {0}", arr[i].id);
Console.WriteLine("---------------------------------");
}
}
)‫פתרון פרוצדורלי (פתרון של ראמי גבאלי‬
static void Main(string[] args)
{
Student[] arr = new Student [1750];
double avg;
InputData(arr);
avg = Average(arr);
Print(arr, avg, "Pascal");
Print(arr, avg, "C");
}
}
‫יתרונות‪/‬חסרונות של פתרון פרוצדורלי‬
‫חסרונות‬
‫כל התכונות ‪( public‬סותר עקרון כימוס)‬
‫הפעולות הן סטטיות ואינן קשורות לעצמים‬
‫יתרונות‬
‫פתרון מצומצם‬
‫מוכר למורים מבחינת תהליך ההוראה‬
‫האם יש דרך ביניים‬
‫?‬
‫בהמשך‪...‬‬
‫סוגי בעיות במבחן בגרות‬
‫מעקב‪ ,‬מציאת טעויות‬
‫כתיבת תכניות קטנות (‪)1-5‬‬
‫התכניות ללא סיפור מעשה‪ :‬דורשות לבצע משהו‬
‫על מערך‪ ,‬לכתוב פעולה המוגדרת מראש‪ ,‬אינן‬
‫דורשות עיבוד נתונים רב שלבי‬
‫דורשות בניית מבנה נתונים‬
‫בעיות עם סיפור מעשה עשיר‪ ,‬רב עצמים‬
‫השאלה הגדולה‬
‫כיצד ללמד פתרון משימות בעייתיות במסגרת‬
‫פרדיגמת תכנות מונחית עצמים?‬
‫התחלת הוראת פעולות‬
‫הגישה המסורתית‪ :‬מחלקה יחידה שמכילה‬
‫פעולות סטטיות‪ ,‬הסיבה העיקרית לשימוש‬
‫בפעולות היא הורדת רמת מורכבות האלגוריתם‪.‬‬
‫הגישה "עצמים תחילה"‪ :‬מתחילים ישר מעצמים‪,‬‬
‫שומרים על עקרון הכימוס‪ ,‬הסיבה העיקרית‬
‫לשימוש בעצמים היא שהעולם סביבנו מורכב‬
‫מעצמים‪ ,‬אנו מעוניינים בפונקציות שעצמים‬
‫נושאים‪.‬‬
‫כיצד אני התחלתי‬
class Program
{
static void Main(string[] args)
{
int i, n;
Console.WriteLine("Enter number");
n = int.Parse(Console.ReadLine());
for (i = 1; i <= n; i++)
{
PrintStars(i);
}
}
// prints the row of n stars
static void PrintStars(int n)
{
int i;
for (i = 1; i <= n; i++)
Console.Write("*");
Console.WriteLine();
}
}
‫המשך הוראת פעולות‪ ,‬היעד הסופי‬
‫הגישה "המסורתית"‬
‫הפעולות נשארות סטטיות‬
‫מלמדים את המחלקות בדומה לרשומה‪ :‬כל התכונות הן‬
‫‪ ,public‬ללא פעולות פנימיות‪ ,‬ללא שיטה בונה‬
‫הגישה "עצמים תחילה"‬
‫מתקדמים בבניית מחלקות תוך שמירה על עקרון הכימוס‬
‫העיצוב הוא רק באמצעות המחלקות‬
‫ניתן להסביר את פעולות המחלקה‪ ,‬עבודה עם מספר‬
‫מחלקות‬
‫כיצד אני רואה את ההמשך‬
‫התחלתי בפעולות סטטיות‬
‫ממשיך בפעולות סטטיות בכל המשימות שלא דורשות שימוש‬
‫ברשומה‬
‫להתחיל להסביר שימוש במחלקות‪ :‬או במחרוזת‪ ,‬או‬
‫‪Random‬‬
‫בסיום הקורס להתחיל להסביר בניית מחלקות‪ :‬תכונות‬
‫פנימיות‪ ,‬פעולות פנימיות‪ ,‬פעולה בונה‪ get ,‬ו‪set-‬‬
‫המטרה היא להגיע לפתרון בעיות ברמה של ‪9-10‬‬
‫לא בטוח שיש צורך להגיע להסבר פעולות מחלקה‬
‫הוראת שימוש במחלקת שרות‬
Student ‫ מחלקה‬,"‫ פתרון "הגיוני‬,Taharut ‫תרגיל‬
class Student
{
private string name;
private string address;
private int id;
private int grade;
private string progLang;// "C" or "Pascal"
public Student(string name, string address, int grade, string proglang, int id)
{
this.name = name;
this.address = address;
this.grade = grade;
this.progLang = proglang;
this.id = id;
}
public int GetGrade(){return this.grade;}
public string GetProgLang(){return this.progLang;}
public void print()
{
Console.WriteLine("Name : {0}", this.name);
Console.WriteLine("Address : {0}", this.address);
Console.WriteLine("Id
:{0} ", this.id);
}
}
Program ‫ מחלקה‬,"‫ פתרון "הגיוני‬,Taharut ‫תרגיל‬
class Program
{
static void Main(string[] args)
{
Student[] arr = new Student[1750];
double avg;
InputData(arr);
avg = Average(arr);
Print(arr, avg, "Pascal");
Print(arr, avg, "C");
}
static void InputData(Student[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("Enter Name, Address, Id num ,Language,Grade
from Student ");
string name = Console.ReadLine();
string address = Console.ReadLine();
int id = int.Parse(Console.ReadLine());
string lang = Console.ReadLine();
int grade = int.Parse(Console.ReadLine());
arr[i] = new Student(name, address, grade, lang, id);
}
Program ‫ מחלקה‬,"‫ פתרון "הגיוני‬,Taharut ‫תרגיל‬
// returns the average grade
static double Average(Student[] arr)
{
int sum = 0;
double avg;
for (int i = 0; i < arr.Length; i++)
sum += arr[i].GetGrade();
avg = (double)sum / arr.Length;
return avg;
}
// prints the student data
static void Print(Student[] arr, double avg, string lang)
{
for(int i=0; i<arr.Length; i++)
if (arr[i].GetGrade() > avg && arr[i].GetProgLang() == lang)
arr[i].print();
}
}
‫הנחיות לפתרון תרגילי בגרות בגישה תמ"ע‬
‫לא חובה להגיע לעיצוב מונחה עצמים מושלם‬
‫יש לממש רק את הפעולות הנחוצות‪ .‬אין צורך‬
‫בהעמסת שיטה בונה‪ ,‬חשוב לבדוק אם יש צורך‬
‫בשיטות ‪ get‬ו‪.set-‬‬
‫מותר להגדיר חלק מן הפעולות כסטטיות במחלקה‬
‫"הראשית"‬
‫הדגש הוא על השימוש הנכון במחלקה שנבנתה‬
‫בתכנית‬
Bazaar ‫ מחלקה‬,Yarid ‫פתרון תרגיל‬
class Bazaar
{
private int numParticipant;
private int numTicketsLottery;
private int numTicketsFree;
private int numTicketsShow;
private int numTicketsBuffet;
public Bazaar()
{
this.numParticipant = 0;
this.numTicketsBuffet = 0;
this.numTicketsLottery = 0;
this.numTicketsFree = 0;
this.numTicketsShow = 0;
}
public void UpdateTickets(int lottery, int buffet, int show)
{
this.numTicketsFree += lottery;
this.numTicketsBuffet += buffet;
this.numTicketsShow += show;
if (lottery > 10)
this.numTicketsFree++;
}
Bazaar ‫ מחלקה‬,Yarid ‫פתרון תרגיל‬
// increments the number of participants by 1
public void IncParticipant()
{
this.numParticipant++;
}
// prints the all bazaar statistics
public void Print()
{
Console.WriteLine("Participants
Console.WriteLine("Lottery tickets
Console.WriteLine("Free tickets
Console.WriteLine("Show tickets
Console.WriteLine("Buffet tickets
}
}
: {0}", this.numParticipant);
: {0}", this.numTicketsLottery);
: {0}", this.numTicketsFree);
: {0}", this.numTicketsShow);
: {0}", this.numTicketsBuffet);
Program ‫ מחלקה‬,Yarid ‫פתרון תרגיל‬
class Program
{
static void Main(string[] args)
{
int lottary, show, buffet;
Bazaar bazaar = new Bazaar();
Console.WriteLine("Enter the lottery tickets number");
lottary = int.Parse(Console.ReadLine());
while (lottary != -1)
{
Console.WriteLine("Enter the show tickets number");
show = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the buffet tickets number");
buffet = int.Parse(Console.ReadLine());
bazaar.IncParticipant();
bazaar.UpdateTickets(lottary, buffet, show);
Console.WriteLine("Enter the lottery tickets number");
lottary = int.Parse(Console.ReadLine());
}
bazaar.Print();
}
}
Class ‫ מחלקה‬,Bakbukim ‫פתרון תרגיל‬
public class Class
{
private int bottles;
private int batteries;
public Class()
{
this.batteries = 0;
this.bottles = 0;
}
// adds the bottles to the total bootles amount
public void AddBottles(int bottles)
{
this.bottles += bottles;
}
// adds the batteries to the total batteries amount
public void AddBatteries(int batteries)
{
this.batteries += batteries;
}
// returns the total points amount
public int TotalPoints()
{
return this.bottles * 3 + this.batteries * 7;
}
}
Program ‫ מחלקה‬,Bakbukim ‫פתרון תרגיל‬
class Program
{
static void Main(string[] args)
{
int bottles, batteries, classNum;
Class class1 = new Class();
Class class2 = new Class();
int points1, points2;
for (int i = 1; i <= 3; i++)
{
Console.WriteLine("Enter the class number");
classNum = int.Parse(Console.ReadLine());
Console.WriteLine("Enter bottles number");
bottles = int.Parse(Console.ReadLine());
Console.WriteLine("Enter batteries number");
batteries = int.Parse(Console.ReadLine());
if (classNum == 1){
class1.AddBatteries(batteries);
class1.AddBottles(bottles);
}
else {
class2.AddBatteries(batteries);
class2.AddBottles(bottles);
}
}
Program ‫ מחלקה‬,Bakbukim ‫פתרון תרגיל‬
points1 = class1.TotalPoints();
points2 = class2.TotalPoints();
if (points1 > points2)
Console.WriteLine("The first won");
else if (points1 < points2)
Console.WriteLine("The second won");
else
Console.WriteLine("Teko");
}
}
‫בהצלחה!‬