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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

The 3 rd and 4 th tutoring session -- A case study about using variables Fall, 2012 Haidong(Haydon) Xue 5:30pm8:30pm 9/11/2012 and 9/12/2012.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to Computer Programming Decisions If/Else Booleans.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Writing Classes (Chapter 4)
CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory.
CSC Programming I Lecture 8 September 9, 2002.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 4: Loops and Files
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 5 Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 11/27/2012 and 11/28/2012 -Class Members vs Instance Members.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
The 1 st and 2 nd tutoring session of CSc2310 Fall, 2012 Haidong Xue.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
CSC Programming I Lecture 6 September 4, 2002.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/13/2012 and 11/14/2012 -Dr Cao’s Assignment 5 -i.e. Problem 4 on page 335 and.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Java Review if Online Time For loop Quiz on Thursday.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Classes and.
The 7 th and 8 th tutoring session Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 9/25/2012 and 9/26/ Review control structures - Improve CheckISBN.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Chapter 7: Class Variables and Methods Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Class Methods.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CSC111 Quick Revision.
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
Chapter 5: Control Structures II
Building Java Programs
Starting Out with Java: From Control Structures through Objects
Building Java Programs
Java Fix a program that has if Online time for Monday’s Program
Building Java Programs
More on Classes and Objects
Java Fix a program that has if Online time for Monday’s Program
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

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

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

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

Example int i=0; while (i<=10){ System.out.print(i + " "); i++; } System.out.println("A"); Results? Loop Structure – “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 Loop Structure – “do-while” Note: there is a “;” at the end

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

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

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

Improve CheckISBN to CheckISNB Pro ng/CheckISBN.java ng/CheckISBN.java Which part can be significantly improved?

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

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

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:

Improve CourseAverage to CourseAveragePro ng/CourseAverage.java ng/CourseAverage.java Which part can be significantly improved?

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:

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;

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:

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:

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

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: Balance in acct2: Balance in acct3:

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: amples/Fraction.java amples/Fraction.java

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: amples/Fraction.java amples/Fraction.java

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!

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

Dr Cao’s Quiz 2 Answer: Not equal Account acct1 = new Account( ); Account acct2 = new Account( ); 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

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

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

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

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

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

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:

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