Introduction to Programming in Java - Practice Quiz | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-lji-1
koofers-user-lji-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 15
Announcements
Quiz – Monday, Oct. 3
Read: Chapter 4, sections 4.11 and 4.12 (pages 100 – 110)
Last time: For loops
for ( ForInit; ForExpression; ForUpdate )
Statement
Draw flowchart, using above labels. Then redraw using below example.
int value = 1;
for ( int i = 0; i < 5; i++ ) {
System.out.println( value );
value *= 2;
}
Another example:
double money = 100;
double interest = .05;
int year;
for ( year = 1; year <= 20; year++ ) {
money *= 1 + interest;
}
S.O.PL("You have $"+ money + " after 20 years.");
Remember, loop control variable does not have to increase, and it does not have to
change its value by 1.
for ( int count = 20; count > 0; count -= 7 ) {
System.out.println( count );
}
Operators +=, -=, *=, /=, %=
In our programs, we frequently use a variable, add a value to it, and assign the result back
into the original variable:
valueSum = valueSum + value;
pf2

Partial preview of the text

Download Introduction to Programming in Java - Practice Quiz | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 15

Announcements Quiz – Monday, Oct. 3 Read: Chapter 4, sections 4.11 and 4.12 (pages 100 – 110) Last time : For loops for ( ForInit; ForExpression; ForUpdate ) Statement Draw flowchart, using above labels. Then redraw using below example. int value = 1; for ( int i = 0; i < 5; i++ ) { System.out.println( value ); value *= 2; } Another example: double money = 100; double interest = .05; int year; for ( year = 1; year <= 20; year++ ) { money *= 1 + interest; } S.O.PL("You have $"+ money + " after 20 years."); Remember, loop control variable does not have to increase, and it does not have to change its value by 1. for ( int count = 20; count > 0; count -= 7 ) { System.out.println( count ); } *Operators +=, -=, =, /=, %= In our programs, we frequently use a variable, add a value to it, and assign the result back into the original variable: valueSum = valueSum + value;

money = money * (1 + interest); This is a very common type of expression, so Java provides a shorthand operator for us valueSum += value; money = 1 + interest; // Also /=, -=, %= String s = ""; for ( int = 0; j < 20; j++ ) { s += ""; } Relationship between while loops and for loops. How can you convert one to the other? Look at the parts, show how they relate to each other What are the ‘rules’ for deciding whether to use a for loop or a while loop?

  • For loop: when you know ahead of time how many iterations there are.
  • While loop: when you just keep going until some condition is true Examples: Given a sorted array of ints: int[] myArray; Use a for or while?
  • to display all the values in the array
  • to find the first element whose value is larger than 15?
  • to display the last 5 elements of the array?
  • to calculate the sum of the elements?
  • to search for an element with the value 27? Quiz Review We’ve covered a lot of material since the last quiz
  • ArrayList
  • arrays
  • while and for loops
  • a lot more about methods and how to call them
  • creating objects with new
  • the this keyword
  • constructor overloading See Quiz2Review.ppt