Docsity

Understanding Classes, Methods, and Objects in Java: A Comprehensive Guide - Prof. Orlena , Study notes of Computer Science

Partial preview of the text

Download Understanding Classes, Methods, and Objects in Java: A Comprehensive Guide - Prof. Orlena and more Study notes Computer Science in PDF only on Docsity!

CS121: Computer Programming I

A) Practice with Java Control Structures B) Methods Dr Olly Gotel [email protected] http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors

This Week’s Agenda (Part A)

  • Lots of practice with conditionals & loops
  • Some loop extras
  • A word on scope & local variables Finishing up with nested loops from week 3’s slides… lots of class examples…

Changing Between Loops

  • Write the following while loop as a for loop: int count = 1; while (count < 5){ System.out.println(“Num = ” + count); count++; }

Answer

for (int count = 1; count <5; count++) System.out.println(“Num = ” + count);

Exercise

  • Write a Java program that accepts student marks from a user (from 0 to 100) until the user enters a bogus value (like 999 or –3): - print out the highest mark entered - print out the lowest mark entered - (use if statements within a while loop) Call your file StudentMarks.java

This is called a sentinel value

An Answer

Uh oh – the most common mistake! What is wrong with this? Hint – look at the initial values of max & min Also – try using a do-while loop import java.util.Scanner; next = scan.nextInt(); Scanner scan = new Scanner(System.in); next = scan.nextInt();

Working With Strings

  • A string in Java is a sequence of characters
  • Sometimes useful to identify separate elements

within a string (called tokens):

“This is a string”

  • Extracting these elements is tokenising the string token token token token delimiter delimiter delimiter

StringTokenizer Class

  • Class from the Java Standard Class Library to work with string elements (part of java.util package)
  • Use as is for now – you can do some interesting loops by processing strings Try this out…

Want to Know What’s Going On?

  • Loop doesn’t do what you expect – need some diagnostic tools?
  • Tracing variables – watching variables change when the program is running – helps with debugging
  • Insert some temporary println() statements in your code to print the value of changing variables – trace statements
  • Remove the trace statements after debugging

Example

int count, sum = 0; System.out.println(“Sum is ” + sum); for (count = 1; count < 10; count++){ sum = sum + count; System.out.println(“Count is ” + count); System.out.println(“Sum is ” + sum); } System.out.println(“Sum = ” + sum);

Example

From [Savitch 2004] – ignore the old ways of reading input (superseded by Scanner in Java 5.0)

When To Use a break Statement

  • When a condition changes inside a loop & you don’t want to carry out the remaining loop statements
  • break statements can make your code hard to read Avoid or use sparingly

Compound Statements – A Bit Xtra

  • Can declare variables inside compound statements
  • Called local variables – used for temporary/transient matters
  • A local variable is only valid inside its local scope // counter does not exist out here { int counter = 10; // Can use counter in here … } // Can’t use counter here A compound statement defines

local scope

counter is a local variable

Scope & Local Variables

while (condition) { int x = 10; // can use x here } // can’t use x here

x is a local variable – it has local scope within the

enclosing braces (i.e. ONLY exists and can be used inside here { here } )