Posted on Leave a comment

FindRange.java solution – Assignment 2

[code lang=”java”]
/*
* File: FindRange.java
* Name:
* Section Leader:
* ——————–
* This file is the starter file for the FindRange problem.
*/

import acm.program.*;

public class FindRange extends ConsoleProgram {
private static final int SENTINEL = 0;

public void run() {
//the smallest number
int smallest = 0;

//the largest
int largest = 0;
//intro
println("The program finds the largest and smallest numbers.");

//get the first number
int number = readInt("? ");
//if there is only one number – make it the smallest and th
// the largest
smallest=number;
largest=smallest;

//loop and a half for the second number
//keep looping until the sentinel is given
while(number!=SENTINEL){
//get the second digit
int number1 = readInt("? ");

// breaking out of the loop
if(number1==SENTINEL){
break;
}

//find the smallest number
if(number1<smallest ){
smallest=number1;
}

//find the largest number
if(number1>largest){
largest=number1;
}
}

//if the first number is a 0
// else print the smallest and largest
if(number == SENTINEL){
println("Please enter a number not 0.");
} else {
println("smallest: "+smallest);
println("largest: "+largest);
}

}
}[/code]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.