Java
Java
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1){
}else{
for(i=2;i<=m;i++){
if(n%i==0){
flag=1;
break;
}//end of else
Output:
3 is prime number.
*********************************************
Example:
import java.util.Scanner;
import java.lang.Math;
temp=n;
while(temp>0)
temp = temp/10;
digits++;
temp = n;
while(temp>0)
//calculates the power of a number up to digit times and add the resultant to the sum
variable
temp = temp/10;
return true;
//driver code
int num;
num=sc.nextInt();
//function calling
if(isArmstrong(i))
Output:
3.RandomNumberExample1.java
import java.lang.Math;
Output:
********************************************
4.CreateObjectExample1.java
void show()
System.out.println("Welcome to javaTpoint");
}
obj.show();
Output:
Welcome to javaTpoint
*********************************************
program:
class Lamp {
// true if light is on
boolean isOn;
void turnOn() {
isOn = true;
isOn = false;
class Main {
led.turnOn();
halogen.turnOff();
Output:
====================================================================
class Lamp {
// stores the value for light
// true if light is on
boolean isOn;
void turnOn() {
isOn = true;
led.turnOn();
output:
============================================================
program:
class OnedimensionalStandard
{
public static void main(String args[])
a[0]=10;//initialization
a[1]=20;
a[2]=30;
//printing array
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
Output:
10
20
30
package ArrayDefinitions;
System.out.println("");
Output:
27 47 67
100 80 150
==========================================================================
class Bike1{
Bike1(){System.out.println("Bike is created");}
//main method
Output:
Bike is created
=================================================
class Student3{
int id;
String name;
//creating objects
s1.display();
s2.display();
Output:
0 null
0 null
==================================================================
In this example, we have created the constructor of Student class that have two parameters.
We can have any number of parameters in the constructor.
class Student4{
int id;
String name;
id = i;
name = n;
s1.display();
s2.display();
Output:
111 Karan
222 Aryan
==============================================
In Java, a constructor is just like a method but without return type. It can also be overloaded
like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a
different task. They are differentiated by the compiler by the number of parameters in the
list and their types.
class Student5{
int id;
String name;
int age;
id = i;
name = n;
id = i;
name = n;
age=a;
s1.display();
s2.display();
Test it Now
Output:
111 Karan 0
222 Aryan 25
======================================================================
class Student{
String name;
//constructor
rollno = r;
name = n;
class TestStaticVariable1{
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
Output:
===============================================
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
class Student{
int rollno;
String name;
college = "BBDIT";
rollno = r;
name = n;
}
//method to display values
class TestStaticMethod{
//creating objects
s1.display();
s2.display();
s3.display();
Test it Now
//Java Program to get the cube of a given number using the static method
class Calculate{
return x*x*x;
System.out.println(result);
Test it Now
Output:125
======================================================
class Bike9{
void run(){
speedlimit=400;
obj.run();
}//end of class
Test it Now
==============================================
class Bike{
}
class Honda extends Bike{
honda.run();
Test it Now
========================================================
honda.run();
Test it Now
===========================================
Ans) Yes, final method is inherited but you cannot override it. For Example:
class Bike{
new Honda2().run();
Test it Now
Output:running...
==================================================