{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Interactive Average with Methods (4)

Interactive Average with Methods (4) - CS 1050 Dr Tucker...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 1050, Dr. Tucker Interactive Average with Methods Spring 2010 // Interactive Average Program with methods // This program asks the user to input two real numbers, // calculates the average of these numbers, and // prints the results —— uses methods // Patricia Tucker, CS 1050, Spring 2010 // methods used // explain() -— explains the program to the user // getNum() —— returns number input by the user // cachvg(double numl, double num2) -- returns the average of numl and num2 // outputResults(double numl, double num2, double average) —— outputs numl, // num2, and average import java.util.*; public class IAw { static Scanner console = new Scanner(System.in); public static void main (String args) throws Exception { double numl, num2; // input values double average; // average of input values // Explain the program to the user explain(); // Input the two numbers System.out.println("Input your first number."); numl = getNum(); System.out.println("Input your second number."); num2 = getNum(); // Determine the average of the two numbers average = cachvg(numl, num2); // Output the results outputResults(numl, num2, average); } //****************************************************************** public static void explain() // explains the program to the user { System.out.println("This program averages 2 real numbers.");} //****************************************************************** public static double getNum() throws Exception // returns the number input by the user on one line { double num; // number input by the user num = console.nextDouble(); return num;} //****************************************************************** public static double cachvg<double a, double b) // returns the average of a and b { return (a + b)/2;} //****************************************************************** public static void outputResults(double numl, double num2, double average) // outputs the values of the two numbers (numl and num2) and their // average (average) // then outputs the programmer's name { System.out.print("The average of " + numl); System.out.println(" and " + num2 + " is " + average); System. out.print1n("Patricia Tucker") ;} //*******************~k********~k***********************~k~k*****~k****** } ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online