Java Sample Program
Java Sample Program
There are three main terminologies in Java coding basics: class, objects, and methods.
•Class: Classes in Java are the blueprint used to contain objects inside.
•It is used to define the object attributes and methods.
•For example, consider a class car with attributes like brand, year of manufacture, etc.
•Objects: An object is an instance of a class based on a real-world entity. Objects are real-world
entities that are created inside the class.
•Methods: Methods are the function which consists of the operation defined inside a class.
Methods are used to perform different operations and actions inside the class.
JDK is an abbreviation for Java Development Kit.
It is an environment of software development used for developing applets and Java applications.
JRE stands for Java Runtime Environment- also written as Java RTE.
It is a set of software tools designed for running other software .
import java.io.*;
public class Simple
{
public static void main(String args[])
{
System.out.println("hello javatpoint");
}
}
import java.io.*;
public class Simple
{
public static void main(String args[])
{
System.out.println("hello javatpoint");
}
}
Sample
Import java.io.*;
class Factorial{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
} Output:
} Factorial of 5 is 120
Sample
import java .io.*;
public class EvenNumbers
{
public static void main(String args[])
{ Output:
int number=100;
List of even numbers from 1 to 100: 2 4 6 8
System.out.print("List of even numbers from 1 to "+number+": "); 10 12 14 16 18 20 22 24 26 28 30 32 34 36
for (int i=1; i<=number; i++) 38 40 42 44 46 48 50 52 54 56 58 60 62 64
66 68 70 72 74 76 78 80 82 84 86 88 90 92
{ 94 96 98 100
//logic to check if the number is even or not
//if i%2 is equal to zero, the number is even
if (i%2==0)
{
System.out.print(i + " ");
}
}
}
Sample prg using constructor
public class Car {
String brand; public class Main {
int year; public static void main(String[] args)
{
// Constructor
// Creating objects of the Car class
public Car(String brand, int year) Car car1 = new Car(“Toyota”, 2020);
{ Car car2 = new Car(“Honda”, 2019);
this.brand = brand; car1.displayInfo();
this.year = year; car2.displayInfo();
} }
// Method to display information about the car }
Output
public void displayInfo() { Brand: Toyota Year: 2020
System.out.println(“Brand: ” + brand); Brand: Honda Year: 2019
System.out.println(“Year: ” + year);
}
//Exercise 1: Write a Java Applications to extract a portion of a character string and print the extracted string.
import java.io.*;
class exe1
{
public static void main(String args[])
{
String s,str,substr;
int extract,start,len,check;
try{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter String : ");
System.out.flush();
str=obj.readLine();
len=str.length();
System.out.print("Enter Starting position to extract characters : ");
System.out.flush();
s=obj.readLine();
start=Integer.parseInt(s);
start=start-1;
if(start<0 || start>len)
{
System.out.println("INVALID POSITION");
System.exit(1);
}
System.out.print("Enter how many characters you want to extract : ");
System.out.flush(); // used to force any buffered data to written immediately without closing filewriter
s=obj.readLine();
extract=Integer.parseInt(s); // converting to string
check=extract+start;
if(check<0 || check>len )
{
System.out.println("TRYING TO EXTRACT INVALID POSITION");
System.exit(1);
} substr=str.substring(start,check);
System.out.println("\nEXTRACTED STRING IS \n"+substr);
}
catch(Exception e) {}
}}
Multiple Inheritance Using Interface
// Interface for personal details
interface PersonalDetails
{
void setPersonalDetails(String name, int age, String address);
void displayPersonalDetails();
}