Exams questions examples 1 Exam example 1 y

  • Slides: 32
Download presentation
Exams questions examples 1

Exams questions examples 1

Exam example 1 . y - ו x שלמים מספרים שני כפרמטרים מקבלת what

Exam example 1 . y - ו x שלמים מספרים שני כפרמטרים מקבלת what הרקורסיבית הפעולה לפניך ( ' נק 10 ) ? what הפעולה מזימון כתוצאה הפלט יהיה מה public static int what(int x, int y) { if(x < 0) return –what(-x, y); if(y < 0) return –what(x, -y); if(x == 0 && y == 0) return 0; return 100 * what(x/10, y/10) + 10*(x%10) + y%10; } // what System. out. println(what(7, 9)); System. out. println(what(-7, 19)); System. out. println(what(17, -9)); System. out. println(what(-7, -19)); 2

Exam example 1 - solution what(-7, 19) { if(x<0) T return – 1* what(7,

Exam example 1 - solution what(-7, 19) { if(x<0) T return – 1* what(7, 19); if(y<0) return –what(x, -y); if(x==0 && y== 0) return 0; return 100*what(x/10, y/10) + 10*(x%10) + y%10; } // what 179 -179 System. out. println(what(7, 9)); 79 System. out. println(what(-7, 19)); -179 System. out. println(what(17, -9)); -1079 System. out. println(what(-7, -19)); 179 what(7, 19) { if(x<0) F return –what(-x, y); if(y<0) F return –what(x, -y); if(x==0 && y== 0) F return 0; return 100*what(0, 1) + 10*7+ 9; } // what 1 what(0, 1) { if(x<0) F return –what(-x, y); if(y<0) F return –what(x, -y); if(x==0 && y== 0) F return 0; return 100*what(0, 0) + 10*0+ 1; } // what 0 what(0, 0) { if(x<0) F return –what(-x, y); if(y<0) F return –what(x, -y); if(x==0 && y== 0) T return 0; return 100*what(x/10, y/10) + 10*(x%10)+y%10; } // what 3

Exam example 2 מספרים ושני , שלם מטיפוס שאיבריו arr מערך , k שלם

Exam example 2 מספרים ושני , שלם מטיפוס שאיבריו arr מערך , k שלם מספר מקבלת kar הרקורסיבית הפעולה לפניך . s<=e כאשר , e, s נוספים שלמים ( ' נק 20 ) public static int kar(int k, int[ ] arr, int s, int e) { if(s == e) if(arr[s] > k) return a[s]; else return 0; else { int p 1 = kar(k, a, s, (s+e)/2); int p 2 = kar(k, a, ((s+e)/2) + 1, e); return p 1+p 2; } // outer if } // kar 4

Exam example 2 - solution 2 8 4 14 5 18 public static int

Exam example 2 - solution 2 8 4 14 5 18 public static int kar(int k, int[ ] arr, int s, int e) { if(s == e) if(arr[s] > k) return a[s]; else return 0; else { int p 1 = kar(k, a, s, (s+e)/2); int p 2 = kar(k, a, ((s+e)/2) + 1), e); return p 1+p 2; } // outer if } //kar ( ' נק 10 ). ב סעיף k - מ הגדולים , ( )כולל e – ו s המקומות בין המערך האיברי כל סכום את מחזירה הפעולה

Exam example 3 , cont. “***this***an***example**of***sentence****” index value 0 t 1 h 2 i

Exam example 3 , cont. “***this***an***example**of***sentence****” index value 0 t 1 h 2 i 3 s 4 * 5 i 6 s. . . Formal parameter : String sentence Returned value : reference to array of char 27 n 28 c 29 e 8

Exam example 3 - solution public static char[ ] rem. Stars(String str) { sent.

Exam example 3 - solution public static char[ ] rem. Stars(String str) { sent. Ar = new char[str. length()]; // help array for(int k = 0; k < str. length(); k++) sent. Arr[k] = str. char. At(k); int i = 0, j = 0, first = 0; // loop counters and help variable while( i < str. length() ) { if( sent. Arr[i] != ‘*‘ ) { first = 1; sent. Arr[j++] = sent. Arr[i++]; } // if else if(first != 0) { sent. Arr[j++] = sent. Arr[i++]; first = 0; } // if else i++; } // while char[ ] fin. Arr = new char[j-1]; // returned array for(int k = 0; k < j -1; k++) fin. Arr[k] = sent. Arr[k]; return fin. Arr; } // rem. Stars 9

Exam example 4 10

Exam example 4 10

Exam example 4 - solution public static int equal. Sum(int arr[ ]) { int

Exam example 4 - solution public static int equal. Sum(int arr[ ]) { int sum. L = 0, sum. R = 0; for(int i = 0; i < arr. length; i++) sum. R += arr[i]; for(int j = 0; j < arr. length; j++) { sum. R -= arr[j]; sum. L += arr[j]; if(sum. R == sum. L) return j; } //for return -1; } // equal. Sum 11

public static String find(String string) { char start, end; int s 1= 0, s

public static String find(String string) { char start, end; int s 1= 0, s 2 = 0, i=0, max = 0, ji = 0, jo = 0; boolean flag=false; String out. String; while (? ? ? 1 ? ? ? ) { s 1= i; start = string. char. At(i); end = (? ? ? 2 ? ? ? ) while (? ? ? 3 ? ? ? ) { flag=true; start = end; if(? ? ? 4 ? ? ? ) (? ? ? 5 ? ? ? ) end = string. char. At(i+1); } // inner while if(flag) { (? ? ? 6 ? ? ? ) if(s 2 -s 1 > max) { max = (? ? ? 7 ? ? ? ) ji = s 1; jo = s 2; (? ? ? 8 ? ? ? ) } // inner if } // outer if (? ? ? 9 ? ? ? ) } // outer while if(jo==string. length() - 1) out. String=string. substring(ji, (jo+1)); else (? ? ? 10 ? ? ? ) return out. String; } // find public static String find(String string) { char start, end; int s 1=0, s 2=0, i=0, max=0, ji = 0, jo = 0; boolean flag=false; String out. String; while(i<string. length() - 1) { s 1= i; start = string. char. At(i); end = string. char. At(i+1); while( start + 1 == end ) { flag=true; start = end; if( i < string. length() - 2 ) i++; end = string. char. At(i+1); } // inner while if(flag) { s 2 = i+1; if(s 2 - s 1 > max) { max = s 2 - s 1; ji = s 1; jo = s 2; flag = false; } // inner if } // outer if i++; } // outer while if(jo == string. length() - 1) out. String = string. substring(ji, (jo+1)); else out. String = string. substring(ji, jo); return out. String; } // find

Exam example 6 – help methods // --- ברשימה האיברים המספר את המחזירה פעולה

Exam example 6 – help methods // --- ברשימה האיברים המספר את המחזירה פעולה --- נוספות בפעולות משתמש אתה אם public static int list. Size(List <Integer> lst). אותן ולתעד לממש עליך חובה { int count = 0; Node<Integer> pos = lst. get. First(); while(pos != null) { count++; pos = pos. get. Next(); // ---n שלם ומספר lst רשימה המקבלת פעולה -- } // while // --- ברשימה n במקום שנמצא לאיבר הפנייה ומחזירה -- return count; public static Node<Integer> n. Place(List <Integer> lst, int n) } // list. Size { Node<Integer> pos = lst. get. First(); while(pos != null && n > 0) { n--; pos = pos. get. Next(); } // while return pos; } // n. Place 15

Example 6 – is. Triple. List method public static boolean is. Triple. List(List<Integer> list)

Example 6 – is. Triple. List method public static boolean is. Triple. List(List<Integer> list) { int num = list. Size(list) / 3; if( num == 0 && num %3 != 0) return false; Node<Integer> pos 1 = lst. get. First(); Node<Integer> pos 2 = n. Place(list, num); Node<Integer> pos 3 = n. Place(list, 2*num); while (pos 3 != null) { if(pos 1. get. Data() != pos 2. ge. Data() || pos 1. get. Data() != pos 3. get. Data()) return false; pos 1 = pos 1. get. Next(); pos 2 = pos 2. get. Next(); pos 3 = pos 3. get. Next(); } // while return true; } // is. Triple. List 16

Exam example 7 - solution public static List<Range. Node> create. Range. List(List<Integer> source. List)

Exam example 7 - solution public static List<Range. Node> create. Range. List(List<Integer> source. List) { int from, to; // help variables List<Range. Node> range. List = new List<Range. Node>(); // List class constructor Node<Integer> pos 1 = source. List. get. First(); Node<Range. Node> pos 2 = range. List. get. First(); while( pos 1 != null ) { from = pos 1. get. Data(); to = from; pos 1 = pos 1. get. Next(); while( pos 1 != null && pos 1. get. Data() – 1 == to ) { to = pos 1. get. Data(); pos 1 = pos 1. get. Next(); } // inner while Range. Node rn = new Range. Node(from, to); // Range. Node class constructor pos 2 = range. List. insert(pos 2, rn); } // outer while return range. List; } // create. Range. List 19

Exam example 8 - solution public static Real. Set copy. Set(Real. Set rs) {

Exam example 8 - solution public static Real. Set copy. Set(Real. Set rs) { Real. Set rs 1 = new Real. Set(); Real. Set rs 2 = new Real. Set(); int n = rs. size(); for( int i = 0; i < n; i++ ) { double x = rs. find. Biggest(); rs 1. insert(x); rs 2. insert(x); rs. remove(x); } // for( int i = 0; i < n; i++ ) { double x = rs 1. find. Biggest(); rs. insert(x); rs 1. remove(x); } // for return rs 2; } // copy. Set ' א סעיף 22

Exam example 8 - solution public static Real. Set buid. Neg. Num(Real. Set rs)

Exam example 8 - solution public static Real. Set buid. Neg. Num(Real. Set rs) { Real. Set rs 1 = copy. Set(rs); double x = rs 1. find. Biggest(); while ( rs 1. size() > 0 && x > 0 ) { rs 1. remove(x); x = rs. find. Biggest(); } // while return rs 1; } // buid. Neg. Num ' ב סעיף 23

Exam example 9 Point : Point המחלקה של UML לפניך private double x private

Exam example 9 Point : Point המחלקה של UML לפניך private double x private double y Point(double x, double y) double get. X( ) double get. Y( ) void set. X(double x) void set. Y(double y) String to. String( ) . הישר אותו על הן אם ובודקת נקודות 3 כפרמטרים המקבלת פעולה כתבו ( ' נק 10 ). א : להיות צריכה הפעולה חתימת public static boolean in. Line(Point p 1, Point p 2, Point p 3) : אם ורק אם ישר אותו על הן (x 1, y 1), (x 2, y 2), (x 3, y 3) הנקודות : תזכורת 24

Exam example 9, cont. ' א סעיף public static boolean in. Line(Point p 1,

Exam example 9, cont. ' א סעיף public static boolean in. Line(Point p 1, Point p 2, Point p 3) { double w 1, w 2; w 1 = (p 1. get. X() – p 2. get. X()) * (p 1. get. Y() – p 3. get. Y()); w 2 = (p 1. get. X() – p 3. get. X()) * (p 1. get. Y() – p 2. get. Y()); return w 1 == w 2; } // in. Line 26

Exam example 9 - solution ' ב סעיף public static boolean test. Line(Point[ ]

Exam example 9 - solution ' ב סעיף public static boolean test. Line(Point[ ] points) { for (int i = 0; i < points. length - 2; i++) for(int j = i+1; j < points. length - 1; j++) for(int k = j + 1; k < points. length; k++) if( in. Line(points[i], points[j], points[k] ) return true; return false; } // test. Line 27

Exam example 10 - solution public static boolean is. List. Cont(List<Integer> lst 1, List<Integer>

Exam example 10 - solution public static boolean is. List. Cont(List<Integer> lst 1, List<Integer> lst 2) { Node<Integer> node 1 = lst 1. get. First(); while(node 1 != null) { boolean found = false; Node<Integer> node 2 = lst 2. get. First(); while((node 2 != null) && (! found)) { if(node 1. get. Data() == node 2. get. Data()) found = true; node 2 = node 2. get. Next(); } // inner while if( ! found) return false; node 1 = node 1. get. Next(); } // outer while return true; } // is. List. Cont 29

Exam example 11 - solution public static Book[ ] name. Writer. Sort(List<Book> books) {

Exam example 11 - solution public static Book[ ] name. Writer. Sort(List<Book> books) { int num. K = 0; // number of Kipling's books int num. S = 0; // number of Swift's books for( Node<Book> curr = books. get. First(); curr != null; curr = curr. get. Next()) { if(curr. get. Data(). get. Writer(). compare. To("Kipling") = = 0) num. K++; if(curr. get. Data(). get. Writer(). compare. To("Swift") = = 0) num. S++; } // for Book [ ] arr = new Book [num. K + num. S]; // output array int ind. K = 0; // Kipling's books array index int ind. S = num. S - 1; // Swift's books array index Node<Book> temp = books. get. First(); while(temp != null) if(temp. get. Data(). get. Writer(). compare. To("Kipling") = = 0) arr[ind. K++] = temp. get. Data(); else arr[ind. S++] = temp. get. Data(); return arr; } // name. Writer. Sort 31

Goodbye and Good luck ! I wish you all the very best for your

Goodbye and Good luck ! I wish you all the very best for your exams ( not only Java ), my students ! 32