110 likes | 237 Views
Final Exam Review. CSE113. Arithmetic Expression. Assume A = 2, B =4, C =10, D=5, evaluate the following expressions. Ans1 = A + B - C + D Ans2 = B * C / A Ans3 = C + B – D/A Ans4 = B * (C/A) Ans5 = C % B * D Ans6 = (C+B – D)/A. While loop.
E N D
Final Exam Review CSE113
Arithmetic Expression • Assume A = 2, B =4, C =10, D=5, evaluate the following expressions. • Ans1 = A + B - C + D • Ans2 = B * C / A • Ans3 = C + B – D/A • Ans4 = B * (C/A) • Ans5 = C % B * D • Ans6 = (C+B – D)/A
While loop • What is the output when the “while” loop given below is executed? Assume number generated are : 0, 4, 3, 2 ,5, 3, 5, 3, 4, 2,…. intans = 0; int count = 1; intnum; while (count < 5) { num = Greenfoot.getRandomNumber(6); if (num % 2 == 0) { ans = ans + num; } count = count + 1; }
While loop • Write a while that adds 4 crabs in a vertical line to the world at locations {(10, 100), (20, 100), (30, 100) etc. } • How will you modify this to add 10 crabs?
Array Definition • You want to create a scenario with a (i) pink worm (pworm.jpg), (ii) blue worm (bworm.jpg), (iii) brown carb (bcrab.jpg), (iv) green crab (gcrab.jpg), (v) red lobster (rlob.jpg). • Write an array definition for this and initialize it with the image files given above.
For statement • Use a “for” statement to add the above objects to the world.
Evaluate the “for” statement • What does the following “for” evaluate? intsumSq = 0; intsq; for (inti = 0; i < 4; i++) { sq = i* I; sumSq = sumSq + sq; }
Evaluate the Switch statement switch (color) { case ‘v’ : {bg.SetColor(Color.VIOLET); break;} case ‘i’ : {bg.SetColor(Color.INDIGO); break;} case ‘b’ : {bg.SetColor(Color.BLUE); break;} case ‘g’ : {bg.SetColor(Color.GREEN); break;} case ‘y’ : {bg.SetColor(Color.YELLOW); break;} case ‘o’ : {bg.SetColor(Color.ORANGE); break;} case ‘r’ : {bg.SetColor(Color.RED); break;} default: { bg.setColor(Color.BLACK);} } If color is assigned ‘i’ what is the color for writing?
Write a switch statement • Write a switch statement that converts a number representing day of the week into a word for the day.
Evaluate the method calls • Using the calculator class and its methods write a set of call that will evaluate: • 5 + 6* 14 -16/3
Write a method • Write a method that computes the distance between two location and outputs the distance on the screen.