0% found this document useful (0 votes)
2 views14 pages

Java Lect3

The document outlines several Java applications focusing on basic programming concepts such as addition, comparison, loops, and decision-making. It includes code examples for adding integers, comparing values using relational and equality operators, and using loops with break and continue statements. Additionally, it poses questions for practice to reinforce the concepts learned.

Uploaded by

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

Java Lect3

The document outlines several Java applications focusing on basic programming concepts such as addition, comparison, loops, and decision-making. It includes code examples for adding integers, comparing values using relational and equality operators, and using loops with break and continue statements. Additionally, it poses questions for practice to reinforce the concepts learned.

Uploaded by

ahmedgamal773j
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

Lecture3

Outline
Another Java Application: Adding Integers
Decision Making: Equality and Relational
Operators
2

Another Java Application: Adding Integers

• Upcoming program
– Use input dialogs to input two values
from user
– Use message dialog to display sum
of the two values
3
1 // Addition program that displays the sum of two numbers.
3
4 // Java packages
5 import [Link]; // program uses JOptionPane
6 [Link]
7 public class Addition {
8 1. import
9 // main method begins execution of Java application
10 public static void main( StringDeclare
args[] variables:
) name and type.
11 {
2. class Addition
12 String firstNumber; // first string entered by user
13 String secondNumber; // second string entered by user 2.1 Declare variables
14 (name and type)
15 int number1; // first number to add
16 int number2; // second number to add
17 int sum; // sum Input first integer
of number1 as a String,
and number2 assign 3.
18 to firstNumber. showInputDialog
19 // read in first number from user as a String
20 firstNumber = [Link]( "Enter first integer" ); 4. parseInt
21
22 // read in second number from user as a String
23 secondNumber = 5. Add numbers, put
24 [Link]( "Enter second integer" ); result in sum
25
Convert strings to integers.
26 // convert numbers from type String to type int
27 number1 = [Link]( firstNumber );
Add, place result in sum.
28 number2 = [Link]( secondNumber );
29
30 // add numbers
31 sum = number1 + number2;
32
4
33 // display result
34 [Link]( null, "The sum is " + sum,
35 "Results", JOptionPane.PLAIN_MESSAGE );
36
37 [Link]( 0 ); // terminate application with window
38
39 } // end method main
40
41 } // end class Addition

Program output
5
1 // [Link]
2 // Compare integers using if statements, relational operators
Outline
3 // and equality operators.
4
5 // Java packages [Link]
6 import [Link];
7
8 public class Comparison { 1. import
9
10 // main method begins execution of Java application
11 public static void main( String args[] ) 2. Class
12 { Comparison
13 String firstNumber; // first string entered by user
14 String secondNumber; // second string entered by user
2.1 main
15 String result; // a string containing the output
16
17 int number1; // first number to compare 2.2 Declarations
18 int number2; // second number to compare
19 2.3 Input data
20 // read first number from user as a string
(showInputDialo
21 firstNumber = [Link]( "Enter first integer:" );
22 g)
23 // read second number from user as a string
24 secondNumber = 2.4 parseInt
25 [Link]( "Enter second integer:" );
26
27 // convert numbers from type String to type int
2.5 Initialize result
28 number1 = [Link]( firstNumber );
29 number2 = [Link]( secondNumber );
30
31 // initialize result to empty String
32 result = "";  2008 MUFIC
33 All rights reserved.
6
34 if ( number1 == number2 )
35 result = result + number1 + " == " + number2;
36
37 if ( number1 != number2 ) Test for equality, [Link]
new string,
38 result = result + number1 + " != " + number2; assign to result.
39
40 if ( number1 < number2 )
41 result = result + "\n" + number1 + " < " + number2; 3. if statements
42
43 if ( number1 > number2 )
4.
44 result = result + "\n" + number1 + " > " + number2;
45
showMessageDialo
46 if ( number1 <= number2 ) g
47 result = result + "\n" + number1 + " <= " + number2;
48
49 if ( number1 >= number2 )
50 result = result + "\n" + number1 + " >= " + number2;
51
52 // Display results
53 [Link]( null, result, "Comparison Results",
54 JOptionPane.INFORMATION_MESSAGE );
55
56 [Link]( 0 ); // terminate application
57
58 } // end method main Notice use of
59
JOptionPane.INFORMATION_MESSAGE
60 } // end class Comparison
7

Program Output
8
1 // Fig. 5.5: [Link] Outline
2 // Summing integers with the for statement.
3 import [Link];
4 increment number by 2 each iteration [Link]
5 public class Sum {
6
7 public static void main( String args[] ) Line 12
8 {
9 int total = 0; // initialize sum
10
11 // total even integers from 2 through 100
12 for ( int number = 2; number <= 100; number += 2 )
13 total += number;
14
15 // display results
16 [Link]( null, "The sum is " + total);
19
20 [Link]( 0 ); // terminate application
21
22 } // end main
23
24 } // end class Sum

 2008 MUFIC
All rights reserved.
9
1 // Fig. 5.11: [Link]
2 // Terminating a loop with break.
3 import [Link];
4 [Link]
5 public class BreakTest {
6 exit for structure (break)
7 public static void main( String args[] ) Line 12
when count equals 5
8 {
9 String output = "";
Loop 10 times
Lines 14-15
10 int count;
11
12 for ( count = 1; count <= 10; count++ ) { // loop 10 times
13
14 if ( count == 5 ) // if count is 5,
15 break; // terminate loop
16
17 output += count + " ";
18
19 } // end for
20
21 output += "\nBroke out of loop at count = " + count;
22 [Link]( null, output );
23
24 [Link]( 0 ); // terminate application
25
26 } // end main
27
28 } // end class BreakTest
10
1 // Fig. 5.12: [Link] Outline
2 // Continuing with the next iteration of a loop.
3 import [Link];
4 [Link]
5 public class ContinueTest {
Skip line 16 and proceed va
to
6
7 public static void main( String args[] ) line 11 when count equals 5
8 { Loop 10 times Line 11
9 String output = "";
10
Lines 13-14
11 for ( int count = 1; count <= 10; count++ ) { // loop 10 times
12
13 if ( count == 5 ) // if count is 5,
14 continue; // skip remaining code in loop
15
16 output += count + " ";
17
18 } // end for
19
20 output += "\nUsed continue to skip printing 5";
21 [Link]( null, output );
22
23 [Link]( 0 ); // terminate application
24
25 } // end main
26
27 } // end class ContinueTest

 2008 MUFIC
All rights reserved.
11
Example
12
13
• Example : Write a program that calculates and
• prints out the Average grade for 5 students or
ends the program by entering -1.
int grade=0, counter =1, sum=0; [Link](" Enter 5 grades
Exit “); while (counter <=5 && grade !=-1)
{
grade = [Link]( );
sum += grade;
counter ++;
}
[Link](" The sum of grades is %d“,sum);
14

Questions
1. Write an application that asks the user to
enter two integers, obtains them from the user
and prints their sum, product, difference and
quotient (division).
2. Write an application that inputs three integers
from the user and displays the sum, average,
product, smallest and largest of the numbers.
3. Write an application that reads an integer and
determines and prints whether it is odd or
even.

You might also like