**************************************** Introduction to Programming, autumn 2006 Exercise 6 Example solutions/Mikko Apiola Happily translated by Samuli Kaipiainen Modified by Janne Korhonen **************************************** What values do the variables get when the following statements are executed by the computer? Explain why. i j k p r m n o int i = 2, j, k = -3; 2 ? -3 double p, r = 5.6; 2 ? -3 ? 5.6 String m, n = " hip hei", o; 2 ? -3 ? 5.6 ? " hip hei" ? j = i + 10*(k+i); 2 -8 -3 ? 5.6 ? " hip hei" ? k = j/3; 2 -8 -2 ? 5.6 ? " hip hei" ? p = j/3; 2 -8 -2 -2.0 5.6 ? " hip hei" ? i = k*i/2; -2 -8 -2 -2.0 5.6 ? " hip hei" ? p = p/r/2; -2 -8 -2 -0.17.. 5.6 ? " hip hei" ? i++; ++i; 0 -8 -2 -0.17.. 5.6 ? " hip hei" ? --j; j--; 0 -10 -2 -0.17.. 5.6 ? " hip hei" ? p++; ++p; 0 -10 -2 1.821.. 5.6 ? " hip hei" ? k += 10; 0 -10 8 1.821.. 5.6 ? " hip hei" ? r -= 1000; 0 -10 8 1.821.. -994.4 ? " hip hei" ? m = n + 20 + 101; 0 -10 8 1.821.. -994.4 " hip hei20101" " hip hei" ? o = 20 + 100 + n; 0 -10 8 1.821.. -994.4 " hip hei20101" " hip hei" "120 hip hei" m = n + (20+100); 0 -10 8 1.821.. -994.4 " hip hei120" " hip hei" "120 hip hei" o = (11+100) + n; 0 -10 8 1.821.. -994.4 " hip hei120" " hip hei" "111 hip hei" m = n + (10-120); 0 -10 8 1.821.. -994.4 " hip hei-110" " hip hei" "111 hip hei" o = (100-20) + n; 0 -10 8 1.821.. -994.4 " hip hei-110" " hip hei" "80 hip hei" **************************************** Introduction to Programming, autumn 2006 Exercise 7 Example solution by Janne Korhonen **************************************** Write one print statement that prints: "Hard, rock /// hallelujah \\\ / Hard, rock, hallelujah..." screams one "monster". System.out.println("\"Hard, rock /// hallelujah \\\\\\\n"+ " / Hard, rock, hallelujah...\"\n"+ "screams one \"monster\"."); *************************************** Introduction to Programming autumn 2006 Exercise 8 Example solution / Mikko Apiola Translated by Samuli Kaipiainen Modified by Janne Korhonen *************************************** Make an interactive program to calculate the lucky numbers. A lucky number is the remainder of dividing the product of one's height, weight and shoe size with the number 49. First the program asks the user's name and then prints also the name of the client on the report of the lucky number. Use descriptive variable names and make the conversation user-friendly. Include comments in the program and pay attention also to the lay-out of the program. Why should the layout be clear? Why should the program output be clear? To whom these concerns are important? ---- "The appearance of the program and the use of descriptive identifier names are useful to programmers (even yourself!). Clear and informative program outputs help the user in using the program and let him/her know what is going on." - Vesa Vainio ---- import java.util.Scanner; public class Exercise8 { private static Scanner reader = new Scanner(System.in); public static void main (String [] args) { // Define variables String name; int height, weight, shoeSize, luckyNumber; // Ask user needed values. // NOTE! They must be integers. System.out.println("Welcome to lucky number counter!"); System.out.print("What's your name? "); name = reader.nextLine(); System.out.print("How tall are you? "); height = reader.nextInt(); System.out.print("How much do you weight? "); weight = reader.nextInt(); System.out.print("What's your shoe size? "); shoeSize = reader.nextInt(); // Count the lucky number luckyNumber = (height*weight*shoeSize) % 49; // ...And report to the user System.out.println(); System.out.println("Hi, " + name + "! Your lucky number is " + luckyNumber + "."); } } **************************************** Introduction to Programming, autumn 2006 Exercise 9 Example solutions/Mikko Apiola Happily translated by Samuli Kaipiainen **************************************** Let: int a, b, c; double p, q, r; Write a boolean expression to describe each of the following cases: 1. a is an odd number 2. b is greater than zero, but smaller than 100 3. b is greater than c, but smaller than a 4. -23.7 < p < -4.14 5. either a is 7 and b is negative, or q is greater than the difference of r and p 6. r < -7.1 or r > 3.1, the sum of c and a is an even number and b is not 666 7. either p is positive or q is positive, but it is not the case that both are positive Answer: 1) (a % 2 == 1) 2) (b > 0) && (b < 100) 3) (b > c) && (b < a) 4) (p > -23.7) && (p < -4.14) 5) (a == 7 && b < 0) || (q > (r - p)) 6) (r < -7.1 || r > 3.1) && ((c + a) % 2 == 0) && b != 666 7) ((p > 0) != (q > 0)) // XOR **************************************** Introduction to Programming, autumn 2006 Exercise 10 Example solution / Vesa Vainio Modified by Samuli Kaipiainen **************************************** Make a program - by using if statements - that reads three numbers and prints them in ascending order. Make two different programs: one that exchanges the values of variables and another that does not. How could you sort four numbers? ANSWER: Either of the principles can be used to sort four numbers. Already with four numbers the programs would be quite annoying and impractical to write. Clearly, more useful ways for sorting numbers are needed... ---- import java.util.Scanner; public class Exercise10 { private static Scanner reader = new Scanner(System.in); public static void main(String[] args){ System.out.print("Please enter the first number: "); int first = reader.nextInt(); System.out.print("Now enter the second number: "); int second = reader.nextInt(); System.out.print("Finally, enter the third number: "); int third = reader.nextInt(); // FIRST PART: print them without changing the variables if (first <= second && first <= third) { System.out.print(first + ", "); if (second <= third) System.out.println(second + ", " + third); else if (third <= second) System.out.println(third + ", " + second); } else if (second <= first && second <= third) { System.out.print(second + ", "); if (first <= third) System.out.println(first + ", " + third); else if (third <= first) System.out.println(third + ", " + first); } else if (third <= first && third <= second) { System.out.print(third + ", "); if (first <= second) System.out.println(first + ", " + second); else if (second <= first) System.out.println(second + ", " + first); } // SECOND PART: change the values of variables int temp = 0; if (first > third) { temp = first; first = third; third = temp; } if (first > second) { temp = first; first = second; second = temp; } if (second > third) { temp = second; second = third; third = temp; } System.out.println(first + ", " + second + ", " + third); } }