Karthikeya Java423 File Final
Karthikeya Java423 File Final
Karthikeya Java423 File Final
Gwalior
History of JAVA.
JAVA was developed by James Gosling un the early 1990s. It was initially developed for
digital devices such as the televisions, etc. Initially it was named as “Oak”, but later it was
renamed as “JAVA”. It was created on the principles like robust, portable, platform
independent, high performance, multithreading, etc. Currently, it is being used in internet
programming, mobile devices, games, e-business solutions etc.
Features of JAVA.
Feature of JAVA include:
Simple
Object-oriented
Portable
Platform independent
Secured
Robust
Architecture neutral
Interpreted
High performance
Multithreaded
Distributed
Dynamic
Usage of JAVA.
Usages of JAVA programming language are:
Mobile App Development
Desktop GUI Applications
Web-based Applications
Gaming Applications
Big Data Technologies
Distributed Applications
Cloud-based Applications
IoT Applications
Drawbacks of JAVA.
Takes much longer time to run compared to C and C++.
Consumes more memory.
Hardware cost is high.
Doesn’t support low level programming, like pointers.
Programmer doesn’t have right to control the garbage collection.
1.Basic programs.
1.1 WAP to add two numbers provided by the user.
import java.util.Scanner;
public class PrimeNumber
{
public static void main(String args[])
{
int num,b,c;
Scanner s=new Scanner(System.in);
System.out.println("Enter A Number");
num =s.nextInt();
b=1;
c=0;
while(b<= num)
{
if((num%b)==0)
c=c+1;
b++;
}
if(c==2)
System.out.println(num +" is a prime number");
else
System.out.println(num +" is not a prime number");
}
}
}
}
}
}
}
2.Array.
2.1 WAP to take and print an array.
public class PrintArray {
}
for(int j = 0;j<arr.length;j++) {
sum=sum+j;
}
System.out.println("Sum of the array: "+sum);
}
}
}
for(int j = 0;j<ar.length;j++) {
sum=sum+j;
}
avg=sum/ar.length;
System.out.println("Sum of the array: "+sum);
System.out.println("Average of the array: "+avg);
}
}
3.Control Statements.
3.1 WAP to check the seasons by if-else statements.
public class ifelse {
}
3.5 WAP to show while loop.
class Box{
double width;
double height;
double depth;
}
//this class declares an object of type Box.
public class Boxdemo {
class Box2{
double width;
double height;
double depth;
void volume() {
System.out.print("Volume: ");
System.out.println(width*height*depth);
}
}
public class Boxdemo2 {
class Rect{
int length;
int breadth;
//constructor to intitialize length and breadth of rectangle
Rect(int l, int b)
{
length = l;
breadth = b;
}
Rect(Rect obj)
{
System.out.println("Copy constructor invoked.\n");
length = obj.length;
breadth = obj.breadth;
}
//method to calculate area of rectangle
int area() {
return length*breadth;
}
}
public class Rectangle {
class JBT2{
int variable = 10;
void method(int variable) {
variable = 15;
System.out.println("Value of Instance variable: "+this.variable);
System.out.println("Value of Local variable: "+variable);
}
void method() {
int variable = 35;
System.out.println("\n");
System.out.println("Value of Instance variable: "+this.variable);
System.out.println("Value of Local variable: "+variable);
}
}
class JBT3 {
class Box5{
double width;
double height;
double depth;
//This is the constructor for Box5
Box5(double w, double h, double d){
width = w;
height = h;
depth = d;
}
//compute and return volume
double volume() {
return width*height*depth;
}
}
public class Boxdemo5 {
5.Overloading.
class OverloadDemo{
void test() {
System.out.println("No parameters");
}
//overload test for one integer parameter.
void test(int a) {
System.out.println("a: "+a);
}
//overload test for two integer parameters.
void test(int a,int b) {
System.out.println("a and b: "+a+" "+b);
}
//overload test for a double parameter
double test(double a) {
System.out.println("double a: "+a);
return a*a;
}
}
class Overload extends OverloadDemo{
class OverloadDemo1{
void test() {
System.out.println("No Parameters.");
}
//Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: "+a+" "+b);
}
//Overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: "+a);
}
}
public class Overload1 {
class Test {
void meth(int i, int j) {
i*=4;
j/=4;
}
}
public class CallByValue {
class Test1{
int a,b;
Test1(int i,int j){
a= i;
b= j;
}
//pass an object
void meth(Test1 o) {
o.a*= 4;
o.b/=4;
}
}
public class CallByRef {
class Factorial{
//this is a recursive function
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1)*n;
return result;
}
}
public class Recursion {
static int b;
System.out.println("x: "+x+"\n");
System.out.println("a: "+a+"\n");
System.out.println("b: "+b);
}
static {
b=a*10;
meth(50);
class StaticDemo{
static int a = 45;
static int b = 100;
static void callme() {
System.out.println("a: "+a+"\n");
}
}
class StaticByName {
6.Inheritance.
6.1 WAP to show the concept of inheritance.
class A{
int i,j;
void showij() {
System.out.println("i and j: "+i+" "+j);
}
}
class B extends A{
int k;
void showk() {
System.out.println("k: "+k);
}
void sum() {
System.out.println("i+j+k: "+(i+j+k));
}
}
public class simpleinheritance {
class Vehicle1{
void run() {
System.out.println("Vehicle is running.");
}
}
class Bike1 extends Vehicle1{
void run()
{
System.out.println("Bike is running safely.");
}
class Bank{
int getRateOfInterest() {
return 0;
}
}
class SBI extends Bank{
int getRateOfInterest() {
return 5;
}
}
class ICICI extends Bank{
int getRateOfInterest() {
return 7;
}
}
class AXIS extends Bank{
int getRateOfInterest() {
return 6;
}
}
public class Test {
class Animal1{
public void move() {
System.out.println("Animals can move.");
}
}
class Dog1 extends Animal1{
public void move() {
super.move();//invokes the super class method
System.out.println("Dogs can walk and run.");
}
}
public class TestDog1 {
7.Exception Handling.
7.1 WAP to implement exception handling.
import java.lang.Throwable;
8.JAVA Packages.
8.1 WAP to demonstrate abstract class and methods.
Protection.java:
package p1;
Derived.java:
package p1;
public Derived() {
System.out.println("Derived constructor.");
System.out.println("n= "+n);
System.out.println("n_pri= "+n_pri);
System.out.println("n_pro= "+n_pro);
System.out.println("n_pub= "+n_pub);
}
SamePackage.java:
package p1;
public SamePackage() {
Protection p =new Protection();
System.out.println("Same package constructor.");
System.out.println("n= "+p.n);
System.out.println("n_pri= "+p.n_pri);
System.out.println("n_pro= "+p.n_pro);
System.out.println("n_pub= "+p.n_pub);
}
Demo.java:
package p1;
}
8.3 WAP to create and implement package p2.
Protection2:
package p2;
public Protection2() {
System.out.println("Derived other package constructor.");
System.out.println("n_pro= "+n_pro);
System.out.println("n_pub= "+n_pub);
}
OtherPackage:
package p2;
public OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("Other package constructor.");
System.out.println("n_pub= "+p.n_pub);
}
Demo2:
package p2;
MyPack:
package MyPack;
TestBalance:
import MyPack.*;
public class TestBalance {
9.JAVA Interfaces.
9.1 WAP to show interfaces in JAVA.
interface MyInterface{
public void method1();
public void method2();
}
public class Inter1 implements MyInterface{
public void method1() {
System.out.println("Implements method1.");
}
public void method2() {
System.out.println("Implements method2.");
}
public static void main(String[] args) {
MyInterface obj = new Inter1();
obj.method1();
interface Inf1{
public void method1();
}
interface Inf2 extends Inf1{
public void method2();
}
public class Demo implements Inf2{
public void method1() {
System.out.println("method1.");
}
public void method2() {
System.out.println("method2.");
}
interface Moveable{
public boolean ismoveable();
}
interface Rollable {
public boolean isrollable();
}
public class Tyre implements Moveable{
int width;
public boolean ismoveable() {
return true;
}
public boolean isrollable() {
return true;
}
public static void main(String[] args) {
Tyre tr = new Tyre();
System.out.println(tr.ismoveable());
System.out.println(tr.isrollable());
10.Multithreading.
10.1 WAP to implement thread using thread class.