Upcoming SlideShare
×

# MELJUN CORTES Jedi course notes intro1-appendix c-answers to exercises

1,521 views

Published on

MELJUN CORTES Jedi course notes intro1-appendix c-answers to exercises

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,521
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
25
0
Likes
0
Embeds 0
No embeds

No notes for slide

### MELJUN CORTES Jedi course notes intro1-appendix c-answers to exercises

1. 1. J.E.D.I. Appendix C : Answers to ExercisesChapter 1 Exercises1.1 Writing Algorithms1. Baking BreadPseudocode: prepare all ingredients pour all ingredients in mixing bowl while batter not smooth yet mix ingredients pour into bread pan place inside oven while bread not yet done wait remove from ovenFlowchart:Introduction to Programming I 1
2. 2. J.E.D.I.2. Logging into your laboratorys computerPseudocode: Let power = computers power button Let in = status of user (initially false) if power == off Press power button Enter "boot" process while in== false enter user name enter password if password and user name correct in = true end whileFlowchart:Introduction to Programming I 2
3. 3. J.E.D.I.3. Getting the average of three numbersPseudocode: Let count = 0 Let sum = 0 Let average = 0 While count < 3 Get number sum = sum + number count++ average = sum/3 Display averageFlowchart:Introduction to Programming I 3
4. 4. J.E.D.I.1.2 Number Conversions1. 198010 to binary, hexadecimal and octalTo Binary: 1980/2 = 990 0 990/2 = 495 0 495/2 = 247 1 247/2 = 123 1 123/2 = 61 1 61/2 = 30 1 30/2 = 15 0 15/2 = 7 1Introduction to Programming I 4
5. 5. J.E.D.I. 1980/2 = 990 0 7/2 = 3 1 3/2 = 1 1 1/2 = 0 1 Binary = 11110111100To Hexadecimal: 0111, 1011, 1100, 7 B C Hexadecimal = 7BCTo Octal: 011, 110, 111, 100 3 6 7 4 Octal = 3674Introduction to Programming I 5
6. 6. J.E.D.I.2. 10010011012 to decimal, hexadecimal and octalTo Decimal: 1*1 = 1 0*2 = 0 1*4 = 4 1*8 = 8 0 * 16 = 0 0 * 32 = 0 1 * 64 = 64 0 * 128 = 0 0 * 256 = 0 1 * 512 = 512 TOTAL= 589 Decimal = 589To Hexadecimal: 0010, 0100, 1101 2 4 D Hexadecimal = 24DTo Octal: 001, 001, 001, 101 1 1 1 5 Octal = 1115Introduction to Programming I 6
7. 7. J.E.D.I.3. 768 to binary, hexadecimal and decimalTo Binary: 111, 110, 7 6 Binary = 111110To Hexadecimal: 0011, 1110, 3 E Hexadecimal = 3ETo Decimal: 6*1= 6 7 * 8 = 56 TOTAL = 62 Decimal = 62Introduction to Programming I 7
8. 8. J.E.D.I.4. 43F16 to binary, decimal and octalTo Binary: 4 3 F 0100, 0011, 1111 Binary = 010000111111To Decimal: F * 1 = 15 3 * 16 = 48 4 * 256 = 1024 TOTAL= 1087 Decimal = 1087To Octal: 010, 000 , 111 , 111 2 0 7 7 Octal = 02077Chapter 2 (No exercises)Introduction to Programming I 8
9. 9. J.E.D.I.Chapter 3 Exercises3.1 Hello World! /** * This class prints the line "Welcome to Java Programming [YourName]!!!" * on screen. */ public class HelloWorld { public static void main(String[] args){ System.out.println("Welcome to Java Programming [YourName]!!!"); } }3.2 The Tree /** * A program that prints four lines on screen */ public class TheTree { public static void main(String[] args){ System.out.println("I think I shall never see,"); System.out.println("a poem as lovely as a tree."); System.out.println("A tree whose hungry mouth is pressed"); System.out.println("Against the Earths flowing breast."); } }Introduction to Programming I 9
10. 10. J.E.D.I.Chapter 4 Exercises4.1 Declaring and printing variables /** * A program that declares different variables * then outputs the values of the variables */ public class VariableSample { public static void main(String[] args){ //declares integer number with 10 as initial value int number = 10; //declares character letter with a as initial value char letter = a; //declares boolean result with true as initial value boolean result = true; //declares String str with "hello" as initial value String str = "hello"; //prints the values of the variables on screen System.out.println("Number = "+number); System.out.println("letter = "+letter); System.out.println("result = "+result); System.out.println("str = "+str); } }4.2 Getting the average of three numbers /** * A program that solves for the average * of the three numbers: 10,20, and 45 * then outputs the result on the screen */ public class AverageNumber { public static void main(String[] args){ //declares the three numbers int num1 = 10; int num2 = 20; int num3 = 45; //get the average of the three numbers // and saves it inside the ave variable int ave = (num1+num2+num3)/3; //prints the output on the screen System.out.println("number 1 = "+num1); System.out.println("number 2 = "+num2); System.out.println("number 3 = "+num3);Introduction to Programming I 10
11. 11. J.E.D.I. System.out.println("Average is = "+ave); } }4.3 Output greatest value /** * A program that outputs the number with * the greatest value given thre numbers */ public class GreatestValue { public static void main(String[] args){ //declares the numbers int num1 = 10; int num2 = 23; int num3 = 5; int max = 0; //determines the highest number max = (num1>num2)?num1:num2; max = (max>num3)?max:num3; //prints the output on the screen System.out.println("number 1 = "+num1); System.out.println("number 2 = "+num2); System.out.println("number 3 = "+num3); System.out.println("The highest number is = "+max); } }4.4 Operator precedence 1. (((a/b)^c)^((d-e+f-(g*h))+i)) 2. ((((((3*10)*2)/15)-2+4)^2)^2) 3. ((r^((((s*t)/u)-v)+w))^(x-(y++)))Introduction to Programming I 11
13. 13. J.E.D.I.5.2 Last 3 words (JOptionPane version) import javax.swing.JOptionPane; /** * A program that asks three words from the user using the JOptionPane * and then displays these three words as a phrase on the screen */ public class LastThreeWords { public static void main(String[] args){ //gets the first word from the user String firstWord = JoptionPane.showInputDialog ("Enter word1"); //gets the second word from the user String secondWord = JoptionPane.showInputDialog ("Enter word2"); //gets the third word from the user String thirdWord = JoptionPane.showInputDialog ("Enter word3"); //displays the message JoptionPane.showMessageDialog(null,firstWord+ " "+secondWord+ " "+thirdWord); } }Introduction to Programming I 13
16. 16. J.E.D.I.6.2 Number in wordsUsing if-else statement: import javax.swing.JOptionPane; /** * Transforms a number input from 1-10 to words * using if-else */ public class NumWords { public static void main(String[] args){ String msg = ""; int input = 0; //gets the input string input = Integer.parseInt(JOptionPane.showInputDialog ("Enter number")); //sets msg to the string equivalent of input if(input == 1) msg = "one"; else if(input == 2) msg = "two"; else if(input == 3) msg = "three"; else if(input == 4) msg = "four"; else if(input == 5) msg = "five"; else if(input == 6) msg = "six"; else if(input == 7) msg = "seven"; else if(input == 8) msg = "eight"; else if(input == 9) msg = "nine"; else if(input == 10) msg = "ten"; else msg = "Invalid number"; //displays the number in words if with in range JOptionPane.showMessageDialog(null,msg); } }Introduction to Programming I 16
17. 17. J.E.D.I.Using switch statement: import javax.swing.JOptionPane; /** * Transforms a number input from 1-10 to words * using switch. */ public class NumWords { public static void main(String[] args){ String msg = ""; int input = 0; //gets the input string input = Integer.parseInt(JOptionPane.showInputDialog ("Enter number")); //sets msg to the string equivalent of input switch(input){ case 1: msg = "one"; break; case 2: msg = "two"; break; case 3: msg = "three"; break; case 4: msg = "four"; break; case 5: msg = "five"; break; case 6: msg = "six"; break; case 7: msg = "seven"; break; case 8: msg = "eight"; break; case 9: msg = "nine"; break; case 10: msg = "ten"; break; default: msg = "Invalid number"; break; } //displays the number in words if with in range JOptionPane.showMessageDialog(null,msg);Introduction to Programming I 17
18. 18. J.E.D.I. } }Introduction to Programming I 18
19. 19. J.E.D.I.6.3 Hundred TimesUsing while-loop: import java.io.*; /** * A program that prints a given name one hundred times * using while loop */ public class HundredNames{ public static void main(String[] args){ BufferedReader reader = new BufferedReader (new InputStreamReader ( System.in)); String name = ""; int counter = 0; //gets the users name try{ System.out.print("Enter name: "); name = reader.readLine(); }catch(Exception e){ System.out.println("Invalid input"); System.exit(0); } //while loop that prints the name one hundred times while(counter < 100){ System.out.println(name); counter++; } } }Introduction to Programming I 19
20. 20. J.E.D.I.Using do-while loop: import java.io.*; /** * A program that prints a given name one hundred times * using do-while loop */ public class HundredNames { public static void main(String[] args){ BufferedReader reader = new BufferedReader (new InputStreamReader ( System.in)); String name = ""; int counter = 0; //gets the users name try{ System.out.print("Enter name: "); name = reader.readLine(); }catch(Exception e){ System.out.println("Invalid input"); System.exit(0); } //do-while loop that prints the name one hundred times do{ System.out.println(name); counter++; }while(counter < 100); } }Introduction to Programming I 20
21. 21. J.E.D.I.Using for loop: import java.io.*; /** * A program that prints a given name one hundred times * using do-while loop */ public class HundredNames { public static void main(String[] args){ BufferedReader reader = new BufferedReader (new InputStreamReader ( System.in)); String name = ""; //gets the users name try{ System.out.print("Enter name: "); name = reader.readLine(); }catch(Exception e){ System.out.println("Invalid input"); System.exit(0); } //for loop that prints the name one hundred times for(int counter = 0; counter < 100; counter++){ System.out.println(name); } } }Introduction to Programming I 21
22. 22. J.E.D.I.6.4 PowersUsing while-loop: import javax.swing.JOptionPane; /** * Computes the power of a number given the base and the * exponent. The exponent is limited to positive numbers only. */ public class Powers { public static void main(String[] args){ int base = 0; int exp = 0; int power = 1; int counter = 0; //gets the user input for base and power using // JOptionPane base = Integer.parseInt (JOptionPane.showInputDialog("Base")); exp = Integer.parseInt (JOptionPane.showInputDialog("Exponent")); //limits the exp to positive numbers only if(exp < 0 ){ JoptionPane.showMessageDialog (null,"Positive numbers only please"); System.exit(0); } //while loop that solves for the power while(counter < exp){ power = power*base; counter++; } //displays the result JoptionPane.showMessageDialog (null,base+" to the "+exp+ " is "+power); } }Introduction to Programming I 22
23. 23. J.E.D.I.Using do-while loop: import javax.swing.JOptionPane; /** * Computes the power of a number given the base and the * exponent. The exponent is limited to positive numbers only. */ public class Powers { public static void main(String[] args){ int base = 0; int exp = 0; int power = 1; int counter = 0; //gets the user input for base and power //using JOptionPane base = Integer.parseInt(JOptionPane.showInputDialog ("Base")); exp = Integer.parseInt(JOptionPane.showInputDialog ("Exponent")); //limits the exp to positive numbers only if(exp < 0 ){ JoptionPane.showMessageDialog (null,"Positive numbers only please"); System.exit(0); } //do-while loop that solves the power given the base // and exponent do{ if(exp != 0) power = power*base; counter++; }while(counter < exp); //displays the result JoptionPane.showMessageDialog(null,base + " to the "+exp + " is "+power); } }Introduction to Programming I 23
24. 24. J.E.D.I.Using for loop: import javax.swing.JOptionPane; /** * Computes the power of a number given the base and the * exponent. The exponent is limited to positive numbers only. */ public class Powers { public static void main(String[] args){ int base = 0; int exp = 0; int power = 1; int counter = 0; //gets the user input for base and power using // JOptionPane base = Integer.parseInt(JOptionPane.showInputDialog ("Base")); exp = Integer.parseInt(JOptionPane.showInputDialog ("Exponent")); //limits the exp to positive numbers only if(exp < 0 ){ JoptionPane.showMessageDialog(null,"Positive numbers only please"); System.exit(0); } //for loop for computing the power for(counter = 0; counter < exp; counter++){ power = power*base; } //displays the result JoptionPane.showMessageDialog(null,base + " to the "+exp + " is "+power); } }Introduction to Programming I 24
25. 25. J.E.D.I.Chapter 7 Exercises7.1 Days of the WeekUsing while loop: /** * Uses an array string to save the days of the wee * then prints it on the screen. */ public class DaysOfTheWeek { public static void main(String[] args){ //declares the String array of the days of the week String[] days = {"Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday"}; int counter = 0; //while loop that prints the days of the week while(counter < days.length){ System.out.println(days[counter]); counter++; } } }Using do-while loop: /** * Uses an array string to save the days of the wee * then prints it on the screen with a do-while loop. */ public class DaysOfTheWeek { public static void main(String[] args){ //declares the String array of the days of // the week String[] days ={"Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday"}; int counter = 0; //do-while loop that prints the days of the // week do{ System.out.println(days[counter]); counter++; }while(counter < days.length);Introduction to Programming I 25
26. 26. J.E.D.I. } }Using for loop: /** * Uses an array string to save the days of the wee * then prints it on the screen with a for loop. */ public class DaysOfTheWeek { public static void main(String[] args){ //declares the String array of the days of // the week String[] days ={"Sunday","Monday","Tuesday", "Wednesday","Thursday","Friday", "Saturday"}; //for loop that prints the days of the week for(int counter = 0; counter < days.length; counter++) System.out.println(days[counter]); } }7.2 Greatest number import javax.swing.JOptionPane; /** * A program that uses JOptionPane to get ten numbers * from the user then outputs the largest number. */ public class GreatestNumber { public static void main(String[] args){ int[] num = new int[10]; int counter; int max = 0; //for loop that gets the 10 numbers from the user for(counter = 0; counter < 10; counter++){ num[counter] = Integer.parseInt (JoptionPane.showInputDialog( "Enter number "+(counter+1))); //gets the maximum number if((counter == 0)||(num[counter] > max)) max = num[counter]; } //displays the number with the greatest numberIntroduction to Programming I 26
27. 27. J.E.D.I. JoptionPane.showMessageDialog(null, "The number with the greatest value is "+max); } }Introduction to Programming I 27
28. 28. J.E.D.I.Chapter 8 Exercises8.1 Print Arguments /** * A program that prints the string from the command line if any. */ public class CommandLineSample { public static void main(String[] args){ //checks if a command line argument exists if(args.length == 0) System.exit(0); //for loop that prints the arguments from the //command line for(int counter=0;counter<args.length; counter++){ System.out.println(args[counter]); } } }Introduction to Programming I 28
29. 29. J.E.D.I.Chapter 9 Exercises9.1 Defining termsSee definitions in book.9.2 Java Scavenger HuntTo the teacher: These are just some sample methods in the Java API that you canuse. Check the Java API for more answers.Sample Usage: public class Homework1 { public static void main(String []args){ //1. endsWith String str = "Hello"; System.out.println( str.endsWith( "slo" ) ); //2. forDIgit System.out.println( Character.forDigit(13, 16) ); //4. floor System.out.println( Math.floor(3.14)); //5. isDigit System.out.println( "0=" + Character.isDigit(0)); System.out.println( "A=" +Character.isDigit(A)); //3. System.exit(1); System.out.println("if this is executed, exit was not called"); } }Class and Method declaration:1. Class: String Method: public boolean endsWith( String suffix )2. Class: Character Method: public static char forDigit( int digit, int radix )3. Class: System Method: public static void exit( int status )4. Class: Math Method: public static double floor( double a )5. Class: Character Method: public static boolean isDigit( char ch )Introduction to Programming I 29
31. 31. J.E.D.I. return add; }Introduction to Programming I 31
32. 32. J.E.D.I. /** * changes the variable add */ public void changeAddress(String add){ this.add = add; } /** * returns the variable tel */ public int getTelNumber(){ return tel; } /** * changes the variable tel */ public void changeTelNumber(int tel){ this.tel = tel; } /** * returns the variable email */ public String getEmailAdd(){ return email; } /** * changes the variable email */ public void changeEmailAdd(String email){ this.email = email; } }Introduction to Programming I 32
34. 34. J.E.D.I. //checks for the appropriate action for // his choice if(act.equals("A")||act.equals("a")) addBook.addEntry(); else if(act.equals("D")||act.equals("d")) addBook.delEntry(); else if(act.equals("V")||act.equals("v")) addBook.viewEntries(); else if(act.equals("U")||act.equals("u")) addBook.updateEntry(); else if(act.equals("Q")||act.equals("q")) System.exit(0); else System.out.println ("Unknown command"); } }Introduction to Programming I 34
37. 37. J.E.D.I.Introduction to Programming I 37