JAVA SAMPLE PROGRAM

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Basic Terms of Java

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 .

JVM stands for Java Virtual Machine.


It provides a runtime environment for driving Java applications or code.

API (Application Programming Interface) – is the way to expose a set


of pre-defined classes and interfaces to external clients to interact with
them, without sharing the implementation details
Write a simple java program

import java.io.*;
public class Simple
{
public static void main(String args[])
{
System.out.println("hello javatpoint");
}
}

To compile: javac Simple.java


To execute: java Simple
Runtime:At runtime, the following steps are performed
compile time
At compile time, the Java file is compiled by Java
Compiler (It does not interact with OS) and converts
the Java code into bytecode.

Classloader: It is the subsystem of JVM that is used to load


class files.
Bytecode Verifier: Checks the code fragments for illegal code
that can violate access rights to objects.
Interpreter: Read bytecode stream then execute the
instructions.
set path=C:\Program Files\Java\jdk1.6.0_23\bin
Write a simple java program

import java.io.*;
public class Simple
{
public static void main(String args[])
{
System.out.println("hello javatpoint");
}
}

To compile: javac Simple.java


To execute: java Simple
Sample Program
import java.io.*;
class GFG
{
public static int sum(int num1, int num2)
{
return num1+num2;
} Output:
// main function 77
public static void main(String[] args)
{
GFG ob = new GFG();
int res = ob. sum(28, 49);
System.out.println(res);
}
}
Factorial of 5 is: 120

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();
}

// Interface for academic details


interface AcademicDetails
{
void setAcademicDetails(String course, int year, double grade);
void displayAcademicDetails();
}
// Student class implementing both interfaces
class Student implements PersonalDetails, AcademicDetails {
private String name;
private int age;
private String address;
private String course;
private int year;
private double grade;

// Implementing methods from PersonalDetails interface


public void setPersonalDetails(String name, int age, String address) {
this.name = name; //this is areference variable in java represent the current object
this.age = age;
this.address = address;
}

public void displayPersonalDetails() {


System.out.println("Personal Details:");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address: " + address);
}
// Implementing methods from AcademicDetails interface
public void setAcademicDetails(String course, int year, double grade)
{
this.course = course;
this.year = year;
this.grade = grade;
}

public void displayAcademicDetails()


{
System.out.println("Academic Details:");
System.out.println("Course: " + course);
System.out.println("Year: " + year);
System.out.println("Grade: " + grade);
}
}
// Main class to test the implementation
public class StudentDetails {
public static void main(String[] args) {
// Creating an instance of Student
Student student = new Student();

// Setting and displaying personal details


student.setPersonalDetails(“RAMU", 20, "123 Main St, Coimbatore");
student.displayPersonalDetails();

// Setting and displaying academic details


student.setAcademicDetails("Computer Science", 2, 3.8);
student.displayAcademicDetails();
}
}
output
Personal Details:
Name: Ramu
Age: 20
Address: 123 Main St, Coimbatore
Academic Details:
Course: Computer Science
Year: 2
Grade: 3.8

You might also like