Lecture Notes, class 2 for csc211.
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
Recap Class 1
Recap Class 1Average Program
Write a Java program to compute the average of two integers. The integers will be entered using a dialog box. The answer will be displayed to the console .
Classes Used
- JOptionPane - has a method to pop up window and get input.
- String - Dialog box returns a String
- Integer - need to parse input into an integer.
- System - used to output to the console
Program Flow 1
- Get first integer
- Parse to an integer
- Get second integer
- Parse to an integer <
- Sum up the two integers.
- Divide by 2.0
- Display result
Program Flow 2
Accumulates the sum as the integers are being read in.
sum
initialized to 0.
Some Useful Math Functions.
The Math class provides us with some useful services (methods) that perform mathematical calculations. Here are a few, for more info Go To
- static int abs( int num )
- static double pow(double base, double exp)
- static double sqrt(double num)
- static double random()
Examples
Can use the return value from a method call as a value to assign a variable.