Java File
Java File
Simple
Object-Oriented Portable
Distributed
Java
Complied and Interpreted
Features
Robust
High Performance
Ans. Java is a general purpose language and the most popular object oriented programming
language. Due to its simplicity and easy to learn features , it is the first choice of programmers
today. The java programming language is related to Cand C++ but is organized rather
differently, with many sources of bugs and complex features of C and C++ omitted and a few
ideas from other languages included. In addition to the object oriented features, it also provides
features such as platform independence, security , multithreading , portability etc. which makes it
well suited for the web and network services , application , platform-independent desktop ,
robotics and any other embedded devices. These feature together makes java no longer just a
programming language but a platform on its own. The java platform has benefits for the end
user as well as development and support personnel.
Java was developed by James Gosling and his colleagues at Sun Microsystem in the
early 1990’s . Unlike conventional language which are generally designed either to be complied
to machine code , or to interpreted from source code at run time, Java application are intended to
be complied to bytecode that can run on any java Virtual Machine(JVM) regardless of the
computer architecture.
FEATURES OF JAVA: Java is a very powerful , cross-platform , object-oriented programming
language which is designed to solve a number of problems in modern programming practice. It
supports many interesting features that makes it an ideal language for software development .
Java supports the following features that are a part of the official website of the Sun
Microsystem:
Simple: Java is a compared and simple language. Program are easy to write and debug as it
omits many clumsy , poorly understood and confusing features of other programming languages
such as C++. These primarily include exclusive of header files, pointers , implicit type-casting ,
structure, union , operator overloading, multiple inheritance , virtual base classes, templates etc.
Object-Oriented: Java is a fully object- oriented language because programming in java is
centered on creating objects , manipulating objects and making objects work together. In Java ,
everything is an object i.e.
All data types with an exception of few primitive types are rendered as object .
All of GUI building blocks in java are objects.
All function are associated with objects and there can be no free floating function as there are in
C++.
Distributed: Java is a distributed language which means that the programs can be designed to
run on computer networks. Java provides an extensive library of classes for communicating
using TCP/IP protocols such as HTTP and FTP. This makes creating network connections much
easier than in C/C++. You can read and write data from and to a file. This helps the programmers
at remote location to work together on the same project.
Robust: Java is designed for writing programs that are highly robust , we mean reliable. Java
puts a lot of emphasis on early checking of possible errors which would otherwise be detected at
execution time in other language. Java also provides strong type checking feature and eliminates
errors prone programming constructs such as pointer.
Java also has a runtime exception handling feature to provide programming supports for
robustness. Java forces the programmer to write the code to deal with exceptions . It can catch
and respond to an exceptional situation so that program can continue its normal execution and
terminate gracefully when runtime error occurs.
The feature of automatic garbage collection makes the programs reliable as it relieves the
programmers from worry of manually and freeing all the dynamic memory.
Secure: Java was designed with the security in mind. As Java is intended to be used in
networked/distributed environments so it implements several security mechanisms to protect you
against malicious code that might try to invade your file security.
Architectural Neutral: One of the key feature of Java that makes it different from other
programming language is architectural neutral. This means that the programs written on one
platform can run on any other platform without having to rewrite or recompile them. In other
words , it follows ‘Write –once-run-anywhere’ approach. This is a significant advantage when
developing applets or applications that are down-loaded from the internet and are needed to run
on different system.
Portable: The portability actually comes from architecture-neutrality. In C/C++ , source code
may run slightly differently on different hardware platform because of how these platforms
implement arithmetic operations. In Java , it has been simplified.
Interpreted: Unlike most of the programming language which are either complied or
interpreted, Java is both complied and interpreted The Java complier translates a java source a
java source file to bytecodes and the Java interpreter executes the translated byte codes directly
on the system that implements the Java Virtual Machine. These two steps of compilation and
interpretation allow extensive code checking and improved security.
High performance : Java programs are complied to portable intermediate form kown as
bytecodes , rather than to native machine level instructions and JVM executes Java bytecode on
any machine on which it is installed. This architecture means that Java programs are faster than
program or scripts written in purely interpreted languages but slower than C and C++ programs
that complied to native machine language.
Multithreaded: Java is also a multithreaded programming language . It allows you to write a
program that can do many tasks simultaneously . Multithreaded enables you to write very
efficient program that makes maximum use of CPU, because idle time can be kept to minimum.
This feature makes Java most suited for the interactive , GUI based networked environment
where idle is common.
Dynamic: Java is designed to be dynamic. Classes are stored in separate files and are loaded into
Java Interpretator only when they are needed. Thus application can decide as it is running what
classes it needs and loaded them when required. In other words , a program can dynamically
extend itself by loading the classes it needs to expand its functionality.
Qus2. Write a program in java find the greatest of three numbers using conditional
operators.
Ans.
import java.util.Scanner;
public class Greatest
{
public static void main(String[] args)
{
int a, b,c,result;
Scanner input=new Scanner(System.in);
System.out.print("Enter the three number------>");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
result=(a>b)?((a>c)?a:c):((b>c)?b:c);
System.out.println(result+ "is Greatest");
}
}
Qus.3 Calculate percentage and grade of student using nested if else statement.
Ans.
import java.util.Scanner;
import java.util.*;
class Per1
{
public static void main(String[] args)
{
int m1,m2,m3;
long sum;
double perc;
System.out.println("Enter marks of student:");
Scanner a=new Scanner(System.in);
m1=a.nextInt();
m2=a.nextInt();
m3=a.nextInt();
sum=m1+m2+m3;
perc=sum/3;
System.out.println("Sum is:="+sum );
System.out.println("Percentage is="+perc);
if((perc>=80 ) && (perc<=100))
{
System.out.println("Grade A");
}
else
if((perc>=60) && (perc<=79))
{
System.out.println("Grade B");
}
if((perc>=50) && (perc<=59))
{
System.out.println("Grade C");
}
else
if((perc>=40 )&& (perc<=49))
{
System.out.println("Grade D");
}
else
if(perc<40)
{
System.out.println("Failed");
}
}
}
Qus.5 Write a program in java to check whether a given number is palindrome or not.
Ans.
import java.util.Scanner;
class Revn
{
public static void main(String[] args)
{
int r , n,rev=0,n1;
Scanner b=new Scanner(System.in);
System.out.println("Enter number:");
n=b.nextInt();
n1=n;
while(n>0)
{
rev=n%10;
rev=rev*10+rev;
n=n/10;
}
if(rev==n1)
System.out.println("Number is Palindrome");
else
System.out.println("Number is not Paindrome");
}
}
b) import java.util.Scanner;
class pattern1
{
public static void main(String[] args)
{
int i, j, n;
Scanner b=new Scanner(System.in);
System.out.print("Enter value for n:");
n=b.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
c)
import java.util.Scanner;
class Pyramid
{
public static void main(String [] args)
{
Scanner input=new Scanner(System.in);
int i,j,n;
char ch='A';
System.out.println("enter the number of lines you want to enter" );
n=input.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(ch);
ch++;
}
System.out.println(" ");
}
}
}
Qus. 9 Write a program in java using switch case that performs following operation:
I. To check whether a number is prime or not
II. To print table of a number
III. To check whether a given number is Armstrong or not
IV. To print Fibonacci series.
Ans.
import java.util.*;
import java.io.*;
class Switchcase1
{
public static void main(String[] args)
{
int i,n,c,f=0,s=1,t,tab,flag=1,arm=0,rev;
System.out.println("Enter your option:");
System.out.println("1. To check wheater a given number is prime or not:");
System.out.println("2.To print table of a number:");
System.out.println("3.To check wheater a given number as armstrong or not :");
System.out.println("4.To print fabbonic series:");
System.out.println("5. To exit from program:");
Scanner a=new Scanner(System.in);
c=a.nextInt();
switch(c)
{
case 1:{
System.out.println("Enter the number to check prime:");
n=a.nextInt();
for(i=2;i<=n;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==1)
System.out.println(n+"is prime number");
else
System.out.println(n+"is not a prime number");
break;
}
case 2:
{
Fibonic series:");
n=a.nextInt();
System.out.println(f);
System.out.println(s);
for(i=1;i<=n-2;i++)
{
t=f+s;
System.out.println(t);
f=s;
s=t;
}
break;
}
default: System.out.println("You have entered wrong option plz
import java.util.Scanner;
class Complex
{
private float real,imag;
Complex(){}
Complex(float r, float i)
{
real=r;
imag=i;
}
void show()
{
if(imag>0)
System.out.println(real+"+"+imag+"i");
else
System.out.println(real+imag+"i");
}
void add(Complex cc1,Complex cc2)
{
real=cc1.real+cc2.real;
imag=cc1.imag+cc2.imag;
}
void subtract(Complex cc1,Complex cc2)
{
real=cc1.real-cc2.real;
imag=cc1.imag-cc2.imag;
}
void multiply(Complex cc1,Complex cc2)
{
real=(cc1.real*cc2.real)-(cc1.imag*cc2.imag);
imag=(cc1.real*cc2.imag)+(cc1.imag*cc2.real);
}
void divide(Complex cc1,Complex cc2)
{
float den=cc2.real*cc2.real+cc2.imag+cc2.imag;
real=(cc1.real*cc2.real+cc1.imag*cc2.imag)/den;
imag=(cc1.imag*cc2.real-cc1.real*cc2.imag)/den;
}};
class Complexdemo
{
public static void main(String[] args)
{
Qus.12 Write a program using constructor to transfer amount from one account to
another.
Ans.
import java.util.Scanner;
class Account
{
private int acctno;
private double balance;
Account(int acc, double b)
{
acctno=acc;
balance=b;
}
void transfer(Account obj , double amt)
{
balance=balance-amt;
obj.balance=obj.balance+amt;
}
void show()
{
System.out.println("Account Number is ="+acctno);
System.out.println("Balance is ="+balance);
}
}
class TransferMoney
{
public static void main(String[] args)
{
Account acct1= new Account(101,5000);
System.out.println("Information about Acct1.....");
acct1.show();
Account acct2=new Account(102,2000);
System.out.println("Information about Acct2....");
acct2.show();
Scanner input=new Scanner(System.in);
System.out.println("Enter amount to be transferred..>");
double amt=input.nextDouble();
acct1.transfer(acct2,amt);
System.out.println("Information after money is transfered....");
acct1.show();
acct2.show();
}
for(int i=0;i<table.length;i++)
{
for(int j=0;j<table.length;j++)
System.out.print(result[i][j]+ " ");
System.out.println();
}
}
static int[][] transpose(int [] []a)
{
int [] [] temp=new int[a[0].length][a.length];
for(int i=0;i<a[0].length;i++)
{
for(int j=0;j<a.length;j++)
temp[i][j]=a[j][i];
}
return temp;
}
}
Ans.
import java.util.Scanner;
class Matmul
{
public static void main(String args[])
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
System.out.println("Enter the first matrix:");
Scanner input=new Scanner(System.in);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=input.nextInt();
for(int j=0;j<3;j++)
b[i][j]=input.nextInt();
System.out.println("Matrix multiplication is as follows:");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println("\n");
}
System.out.println("\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println("\n");
}
}
}
Qus.16 Show the use of abstract class and abstract methods in java.
Ans.
import java.util.Scanner;
abstract class Shape
{
abstract void area();
abstract void circumference();
}
class Rectangle extends Shape
{
private double length,breadth;
Rectangle(double x,double y)
{
length=x; breadth=y;
}
public void area()
{
System.out.println("area of rectangle is="+ (length*breadth));
}
public void circumference()
{
System.out.println("Circumference of Rectangle is="+2*(length+breadth));
}
}
class Circle extends Shape
{
private double radius;
Circle(double r)
{
radius=r;
}
public void area()
{
System.out.println("Area of circle is ="+(Math.PI*radius*radius));
}
public void circumference()
{
System.out.println("Circumference of circle is="+2*Math.PI*radius);
}
}
class AbstractDemo
{
public static void main( String[] args)
{
Shape s;
Rectangle r= new Rectangle(10,20);
s=r;
s.area();
s.circumference();
Circle c=new Circle(5);
s=c;
s.area();
s.circumference();
}
}
{
return (length*breadth);
}
public double circumference()
{
return(2*(length+breadth));
}
}
class Circle extends Shape
{
private double radius;
Circle(double r)
{
radius=r;
}
public double getRadius()
{
return radius;
}
public double area()
{
return(Math.PI*radius*radius);
}
public double circumference()
{
return(2*Math.PI*radius);
}
}
interface CenteredShape
{
public void setCenter(double x, double y);
public double getCenterx();
public double getCentery();
}
class CenteredRectangle extends Rectangle implements CenteredShape
{
private double centerx , centery;
public CenteredRectangle(double cx, double cy , double l, double b)
{
super(l,b);
centerx=cx;
centery=cy;
}
public void setCenter(double x, double y)
{
centerx=x;
centery=y;
}
public double getCenterx()
{
return centerx;
}
public double getCentery()
{
return centery;
}
}
class MultipleInheritenceDemo
{
public static void main(String[] args)
{
Shape s;
CenteredRectangle r=new CenteredRectangle(2,3,4,5);
s=r;
System.out.println("Area of centered rectangle is="+s.area());
CenteredRectangle r1=(CenteredRectangle)s;
System.out.println("coordinates are=("+r1.getCenterx()+","+r1.getCentery()+")");
}
}
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle(5);
Rectangle r3=new Rectangle(7,8);
System.out.println("Area of first rectangle is "+r1.area());
System.out.println("Area of square is "+r2.area());
System.out.println("Area of second rectangle is "+r3.area());
}
}
Qus.21 Write a program in java to create multiple threads in java by extending Thread
class.
class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=1;i<5;i++)
{
System.out.println(i+" "+getName());
try
{
int delay=(int)Math.random()*1000;
sleep(delay);
}
catch(InterruptedException e) {}
}
System.out.println("DONE!" + getName());
}
}
class multiplethread
{
public static void main(String args[])
{
simpleThread thread1=new simpleThread("manu");
thread1.start();
simpleThread thread2=new simpleThread("Hanu");
thread2.start();
}