0% found this document useful (0 votes)
5 views23 pages

Csbp219 Sp24 Java Week2

The document outlines the curriculum for CSBP219: Object Oriented Programming, focusing on the basics of Java, including input/output operations, formatted output, and control structures. It includes examples of Java programs, exercises for students, and explanations of string comparisons and conditional expressions. The content is structured for a Spring 2025 course, with practical exercises to reinforce learning.

Uploaded by

hudamkhaled2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

Csbp219 Sp24 Java Week2

The document outlines the curriculum for CSBP219: Object Oriented Programming, focusing on the basics of Java, including input/output operations, formatted output, and control structures. It includes examples of Java programs, exercises for students, and explanations of string comparisons and conditional expressions. The content is structured for a Spring 2025 course, with practical exercises to reinforce learning.

Uploaded by

hudamkhaled2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CSBP219: Object Oriented Programming

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?

public class PrintTest {


public static void main(String[] args) {
System.out.println("A");
System.out.println();
System.out.print("B");
System.out.println("C");
System.out.print("D");
System.out.print(" ");
System.out.println("E");
Slide 3

}
}

3
Output
Slide 4

4
Output
Exercises: what is the output of the following program?

public class PrintTest {


public static void main(String[] args) {
System.out.println(29/7);
System.out.println("Hello \t there");
System.out.print("4 + 7");
System.out.println(4+7);
System.out.print("D"+4+7);
System.out.print("D"+(4+7));
System.out.print("Hello \n there");
Slide 5

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 store the output of a program in a file, class


PrintWriter is used:

 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 = "";

if(score >= 90)


grade = "A";
if(score < 90 && score >= 80)
grade = "B";
double rate = console.nextDouble();
 if … else
int hours = console.nextInt();
double wages;

if (hours <= 40)


wages = rate * hours;
else
wages = (rate * 40) + 1.5 * (rate * (hours -
40)); 17
Conditional Expressions
Scanner console = new
 if … else if … else Scanner(System.in);
int score = console.nextInt();
String grade = "";

if( score >= 90)


grade = "A";
else if (score >= 80)
grade = "B";
else if (score >= 70)
grade = "C";
else if (score >= 60)
grade = "D";
else
grade = "F";
18
String Comparisons
Compare the values of String objects: strings
are compared character by character, starting
with the first character:
1. equals method
2. compareTo method

Compare the references of the String


variables: strings are compared to determine if
they reference the same String object:
1. ==
2. !=

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

You might also like