1 / 31

The 9 th and 10 th tutoring session Fall, 2012

The 9 th and 10 th tutoring session Fall, 2012. Review loop structures and improve CheckISBN and CourseAverage Go through Dr Cao’s Quiz2 Go through Dr Cao’s Assignment 2. Haidong Xue. 5:30pm—8:30pm 10 /2/2012 and 10/3/2012 . CSc2310 Tutoring Time: 5:30pm-8:30pm

chars
Download Presentation

The 9 th and 10 th tutoring session Fall, 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The 9thand 10thtutoring sessionFall, 2012 • Review loop structures and improve CheckISBN and CourseAverage • Go through Dr Cao’s Quiz2 • Go through Dr Cao’s Assignment 2 HaidongXue 5:30pm—8:30pm 10/2/2012 and 10/3/2012

  2. CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue There are 2 sections: 1. Review • Review loop structures and improve CheckISBN and CourseAverage • Go through Dr Cao’s Quiz2 • Go through Dr Cao’s Assignment 2 2. Q&A • Answer your questions about java programming

  3. Loop Structure – “while” • Grammar while(boolean expression) {code block} • Function: Repeat that: if the boolean expression is true, execute the code block once

  4. Loop Structure – “while” • Example inti=0; while (i<=10){ System.out.print(i+ " "); i++; } System.out.println("A"); • Results?

  5. Loop Structure – “do-while” • Grammar do {code block} while(boolean expression); • Function: 1. Execute the code block once 2. Repeat that: if the boolean expression is true, execute the code block once Note: there is a “;” at the end

  6. Loop Structure – “do-while” • Example inti=20; do { System.out.print(i+ " "); i++; } while(i<=10); System.out.println("A"); • Results?

  7. Loop Structure – “for” • Grammar for(expression 1; boolean expression; expression 2 ) { code block } • Function: expression 1; while(boolean expression){ code block; expression 2; }

  8. Loop Structure – “for” • Example for(int i=0; i<=10; i++){ System.out.print(i+ " "); } System.out.println("A"); • Results?

  9. Improve CheckISBN to CheckISNB Pro • http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CheckISBN.java • Which part can be significantly improved?

  10. Improve CheckISBN to CheckISNB Pro (…) int total = 10 * Integer.parseInt(reducedISBN.substring(0, 1)) + 9 * Integer.parseInt(reducedISBN.substring(1, 2)) + 8 * Integer.parseInt(reducedISBN.substring(2, 3)) + 7 * Integer.parseInt(reducedISBN.substring(3, 4)) + 6 * Integer.parseInt(reducedISBN.substring(4, 5)) + 5 * Integer.parseInt(reducedISBN.substring(5, 6)) + 4 * Integer.parseInt(reducedISBN.substring(6, 7)) + 3 * Integer.parseInt(reducedISBN.substring(7, 8)) + 2 * Integer.parseInt(reducedISBN.substring(8, 9)); (…)

  11. Improve CheckISBN to CheckISNB Pro Your improved code: (hint: use a loop)

  12. Improve CheckISBN to CheckISNB Pro My code: int total = 0; for(int i=0; i<=8; i++){ total += (10-i) * Integer.parseInt(reducedISBN.substring(i, i+1)); } CheckISBNProis at: http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CheckISBNPro.java

  13. Improve CourseAverage to CourseAveragePro • http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CourseAverage.java • Which part can be significantly improved?

  14. Improve CourseAverage to CourseAveragePro • // Collect 8 program scores • System.out.print("Enter program 1 score (0-20):"); • double program1 = s.nextDouble(); • System.out.print("Enter program 2 score (0-20):"); • double program2 = s.nextDouble(); • System.out.print("Enter program 3 score (0-20):"); • double program3 = s.nextDouble(); • System.out.print("Enter program 4 score (0-20):"); • double program4 = s.nextDouble(); • System.out.print("Enter program 5 score (0-20):"); • double program5 = s.nextDouble(); • System.out.print("Enter program 6 score (0-20):"); • double program6 = s.nextDouble(); • System.out.print("Enter program 7 score (0-20):"); • double program7 = s.nextDouble(); • System.out.print("Enter program 8 score (0-20):"); • double program8 = s.nextDouble(); • // Calculate the average program score • double programAverage = (program1 + program2 + program3 + program4 + program5 + program6 + program7 + program8)/8; Your improved code:

  15. Improve CourseAverage to CourseAveragePro • // Collect 8 program scores • System.out.print("Enter program 1 score (0-20):"); • double program1 = s.nextDouble(); • System.out.print("Enter program 2 score (0-20):"); • double program2 = s.nextDouble(); • System.out.print("Enter program 3 score (0-20):"); • double program3 = s.nextDouble(); • System.out.print("Enter program 4 score (0-20):"); • double program4 = s.nextDouble(); • System.out.print("Enter program 5 score (0-20):"); • double program5 = s.nextDouble(); • System.out.print("Enter program 6 score (0-20):"); • double program6 = s.nextDouble(); • System.out.print("Enter program 7 score (0-20):"); • double program7 = s.nextDouble(); • System.out.print("Enter program 8 score (0-20):"); • double program8 = s.nextDouble(); • // Calculate the average program score • double programAverage = (program1 + program2 + program3 + program4 + program5 + program6 + program7 + program8)/8; My code: // Collect 8 program scores and calculate the average program score double programAverage = 0; for(int i=1; i<=8; i++) { System.out.print("Enter program "+ i +" score (0-20):"); double program = s.nextDouble(); programAverage+=program; } programAverage /= 8;

  16. Improve CourseAverage to CourseAveragePro // Collect 5 quiz scores System.out.print("Enter quiz 1 score (0-10):"); double quiz1 = s.nextDouble(); System.out.print("Enter quiz 2 score (0-10):"); double quiz2 = s.nextDouble(); System.out.print("Enter quiz 3 score (0-10):"); double quiz3 = s.nextDouble(); System.out.print("Enter quiz 4 score (0-10):"); double quiz4 = s.nextDouble(); System.out.print("Enter quiz 5 score (0-10):"); double quiz5 = s.nextDouble(); // Calculate the average quiz score double quizAverage = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5)/5; Your improved code:

  17. Improve CourseAverage to CourseAveragePro // Collect 5 quiz scores System.out.print("Enter quiz 1 score (0-10):"); double quiz1 = s.nextDouble(); System.out.print("Enter quiz 2 score (0-10):"); double quiz2 = s.nextDouble(); System.out.print("Enter quiz 3 score (0-10):"); double quiz3 = s.nextDouble(); System.out.print("Enter quiz 4 score (0-10):"); double quiz4 = s.nextDouble(); System.out.print("Enter quiz 5 score (0-10):"); double quiz5 = s.nextDouble(); // Calculate the average quiz score double quizAverage = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5)/5; My code: // Collect 5 quiz scores and calculate the average quiz score double quizAverage = 0; for(int i=1; i<=5; i++) { System.out.print("Enter quiz " + i + " score (0-10):"); double quiz = s.nextDouble(); quizAverage+= quiz; } quizAverage /= 5; CourseAveragePro is at: http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CourseAveragePro.java

  18. Dr Cao’s Quiz 2 1. Let Account be the bank account class discussed in Chapter 3. What balance will be stored in acct1, acct2, and acct3 after the following statements have been executed? Account acct1 = new Account(100.00); Account acct2 = acct1; Account acct3 = new Account(200.00); acct1.withdraw(50.00); acct2.deposit(100.00); acct3.deposit(50.00);

  19. Dr Cao’s Quiz 2 Account acct1 = new Account(100.00); Account acct2 = acct1; Account acct3 = new Account(200.00); acct1.withdraw(50.00); acct2.deposit(100.00); acct3.deposit(50.00); Balance: 100 Balance: 200 acct3 acct1 acct2 Answer: Balance in acct1: 150.00. Balance in acct2: 150.00. Balance in acct3: 250.00.

  20. Dr Cao’s Quiz 2 2. Suppose that f1 and f2 are Fraction objects, where Fraction is the class described in Chapter 3. Write a statement that adds f1 and f2 and stores the result in a variable named f3. (The method that adds fractions is named add.) You may assume that f3 has already been declared as a Fraction variable. f3 = f1.add(f2); Fraction.java: http://www.cs.gsu.edu/~hxue1/csc2310/CodeExamples/Fraction.java

  21. Dr Cao’s Quiz 2 2. Suppose that f1 and f2 are Fraction objects, where Fraction is the class described in Chapter 3. Write a statement that adds f1 and f2 and stores the result in a variable named f3. (The method that adds fractions is named add.) You may assume that f3 has already been declared as a Fraction variable. f3 = f1.add(f2); Fraction.java: http://www.cs.gsu.edu/~hxue1/csc2310/CodeExamples/Fraction.java

  22. Dr Cao’s Quiz 2 3. Show the output of the following program. public class Problem47 { public static void main(String[] args) { String msg1 = "CSc 226J is available next quarter"; String msg2 = "Get a grip!"; String msg3 = "Now, let's eat!"; String sub1 = msg1.substring(7, 15); // it is “J is ava” String sub2 = msg2.substring(6, 8); // it is “gr” String sub3 = msg3.substring(11, msg3.length()); //it is “eat!” System.out.println(sub1.substring(0, 1) + // it is “J” sub1.substring(5, sub1.length()) + // it is “ava” sub1.substring(1, 5) + // it is “ is ” sub2 + sub3); // it is “great” } } Answer: Java is great!

  23. Dr Cao’s Quiz 2 4. What will be printed when the following statements are executed? Account acct1 = new Account(1000.00); Account acct2 = new Account(1000.00); if (acct1 == acct2) System.out.println("Equal"); else System.out.println("Not equal");

  24. Dr Cao’s Quiz 2 Account acct1 = new Account(1000.00); Account acct2 = new Account(1000.00); if (acct1 == acct2) System.out.println("Equal"); else System.out.println("Not equal");   Balance: 1000 Balance: 1000 acct1 acct2 Note: operator “==” only tells if they are pointing to the same object Answer: Not equal

  25. Dr Cao’s Quiz 2 5. Following is part of the source codes for NFLTeam3 and NFLGameDay3 classes. To make sure one can run following NFLGameDay3 and print the meaningful win/loss number of the teams on the screen, please write necessary methods to finish the class of NFLTeam3 (hints: at least 3 more methods are needed). public class NFLGameDay3 { public static void main (String [] args){ NFLTeam3 falcons = new NFLTeam3("falcons"); NFLTeam3 steelers = new NFLTeam3("steelers"); falcons.lossAgame(steelers); System.out.println (falcons); System.out.println (steelers); } }

  26. public class NFLGameDay3 { public static void main (String [] args){ NFLTeam3 falcons = new NFLTeam3("falcons"); NFLTeam3 steelers = new NFLTeam3("steelers"); falcons.lossAgame(steelers); System.out.println (falcons); System.out.println (steelers); } } public class NFLTeam3{ private int win; private int loss; private String name; public void winAgame(){ win++; } public void winAgame(NFLTeam3 teamB){ win++; teamB.lossAgame(); } public void lossAgame(){ loss++; } public NFLTeam3(String eName){ win=0; loss=0; name = eName; } public void lossAgame(NFLTeam3 teamB){ loss++; teamB.winAgame(); } public String toString (){ String shortName=name.substring(0, 3).toUpperCase(); return shortName + " Win/Loss: " + win + " / "+ loss + " games "; } }

  27. Dr Cao’s HW2 • Page 125 Problem 3 /* * The constructor with 1 parameter */ public Fraction( int nu) { this.numerator= nu; this.denominator= 1; } /* * The constructor with 0 parameter */ public Fraction() { this.numerator= 0; this.denominator= 1; }

  28. /* * Multiply */ public Fraction multiply (Fraction f) { inta = this.numerator; intb = this.denominator; intc = f.numerator; intd = f.denominator; intnewNu = a*c; intnewDe = b*d; return new Fraction(newNu, newDe); } /* * Subtract */ public Fraction subtract(Fraction f) { inta = this.numerator; intb = this.denominator; intc = f.numerator; intd = f.denominator; intnewNu = a*d-b*c; intnewDe = b*d; return new Fraction(newNu, newDe); }

  29. /* * Divide */ public Fraction divide (Fraction f) { inta = this.numerator; intb = this.denominator; intc = f.numerator; intd = f.denominator; intnewNu = a*d; intnewDe = b*c; return new Fraction(newNu, newDe); } The complete code is at: http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/Fraction.java

  30. Dr Cao’s HW2 • Page 125 Problem 5 public class ConverDate { public static void main(String[] args) { System.out.print("Enter date to be converted: "); Scanner s = new Scanner(System.in); String month = s.next(); month = month.substring(0, 1).toUpperCase() // set first letter to upper case + month.substring(1, month.length()).toLowerCase(); // set the rest to lower case String day = s.next(); day = day.substring(0, day.length()-1); // remove the "," String year = s.next(); s.close(); System.out.println("Converted date: " + day + " " + month + " " + year); } } http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/ConvertDate.java Code:

  31. Test all your improve code and let me know your questions I will be here till 8:30pm

More Related