01 Lesson - Basic Input output
01 Lesson - Basic Input output
Goal(s):
● To write Java programs that performs simple computations
Minds On…
Writing a program involves designing a strategy for solving a problem and then using a
programming language to implement that strategy.
First, let’s consider the simple problem of computing the area of a circle. How do we
write a program for solving this?
Writing a program involves two things: Designing an algorithm and translating the
algorithm into programming instructions, or code.
Tip: It’s a good practice to outline your program in the form of an algorithm before you
begin coding.
Line 4 declares two variables: radius and area of type double(real numbers).
Line 6 computes the area of the circle and stores the value in the variable name
‘area’.
Line 8 simply displays the output to the console (see the black window below).
Spend around 5-10 minutes looking through and taking note of the different methods and
constants the Java’s Math library provides here.
Reading Input From the Keyboard
Key Point
Reading input from the keyboard enables the program to accept input from the user. Let’s
rewrite the circle area program that will allow the user to input the radius from the keyboard.
Link
Identifiers
Key Point
Identifiers are the names that identify elements such as variables and methods in a
program.
In the circle area program, the variables radius and area are identifiers. All identifiers
must obey the following rules:
▪ An identifier must start with a lowercase letter; it cannot start with a number.
▪ An identifier cannot be a key word. More on reserved words later. Here is a list.
▪ An identifier can be of any length, but your Java compiler may impose
restrictions. Use identifiers of 31 characters or fewer to ensure portability.
For example, area and radius are legal identifiers, whereas 2A and d+4 would be illegal
identifiers because they do not follow the rules. The compiler detects illegal identifiers and
reports them as syntax errors.
Note: Since Java is case-sensitive, area, Area, and AREA are all different identifiers.
Tip: Identifiers are used to name variables, methods, and other things in a program. Descriptive
identifiers make programs easy to read. Avoid using abbreviations for identifiers—using
complete words is more descriptive. For example, numberOfStudents is better than
numStuds, numOfStuds, or even numOfStudents.
We will generally use descriptive names for complete programs. However, for brevity we will
occasionally use variable names such as i, j, k, x, y, and z in code snippets for testing
something out.
Assessment
1. Show the output of the following code:
5. }
6. }
5. Consider the code below whose purpose is to calculate the total area of a
building?
Link
You are to write the appropriate code which allows the user to enter the number of
floors as an integer and floor area as a double on lines 9 and 12 respectively.
3. double average;
5. }
7. import java.util.Scanner;
8. number3 = input.nextDouble();
9. }
7. Which of the following identifiers are valid? Which are Java keywords?
miles, Test, a++, --a, 4#R, $4, #44, apps
main, double, int, x, y, radius
Programming Exercises
1. (Convert Celsius to Fahrenheit) Write a program that reads a Celsius degree in a
double value from the console, then converts it to Fahrenheit and displays the
result. The formula for the conversion is as follows:
fahrenheit = (9 / 5) * celsius + 32
Hint: In Java 9/5 is 1 because 5 goes into 9 once. However 9.0/5 is 1.8. Read a bit more
about this at this link.
2. (Compute the volume of a cylinder) Write a program that reads in the radius and length
of a cylinder and computes the area and volume using the following formulas:
area = radius * radius *π
volume = area * length
3. (Convert feet into meters) Write a program that reads a number in feet, converts it to
meters, and displays the result. One foot is 0.305 meter. Here is a sample run:
4. (Convert pounds into kilograms) Write a program that converts pounds into kilograms.
The program prompts the user to enter a number in pounds, converts it to
kilograms, and displays the result. One pound is 0.454 kilograms.
Here is a sample run:
5. (Financial application: calculate tips) Write a program that reads the subtotal and
the gratuity rate, then computes the gratuity and total. For example, if the user enters
10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total.
Here is a sample run:
6. (Sum the digits in an integer) Write a program that reads an integer between 0 and 1000 and
adds all
the digits in the integer. For example, if an integer is 932, the sum of all its digits is
14. Hint: Use the % operator to extract digits, and use the / operator to remove the
extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.
Here is a sample run:
7. (Find the number of years) Write a program that prompts the user to enter the minutes
(e.g., 1 billion), and displays the number of years and days for the minutes. For
simplicity, assume a year has 365 days. Here is a sample run:
8. (Health application: BMI) Body Mass Index (BMI) is a measure of health on weight. It
can be calculated by taking your weight in kilograms and dividing by the square of
your height in meters. Write a program that prompts the user to enter a weight in
pounds and height in inches and displays the BMI. Note that one pound is 0.45359237
kilograms and one inch is 0.0254 meters. Here is a sample run:
9.