Survey

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
```CS110D Programming Language I
Lab8 : More Loops (model answer)
Computer Science Department
Fall 2016
Lab Objectives:
After finishing this lab, the students will practice:
 Using different looping mechanisms.
 Using continue and break statements.
 Using nested control structures.
Lab Exercise 1:
Problem Description
For the given program segments, read the code and write the expected output in the space provided
below. [Note: Do not execute these statements on a computer.]
Program Segment
for (int i = 0; i <= 12; i++) {
if (i % 2 == 0) {
continue;
}
if (i == 7) {
break;
}
System.out.printf("%d \n", i);
}
Page|2
Program Segment
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 5; j++) {
System.out.printf("%4d", i*j);
}
System.out.println();
}
Program Segment
Int num = 4;
do {
System.out.println(num);
num+=4;
} while (num <= 20)
Page|3
Lab Exercise 2:
Problem Description
The process of finding the largest value (i.e., the maximum of a group of values) is used frequently in
computer applications. For example, a program that determines the winner of a sales contest would
input the number of units sold by each salesperson. The salesperson who sells the most units wins the
contest.
Use a do..while loop to write a java application that inputs a series of 10 integers then determines
and prints the largest integer. Your program should use at least the following three variables:
A) Counter: A counter to count to 10 (i.e., to keep track of how many numbers
have been input and to determine when all 10 numbers have been processed).
B) Number: The integer most recently input by the user.
C) Largest: The largest number found so far.
Sample output
Enter number: 56
Enter number: -10
Enter number: 200
Enter number: 25
Enter number: 8
Enter number: 500
Enter number: -20
Enter number: 678
Enter number: 345
Enter number: 45
The Largest Number is 678
Code
import java.util.Scanner;
public class LargestNum{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number, count = 0;
int largest = -100000000;
do {
System.out.println("Enter number: ");
number = input.nextInt();
if (number > largest){
largest = number;
}
count++;
} while (count<10);
System.out.println("Largest number is " + largest);
}
}
Page|4
Follow-up Questions and Activities
Modify the code to allow the user to enter unknown number of integers that ends with -9999 as sentinel
value.
Code
import java.util.Scanner;
public class LargestNum{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
int largest = -100000000;
do {
System.out.println("Enter number or -9999 to exit: ");
number = input.nextInt();
if (number == -9999){
break;
}
if (number > largest){
largest = number;
}
} while (true);
System.out.println("Largest number is " + largest);
}
}
Page|5
Assignment Problem(s)
Question 1
Write a java program that asks the user to input the length and width of a rectangle. Then it should
draw this rectangle on the screen using stars. [Hint: Use a for loop]
Sample output
Code
import java.util.Scanner;
public class RegtanglePrint{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int length, width;
System.out.print("Enter the length of the rectangle: ");
length = input.nextInt();
System.out.print("Enter the width of the rectangle: ");
width = input.nextInt();
for (int i = 0; i < width; i++) {
for (int j = 0; j < length; j++) {
System.out.print(" * ");
}
System.out.println();
}
}
}
Page|6
Question 2
Use a do..while loop to write a java program that asks the user to input a sequence of integers. Then it
should find and display each of the following:
a)
b)
c)
d)
Number of integers.
Sum of integers.
Number of odd integers.
Number of even integers.
[Hint: Use (-9999) as a sentinel value to end the sequence and skip an endless loop]
Code
import java.util.Scanner;
public class NumSummary{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number, odd = 0, even = 0, sum = 0, count = 0;
do {
System.out.println("Enter number or -9999 to stop: ");
number = input.nextInt();
if (number == -9999)
break;
if (number % 2 == 0)
even++;
else
odd++;
sum += number;
count++;
} while (true);
System.out.println("Number
System.out.println("Sum of
System.out.println("Number
System.out.println("Number
of integers is " + count);
integers is " + sum);
of odd integers is " + odd);
of even integers is " + even);
}
}
Page|7
```
Related documents