Presentation is loading. Please wait.

Presentation is loading. Please wait.

The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage.

Similar presentations


Presentation on theme: "The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage."— Presentation transcript:

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

2 CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue 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 Grammar while(boolean expression) {code block} Function: Repeat that: if the boolean expression is true, execute the code block once Loop Structure – “while”

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

5 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 Loop Structure – “do-while” Note: there is a “;” at the end

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

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

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

9 Improve CheckISBN to CheckISNB Pro http://www.cs.gsu.edu/~hxue1/csc2310Tutori ng/CheckISBN.java http://www.cs.gsu.edu/~hxue1/csc2310Tutori ng/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)); } CheckISBNPro is at: http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CheckISBNPro.java http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CheckISBNPro.java

13 Improve CourseAverage to CourseAveragePro http://www.cs.gsu.edu/~hxue1/csc2310Tutori ng/CourseAverage.java http://www.cs.gsu.edu/~hxue1/csc2310Tutori ng/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 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 acct1 acct2 acct3 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/CodeEx amples/Fraction.java http://www.cs.gsu.edu/~hxue1/csc2310/CodeEx amples/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/CodeEx amples/Fraction.java http://www.cs.gsu.edu/~hxue1/csc2310/CodeEx amples/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 Answer: Not equal 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 acct1 acct2 Note: operator “==” only tells if they are pointing to the same object

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 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 "; } 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); }

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

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

29 /* * Divide */ public Fraction divide (Fraction f) { int a = this.numerator; int b = this.denominator; int c = f.numerator; int d = f.denominator; int newNu = a*d; int newDe = b*c; return new Fraction(newNu, newDe); } The complete code is at: http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/Fraction.java 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); } Code: http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/ConvertDate.java

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


Download ppt "The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage."

Similar presentations


Ads by Google