Csbp219 Sp24 Java Week2
Csbp219 Sp24 Java Week2
Spring 2025
Week 2-Part 1
Basics of Java
Reading and Writing from Keyboard and Files (I/O events).
Objectives
Output Results to Console
Formatted output
Input data into memory using input statements
File input & output
2
Output
Exercises: what is the output of the following program?
}
}
3
Output
Slide 4
4
Output
Exercises: what is the output of the following program?
System.out.println('A');
}
}
5
Formatting Output with printf
To format the output in specific manner,
method printf() is used:
Example
public class PrintTest {
public static void main(String[] args) {
int centimeters = 100;
System.out.printf(("There are %.2f inches in %d centimeters. \
n",
centimeters / 2.54, centimeters);
}
}
6
String Method format
Formatting the output can be done using the
String method format:
Example:
7
Input (Read) Statement
Java provides the class Scanner.
Scanner console = new Scanner(System.in);
Example:
Slide 8
8
Input (Read) Statement
Example:
Slide 9
9
File Input / Output
To input a file, a Scannerobject is initialized with a
FileReader object:
To close the input & output files, the method close is used:
inputFile.close();
outFile.close();
10
File Input / Output
11
Exercises
Write a program that will ask the user to input three
numbers, then calculates and displays their sum and
their average.
Write a program that will ask the user to input the
length and the width of a rectangle, then calculates and
displays the area and the perimeter of the rectangle.
Write a program that will ask the user to input an
amount of money in dollars, then displays the same
amount of money in dirhams.
Write a program that will open a file for reading, then
reads a student first name, his course work mark (out
of 65), and his final exam mark (out of 35). It will then
open another file for writing and write the student first
name, and his final mark (out of 100)
12
CSBP219: Object Oriented Programming
Spring 2024
Week 2 – Part 2
Basics of Java
Control Structure – Selection
Objectives
In this chapter you will learn:
Control structures
Relational & logic operators
Logic (boolean) expressions
if , if…else, if .. else if .. else
String comparison
14
Relational Operators
15
Logical (Boolean) Operators & Expressions
Logical (Boolean) Operators & Expressions
16
Conditional Expressions
Scanner console = new Scanner(System.in);
int score = console.nextInt();
if String grade = "";
19
equals Method
“Hi”.equals(“Hi”) True
“Billy”.equals(“Bill”)
“Bigger”.equals(“Big”)
“Hello”.equals(“hello”)
“Sun”.equals(“Sunny”)
20
compareTo Method
More than
“Hi”.compareTo(“Hi”) =0
“Hello”.compareTo(“Hi”) <0
“Ben”.compareTo(“Air”) >0
“Hello”.compareTo(“Hen <0
”)
“Billy”.compareTo(“Bill”) >0
“Big”.compareTo(“Bigger <0
”)
“Hello”.compareTo(“hell <0
o”) 21
Comparing String Variables
22
Exercises
Write a program that will ask the user to input
three numbers, then displays them in descending
order.
Write a program that will ask the user to input
three names, then displays them in ascending order.
Write a program that will ask the user to input his
weight in kilo grams and height in meters, then
calculates his BMI as follows:
BMI = weight / height**2
The program then displays “Underweight” if the BMI
is less than 18.5, “Normal” if the BMI is between 18.5
and 25, “Overweight” if the BMI is between 25 and
30, and “Obese” if the BMI is over 30.
23