Professional Documents
Culture Documents
Topic/Courseto JAVA
Sub-Topic (Example: name of college)
Program Internal
Why and How Java?
• History of Java
• Why Java?
• Where Java?
• Compile and run the java program (Assuming program is saved as Hello.java
• Web Application
• Enterprise Application
• Mobile Application
Types of Java Editions
• Java SE – Standard Edition
• JavaFX
JVM Architecture
• JVM gives the definitions …
• JVM works with …
• Memory area
• Loads code
• Class file format
• Verifies code
• Register set
• Executes code
• Garbage-collected heap
• Provides runtime Environment
• Fatal error reporting etc.
JVM Architecture
Path Setting
• Temporary Path
• Permanent Path
• Package Statement
• Import Statements
• Interface Statement
• Class Definition
• Optional
• Optional
• Keyword
• if you want to declare many classes within one element, then you can
declare it within a package
Import Statement
• Keyword
• Used to import built-in and user-defined packages into your java source
file
• Use the '*' character to declare all the classes belonging to the package.
• Optional
• Similar to classes
• Modifiers • Keywords
• There may be many classes in a Java program, and only one class defines
the main method
A) Source code
B) Byte code
C) .obj
D) .exe
Question 2
Which of the tool is used to compile java code ?
A) java
B) javadoc
C) jar
D) javac
What is javac?
Question 3
Which of the following tool used to execute java code.
A) java
B) javadoc
C) jar
D) javac
Question 4
What is use of interpreter?
•Every variable is assigned a data type which designates the type and
quantity of value it can hold.
Variable-
Data-type
name
int count;
Variable Initialization Container
named “count”
holding a value
To initialize a variable, you must assign it a valid value. 100
count=100;
100
count
You can combine variable declaration and initialization.
int count=100;
NAMING CONVENTION OF VARIABLES
• can start with underscore(‘_’) but not with digits.
• Entity binding Data member and Member methods in one single unit
• Data Member(Properties)
• Member Methods(Behavior)
Class
In other words class is said to be a blueprint or a template.
Blueprint
Topic/Course
Sub-Topic (Example: name of college) operators
• Unary operators
X=Y–1
• Binary operators
• Ternary operators
operands
Operators in Java
• Unary operators • Logical operators
• Arithmetic operators • Ternary operators
• Relational operators • Assignment operators
• Bitwise operators • instanceof operator
Unary operators
~ Negating an expression ~x
output
15
5
50
2
0
Relational operators
• Can be applied to the integer types, long, int, short, char, and byte.
Bitwise operators
Operator Description Example
& Returns bit by bit AND of input values x&y
<< shifts the bits of the number to the left and fills 0 on voids left as a result x << 2
>>> shifts the bits of the number to the right and fills 0 on voids left as a result x >>>2
1 public class Main { output
2 public static void main(String args[]){
3 int a = 10;
4 int b = 20; 0
5 System.out.println(a&b); 30
6 System.out.println(a|b); -11
7 System.out.println(~a); 40
10 System.out.println(a<<2); 2
11 System.out.println(a>>2); 2
12 System.out.println(a>>>2);
13 }
14 }
Logical operators
• The second condition is not evaluated if the first one is false, i.e. it has a
short-circuiting effect.
Logical operators
Operator Description Example
&& Returns true if both statements are true x < 5 && x < 10
! Reverse the result, returns false if the result is true !(x < 5 && x < 10)
1 public class Test { output
2 public static void main(String args[]){
3 boolean a = true;
4 boolean b = false; false
5 System.out.println(a&&b); true
6 System.out.println(a||b); true
7 System.out.println(!(a && b));
10 }
11 }
Ternary operator
+= Adds right operand to the left operand and assign the result to left operand. C += A
Subtracts right operand from the left operand and assign the result to left
-= C -= A
operand.
Multiplies right operand with the left operand and assign the result to left C *= A
*=
operand.
Divides left operand with the right operand and assign the result to left C /= A
/=
operand.
%= Takes modulus using two operands and assign the result to left operand. C %= A
Assignment operators
Operator Description Example
<<= Left shift AND assignment operator C <<= 2
• The operator checks whether the object is of a particular type (class type
or interface type).
1 public class Test { output
2 public static void main(String args[]) {
3 String name = "James";
4 boolean result = name instanceof String;
5 System.out.println( result ); true
6 }
7 }
10
11
Precedence and associativity
• Operator precedence determines which operator is evaluated first when
an expression has more than one operators.
1. Nothing
2. Error
1 // Predict the output
2 class Test {
3 public static void main(String args[]) {
4 System.out.println(10 + 20 + “Face");
5 System.out.println(“Face" + 10 + 20);
6 }
7 }
OUTPUT
1. 30Face Face30
2. 1020Face Face1020
3. 30Face Face1020
4. 1020Face Face30
1 // Predict the output
2 class Test
3 {
4 public static void main(String args[])
5 {
6 String s1 = “FACE";
7 String s2 = “FACE";
8 //System.out.println(s1==s2);
9 System.out.println("s1 == s2 is:" + s1 == s2);
10 }
}
OUTPUT
1. true
2. false
3. compiler error
4. throws an exception
THANK YOU
Write a program to perform Arithmetic operations such as addition,
subtraction, multiplication and division.
Others methods:
1. System.out.println()
2. System.out.print()
3. System.out.printf()
Let us see an example
class AssignmentOperator
{
public static void main(String[] args)
{
System.out.println("Java programming.");
}
}
Difference between print(), println() and printf()
•println() - prints string inside the quotes similar like print() method. Then
the cursor moves to the beginning of the next line.
class Variables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
Print concatenated strings
You can use + operator to concatenate strings and print it.
class PrintVariables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
Consider this code snippet
int a = 3;
int b = 4;
System.out.println( a + b );
System.out.println( "3" + "4" );
System.out.println( "" + a + b );
System.out.println( 3 + 4 + a + " " + b + a );
System.out.println( "Result: " + a + b );
System.out.println( "Result: " + ( a + b ) );
Printing characters
You can use + operator to concatenate strings and print it.
char a=65;
char b='A';
System.out.println(a);
System.out.println(b);
READING INPUT
READING INPUT FROM CONSOLE
In Java, there are three different ways for reading input from
the user in the command line environment(console).
• The main purpose of the Scanner class is to parse primitive types and strings
using regular expressions, however it is also can be used to read input from
the user in the command line.
SCANNER CLASS
import java.util.Scanner; int a = in.nextInt();
System.out.println("You entered
class GetInputFromUser integer "+a);
{
public static void main(String args[]) float b = in.nextFloat();
{ System.out.println("You entered
// Using Scanner for Getting Input float "+b);
from User }
Scanner in = new }
Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered
string "+s);
CONSOLE CLASS
• It has been becoming a preferred way for reading user’s input from the
command line.
System.out.println(name);
}
}
COMMAND LINE ARGUMENTS
COMMAND LINE ARGUMENTS
The java command-line argument is an argument i.e. passed at
run time.
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
Program 1 : Adding two integers using command line arguments
class A
Predict the output .
{
public static void main(String args[]) 9
{
System.out.println(args[0]+args[1]);
} 18
}
Program 1 : Adding two integers using command line arguments
class A{
public static void main(String args[]) Predict the output .
{
9
System.out.println(Integer.parseInt(args[0])
+Integer.parseInt(args[1]));
}
}
Program 2 : Concatenating two strings
class A
{ Predict the output .
public static void main(String args[])
Hai Hello
{
System.out.println(args[0]+args[1]);
}
}
Program 3 : Find average of your marks (5 subjects)
class A
{
public static void main(String args[]) Predict the output .
{ Input : java A 67 98 91 78 98
float avg;
avg = (args[0]+args[1]+args[2]+args[3]+args[4])/5; 86.4
System.out.println(avg);
}
}
Program 3 : Find average of your marks (5 subjects)
class A
{
public static void main(String args[]) Predict the output .
{ Input : java A 67 98 91 78 98
float avg;
avg= 86.4
(Float.valueOf(args[0])+Float.valueOf(args[1])+
Float.valueOf(args[2])+Float.valueOf(args[3])+F
loat.valueOf(args[4]))/5;
System.out.println(avg);
}
}
THANK YOU
PRINTING
• There are three standard streams, all are managed by the
java.lang.System class
• Standard input--referenced by System.in
– Used for program input, typically reads input entered by the user.
• Standard output--referenced by System.out
– Used for program output, typically displays information to the user.
• Standard error--referenced by System.err
– Used to display error messages to the user.
PRINTING
The basic output statement is :
System.out.println( );
Others methods:
1. System.out.println()
2. System.out.print()
3. System.out.printf()
Let us see an example
class AssignmentOperator
{
public static void main(String[] args)
{
System.out.println("Java programming.");
}
}
Difference between print(), println() and printf()
•println() - prints string inside the quotes similar like print() method. Then
the cursor moves to the beginning of the next line.
class Variables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
Print concatenated strings
You can use + operator to concatenate strings and print it.
class PrintVariables
{
public static void main(String[] args)
{
Double number = -10.6;
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
Consider this code snippet
int a = 3;
int b = 4;
System.out.println( a + b );
System.out.println( "3" + "4" );
System.out.println( "" + a + b );
System.out.println( 3 + 4 + a + " " + b + a );
System.out.println( "Result: " + a + b );
System.out.println( "Result: " + ( a + b ) );
Printing characters
You can use + operator to concatenate strings and print it.
char a=65;
char b='A';
System.out.println(a);
System.out.println(b);
READING INPUT
READING INPUT FROM CONSOLE
In Java, there are three different ways for reading input from
the user in the command line environment(console).
• The main purpose of the Scanner class is to parse primitive types and strings
using regular expressions, however it is also can be used to read input from
the user in the command line.
SCANNER CLASS
import java.util.Scanner; int a = in.nextInt();
System.out.println("You entered
class GetInputFromUser integer "+a);
{
public static void main(String args[]) float b = in.nextFloat();
{ System.out.println("You entered
// Using Scanner for Getting Input float "+b);
from User }
Scanner in = new }
Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered
string "+s);
CONSOLE CLASS
• It has been becoming a preferred way for reading user’s input from the
command line.
System.out.println(name);
}
}
COMMAND LINE ARGUMENTS
COMMAND LINE ARGUMENTS
The java command-line argument is an argument i.e. passed at
run time.
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
Program 1 : Adding two integers using command line arguments
class A
Predict the output .
{
public static void main(String args[]) 9
{
System.out.println(args[0]+args[1]);
} 18
}
Program 1 : Adding two integers using command line arguments
class A{
public static void main(String args[]) Predict the output .
{
9
System.out.println(Integer.parseInt(args[0])
+Integer.parseInt(args[1]));
}
}
Program 2 : Concatenating two strings
class A
{ Predict the output .
public static void main(String args[])
Hai Hello
{
System.out.println(args[0]+args[1]);
}
}
Program 3 : Find average of your marks (5 subjects)
class A
{
public static void main(String args[]) Predict the output .
{ Input : java A 67 98 91 78 98
float avg;
avg = (args[0]+args[1]+args[2]+args[3]+args[4])/5; 86.4
System.out.println(avg);
}
}
Program 3 : Find average of your marks (5 subjects)
class A
{
public static void main(String args[]) Predict the output .
{ Input : java A 67 98 91 78 98
float avg;
avg= 86.4
(Float.valueOf(args[0])+Float.valueOf(args[1])+
Float.valueOf(args[2])+Float.valueOf(args[3])+F
loat.valueOf(args[4]))/5;
System.out.println(avg);
}
}
THANK YOU
Decision Making
Topic/Course
Sub-Topic (Example: name of college)
Decision/Selection statements
• if
• if else
• nested if
• if else if
• switch case
Simple if
if (condition)
{
// Executes this block if condition is true
}
1 public class IfExample { output
2 public static void main(String[] args) {
3 int age=20;
4 if(age>18){ Age is greater than 18
5 System.out.print("Age is greater than 18");
6 }
7 }
8 }
if else
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
1 class IfElseDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 10; i is smaller than 15
6 if (i < 15)
7 System.out.println("i is smaller than 15");
8 else
9 System.out.println("i is greater than 15");
10 }
11 }
Nested if
• An if else statement can contain any sort of statement within it.
• It can contain another if-else statement
• An if-else may be nested within the if part.
• An if-else may be nested within the else part.
• An if-else may be nested within both parts.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
1 class NestedIfDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 10; i is smaller than 15
6 if (i == 10) i is smaller than 12 too
7 {
8 if (i < 15)
9 System.out.println("i is smaller than 15");
10 if (i < 12)
11 System.out.println("i is smaller than 12 too");
12 else
13 System.out.println("i is greater than 15");
14 }
15 }
16 }
if else if
if (condition1)
statement1;
else if (condition2)
statement2;
.
.
else
statement n;
1 class ifelseifDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 20;
6 i is 20
7 if (i == 10)
8 System.out.println("i is 10");
9 else if (i == 15)
10 System.out.println("i is 15");
11 else if (i == 20)
12 System.out.println("i is 20");
13 else
14 System.out.println("i is not present");
15 }
16 }
switch case
switch (expression)
{
case value1: statement1;
break;
case value2: statement2;
break;
.
.
case valueN: statementN;
break;
default: statementDefault;
}
1 class SwitchCaseDemo output
2 {
3 public static void main(String args[])
4 {
5 int i = 9;
6 switch (i) i is greater than 2.
7 {
8 case 0:
9 System.out.println("i is zero.");
10 break;
11 case 1:
12 System.out.println("i is one.");
13 break;
14 case 2:
15 System.out.println("i is two.");
16 break;
17 default:
18 System.out.println("i is greater than 2.");
19 }
20 }
21 }
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 int x = 10;
6 if (x) {
7 System.out.println("HELLO");
8 } else {
9 System.out.println("BYE");
10 }
11 }
12 }
13
14
15
16
17
18
19
20
21
22
OUTPUT
1. HELLO
2. Compile time error
3. Runtime Error
4. BYE
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 int x = 10;
6 if (x)
7 System.out.println("HELLO");
8 System.out.println("WELCOME");
9
10 else
11 {
12 System.out.println("BYE");
13 }
14 }
15 }
OUTPUT
1. HELLO WELCOME
2. HELLO
3. BYE
4. Compile time error
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 if (true)
6 ;
7 }
8 }
OUTPUT
1. No Output
2. Compile time error
3. Runtime error
4. Runtime Exception
1 // Predict the output
2 class MainClass {
3 public static void main(String[] args)
4 {
5 int x = 10;
6 switch (x + 1 + 1) {
7 case 10:
8 System.out.println("HELLO");
9 break;
10 case 10 + 1 + 1:
11 System.out.println(“BYE");
12 break;
13 }
14 }
15 }
OUTPUT
1. Nothing
2. Error
THANK YOU
Question 1
Write a program to get two integers n1 and n2 from the user and write a
program to relate 2 integers as equal to, less than or greater than..
e Vowel
b Consonant
$ Not an alphabet
1 import java.util.Scanner;
2 class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner obj = new Scanner(System.in);
7 char input = obj.next().charAt(0);
8 if(input >='a' && input <= 'z' || input >='A' && input <= 'Z')
9 {
10 if(input =='a'|| input =='e'||input=='i'||input=='o'||input=='u'||
11 input =='A'|| input =='E'||input=='I'||input=='O'||input=='U')
12 System.out.println("Vowel");
13 else
14 System.out.println("Consonant");
15 }
16 else
17 System.out.println("Not an alphabet");
18 }
19 }
20
21
22
Question 3
The newly appointed Vice-Chancellor of Anna University wanted to create an
automated grading system for the students to check their grade. When a student
enters a mark, the grading system displays the corresponding grade. Write a program
to solve the given problem. The grades for marks 100 - S, 90-99 is A, 80-89 is B, 70-79
is C, 60-69 is D, 50-59 is E and less than 50 is F.
• for
• while
• do while
• Enhanced for
for
while(condition)
{
statement(s) ;
}
1 class whileLoopDemo output
2 {
3 public static void main(String args[])
4 {
5 int x = 1; Value of x:1
6 while (x <= 4) Value of x:2
7 { Value of x:3
8 System.out.println("Value of x:" + x); Value of x:4
9 x++;
10 }
11 }
12 }
do while
• It is used to iterate a part of the program several times.
• Use do while if the number of iteration is not fixed and you must have
to execute the loop at least once.
Syntax:
do
{
statement(s) ;
} while(condition);
1 class dowhileloopDemo output
2 {
3 public static void main(String args[])
4 {
5 int x = 21;
6 do
7 { Value of x: 21
8 System.out.println("Value of x:" + x);
9 x++;
10 }while (x < 20);
11 }
12 }
What is the difference between
while and do while ?
Enhanced for
• break
• continue
• return
break
• Used to continue the loop, it continues the current flow of the program
and skips the remaining code at the specified condition.
• The continue statement is used in loop control structure when you
need to jump to the next iteration of the loop immediately.
• It can be used with for loop or while loop.
1 public class ContinueExample { output
2 public static void main(String[] args) {
3 for(int i=1;i<=10;i++){
4 if(i==5){ 1
5 continue; 2
6 } 3
7 System.out.println(i); 4
8 } 6
9 } 7
10 } 8
9
10
return
1. 111
2. 222
3. 333
4. error
1 // Predict the output
2 class Test {
3 static String s = "";
4 public static void main(String[] args)
5 {
6 P:
7 for (int i = 2; i < 7; i++) {
8 if (i == 3)
9 continue;
10 if (i == 5)
11 break P;
12 s = s + i;
13 }
14 System.out.println(s);
15 }
16 }
OUTPUT
1. 32
2. 23
3. 24
4. 42
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 for (int i = 0; i < 10; i++)
6 int x = 10;
7 }
8 }
9
OUTPUT
1. No Output
2. Compile time error
3. Runtime error
4. Runtime Exception
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 int i = 0;
6 for (System.out.println("HI"); i < 1; i++)
7 System.out.println("HELLO");
8 }
9 }
OUTPUT
1. HI HELLO
2. No Output
3. Compile time error
4. HELLO
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 for (int i = 0;; i++)
6 System.out.println("HELLO");
7 }
8 }
OUTPUT
• for
• while
• do while
• Enhanced for
for
while(condition)
{
statement(s) ;
}
1 class whileLoopDemo output
2 {
3 public static void main(String args[])
4 {
5 int x = 1; Value of x:1
6 while (x <= 4) Value of x:2
7 { Value of x:3
8 System.out.println("Value of x:" + x); Value of x:4
9 x++;
10 }
11 }
12 }
do while
• It is used to iterate a part of the program several times.
• Use do while if the number of iteration is not fixed and you must have
to execute the loop at least once.
Syntax:
do
{
statement(s) ;
} while(condition);
1 class dowhileloopDemo output
2 {
3 public static void main(String args[])
4 {
5 int x = 21;
6 do
7 { Value of x: 21
8 System.out.println("Value of x:" + x);
9 x++;
10 }while (x < 20);
11 }
12 }
What is the difference between
while and do while ?
Enhanced for
• break
• continue
• return
break
• Used to continue the loop, it continues the current flow of the program
and skips the remaining code at the specified condition.
• The continue statement is used in loop control structure when you
need to jump to the next iteration of the loop immediately.
• It can be used with for loop or while loop.
1 public class ContinueExample { output
2 public static void main(String[] args) {
3 for(int i=1;i<=10;i++){
4 if(i==5){ 1
5 continue; 2
6 } 3
7 System.out.println(i); 4
8 } 6
9 } 7
10 } 8
9
10
return
1. 111
2. 222
3. 333
4. error
1 // Predict the output
2 class Test {
3 static String s = "";
4 public static void main(String[] args)
5 {
6 P:
7 for (int i = 2; i < 7; i++) {
8 if (i == 3)
9 continue;
10 if (i == 5)
11 break P;
12 s = s + i;
13 }
14 System.out.println(s);
15 }
16 }
OUTPUT
1. 32
2. 23
3. 24
4. 42
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 for (int i = 0; i < 10; i++)
6 int x = 10;
7 }
8 }
9
OUTPUT
1. No Output
2. Compile time error
3. Runtime error
4. Runtime Exception
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 int i = 0;
6 for (System.out.println("HI"); i < 1; i++)
7 System.out.println("HELLO");
8 }
9 }
OUTPUT
1. HI HELLO
2. No Output
3. Compile time error
4. HELLO
1 // Predict the output
2 class Test {
3 public static void main(String[] args)
4 {
5 for (int i = 0;; i++)
6 System.out.println("HELLO");
7 }
8 }
OUTPUT
Input Format:
Input consists of a single integer which corresponds to n.
Output Format:
Output consists of the terms in the series separated by a blank space.
Input Format:
Input consists of a single integer which corresponds to n.
Output Format:
Output consists of the terms in the series separated by a blank space..
Input Format:
Input consists of a single integer which corresponds to n.
Output Format:
Output consists of the terms in the series separated by a blank space..
Input Format:
Input consists of a single integer which corresponds to n.
Output Format:
Output consists of the terms in the series separated by a blank space..
Input Format:
Input consists of a single integer which corresponds to n.
Output Format:
Output consists of the terms in the series separated by a blank space.
Output Format:
Print the amoeba size.
Refer the sample output for formatting.
Output Format:
If the given number is a trendy number, then print "Trendy Number".
Otherwise, print "Not a Trendy Number".
Sample Input: Sample Output:
791 Trendy Number
Question 10.1
Write a program to that allows the user to enter 'n' numbers and finds the
number of positive numbers entered and the number of negative
numbers entered using a while loop.
Input Format:
Input consists of n+1 integers. The first integer corresponds to n. The next
n integers correspond to the numbers to be added. Consider 0 to be a
positive number.
Output Format:
Refer Sample Input and Output for formatting specifications.
Question 10.2
Sample Input: Sample Output:
Enter the values of n The number of positive number entered
4 is 2 and the sum is 11
5
-2
-1
6
How many times do you have to roll a pair of dice before they come up
snake eyes? You could do the experiment by rolling the dice by hand.
Write a computer program that simulates the experiment.
The program should report the number of rolls that it makes before the
dice come up snake eyes. (Note: "Snake eyes" means that both dice
show a value of 1.)
You can simulate rolling one die by choosing one of the integers 1, 2, 3,
4, 5, or 6 at random. The number you pick represents the number on the
die after it is rolled. The expression
(int)(Math.random()*6) + 1
does the computation you need to select a random integer between 1 and
6.
Which integer between 1 and 10000 has the largest number of divisors,
and how many divisors does it have? Write a program to find the
answers and print out the results. It is possible that several integers in
this range have the same, maximum number of divisors. Your program
only has to print out one of them
You might need some hints about how to find a maximum value. The
basic idea is to go through all the integers, keeping track of the largest
number of divisors that you've seen so far. Also, keep track of the
integer that had that number of divisors.
Write a program that will evaluate simple expressions such as 17 +
3 and 3.14159 * 4.7.
The expressions are to be typed in by the user. The input always
consist of a number, followed by an operator, followed by another
number.
The operators that are allowed are +, -, *, and /.
Your program should read an expression, print its value, read
another expression, print its value, and so on. The program should
end when the user enters 0 as the first number on the line.
Write a program that reads one line of input text and breaks it up into
words.
The words should be output one per line. A word is defined to be a
sequence of letters.
Any characters in the input that are not letters should be discarded. For
example, if the user inputs the line
He said, "That's not a good idea."
then the output of the program should be
He
Said
that
s
Not
a
good idea
Clue:
To test whether a character is a letter, you might use (ch >= 'a' && ch
<= 'z') || (ch >= 'A' && ch <= 'Z')
. However, this only works in English and similar languages. A better
choice is to call the standard function Character.isLetter(ch), which
returns a boolean value of true if ch is a letter and false if it is not.
This works for any Unicode character. For example, it counts an
accented e, é, as a letter.
THANK YOU
Java Inner Class
• Example
STUDENT s1 = new STUDENT();
STUDENT.DATE dob = s1.new DATE();
1 class STUDENT
2 {
3 int roll_no=25;
4 String name="KUMAR";
5 class DATE
6 {
7 int dd=25,mm=12,yy=2000;
8 }
9 }
10 class ic1
11 {
12 public static void main(String[] args)
13 {
14 STUDENT s1 = new STUDENT();
15 STUDENT.DATE dob = s1.new DATE();
16
17 System.out.println("Roll Number : "+s1.roll_no);
18 System.out.println("Name : "+s1.name);
19 System.out.println("DoB : "+dob.dd+":"+dob.mm+":"+dob.yy);
20 }
21 }
22
OUTPUT
>javac ic1.java
>java ic1
Roll Number : 25
Name : KUMAR
DoB : 25:12:2000
>
OUTPUT
>javac mic1.java
>java mic1
Roll Number : 123
Name : RAMU
Result :*PASS*
>
OUTPUT
>javac aic1.java
>java aic1
My Co-curricular Activities: Seminars & Conferences
My Extra-curricular Activities: Cricket & Chess
>
OUTPUT
>javac aic3.java
>java aic3
Main Thread
Child Thread
>java aic3
Main Thread
Child Thread
>java aic3
Main Thread
Child Thread
>
OUTPUT
>javac aic4.java
>java aic4
Main Thread
Child Thread
>java aic4
Main Thread
Child Thread
>java aic4
Main Thread
Child Thread
>
OUTPUT
>javac arithmetic.java
>java ARITHMETIC
Inside inner class
a = 5 b = 3
Sum = 8
Difference = 2
Product = 15
Quotient = 1
Reminder = 2
>
Note:
Take care of case-sensitiveness of filename while execution
OUTPUT
>javac snc1.java
>java snc1
static outer_b = 200
private static outer_c = 300
>
OUTPUT
>javac snc2.java
>java snc2
Inside static method of static nested class
static outer_b = 200
private static outer_c = 300
>
OUTPUT
>javac ni1.java
>java ni1
display method of interface inside a class
>
OUTPUT
>javac ni2.java
>java ni2
nested interface: display method
nested interface: display method
>
OUTPUT
Answer : D)
Question 4
Answer : B)
Question 5
Which is true about an anonymous inner class?
A) It can extend exactly one class and implement exactly one interface
B) It can extend exactly one class and can implement multiple interfaces
C) It can extend exactly one class or implement exactly one interface
D) It can implement multiple interfaces regardless of whether it also
extends a class
SOLUTION
C) It can extend exactly one class or implement exactly
one interface
Answer : C)
Question 6
Which among the following best describes a nested class?
A) Encapsulation
B) Inheritance
C) Binding
D) Abstraction
SOLUTION
B) Inheritance
Answer : B) Inheritance
Question 8
Non-static nested classes have access to _________ from
enclosing class.
A) Private members
B) Protected members
C) Public members
D) All the members
SOLUTION
D) All the members
The non-static nested class can access all the members of the
enclosing class.
All the data members and member functions can be accessed
from the nested class.
Even if the members are private, they can be accessed.
Answer : A)
Question 11
A static nested class is _____________ class in behavior that is
nested in another _________ class.
The inner class will have more preference for its local members
than those of the enclosing members.
Hence it will shadow the enclosing class members.
This process is known as shadowing.
Answer : B)
Question 13
Instance of inner class can exist only _______________
enclosing class.
A) Within
B) Outside
C) Private to
D) Public to
SOLUTION
A) Within
Answer : A) Within
Question 14
A nested class can have its own static members.
A) True B) False
SOLUTION
B) False
Answer : B) False
Question 15
How to access static nested classes?
A) OuterClass.StaticNestedClass
B) OuterClass->StaticNestedClass
C) OuterClass(StaticNestedClass)
D) OuterClass[StaticNestedClass]
SOLUTION
A) OuterClass.StaticNestedClass
Like any other member of the class, the static nested class uses
the dot operator to be accessed.
The reason behind is, the static classes can’t work with
instances, hence we use enclosing class name to access static
nested class.
Answer : A) OuterClass.StaticNestedClass
Question 16
What is the output of the following java program?
A) 15 B) 9
C) 5 D) Compilation Error
1 public class Outer
2 {
3 public static int temp1 = 1;
4 private static int temp2 = 2;
5 public int temp3 = 3;
6 private int temp4 = 4;
7 public static class Inner
8 {
9 private static int temp5 = 5;
10
11 private static int getSum()
12 {
13 return (temp1 + temp2 + temp3 + temp4 + temp5);
14 }
15 }
16 public static void main(String[] args)
17 {
18 Outer.Inner obj = new Outer.Inner();
19 System.out.println(obj.getSum());
20 }
21
22 }
SOLUTION
D) Compilation Error
Answer : C) 200
Question 18
What is the output of the following java program?
C) 25 D) 20
1
2 interface Anonymous
3 {
4 public int getValue();
5 }
6 public class Outer
7 {
8 private int data = 15;
9 public static void main(String[] args)
10 {
11 Anonymous inner = new Anonymous()
12 {
13 int data = 5;
14 public int getValue() { return data; }
15 public int getData() { return data; }
16 };
17 Outer outer = new Outer();
18 System.out.println(inner.getValue() + inner.getData() +
19 outer.data);
20 }
21 }
22
SOLUTION
A) Compilation Error
C) 1020 D) None
1
2 public class Outer {
3 private int data = 10;
4 class Inner {
5 private int data = 20;
6 private int getData() { return data; }
7 public void main(String[] args)
8 {
9 Inner inner = new Inner();
10 System.out.println(inner.getData());
11
12 }
13 }
14 private int getData() { return data; }
15 public static void main(String[] args)
16 {
17 Outer outer = new Outer();
18 Outer.Inner inner = outer.new Inner();
19 System.out.printf("%d", outer.getData());
20 inner.main(args);
21 }
22 }
SOLUTION
A) Compilation Error
C) 100 D) None
1
2 interface OuterInterface
3 {
4 public void InnerMethod();
5 public interface InnerInterface
6 {
7 public void InnerMethod();
8 }
9 }
10 public class Outer implements OuterInterface.InnerInterface, OuterInterface
11 {
12 public void InnerMethod()
13 {
14 System.out.println(100);
15 }
16 public static void main(String[] args)
17 {
18 Outer obj = new Outer();
19 obj.InnerMethod();
20 }
21 }
22
SOLUTION
C) 100
Answer : C) 100
THANK YOU