Page 1 of 29
Computer Science Notes
Chapter 2: Elementary Programming
These notes are meant to accompany Introduction to Java Programming: Brief Version, eighth edition by Y.
Daniel Lang.
Programming Skills in a Nutshell:
At the end of this chapter you should have the following programming skills:
1. Declare numeric variables to store numbers and perform calculations with those numeric variables.
2. Declare string and character variables to store text.
3. To get information from the user of a program.
4. To program with proper style.
5. To understand the fundamental flow of any program:
a. Tell the user what the program does.
b. Get any needed information, either from the user or from another input source.
c. Perform any needed calculations.
d. Report results as output to the monitor or other output source.
6. To understand how to translate a programming problem into code:
a. Decide what the program is supposed to do (unless someone tells you first).
b. Decide what information you’ll need, where you’ll get it, and where you’ll store it.
c. Write out how you would do the task yourself.
d. Translate that task into code using the programming techniques you have at your disposal.
7. Here is a template using the key programming skills you should have at this point:
Page 2 of 29
import java.util.Scanner;
// The import statement tells the compiler where to find additional classes to be used.
// In this case, the Scanner class will be used.
// Scanner reads in and stores keystrokes from the keyboard.
/**The Chap02Basics class implements an application that
* illustrates the basics of computer programming:
* retrieving user input, performing calculations,
* and generating output in a console.
* Specifically, this program computes the area, hypotenuse, and perimeter
* of a right triangle, given the lengths of the two legs.
* This is meant as an example of the material in Chapter 2 of the text
* _Introduction to Java Programming: Brief Version_, 8th ed. by Y. D. Liang
* @author Kevin Mirus
*/
public class Chap02Basics
{
/** Calculates the area, hypotenuse, and perimeter of a right triangle,
* given the lengths of the two legs.
* @param args is not used.
*/
public static void main(String[] args)
{
//Tell the user what the program does
System.out.println("This program will calculate the hypotenuse, " +
"perimeter, and area of a right triangle given the lengths " +
"of its legs.");
// ********************************************************************
// Here is the portion of the code that reads data into memory.
// ********************************************************************
//Declare variables for the data to be read in:
//i.e., the lengths of the two legs
double legA, legB;
//Declare a String object to store the uer's name,
//and initalize that String to store the default name Clyde
String userName = "Clyde";
//Create a Scanner object for reading in the user's input
Scanner keyboard = new Scanner(System.in);
//Prompt the user for his/her name, and store in userName
System.out.println("Please enter your name: ");
userName = keyboard.next();
//Prompt the user for the lengths of the legs,
//and store those two pieces of information in legA and legB.
System.out.println("Please enter the length of the "
+ "first leg of the right triangle:");
legA = keyboard.nextDouble();
System.out.println("Please enter the length of the "
+ "second leg of the right triangle:");
legB = keyboard.nextDouble();
// ********************************************************************
// Here is the portion of the code that
// performs calculations on the data in memory.
// ********************************************************************
//Declare variables for the quantities to be calculated.
double hypotenuse, perimeter , area;
Page 3 of 29
//Calculate the hypotenuse using the Pythagorean Theorem:
// a^2 + b^2 = c^2
// c = sqrt(a^2 + b^2)
hypotenuse = Math.sqrt(legA*legA + legB*legB);
//Calculate the perimeter by adding up the lengths of the three sides.
perimeter = legA + legB + hypotenuse;
//Calculate the area using the formula area = (1/2)(base)(height).
area = 0.5 * legA * legB;
// ********************************************************************
// Here is the portion of the code that displays
// memory values for output.
// ********************************************************************
//Report the results for hypotenuse.
System.out.println();
System.out.println("Okay, " + userName + ", the hypotenuse of your "
+ "right triangle is: " + hypotenuse);
//Report the results for perimeter.
System.out.println("The perimeter of your "
+ "right triangle is: " + perimeter);
//Report the results for area
System.out.println("The area of your "
+ "right triangle is: " + area);
//Tell the user that the program is done.
System.out.println("\nI hope you enjoyed your results.");
}//end of method main(String[])
}//end of class Chap02Basics
This program will calculate the hypotenuse, perimeter, and area of a right triangle given
the lengths of its legs.
Please enter your name:
Kevin
Please enter the length of the first leg of the right triangle:
2.8
Please enter the length of the second leg of the right triangle:
5.75
Okay, Kevin, the hypotenuse of your right triangle is: 6.3955062348495915
The perimeter of your right triangle is: 14.945506234849592
The area of your right triangle is: 8.049999999999999
I hope you enjoyed your results.