homework4 - 15-100 HOMEWORK 4 FALL 2007Read sections...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-100 HOMEWORK 4 - FALL 2007Read sections 3.5-3.8 in your textbook and answer the following questions based on your readings and the course lectures.1. How many asterisks do the following code fragments print out? Explain each of your answers. (a) int i = 1; while (i != 20) { System.out.println("*"); i++; } (b) int i = 0; do { System.out.println("*"); i *= 3; } while (i < 20); (c) for (int i = 1; i < 100; i *= 2) System.out.println("*"); (d) for (int i = 1; i <= 9; i += 2) for (int j = i; j <= 9; j++) System.out.println("*"); 2. Write a Java code fragment that uses a do-whileloop to ask the user to input an integer representing an hour between 1 and 12 and repeat if the user enters an integer out of this range until the user finally enters a valid hour. If the user enters an invalid hour, you should print an error message as well. 3. Consider the sum of the first n positive odd numbers: sum = 1 + 3 + 5 + 7 + ... + (2n-1)....
View Full Document

This note was uploaded on 01/25/2010 for the course CS 15100 taught by Professor Tom during the Fall '07 term at Carnegie Mellon.

Page1 / 2

homework4 - 15-100 HOMEWORK 4 FALL 2007Read sections...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online