Thursday, 19 November 2020
Java Applets
Java Applets
Applets :-
An applets is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following :
1. An applet is a Java class that extends the java.applet.Applet class.
2. A main ( ) method is not invoked on an applet , and an applet class will not define main( ).
3. Applets are designed to be embedded within an HTML page.
4. When a user view an HTML page that contains an applet , the code for the applet is downloaded o the user's machine.
5. A JVM is required to view an applet . The JVM can be either a plug- in of the Web browser or a separate runtime environment.
6. The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
7. Applets have strict security rules that are enforced by the Web browser . The security of an applet is often referred to as sandbox security , comparing the applet to a child playing in a sandbox with various rules that must be followed.
8. Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
9. Applets are the small programs while applications are larger programs.
10. Applets are designed just for handling the client site problems , while the Java applications are designed to work with the client as well as server.
Thank you
Sunday, 8 November 2020
Example of this keyword in JAVA
Example of this keyword in JAVA
Program:-
class Student
{
int id;
String name;
Student(int id, String name)
{
this.id = id;
this.name = name;
}
void display( )
{
System.out.println(id +" "+ name);
}
public static void main(String args[])
{
Student s1 = new student(111, "Karan");
Student s2 = new student(222, "Ram");
s1.display( );
s2.display( );
}
}
OUTPUT:-
111 Karan
222 Ram
Write a program to accept a number through command line argument and find factorial of the number.
Program:-
class Fact
{
public static void main(String args[])
{
if(args.length !=1)
System.out.println("Wrong arguments");
else
{
int n = Integer.parselnt(args[0]);
int f=1;
for(int i = 1;i<=n;i++)
f = f*i;
System.out.println("Factorial of "+n+" is + f);
}
}
}
OUTPUT:-
C:\java\bin>java Fact 5
Factorial of 5 is 120
Write a program to read two number from command line argument and add them.
Write a program to read two number from command line argument and add them.
Program:-
class Sum
{
public static void main(String ar[])
{
int x, y,s;
x = integer.parselnt(ar[0]);
y = integer.parselnt(ar[1]);
s = x+y;
System.out.println("sum of "+x+" and"+y+" is " +s);
}
}
Friday, 6 November 2020
What are the characteristics and jealous of OOP(Object Oriented Programming?
What are the characteristics and jealous of OOP(Object Oriented Programming?
Characteristics of OOPS are:-
1. Emphasis is on data rather than procedure.
2. Programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data structure.
5. Data is hidden and cannot be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added whenever necessary.
8. Follows bottom up approach in program design.
Thursday, 5 November 2020
Write the JAVA program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.
Write the JAVA program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.
PROGRAM :-
class SumofDigit
{
public static void main(String args[])
{
int sum = 0;
int count = 0;
for( int i = 100; i<=200; i++)
{
if(i % 7== 0)
{
sum + = i;
count++;
}
}
System.out.println("Number of integers = "+count);
System.out.println("Sum of integers = "+sum);
}
}
OUTPUT :
Number of integers = 14
Sum of integers = 2107
JAVA program to input and print n number using array.
JAVA program to input and print n number using array :-
PROGRAM :-
import java.io.*;
class Clsarray
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new inputStreamReader(System.in));
int i, n;
int a[] = new int[100];
System.out.print("Enter the value of n = ");
n = Integer.parselnt(br.readLine());
System.out.println("Enter the values");
for(i =0; i<n; i++)
{
a[i] = Integer.parselnt(br.readLine());
}
System.out.println("The values are");
for(i =0; i<n; i++
{
System,out.print(a[i] +" ");
}
}
}
OUTPUT :-
Enter the value of n =5
Enter the values
10
20
30
40
50
The values are
10 20 30 40 50
The Type Promotion Rules in JAVA
The Type Promotion Rules :-
Java defines several type promotion rules that apply to expressions. They are as follows : all byte , short , and char values are promoted ti int. Then , if one operand is a long , the whole expression is promoted to long .If one operand is a float , the entire expression is promoted to float. If any of the operands is double. the result is double.
The following program demonstrates how each value in the expression gets promoted to match the second argument to each binary operation :
Program :-
class Promote
{
public static void main(String args[])
{
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) +(i / c) -(d*s);
System.out.println((f * b) + "+" +(i /c) + "-" +(d *s));
System.out.println("result =" +result);
}
}
OUTPUT:-
238.14 +515 -126.3616
result =626.7784146484375
The type promotions that occur in following line from program :-
double result = (f *b) + (i / c) - (d * s);
In the first subexpression, f *b , b is promoted to a float and the result of the subexpression is float. Next , in the subexpression i /c , c is promoted to int , and the result is of type int . Then , in d* s, the value of s is promoted to double, and the type of the subexpression is double.
Finally , these three intermediate values, float , int , and double are considered. The outcome of float plus an int is a float. Then the resultant float minus the last double is promoted to double, which is the type for the final result of the expression.
Enhanced for loop in JAVA
Enhanced for loop in JAVA
As of Java 5, the enhanced for loop was introduced.This is mainly used for arrays.
The syntax of enhanced for loop is :-
for(declaration : expression)
{
// Statement
}
Declaration : -
The newly declared block variable , Which is of a type compatible with the elements of the array we are accessing . The variable will be available within the for block and its value would be the same as the current array element .
Expression :-
This evaluates to the array we need to loop through. The expression can be an array variable or method call that returns an array .
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
int [] numbers ={10,20,30,40,50}
for(int x :numbers)
{
System.out.print(x);
System.out.print(",");
}
System.out.print("\n");
String [] names ={"Sanika","Chahat","Ritesh","Simran"};
for(String name :names)
{
System.out.print(name);
System .out.print(",");
}
}
}
OUTPUT :-
10 , 20 , 30 , 40 , 50 ,
Sanika , Chahat , Ritesh , Simran ,
Wednesday, 4 November 2020
The for Loop in JAVA
The for Loop :-
A for loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when we know how many times a task is to be repeated.
The syntax of a for loop is :-
for (initialization; Boolean _expression ;update)
{
//Statements
}
Here is the flow of control in a for loop :-
The initialization step is executed first, and only once .This step allow us to declare and initialize any loop control variables. We are not required to put a statement here, as long as a semicolon appears . Next , the Boolean expression is evaluated . if it is true , the body of is executed. if it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop .
After the body of for loop executes, the flow of control jumps back up to the update statement . This statement allow us to update any loop control variables.
This statement can be left blank, as long as a semicolon appears after the Boolean expression . The Boolean expression is now evaluated again . If it is true , the loop executes and the process repeats itself (body of loop , then update step, then Boolean expression ). after the Boolean expression is false , the for loop terminates.
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
for(int x =10; x<15; x = x+1)
{
System .out.print("value of x :" +x);
System .out.print("\n");
}
}
}
OUTPUT :-
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
The do......while Loop in JAVA
The do......while Loop :-
A do.....while loop is similar to a while loop , except that a do .....while loop is guaranteed to execute at least one time.
The syntax of a do.....while loop is :-
do
{
//Statements
}
while(Boolean_expression);
Notice that the Boolean expression appears at the end of loop , so the statement in the loop execute once before the Boolean is tested. If the Boolean expression is true, The flow of control jumps back up to do , and the statement in the loop execute again.This process repeats until the Boolean expression is false.
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
int x =10;
do
{
System.out.print("value of x :" +x);
x++;
System.out.print("\n");
}
while(x <15);
}
}
OUTPUT :-
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
The while Loop in JAVA
The while Loop in JAVA
A while loop is a control structure that allows us to repeat a task a certain number of times.
The syntax of a while loop is :-
while(boolean_expression)
{
//Statements
}
When executing , if the boolean _expression result is true, then the action inside the loop will be executed. This will continue as long as the expression result is true.Here key point of the while loop is that the loop might not ever run . When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
int x =10;
while(x<15)
{
System .out.print("value of x :"+x);
x++;
System.out.print("\n");
}
}
}
OUTPUT :-
value of x :10
value of x: 11
value of x: 12
value of x: 13
value of x: 14
The switch Statement in JAVA
The switch Statement :-
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case , and the variable being switched on is checked for each case.
The syntax of enhanced for loop is :
switch(expression )
{
case value:
//Statements
break; //optional
case value:
//Statements
break; //optional
// You can have any number of case statements.
default: //Optional
//Statements
}
EXAMPLE :-
public class Test
{
public static void main(String args[])
{
//char grade = args[0].charAt(0);
char grade = 'C' ;
switch(grade)
{
case 'A' :
System.out.println( "Excellent !");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is "+ grade);
}
}
Compile and run above program using various command line arguments.This would produce the following result :
C:\java\bin>java Test
Well done
Your grade is a C
Nested if....... else Statement in JAVA
Nested if....... else Statement :-
It is always legal to nest if -else statements which means we can use one if or else if statement inside another if or else if statement.
The syntax for a nested if .....else is as follow :
if(Boolean _expression 1)
{
//Executes when the Boolean expression 1 is true
if(Boolean _expression 2)
{
//Executes when the Boolean expression 2 is true
}
}
We can nest else if .....else in the similar way as we have nested if statement.
Example :-
public class Test
{
public static void main(String args[])
{
int x = 30;
int y = 10;
if(x == 30)
{
if(y == 10)
{
System .out.print("X = 30 and Y = 10");
}
}
}
}
OUTPUT :-
X = 30 and Y = 10
Tuesday, 3 November 2020
The if .....else if.......else statement in JAVA
The if .....else if.......else statement :-
An if statement can followed by an optional else if.......else statement , which is very useful to test variouse conditions using single if ......else statement. When using if , else if, else statement there are few points to keep in mind .
1. An if can have zero or one else's and it must come after any else if 's.
2. An if can have zero to many else if's and they must come before the else.
3. Once an else if succeeds, none of the remaining else if's or else's will be tested.
The syntax of an if..... else is :-
if (Boolean _ expression 1)
{
// Executes when the Boolean expression 1 is true
}
else if (Boolean _ expression 2)
{
// Executes when the Boolean expression 2 is true
}
else if (Boolean _expression 3)
{
//Executes when the Boolean expression 3 is true
}
else
{
//Executes when the none of the above condition is true.
}
EXAMPLE :-
public class Test
{
public static void main (String args[])
{
int x = 30;
if (x == 10)
{
System .out.print("Value of X is 10");
}
else if(x == 20)
{
System .out.print("Value of X is 20");
}
else if(x == 30)
{
System .out.print("Value of X is 30");
}
else
{
System .out.print("This is else statement");
}
}
}
OUTPUT :-
Value of X is 30
The Continue Keyword in JAVA
The Continue Keyword in JAVA :-
The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop . In a for loop, the continue keyword causes flow of control to immediately jump to the update statement. In a while loop or do/ while loop , flow of control immediately jumps to the Boolean expression .
The syntax of a continue is a single statement inside any loop :-
continue;
EXAMPLE:-
public class Test
{
public static void main(String args[])
{
int[] numbers = {10,20,30,40,50};
for(int x: numbers)
{
if( x== 30)
{
continue;
}
System .out .print(x);
System .out.print("\n");
}
}
}
OUTPUT :-
10
20
40
50
The Break Keyword in JAVA
The Break Keyword in JAVA :-
The break keyword is used to stop the entire loop. The break keyword must be used inside any loop or a switch statement. The break keyword will stop the execution of the innermost loop and start execcuting the next line of code after the block .
The syntax of a break is a single statement inside any loop :
break;
Example :-
public class Test
{
public static void main(String args[])
{
int [] numbers = {10,20,30,40,50};
for(int x : numbers)
{
if (x == 30)
{
break;
}
System .out.print(x);
System.out.print("\n");
}
}
}
OUTPUT :-
10
20
Monday, 2 November 2020
( if ..........else statement ) of JAVA
( if ..........else statement ) of JAVA
An if statement can be followed by an optional else statement , which executes when the Boolean expression is false.
SYNTAX :-
if(Boolean_ expression )
{
// Execute when the Boolean expression is true
}
else
{
// Executes when the Boolean expression is faulse
}
EXAMPLE :-
public class Test
{
public static void main (String args[])
{
int x =30;
if ( x<20)
{
System .out.print("This is if statement");
}
else
{
System .out .print("This is else statement");
}
}
}
OUTPUT :
This is else statement
(If statements) in JAVA
(If statements) in JAVA
The if statement :-
An if statement consists of a boolean expression followed by one or more statements.
SYNTAX :-
if ( Boolean expression)
{
// statements will execute if the Boolean expresion is true
}
If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement ( after the closing curly brace ) will be executed.
EXAMPLE :-
public class Test
{
public static void main (String args[] )
{
int x = 10;
if(x< 20);
{
System .out. println (" This is if statement ");
}
}
}
OUTPUT :
This is if statement
Object Oriented Programming And Procedure Oriented Programming
Object Oriented Programming And Procedure Oriented Programming
Object Oriented Programming :-
Object Oriented Programming is programming concept which is focused on object rather than actions and data rather than logic.
Object Oriented Programming concept is a way to remove some faults or limitations of POP . It puts much importance on data and does not allow data to move freely around the whole program . OOP binds particular data and functions of data in units called objects which protect data from getting motivation by other functions.
Procedure Oriented Programming :-
As the name implies, Procedure Oriented Programming contains step by step procedure to execute. Here the problems get decomposed into small parts and then to solve each part one or more functions are used.
Sunday, 1 November 2020
Can an applicatoin have multiple classes having main method in same JAVA file ?
1. Can an applicatoin have multiple classes having main method in same JAVA file ?
Yes it is possible . While starting the application we mention the class name to be run . The JVM will look for the Main method only in the class whosw name we have mentioned . Here there is not conflict amongst the multiple classes having main ( ) method.
2. Give the name of top class of all classes in java and the top package which is the default package.
Top class of all classes in JAVA is object class.
Top package which is default package in java .lang .package.
If string args[] is not written in main() method .
If string args[] is not written in main( ) method .
When main( ) method is written without Sring args[] as :
public static void main ( )
The code will compile but JVM cannot run the code because it cannot recognize the main ( ) as the method from where it should start execution of the Java program .
Remember JVM always looks for main ( ) method with string type array as parameter .
Thank you 💖
Explain Public Static Void Main(string args[]).
Explain:- Public Static Void Main(string args[]).
Public Static Void Main(string args[])
1. Public :-It is a keyword and denotes that any other class ( JVM) can call the main () method without any restrictions.
2. Static :- It is a keyword and denotes that any other class (JVM) can call the main() method without the help of an object . It can be accessed without creating the instance of a Class.
3. Void :- It is a keyword and denotes that main () method does not return a value.
4. Main() :- It is the name of method . This Method name is searched by JVM as a starting point for an application with a particular signature only .
5. String args[] :- The parameter is a String array by name args. The string array is used to access command- line arguments . it is the parameter to main method.
Thank you
💗
Subscribe to:
Posts (Atom)
Student Marks Calculation app using java
import javax.swing. *; import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; public class Student { private J...
-
The Type Promotion Rules :- Java defines several type promotion rules that apply to expressions. They are as follows : all byt...
-
Java Environment Java environment includes a number of development tools , classes and methods. The development tools are part ...
-
JAVA program to input and print n number using array :- PROGRAM :- import java.io.*; class Clsarray { public static void main(St...