31
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

The 9 th and 10 th tutoring session Fall, 2012

  • Upload
    chars

  • View
    44

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: The  9 th and  10 th tutoring session Fall, 2012

The 9th and 10th tutoring sessionFall, 2012

Haidong Xue

5:30pm—8:30pm10/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

Page 2: The  9 th and  10 th tutoring session Fall, 2012

• 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

Page 3: The  9 th and  10 th tutoring session Fall, 2012

• Grammarwhile(boolean expression){code block}• Function: Repeat that: if the boolean expression is true, execute the code block once

Loop Structure – “while”

Page 4: The  9 th and  10 th tutoring session Fall, 2012

• Exampleint i=0;while (i<=10){ System.out.print(i + " "); i++;}System.out.println("A");

• Results?

Loop Structure – “while”

Page 5: The  9 th and  10 th tutoring session Fall, 2012

• Grammardo{code block}while(boolean expression);• Function: 1. Execute the code block once2. Repeat that: if the boolean expression is true, execute the code block once

Loop Structure – “do-while”

Note: there is a “;” at the end

Page 6: The  9 th and  10 th tutoring session Fall, 2012

• Exampleint i=20;do { System.out.print(i + " "); i++;}while(i<=10);System.out.println("A");

• Results?

Loop Structure – “do-while”

Page 7: The  9 th and  10 th tutoring session Fall, 2012

• Grammarfor(expression 1; boolean expression; expression 2 ){ code block }• Function: expression 1;while(boolean expression){ code block; expression 2;}

Loop Structure – “for”

Page 8: The  9 th and  10 th tutoring session Fall, 2012

• Example

for(int i=0; i<=10; i++){ System.out.print(i + " ");}System.out.println("A");

• Results?

Loop Structure – “for”

Page 9: The  9 th and  10 th tutoring session Fall, 2012

Improve CheckISBN to CheckISNB Pro

• http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CheckISBN.java

• Which part can be significantly improved?

Page 10: The  9 th and  10 th tutoring session Fall, 2012

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));(…)

Page 11: The  9 th and  10 th tutoring session Fall, 2012

Improve CheckISBN to CheckISNB Pro

Your improved code:

(hint: use a loop)

Page 12: The  9 th and  10 th tutoring session Fall, 2012

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

Page 13: The  9 th and  10 th tutoring session Fall, 2012

Improve CourseAverage to CourseAveragePro

• http://www.cs.gsu.edu/~hxue1/csc2310Tutoring/CourseAverage.java

• Which part can be significantly improved?

Page 14: The  9 th and  10 th tutoring session Fall, 2012

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:

Page 15: The  9 th and  10 th tutoring session Fall, 2012

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 scoredouble 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;

Page 16: The  9 th and  10 th tutoring session Fall, 2012

Improve CourseAverage to CourseAveragePro

// Collect 5 quiz scoresSystem.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 scoredouble quizAverage = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5)/5;

Your improved code:

Page 17: The  9 th and  10 th tutoring session Fall, 2012

Improve CourseAverage to CourseAveragePro

// Collect 5 quiz scoresSystem.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 scoredouble quizAverage = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5)/5;

My code:

// Collect 5 quiz scores and calculate the average quiz scoredouble 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

Page 18: The  9 th and  10 th tutoring session Fall, 2012

Dr Cao’s Quiz 21. 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);

Page 19: The  9 th and  10 th tutoring session Fall, 2012

Dr Cao’s Quiz 2Account 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.

Page 20: The  9 th and  10 th tutoring session Fall, 2012

Dr Cao’s Quiz 22. 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

Page 21: The  9 th and  10 th tutoring session Fall, 2012

Dr Cao’s Quiz 22. 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

Page 22: The  9 th and  10 th tutoring session Fall, 2012

Dr Cao’s Quiz 23. 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!

Page 23: The  9 th and  10 th tutoring session Fall, 2012

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

Page 24: The  9 th and  10 th tutoring session Fall, 2012

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

Balance: 1000

acct1 acct2

Note: operator “==” only tells if they are pointing to the same object

Page 25: The  9 th and  10 th tutoring session Fall, 2012

Dr Cao’s Quiz 25. 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);}

}

Page 26: The  9 th and  10 th tutoring session Fall, 2012

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

}

Page 27: The  9 th and  10 th tutoring session Fall, 2012

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

Page 28: The  9 th and  10 th tutoring session Fall, 2012

/* * 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);}

Page 29: The  9 th and  10 th tutoring session Fall, 2012

/* * 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

Page 30: The  9 th and  10 th tutoring session Fall, 2012

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

Page 31: The  9 th and  10 th tutoring session Fall, 2012

Test all your improve code and let me know your questions

I will be here till 8:30pm