Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming
-
Upload
anjan-mahanta -
Category
Education
-
view
111 -
download
1
Transcript of Java Programming
![Page 2: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/2.jpg)
2
About the JAVA Technology
• Java technology is both a programming language and
a platform
• The Java programming language is a high-level
language that can be characterized by all of the
following
– Simple
– Architecture neutral
– Object oriented
– Portable
![Page 3: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/3.jpg)
3
About the JAVA Technology
• In the Java programming language, all source code is
first written in plain text files ending with the .java
extension.
• Those source files are then compiled into .class files
by the Java compiler (javac).
• A .class file does not contain code that is native to
your processor; it instead contains bytecodes-- the
machine language of the Java Virtual Machine.
![Page 4: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/4.jpg)
4
Concepts of Objects
Name the person(s) who developed Java?
Answer: Java was developed by James
Gosling & Patrick Naughton at Sun
Microsystems, Inc. in 1991.
What was the initial name of Java?
Answer: Java language was initially called
“Oak” and was renamed “Java” in 1995.
![Page 5: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/5.jpg)
5
Concepts of Objects
Important features of JAVA.
Java is a platform - independent language
It is highly reliable
It is a distributed language
It is an object oriented language
What kind of files contain Java source code?
Answer: The Java source code is saved in files with
names the end with “.java”.
![Page 6: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/6.jpg)
6
Concepts of Objects
What is source code?
Answer: Source code is the plain text that makes up
the part or all of a computer program.
What is bytecode?
Answer: Bytecode is a low-level computer language
translation of Java source code program.
![Page 7: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/7.jpg)
7
About the JAVA Technology
• The Java launcher tool (java) then runs your
application with an instance of the Java Virtual
Machine.
![Page 8: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/8.jpg)
8
Java compilation process
Java
Program
Java
Compiler
(javac)
Java
Byte Code Can be
executed
MS-DOS
Windows (X)
UNIX
Any other (O.S.)
Source Code Java Virtual
Machine
Platform
Independence
![Page 9: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/9.jpg)
9
About the JAVA Technology
• Because the Java Virtual Machine is available on
many different operating systems, the same .class
files are capable of running on
– Microsoft Windows,
– the Solaris TM Operating System (Solaris OS),
– Linux, or
– MacOS.
![Page 10: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/10.jpg)
10
1. Concepts of Objects
What is a Java virtual machine?
Answer: A Java virtual machine(JVM) is a software system that translates and executes Java bytecode.
Name two types of Java programs?
Answer: We can develop two types of Java programs
1. Stand-alone application
2. Web applets
![Page 11: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/11.jpg)
11
Concepts of Objects
What is an object?
Answer: An object is an identifiable entity with some
characteristics and behavior.
Example:
Object - Person
Variables - First name, Last name, Age, Weight
![Page 12: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/12.jpg)
12
Creating your First Application
• Your first application, HelloWorldApp, will simply
display the greeting "Hello world!". To create this
program, you will:
Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
Compile the source file into a .class file. The Java
compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine can understand. The instructions contained within this file are known as bytecodes.
Run the program. The Java launcher (java) uses the Java Virtual Machine to run your application.
![Page 13: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/13.jpg)
13
Create a Source File
• First, start your editor. You can launch the NotePad editor from
the Start menu by selecting Programs > Accessories >
NotePad. In a new document, type in the following code:
• /**
• * The HelloWorldApp class implements an application that
• * simply displays "Hello World!" to the standard output.
• */
• class HelloWorldApp {
• public static void main(String[] args) {
• //Display "Hello World!"
• System.out.println("Hello World!");
• }
• }
• You can save the file as HelloWorldApp.java
![Page 14: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/14.jpg)
14
Saving a Source File
• You can save the file as HelloWorldApp.java
![Page 15: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/15.jpg)
15
Compile the Source File
• Bring up a shell, or "command," window
• You can do this from the Start menu by choosing MS-DOS
Prompt (Windows 95/98) or Command Prompt (Windows
NT/XP), or by choosing Run... and then entering cmd
![Page 16: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/16.jpg)
16
Compile and Run
• To Compile the Program, execute the compiler, javac,
specifying the name of the source file on the command line
C:\> javac HelloWorldApp.java
• The java compiler creates a file called HelloWorldApp.class
• To run the program, we must use the Java Interpreter called
Java by,
java HelloWorldApp
• The output is displayed,
Hello World!
![Page 17: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/17.jpg)
17
Setting up a project
• Click on NetBeans icon in your desktop
• Choose File > New Project (Ctrl-Shift-N)
• Under Categories, select General
• Under Projects, select Java Class Library
• Click Next
• Under Project Name, enter MyProject1
• Click Finish
![Page 18: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/18.jpg)
18
Creating a new program
• Choose File > New Project
• Under Categories, select General
• Under Projects, select Java Application
• Click Next
• Under Project Name, enter Example1
• Click Finish
![Page 19: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/19.jpg)
19
Example 1
Type in the following code:
package example1;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello Java");
}
}
![Page 20: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/20.jpg)
20
Explanation of the program
• /** Creates a new instance of Main */
• This is a comment
![Page 21: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/21.jpg)
21
Explanation of the program
• package example1;
• name of the program
![Page 22: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/22.jpg)
22
Explanation of the program
• public static void main(String[ ] args) {
• This line begins the main() method.
![Page 23: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/23.jpg)
23
Explanation of the program
• // TODO code application logic here
• This is a single line comment.
• It begins with // and ends at the end of the line.
![Page 24: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/24.jpg)
24
Explanation of the program
• System.out.println("Hello Java");
• This line outputs the string “Hello Java” followed by a new line on the screen.
• The println() statement ends with a semicolon.
• All statements in Java end with a semicolon.
• The first } is the program ends main()
• The last } ends the class definition.
![Page 25: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/25.jpg)
25
Compiling and Running
• Click on Run in the Menu bar
• Select Run File
• Click on “Run Main.Java”
To save
• Press CTRL+S
![Page 26: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/26.jpg)
26
Example 2
package example2;
public static void main(String args[]) {
int num;
num=100;
System.out.println ("This is num: " + num);
num=num * 2;
System.out.print ("The value of num * 2 is ");
System.out.println(num);
}
}
![Page 27: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/27.jpg)
27
Example 3 package example3;
public static void main(String args[]) {
int x,y;
x=10;
y=20;
if (x<y) System.out.println(“x is less than y”);
x=x*2;
if (x==y) System.out.println(“x now equal to y”);
x=x+2;
if (x>y) System.out.println(“x now greater than y”);
}
}
![Page 28: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/28.jpg)
28
Example 4
package example4;
public static void main(String args[]) {
int x;
for(x=0; x<10; x=x+1)
System.out.println(“This is x:” +x);
}
}
![Page 29: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/29.jpg)
29
Lab Assignments
1. Write a program to display the following output
My name is ______
I am ____ years old
I study in IEP2 at LCCT
![Page 30: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/30.jpg)
30
Example 5
package example5;
public static void main(String args[]) {
int x,y;
y=20;
for(x=0; x<10; x++) {
System.out.println(“This is x:” +x);
System.out.println(“This is Y:” +y);
y=y-2;
}
}
}
![Page 31: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/31.jpg)
31
Seperators
Symbol Name Purpose
( ) Parentheses Used to contain list of
parameters
{ } Braces Used to maintain the
value of arrays
[ ] Brackets Used to declare array type
; Semicolon Terminates statements
, Comma Used to chain statements
together inside a for
statement
. Period Used to separate package
names and variables
![Page 32: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/32.jpg)
32
Data Types
• Integers
– This group includes byte, short, int, long which are for whole
valued signed numbers.
• Floating-point numbers
– This group includes float and double, which represent numbers with
fractional precision.
• Characters
– This group includes char, which represents symbols in a character
set, like letters and numbers.
• Boolean
– This group includes boolean, which is a special type for
representing true/false values.
![Page 33: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/33.jpg)
33
Integers
Name Width in Bits
long 64
int 32
short 16
byte 8
![Page 34: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/34.jpg)
34
Example 6
package example6;
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
lightspeed = 186000;
days=1000; // number of days
seconds=days * 24 * 60 * 60; // convert to seconds
distance=lightspeed * seconds; // compute distance
System.out.print(“In ” + days);
System.out.print(“ days light will travel about ”);
System.out.println(distance + “ miles.”);
}
}
![Page 35: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/35.jpg)
35
Floating-Point Types
Name Width in Bits
double 64
float 32
![Page 36: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/36.jpg)
36
Example 7
package example7;
public static void main(String args[]) {
double pi, r, a;
r= 10.8; // radius of circle
pi=3.1416; // value of pi
a= pi * r * r; // compute area
System.out.println(“Area of circle is: ” + a);
}
}
![Page 37: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/37.jpg)
37
Example 8 (Character)
package example8;
public static void main(String args[]) {
char ch1, ch2;
ch1=88; // code for X
ch2= ‘Y’;
System.out.print (“ch1 and ch2: ”);
System.out.println(ch1 + “ ” +ch2);
}
}
![Page 38: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/38.jpg)
38
Example 9 (Character)
package example9;
public static void main(String args[]) {
char ch1;
ch1=‘X’;
System.out.println (“ch1 contains: ” + ch1);
ch1++; // increment ch1
System.out.println(“ch1 is now ” +ch1);
}
}
![Page 39: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/39.jpg)
39
Example 10 (Booleans)
package example10;
public static void main(String args[]) {
boolean b;
b = false;
System.out.println(“b is ” +b);
b=true;
System.out.println(“b is ” +b);
}
}
![Page 40: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/40.jpg)
40
Example 11
(Dynamic Initialization) package example11;
public static void main(String args[]) {
double a=3.0, b=4.0;
// c is dynamically initialized
double c = Math.sqrt(a*a + b*b);
System.out.println(“Hypotenuse is ” +c);
}
}
![Page 41: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/41.jpg)
41
Example12
(Variable life time) package example12;
public static void main(String args[]) {
int x;
for(x=0; x<3; x++) {
int y=-1; // y is initialized each time
System.out.println(“Y is: ” +y); //this always prints -1
y=100;
System.out.println(“Y is now: ” +y);
}
}
}
![Page 42: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/42.jpg)
42
Example 13
(Automatic Conversions) package example 13;
public static void main(String args[]) {
byte b;
int i=257;
double d=323.142;
System.out.println(“\nConversion of int to byte.”);
b=(byte) i;
System.out.println(“i and b ” + i + “ ” +b);
System.out.println(“\nConversion of double to int.”);
i=(int) d;
System.out.println(“d and i ” + d + “ ” +i);
System.out.println(“\nConversion of double to byte.”);
b=(byte) d;
System.out.println(“d and b ” + d + “ ” +b);
}
}
![Page 43: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/43.jpg)
43
Arrays
• An array is a group of like-typed variables
that are referred to by a common name.
• One-dimensional Arrays
Syntax,
type var-name[];
Example,
int month_days[];
![Page 44: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/44.jpg)
44
Example 14
(One-Dimensional Arrays) package example14;
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0]=31;
month_days[1]=28;
month_days[2]=31;
month_days[3]=30;
month_days[4]=31;
month_days[5]=30;
month_days[6]=31;
month_days[7]=31;
month_days[8]=30;
month_days[9]=31;
month_days[10]=30;
month_days[11]=31;
System.out.println(“April has” + month_days[3] + “days”);
}
}
![Page 45: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/45.jpg)
45
Example 15
(One-Dimensional Arrays) package example15;
public static void main(String args[]) {
int month_days[]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println(“April has” + month_days[3] + “days”);
}
}
![Page 46: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/46.jpg)
46
Example 16
(One-Dimensional Arrays) package example16;
public static void main(String args[]) {
double nums[]={ 10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for (i=0; i<5; i++)
result = result + nums[i];
System.out.println(“Average is” + result / 5);
}
}
![Page 47: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/47.jpg)
47
Example 17
(Two-Dimensional Arrays) package example17;
public static void main(String args[]) {
int twoD[] []= new int [4] [5];
int i, j, k = 0;
for(i=0; i<4; i++)
for (j=0;j<5;j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + “ ”);
System.out.println();
}
}
}
![Page 48: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/48.jpg)
48
Example 17
(Two-Dimensional Arrays) package example17;
OUTPUT
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
![Page 49: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/49.jpg)
49
Example 18
(Two-Dimensional Arrays) package example18;
public static void main(String args[]) {
int twoD[] []= new int [4] [ ];
twoD[0] = new int [1];
twoD[1] = new int [2];
twoD[2] = new int [3];
twoD[3] = new int [4];
int i,j,k=0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j]=k;
k++;
}
for(i=0; i<4; i++){
for(j=0; j<i+1; j++)
System.out.print (twoD[i][j] + “ ”);
System.out.println();
}
}
}
![Page 50: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/50.jpg)
50
Example 18
(Two-Dimensional Arrays) package example18;
OUTPUT
0
1 2
3 4 5
6 7 8 9
![Page 51: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/51.jpg)
51
Example 19
(Two-Dimensional Arrays) package example19;
public static void main(String args[]) {
double m[] []= {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i,j;
for (i=0; i<4; i++) {
for (j=0; j<4; j++)
System.out.print(m[i][j] + “ ”);
System.out.println();
}
}
}
![Page 52: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/52.jpg)
52
Example 19
(Two-Dimensional Arrays) package example19;
OUTPUT
0.0 0.0 0.0 0.0
0.0 1.0 2.0 3.0
0.0 2.0 4.0 6.0
0.0 3.0 6.0 9.0
![Page 53: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/53.jpg)
53
Example 20
(Three-Dimensional Arrays) package example20;
public static void main(String args[]) {
int threeD[][][]= new int [3][4][5];
int i,j,k;
for(i=0; i<3; i++)
for(j=0;j<4;j++)
for(k=0;k<5;k++)
threeD[i][j][k]=i*j*k;
for(i=0; i<3; i++) {
for(j=0;j<4;j++) {
for(k=0;k<5;k++)
System.out.print (threeD[i][j][k] + “ ”);
System.out.println();
}
System.out.println();
}
}
}
![Page 54: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/54.jpg)
54
Example 20
(Three-Dimensional Arrays) package example20;
OUTPUT
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
![Page 55: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/55.jpg)
55
Arithmetic Operators
Operator Result
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
![Page 56: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/56.jpg)
56
Arithmetic Operators
Operator Result
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
![Page 57: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/57.jpg)
57
Example 21
(Arithmetic Operators) package example21;
public static void main(String args[]) {
System.out.println (“Integer Arithmetic”);
int a=1+1;
int b=a*3;
int c=b/4;
int d=c-a;
int e=-d;
System.out.println (“a= ” +a);
System.out.println (“b= ” +b);
System.out.println (“c= ” +c);
System.out.println (“d= ” +d);
System.out.println (“e= ” +e);
System.out.println(“\n Floating Point Arithmetic”);
double da=1+1;
double db=da*3;
double dc=db / 4;
double dd=dc-a;
double de=-dd;
![Page 58: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/58.jpg)
58
Example 21
(Arithmetic Operators)
System.out.println (“da= ” +da);
System.out.println (“db= ” +db);
System.out.println (“dc= ” +dc);
System.out.println (“dd= ” +dd);
System.out.println (“de= ” +de);
}
}
![Page 59: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/59.jpg)
59
Example 22
(Modulas Operator)
• The modulus operator, % returns the remainder of a division operator.
package example22;
public static void main(String args[]) {
int x= 42;
double y=42.25;
System.out.println (“x mod 10= ” + x % 10);
System.out.println (“y mod 10= ” + y % 10);
}
}
![Page 60: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/60.jpg)
60
Example 23 (Arithmetic Assignment Operator)
package example23;
public static void main(String args[]) {
int a=1;
int b=2;
int c=3;
a+=5;
b*=4;
c+=a*b;
c%=6;
System.out.println(“a = ” + a);
System.out.println(“b = ” + b);
System.out.println(“c = ” + c);
}
}
![Page 61: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/61.jpg)
61
Example 24 (Increment & Decrement)
package example24;
public static void main(String args[]) {
int a=1;
int b=2;
int c;
int d;
c= ++b;
d=a++;
c++;
System.out.println(“a = ” + a );
System.out.println(“b = ” + b );
System.out.println(“c = ” + c );
System.out.println(“d = ” + d );
}
}
![Page 62: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/62.jpg)
62
Relational & Logical Operators
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
& Logical AND
| Logical OR
?: Ternary if-then-else
![Page 63: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/63.jpg)
63
Example 25 (The ? Operator)
Syntax:
expression1 ? Expression 2 : Expression 3
package example25;
public static void main(String args[]) {
int i,k;
i=10;
k=i<0 ? -i: i;
System.out.println(“i = ” + k );
}
}
![Page 64: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/64.jpg)
64
Control Statements
• If is used to route program execution through two different paths.
Syntax:
if (condition) statement1;
else statement2;
Example:
int a,b;
if (a<b) a=0;
else
b=0;
![Page 65: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/65.jpg)
65
IF Statement
Example 26 package example26;
public static void main(String args[]) {
int i,k;
i=10;
k=20;
if (i<k) System.out.println(“i is less than k” );
else System.out.println(“k is less than i”);
}
}
![Page 66: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/66.jpg)
66
Nested IF Statement
package example27;
public static void main(String args[]) {
int month = 4;
String season;
if (month == 12 || month == 1 || month == 2)
season=“Winter”;
else if (month == 3 || month == 4 || month == 5)
season=“Spring”;
else if (month == 6 || month == 7 || month == 8)
season=“Summer”;
else if (month == 9 || month == 10 || month == 11)
season=“Autumn”;
else
season=“Bogus Month”;
System.out.println(“April is in the ” + season + “.” );
}
}
![Page 67: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/67.jpg)
67
Switch Case
package example28;
public static void main(String args[]) {
int month = 4;
String season;
switch (month) {
case 12:
case 1:
case 2:
season=“Winter”;
break;
case 3:
case 4:
case 5:
season=“Spring”;
break;
![Page 68: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/68.jpg)
68
Switch Case
case 6:
case 7:
case 8:
season=“Summer”;
break;
case 9:
case 10:
case 11:
season=“Autumn”;
break;
default:
season=“Bogus Season”;
}
System.out.println(“April is in the ” + season + “.” );
}
}
![Page 69: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/69.jpg)
69
Input Example 1
• Write a program to input a number and
check whether this number is even or odd.
![Page 70: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/70.jpg)
70
Code
Example 1 import java.io.*;
public static void main(String s[]) throws IOException{
int n;
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
System.out.println("Enter number");
String x=I.readLine();
n=Integer.parseInt(x);
if(n%2==0)
System.out.println("Even number");
else
System.out.println("Odd number");
}
}
![Page 71: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/71.jpg)
71
Input Example 2
• Write a program to input a letter and check
whether this letter is vowel or consonant.
![Page 72: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/72.jpg)
72
Code
Example 2 import java.io.*;
public static void main(String s[])throws IOException {
char c;
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
System.out.println("Enter a letter: ");
c=(char)I.read();
if (c=='a' || c=='e' ||c=='i'||c=='o'||c=='u' )
System.out.println("Vowel");
else
System.out.println("Consonant");
}
}
![Page 73: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/73.jpg)
73
Input Example 3
• Write a program to input three numbers
and print the highest number.
![Page 74: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/74.jpg)
74
Code
Example 3 import java.io.*;
public static void main(String s[])throws IOException {
int a,b,c,max;
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
System.out.println ("Enter three numbers");
String s1=I.readLine();
String s2=I.readLine();
String s3=I.readLine();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
if(a>b && a>c)
max=a;
else if(b>a && b>c)
max=b;
else
max=c;
System.out.println("Maximum Number= " +max);
}
}
![Page 75: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/75.jpg)
75
Input Example 4
• Write a program to input three numbers
and print the numbers in ascending order. First number = 7
Second number = 3
Third number = 5
Output
3
5
7
![Page 76: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/76.jpg)
76
Code 4.1
import java.io.*;
public static void main(String s[])throws IOException {
int a,b,c,max,mid,min;
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
System.out.println("Enter three numbers: ");
String s1=I.readLine();
String s2=I.readLine();
String s3=I.readLine();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
![Page 77: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/77.jpg)
77
Code 4.2
if(a>b)
{
if(b>c)
{
max=a;
mid=b;
min=c;
}
else
{
max=a;
mid=c;
min=b;
}
System.out.println( +min);
System.out.println(+mid);
System.out.println(+max);
}
![Page 78: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/78.jpg)
78
Code 4.3
else if(b>c)
{
if(c>a)
{
max=b;
mid=c;
min=a;
}
else
{
max=b;
mid=a;
min=c;
}
System.out.println( +min);
System.out.println(+mid);
System.out.println(+max);
}
![Page 79: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/79.jpg)
79
Code 4.4
else if(c>a)
{
if(a>b)
{
max=c;
mid=a;
min=b;
}
else
{
max=c;
mid=b;
min=a;
}
System.out.println( +min);
System.out.println(+mid);
System.out.println(+max);
}
}
}
![Page 80: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/80.jpg)
80
For…Loop
• For_Example1
• Write a program to print your name n
number of times.
![Page 81: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/81.jpg)
81
Code
import java.io.*;
public static void main(String s[]) throws IOException{
int n,i;
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
System.out.println("Enter how many times? ");
String x=I.readLine();
n=Integer.parseInt(x);
for(i=1; i<=n; i++)
{
System.out.println(“Your name”);
}
}
}
![Page 82: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/82.jpg)
82
• For_Example2
• Write a program to enter n numbers and
display their sum.
How many numbers? 3
Enter number 1 5
Enter number 2 10
Enter number 3 5
Sum = 20
![Page 83: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/83.jpg)
83
Code
import java.io.*;
public static void main(String s[]) throws IOException{
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int i,n,sum,num;
sum=0;
System.out.println("Enter how many numbers");
String x=I.readLine();
n=Integer.parseInt(x);
for(i=1;i<=n;i++)
{
System.out.println("Enter number "+ i );
num=Integer.parseInt(I.readLine());
sum=sum+num;
}
System.out.println("Sum= "+sum);
}
}
![Page 84: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/84.jpg)
84
• For_Example3
• Write a program to find the sum of the
following series:
12 + 32 + 52 +………+ upto nth terms.
![Page 85: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/85.jpg)
85
Code
import java.io.*;
public static void main(String s[]) throws IOException{
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int i,n,sum,sq;
sum=0;
System.out.println("Enter the nth term: ");
String x=I.readLine();
n=Integer.parseInt(x);
for(i=1;i<=n;i=i+2)
{
sq=i*i;
sum=sum+sq;
}
System.out.println("Sum= "+sum);
}
}
![Page 86: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/86.jpg)
86
String Function
substring() substring(int beginIndex)
• Returns a new string that is a substring of
this string.
![Page 87: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/87.jpg)
87
String Handling
• 5.1
• Write a program to enter a string and print
this string in reverse order
• Example:
lampang
gnapmal
![Page 88: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/88.jpg)
88
String Handling
Example 5.1
import java.io.*;
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int i,l;
System.out.println("Enter a string");
String str=I.readLine();
l=str.length();
for(i=l-1;i>=0;i--)
{
System.out.print(str.substring(i,i+1));
}
}
}
![Page 89: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/89.jpg)
89
String Handling
• 5.2
• Write a program to enter a string and print
initial character of each word of the string.
• Example:
input: Read Only Memory
output: ROM
![Page 90: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/90.jpg)
90
String Handling
Example 5.2 import java.io.*;
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
String str;
int i,l;
System.out.println("Enter a string");
str=I.readLine();
str=" " + str;
l=str.length();
for(i=0;i<=l-1;i++)
{
if(str.charAt(i)==' ')
{
System.out.print(str.charAt(i+1));
}
}
}
}
![Page 91: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/91.jpg)
91
String Handling
• 5.3
• Write a program to enter a string print it in the following format.
• Example:
input: HELLO
output:
H
H E
H E L
H E L L
H E L L O
![Page 92: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/92.jpg)
92
String Handling
Example 5.3 import java.io.*;
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
String str;
int i,l,j;
System.out.println("Enter a string");
str=I.readLine();
l=str.length();
for(i=1;i<=l;i++)
{
System.out.print(str.substring(0,i));
}
}
}
![Page 93: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/93.jpg)
93
String Handling
• 5.4
• Write a program to enter a string print it in the following format.
• Example:
input: HELLO
output:
H E L L O
H E L L
H E L
H E
H
![Page 94: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/94.jpg)
94
String Handling
Example 5.4 import java.io.*;
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
String str;
int i,l,j;
System.out.println("Enter a string");
str=I.readLine();
l=str.length();
for(i=l;i>=1;i--)
{
System.out.print(str.substring(0,i));
}
}
}
![Page 95: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/95.jpg)
95
String Handling
• 5.5
• Write a program to enter a string print it in the following format.
• Example:
input: HELLO
output:
H E L L O
E L L O
L L O
L O
O
![Page 96: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/96.jpg)
96
String Handling
Example 5.5 import java.io.*;
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
String str;
int i,l,j;
System.out.println("Enter a string");
str=I.readLine();
l=str.length();
for(i=0;i<=l-1;i++)
{
System.out.print(str.substring(i,5));
}
}
}
![Page 97: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/97.jpg)
97
String Handling
• 5.6
• Write a program to enter a string print it in the following format.
• Example:
input: HELLO
output:
O
L O
L L O
E L L O
H E L L O
![Page 98: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/98.jpg)
98
String Handling
Example 5.6 import java.io.*;
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
String str;
int i,l,j;
System.out.println("Enter a string");
str=I.readLine();
l=str.length();
for(i=l-1;i>=0;i--)
{
System.out.print(str.substring(i,5));
}
}
}
![Page 99: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/99.jpg)
99
Review
Enter how many subjects? 3
Enter marks in subject1: 90
Enter marks in subject2: 60
Enter marks in subject3: 80
Grade = 3
![Page 100: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/100.jpg)
100
Solution
public static void main(String s[])throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int i,n,num,sum,grade,gpa;
sum=0;
System.out.println("Enter how many subjects: ");
String x =I.readLine();
n=Integer.parseInt(x);
for(i=1;i<=n;i++)
{
System.out.println("Enter marks of subject " + i + ": ");
num=Integer.parseInt(I.readLine());
sum=sum+num;
}
![Page 101: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/101.jpg)
101
Solution
gpa= sum / n;
if (gpa >=80 && gpa<=100)
grade=4;
else if
(gpa >=70 && gpa <= 79)
grade=3;
else if
(gpa >=60 && gpa <=69)
grade=2;
else if
(gpa >=50 && gpa <=59)
grade=1;
else
grade=0;
System.out.println("Grade= " + grade);
}
}
![Page 102: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/102.jpg)
102
Exercise - 1
• Write a program to enter the monthly
salary and display the income tax with the
help of following table
Monthly Salary Income Tax
7,499 baht or less 20 % of monthly salary
7,500 – 9,999 30 % of monthly salary
10,000 or more 40 % of monthly salary
![Page 103: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/103.jpg)
103
Exercise - 2
*** MENU ***
1.To find area of rectangle
2. To find area of circle
3. To find area of square
Enter your choice: 1
Enter length & width ?
Area of rectangle=?
![Page 104: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/104.jpg)
104
Exercise - 2
*** MENU ***
1.To find area of rectangle
2. To find area of circle
3. To find area of square
Enter your choice: 2
Enter radius of circle ?
Area of circle=?
![Page 105: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/105.jpg)
105
Exercise - 2
*** MENU ***
1.To find area of rectangle
2. To find area of circle
3. To find area of square
Enter your choice: 3
Enter side of square ?
Area of square=?
![Page 106: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/106.jpg)
106
Solution
public static void main(String s[]) throws IOException
{
InputStreamReader R= new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int ch,l,w,side,arrect,arsq;
double r,arcir;
compute ob=new compute();
System.out.println(" *** MENU *** ");
System.out.println("1.To find area of rectangle");
System.out.println("2. To find area of circle");
System.out.println("3. To find area of square");
System.out.println("Enter your choice:");
ch=Integer.parseInt(I.readLine());
![Page 107: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/107.jpg)
107
Solution
switch(ch)
{
case 1:
{
System.out.print("Enter length & width");
l=Integer.parseInt(I.readLine());
w=Integer.parseInt(I.readLine());
arrect=ob.area(l,w);
System.out.print("Area of rectangle=" + arrect);
break;
}
![Page 108: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/108.jpg)
108
Solution
case 2:
{
System.out.print("Enter radius of circle");
r=Double.parseDouble(I.readLine());
arcir=ob.area(r);
System.out.print("Area of circle =" +arcir);
break;
}
case 3:
{
System.out.print("Enter side of square");
side=Integer.parseInt(I.readLine());
arsq=ob.area(side);
System.out.print("Area of square=" +arsq);
break;
}
default:
System.out.print("Invalid choice");
}
}
}
![Page 109: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/109.jpg)
109
Exercise
Enter how many numbers? 3
Enter number 1: 5
Enter number 2: 7
Enter number 3: 2
Minimum number = 2
Maximum number = 7
![Page 110: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/110.jpg)
110
Solution
public static void main(String s[]) throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int n,num,i,max,min,sum,avg;
max=0;
min=100;
sum=0;
System.out.println("Enter how many numbers: ");
String x =I.readLine();
n=Integer.parseInt(x);
for(i=1;i<=n;i++)
{
System.out.println("Enter number " + i + ": ");
num=Integer.parseInt(I.readLine());
![Page 111: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/111.jpg)
111
Solution
sum=sum+num;
if (num > max)
max=num;
else
max=max;
if (num<min)
min=num;
else
min=min;
}
avg=sum/n;
System.out.println("Maximum Number :" +max);
System.out.println("Minimun Number :" +min);
System.out.println(“Average Number :” +avg);
}
}
![Page 112: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/112.jpg)
112
Exercise
Enter how many products: 2
Enter product price 1:$ 50
Enter product price 2:$ 50
Enter discount % :$ 10
Total Price =$ 90
![Page 113: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/113.jpg)
113
public static void main(String s[]) throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int n,i,sum,num,d;
sum=0;
System.out.println("Enter how many products: ");
String x =I.readLine();
n=Integer.parseInt(x);
for(i=1;i<=n;i++)
{ System.out.println("Enter product price " + i + ": ");
num=Integer.parseInt(I.readLine());
sum=sum+num;
} System.out.println("Enter discount % :");
String y =I.readLine();
d=Integer.parseInt(y);
sum= sum-(sum*d/100);
System.out.println("Total Price =" + sum);
}
}
![Page 114: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/114.jpg)
114
public static void main(String s[]) throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int n,i,m;
System.out.println("Enter number: ");
String x =I.readLine();
n=Integer.parseInt(x);
for(i=1;i<=10;i++)
{
m=i*n;
System.out.println(n + "*"+ i + "=" +m);
}
}
}
![Page 115: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/115.jpg)
115
Exercise
Enter number1: 2
Enter number2: 2
** Select Operation **
** a add(+) ** ** s sub(-) **
** m mul(*) **
** d div(/) **
Enter your choice: a
Addition of 2 + 2 = 4
![Page 116: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/116.jpg)
116
Solution
public static void main(String s[]) throws IOException {
InputStreamReader R=new InputStreamReader (System.in);
BufferedReader I=new BufferedReader(R);
int n,i,m,add;
char c;
System.out.println("Enter number1: ");
String x =I.readLine();
n=Integer.parseInt(x);
System.out.println("Enter number2: ");
String y=I.readLine();
m=Integer.parseInt(y);
![Page 117: Java Programming](https://reader031.fdocuments.in/reader031/viewer/2022021507/58f2549f1a28abc10e8b45eb/html5/thumbnails/117.jpg)
117
System.out.println("Select Operation");
System.out.println("a. add(+)");
System.out.println("s. sub(-)");
System.out.println("m. mul(*)");
System.out.println("d. div(/)");
System.out.println("Enter your choice");
c=(char)I.read();
switch(c)
{
case 'a':
{
add=n+m;
System.out.println("Addition ="+ add);
}
default:
System.out.print("Invalid choice");
}
}
}