0% found this document useful (0 votes)
5 views

01 Lesson - Basic Input output

The document outlines the basics of Java programming, focusing on writing simple programs that perform computations, reading user input, and using identifiers for variables and methods. It includes an algorithm for calculating the area of a circle, programming tips, and exercises for practice. Additionally, it covers the rules for naming identifiers and provides various programming exercises to reinforce the concepts learned.

Uploaded by

j2cvpthc7h
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

01 Lesson - Basic Input output

The document outlines the basics of Java programming, focusing on writing simple programs that perform computations, reading user input, and using identifiers for variables and methods. It includes an algorithm for calculating the area of a circle, programming tips, and exercises for practice. Additionally, it covers the rules for naming identifiers and provides various programming exercises to reinforce the concepts learned.

Uploaded by

j2cvpthc7h
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lesson 1 – Basics of Java

Goal(s):
● To write Java programs that performs simple computations

● To read input from the keyboard

● To use identifiers to name elements such as variables and functions

● To use variables to store data

● To program using assignment statements and assignment expressions

Minds On…

Calculate the perimeter and area of a circle with radius 10 cm.


Action …

Writing a Simple Program


Key Point

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.

Here is the algorithm for computing the area of a circle:


1. Read in the circle’s radius
2. Compute the area using the formula:
area = π x radius x radius = π r 2
3. Display the result

Tip: It’s a good practice to outline your program in the form of an algorithm before you
begin coding.

Now let’s translate our algorithm to write a program in Java.


Program output

Line 4 declares two variables: radius and area of type double(real numbers).

Lines 4,6 contain comments. Comments are programmer’s notes to themselves or


about the program. In Java, one of the ways of starting a comment is by using the
double forward slash //.

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

Go to the link so you can experiment with it.


Spend around 5-10 minutes looking through and taking note of the different methods the
Scanner class provides us here. Specifically look at how you would scan in integers, doubles,
and strings.

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 is a string comprising letters, digits, and underscores (_).

▪ 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:

2. Rearrange the following lines of code to form a correct program.

1. double area = radius*radius*Math.PI;

2. public class Main{

3. System.out.println(“The area of the circle of radius ”+radius+ “ is ” + area);

4. public static void main(String[] args){

5. }

6. }

7. double radius = 20;

3. Match each word with the blank next to each sentence.


4. What is the exact output of the following code?

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.

6. Rearrange the following code to produce the correct output.

1. average = (number1 + number2 + number3)/3;

2. public static void main(String[] args){

3. double average;

4. public class Main{

5. }

6. double number1 = 9, number2 = 2, number3;

7. import java.util.Scanner;

8. number3 = input.nextDouble();

9. }

10. System.out.println(“The average of numbers ” +number1+ “, ”+ number2+ “,


”+number3+ “ is ”+ average);

11. System.out.print(“Enter a number: ”);

12. Scanner input = new Scanner(System.in);

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.

Here is a sample run.

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

Here is a sample run:

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.

You might also like