User Tools

Site Tools


it-artikel:java:my-solution-on-assignment-2-problem-5

My Solution on Assignment 2 - Problem 5

This is my Solution of Assignment 2 - Problem 5 from the Computer Science Course CS106A of Prof. Mehran Sahami at the STANFORD University.

FindRange.java
/*
 * File: FindRange.java
 * Name: Axel Werner
 * Section Leader: 
 * --------------------
 * This file is the starter file for the FindRange problem.
 */
 
import acm.program.*;
 
public class FindRange extends ConsoleProgram {
 
 
	/**
	 * Main Program to requests a list of integers from the user
	 * to determine and report the min. + max. numbers in the list.
	 * 
	 */
	public void run() {
		println("FindRange\n"+
				"==============================\n\n"	);
 
		println(	"Please enter a list of integer Numbers, one a line,\n" +
					"or enter '" + SENTINEL + "' to finish the data entry.\n"	);
 
		input = readInt("?:");
		if(input==SENTINEL){
			/*
			 * Special Case 2:
			 * No values has been entered. Aborting Programm with
			 * special note.
			 */
			println("No Values has been entered. Aborting Programm.");
		} else {
			/*
			 * Special Case 1:
			 * Since this is the FIRST VALUE it is also the smallest
			 * AND the largest entry so far i put it
			 * to the Stats.
			 * Background:
			 * integer vars seem to get a "default value" of "ZERO" when defined.
			 * That makes is problematic when comparing with min and max. 
			 */
			max=input;
			min=input;
			while(	input != SENTINEL	){
				if(input>max){
					max=input;
				}
				if (input<min) {
					min=input;
				}
				input = readInt("?:");
			}
			println(	"Thanks!\n"	);
			println(	"Smallest Number entered is: "	+ min	);
			println(	"Largest Number entered is: "	+ max	);
		}
	}
 
 
	private static final int SENTINEL = 0;
 
	private int min;
	private int max;
	private int input;
}

Axel Werner 2012-04-02 17:09

it-artikel/java/my-solution-on-assignment-2-problem-5.txt · Last modified: 2022-08-31 12:30 by 127.0.0.1