Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Programming with Java: CE 274

Course Lecturer: Vincent M. Nofong, Ph.D.


Computer Science and Engineering Department
University of Mines and Technology, Tarkwa - Ghana

February 10, 2019

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

What is a Variable?
A variable is a storage location in a computer program.
Each variable has a name and holds a value.
Variables are used to store values to be used later in a program.
Why are they called variables?
Because their values can be changed

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

About Variables
They are for representing data of a certain type.
To use a variable, you declare it by telling the compiler its
name as well as the type of data it can store.
Why must we declare the variables?
To tell the compiler to allocate an appropriate memory space for the
variable based on its data type.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Variable Declaration
The syntax for declaring a variable is:
dataType variableName = value; OR dataType variableName;
For example:

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Variable Declaration

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Variable Declaration
There are three types of variables in Java:
1 Local variable: A variable which is declared inside a method.
2 Instance Variable: A variable which is declared inside the
class but outside the method, and is not static
3 Static variable: A variable that is declared as static.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Variable Declaration

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variable Declaration - Data Types
There are two data types available in Java:
1 Primitive Data Types
byte (number, 1 byte)
short (number, 2 bytes)
int (number, 4 bytes)
long (number, 8 bytes)
float (float number, 4 bytes)
double (float number, 8 bytes)
char (a character, 2 bytes)
boolean (true or false, 1 bit)
2 Non-primitive
String, Array, etc
Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274
Elementary Programming
Variable Declaration - Things to Note
Few simple rules for the names of variables, methods, and classes:
Names must start with a letter or the underscore ( ) character.
You cannot use other symbols such as ? or %
Spaces are not permitted inside names
Names are case sensitive, i.e., studentMark and studentmark
are different names
Reserved words e.g. double or class cannot be used as names
Variables of the same type can be declared together. E.g:
int i, j, k;

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Identify and Solve the Problem in this Code

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Assignment Statements
Are used to designate values for variables.
In Java, the equal to sign (=) is used as the assignment
operator
Assignment Expressions
An expression representing a computation involving values,
variables, and operators that, taking them together, evaluates
to a value.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Assignment Statements and Assignment Expressions

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Variables

Identify and Solve the Problem in this Code

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Arithmetic

Solve: 2 + 3 ∗ 4 − 5 ∗ 6 =?
Arithmetic Operators

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Arithmetic

Arithmetic Expressions in Java


Arithmetic expressions must be written in straight-line form to
facilitate entering programs into the computer.
For instance, x = ba will be written as x = a/b;

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Arithmetic

Arithmetic Functions - Rules of Operator Precedence

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Arithmetic

Solve: 3 ∗ 6 % 5 + 6/3 − 8 =?
Best way to write Arithmetic in Java?
Use parentheses
Functions in parentheses are evaluated first

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Identify and Solve the problem with this code

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Input Reading from Keyboard or Console

Why read input from Keyboard?


- To enable programs accept input from the user
Java uses:
1 System.out to refer to the standard output device, and
2 System.in to refer to the standard input device.
NOTE: By default:
1 The output device is the monitor, and,
2 The input device is the Keyboard

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Input Reading from Keyboard or Console

Java print, println, etc methods display the value to console


Console input is not directly supported in Java.
However, you can use the Scanner or BufferedReader classes to
create objects that read from the System.in
Distinctions between print and println - practicals

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Input Reading from Keyboard or Console

Scanner vs BufferedReader

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Input Reading from Keyboard or Console

For the meantime, we will play with the Scanner.


BufferedReader - reading assignment

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Input Reading from Keyboard or Console

Scanner Syntax
The Scanner class can be used to create an object to read input
from System.in as:
Scanner input = new Scanner(System.in);
The syntax new Scanner(System.in):
Creates an object of the Scanner type
The syntax Scanner input:
Declares “input” as a variable whose type is Scanner.
Note: Scanner objects must be closed once they are no more being
used.
Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274
Elementary Programming
Practicals
(a) Create a Java program that when run:
1 Asks the user for his/her name
2 Accepts user input from keyboard
3 Displays a welcome message with the user name.
(b) Modify the above program such that when run:
1 Asks the user for his/her name
2 Asks the user for his/her age as an integer
3 Accepts user inputs from keyboard
4 Displays a message with the user’s name and age.
How do you ensure only integers are entered by the user?
We will treat this under exception handling.
Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274
Elementary Programming
Input Reading from Keyboard or Console

Practicals
(c) Create a Java program that when run:
1 Asks a user for the length of the triangle
2 Accepts an integer as the length from the keyboard
3 Asks the user for hight of the triangle
4 Accepts a double as the height from the keyboard
5 Computes the area of the triangle
6 Displays the computed are of the triangle.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming

Numeric Data Types and Operators


Java has six numeric data types for:
Integers (byte, short, int, long)
Floating point numbers (float, double)
with five operators (+, −, /, ∗, % )

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Numeric Data Types

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Numeric Operators
(+, −, /, ∗, % )
We have all used them before. We know what they do.
Practicals
Create a Java program that when run:
1 Ask user to enter time in seconds as an integer
2 Accepts user input
3 Converts the time in seconds into minutes and seconds
4 Displays the conversion to user.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Numeric Operators - Increment and Decrement Operators


The Increment Operator ++
A shorthand for incrementing a variable by 1
The Decrement Operator (−−)
A shorthand for decrementing a variable by 1
Examples: Given int i =2; int j =2;
i++; ⇒ i becomes 3
j−−; ⇒ j becomes 1

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Numeric Operators - Increment and Decrement Operators


Increment and decrement operators could be:
Preincrement (++i) OR Postincrement (i++)
Predecrement (−−i) OR Postdecrement (i−−)

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Numeric Operators - Increment and Decrement Operators

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Numeric Type Conversion


Can we perform an operation with two numeric types? YES
1/2 =?
1.0/2 =?
You can always assign a value to a numeric variable whose
type supports a larger range of values.
Example: you can assign a long value to a float variable.
You cannot, however, assign a value to a variable of a type
with a smaller range unless you use type casting

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Numeric Type Conversion


What is casting?
An operation that converts a value of one data type into a
value of another data type.
About casting
Widening type: Casting a type with a small range to a type
with a larger range.
Narrowing type: Casting a type with a large range to a type
with a smaller range.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Numeric Type Conversion


Integer to double - System.out.println((double)2/3))
Double to integer - System.out.println((int)2.34)
Note: Fractional part will be truncated
Learn the rest
Can String be converted into a double, integer, float, etc.?
YES
Given: String height = ”1.76”; how can it be converted to a
double? double newHeight = Double.parseDouble(height);

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Decision Making - Equality and Relational Operators

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Revision

Decision Making - Equality and Relational Operators

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming
Decision Making - Equality and Relational Operators

Practicals
Create a Java program that when run:
1 Generates a random number between 0 and 4
2 Asks the user to guess the generated number
3 Accepts user input
4 Displays “Congratulations, you won” if user input is same as
random number
5 Displays “You lost, number too high” if user input is higher
than random number
6 Displays “You lost, number too low” if user input is lower than
random number.
Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274
Elementary Programming

Practicals
Write a program that reads two integer inputs from a user and
determines if the first is a multiple of the second.

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Elementary Programming

Practicals
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:
9
Fahrenheit = ∗ celsius + 32 (1)
5

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


Practicals
Body Mass Index (BMI) is a measure of health based on height and
weight. It can be calculated by taking your weight in kilograms and
dividing it by the square of your height in meters. The
interpretation of BMI for people 20 years or older is as follows:
BMI < 18.5: Underweight
18.5 ≤ BMI < 25.0: Normal
25.0 ≤ BMI < 30.0: Overweight
30.0 ≤ BMI: Obese
Write a program that prompts the user to enter a weight in
kilograms and height in meters and displays the BMI and the health
indication of the person

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274


References

Y. Daniel Liang (2014), Introduction to Java Programming,


Comprehensive Version, 10th Edition
Cay S. Horstmann (2012), Java Concepts: Early Objects, 7th
Edition.
Deitel, P. and Deitel, H. (2015) Java How to Program, 10th
Edition (Early Objects)

Course Lecturer: Vincent M. Nofong, Ph.D. Programming with Java: CE 274

You might also like