Object Oriented Programming LAB FILE
LIST OF PROGRAMS
S.No |
PROGRAMS |
||
1. |
Introduction to Java. |
|
|
2. |
Write a Program to Print
HelloWorld. |
|
|
3. |
Write a Program to add two numbers
with out Scanner. |
|
|
4. |
Write a program to perform all
mathematical calculations without Scanner. |
|
|
5. |
Write
a program two sum two numbers by taking user input. |
|
|
6 |
Write a program to make menu using
Switch case. |
|
|
7. |
Write a
Program to perform single inheritance that can solve a real life reusability
problem. |
|
|
8. |
Write a program showing multi level
inheritance where each class at new features and inherit some features from
the base class |
|
|
9 |
Write a program to perform multiple
inheritance using interfaces |
|
|
10. |
Write a program of abstract class |
|
|
11. |
Write a program to perform
exception handling using try and catch |
|
|
12. |
Write a program to create an
application using swing |
|
|
PROGRAM NO: 1
AIM: Introduction to Java
THEORY: JAVA was developed by
James Gosling at Sun Microsystems Inc in the year 1995, later
acquired by Oracle Corporation. It is a simple programming language. Java makes
writing, compiling, and debugging programming easy. It helps to create
reusable code and modular programs. Java is a class-based, object-oriented programming
language and is designed to have as few implementation dependencies as
possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code
can run on all platforms that support Java. Java applications are compiled to
byte code that can run on any Java Virtual Machine. The syntax of Java is
similar to c/c++.
Java Terminology
Before
learning Java, one must be familiar with these common terms of Java.
1. Java Virtual Machine(JVM): This is generally referred to as JVM. There are three execution phases of a program.
They are written, compile and run the program.
·
Writing a program
is done by a java programmer like you and me.
·
The compilation
is done by the JAVAC compiler which is a
primary Java compiler included in the Java development kit (JDK). It takes the
Java program as input and generates bytecode as output.
·
In the Running phase of a program, JVM executes
the bytecode generated by the compiler.
Now, we understood that the function of Java
Virtual Machine is to execute the bytecode produced by the compiler. Every
Operating System has a different JVM but the output they produce after the
execution of bytecode is the same across all the operating systems. This is why
Java is known as a platform-independent language.
2. Bytecode in the Development process: As discussed, the Javac
compiler of JDK compiles the java source code into bytecode so that it can be
executed by JVM. It is saved as .class file by
the compiler. To view the bytecode, a disassembler like javap can be used.
3. Java Development Kit(JDK): While we were using the term JDK when we learn
about bytecode and JVM. So, as the name suggests, it is a complete Java
development kit that includes everything including compiler, Java Runtime
Environment (JRE), java debuggers, java docs, etc. For the program to execute
in java, we need to install JDK on our computer in order to create, compile and
run the java program.
4. Java Runtime Environment (JRE): JDK includes JRE. JRE installation on our computers
allows the java program to run, however, we cannot compile it. JRE includes a
browser, JVM, applet supports, and plugins. For running the java program, a
computer needs JRE.
Primary/Main
Features of Java
1. Platform Independent: Compiler converts source code to bytecode and then
the JVM executes the bytecode generated by the compiler. This bytecode can run
on any platform be it Windows, Linux, or macOS which means if we compile a
program on Windows, then we can run it on Linux and vice versa. Each operating
system has a different JVM, but the output produced by all the OS is the same
after the execution of bytecode. That is why we call java a
platform-independent language.
2. Object-Oriented Programming Language: Organizing the program in the terms of collection
of objects is a way of object-oriented programming, each of which represents an
instance of the class.
The
four main concepts of Object-Oriented programming are:
·
Abstraction
·
Encapsulation
·
Inheritance
·
Polymorphism
3. Simple: Java is one of the simple languages as it
does not have complex features like pointers, operator overloading, multiple
inheritances, and Explicit memory allocation.
4. Robust: Java language is robust which means
reliable. It is developed in such a way that it puts a lot of effort into
checking errors as early as possible, that is why the java compiler is able to
detect even those errors that are not easy to detect by another programming
language. The main features of java that make it robust are garbage collection,
Exception Handling, and memory allocation.
5. Secure: In java, we don’t have pointers, so
we cannot access out-of-bound arrays i.e it shows ArrayIndexOutOfBound Exception if we try to do so.
That’s why several security flaws like stack corruption or buffer overflow are
impossible to exploit in Java. Also java programs run in an environment that is
independent of the os(operating system) environment which makes java programs
more secure .
6. Distributed: We can create distributed
applications using the java programming language. Remote Method Invocation and
Enterprise Java Beans are used for creating distributed applications in java.
The java programs can be easily distributed on one or more systems that are
connected to each other through an internet connection.
7. Multithreading: Java supports multithreading. It
is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of the CPU.
8. Portable: As we know, java code written on one
machine can be run on another machine. The platform-independent feature of java
in which its platform-independent bytecode can be taken to any platform for
execution makes java portable.
9. High Performance: Java
architecture is defined in such a way that it reduces overhead during the
runtime and at some time java uses Just In Time (JIT) compiler where the
compiler compiles code on-demand basics where it only compiles those methods
that are called making applications to execute faster.
10. Dynamic flexibility: Java being completely object-oriented gives us the
flexibility to add classes, new methods to existing classes and even
create new classes through sub-classes. Java even supports functions written in
other languages such as C, C++ which are referred to as native methods.
11. Sandbox Execution: Java
programs run in a separate space that allows user to execute their applications
without affecting the underlying system with help of a bytecode verifier.
Bytecode verifier also provides additional security as its role is to check the
code for any violation of access.
12. Write Once Run Anywhere: As discussed above java application generates
a ‘.class’ file which corresponds to our applications(program) but contains
code in binary format. It provides ease t architecture-neutral ease as bytecode
is not dependent on any machine architecture. It is the primary reason java is
used in the enterprising IT industry globally worldwide.
13. Power of compilation and interpretation: Most languages are designed with purpose
either they are compiled language or they are interpreted language. But java
integrates arising enormous power as Java compiler compiles the source code to
bytecode and JVM executes this bytecode to machine OS-dependent
executable code.
PROGRAM NO: 2
AIM:
Write a Program to Print HelloWorld.
PROGRAM CODE:
class Hello{
public static
void main(String[] args){
System.out.println("Hello
World");
}
}
PROGRAM NO: 3
AIM: Write a Program to add two numbers without Scanner.
PROGRAM CODE:
class ADD {
public static void main(String[] args) {
int a=10,b=12,sum;
sum=a+b;
System.out.println("sum of two
no.s is: "+sum);
}
}
By
objects:
class A{
int a,b;
void input(int c, int d)
{
a=c;
b=d;
}
void add(){
System.out.println("sum is: "+a+b);
}
}
class ADD1{
public static void main(String[] args){
A obj = new A();
obj.input(3,5);
obj.add();
}
}
PROGRAM NO: 4
AIM: Write a program to perform all mathematical
calculations without Scanner.
PROGRAM CODE:
class Calc {
public static void main(String[] args) {
int a=10,b=12,sum,sub,product,div,mod;
sum=a+b;
sub=a-b;
product=a*b;
div=a/b;
mod=a%b;
System.out.println("sum of two no.s
is: "+sum);
System.out.println("differnce of two
no.s is: "+sub);
System.out.println("product of two
no.s is: "+product);
System.out.println("division of two
no.s is: "+div);
System.out.println("modulus of two
no.s is: "+mod);
}
}
PROGRAM
NO: 5
AIM:
Write
a program two sum two numbers by taking user input.
PROGRAM
CODE:
import
java.util.*;
class AddScan {
public static
void main(String args[]){
int a,b,sum;
Scanner sc = new Scanner(System.in);
System.out.print("enter the first no.:
");
a=sc.nextInt();
System.out.print("enter the second
no.: ");
b=sc.nextInt();
sum=a+b;
System.out.println("sum is: "+sum);
}
}
AIM:
Write a program to make menu using Switch case.
PROGRAM
CODE:
import java.util.Scanner;
class A{
public static void main(String args[]){
int a,b;
char operation;
Scanner input = new Scanner(System.in);
System.out.println("enter the first
no.:");
a = input.nextInt();
System.out.println("enter the first
no.:");
b = input.nextInt();
System.out.println("choose the
operation (+,-,*,/,%): ");
operation = input.next().charAt(0);
switch(operation){
case '+':
System.out.println("sum of two no.s
is: ");
System.out.println(a+b);
break;
case '-':
System.out.println("difference of two
no.s is: ");
System.out.println(a-b);
break;
case '*':
System.out.println("product of two
no.s is: ");
System.out.println(a*b);
break;
case '/':
System.out.println("division of two
no.s is: ");
System.out.println(a/b);
break;
case '%':
System.out.println("modulus of two
no.s is: ");
System.out.println(a%b);
break;
default:
System.out.println("Invalid
input!!");
break;
}
input.close();
}
}
PROGRAM NO: 7
AIM: Write a Program
to perform single inheritance that can solve a real life reusability problem.
PROGRAM
CODE:
class Phone {
void call(){
System.out.println("call");
System.out.println(" ");
}
}
class Smartphone extends Phone{
void vedio_call(){
System.out.println("vedio call");
}
}
class SingleInheritance{
public static void main(String args[]){
Smartphone obj =new Smartphone();
obj.call();
obj.vedio_call();
}
}
PROGRAM NO: 8
AIM: Write a program showing
multi level inheritance where each class at new features and inherit some
features from the base class
PROGRAM CODE:
class
Telephone {
public void call(){
System.out.println("Calling
phone");
}
}
class
Phone extends Telephone{
public void sms(){
System.out.println("SMS
phone");
}
}
class
Smartphone extends Phone{
public void camera(){
System.out.println("Camera
phone");
}
}
class
MultilevelInheritance{
public static void main(String args[]){
Smartphone obj = new Smartphone();
obj.call();
obj.sms();
obj.camera();
}
}
PROGRAM NO: 9
AIM:
Write a program to perform multiple inheritance using interfaces.
PROGRAM
CODE:
interface Android{
public
void Playstore();
}
interface IOS{
public
void Applestore();
}
class Smartphone implements Android,IOS{
public
void Playstore(){
System.out.println("Android
applicatiions are here ");
}
public
void Applestore(){
System.out.println("IOS
Applications are here ");
}
}
class MultipleInheritance{
public
static void main(String args[]){
System.out.println("Smartphone");
Smartphone
obj = new Smartphone();
obj.Playstore();
obj.Applestore();
}
}
PROGRAM NO: 10
AIM:
Write a program of abstract class.
PROGRAM
CODE:
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){
System.out.println("running
safely");
}
public static void main(String
args[]){
Bike obj = new Honda4();
obj.run();
}
}
PROGRAM NO: 11
AIM:
Write a program to perform exception handling using try and catch
PROGRAM
CODE:
public class ExceptionHandle {
public static void main(String[] args) {
try
{
int data=50/0;
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
PROGRAM NO: 12
AIM:
Write a program to create an application using swing
PROGRAM
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingAddApp
{
public static void main(String args[])
{
Addp obj=new Addp();
}
}
class Addp extends JFrame
implements ActionListener
{
JLabel l1;
JTextField t1;
JLabel l2;
JTextField t2;
JButton b;
JLabel l3;
public Addp()
{
setLayout(new FlowLayout());
l1=new JLabel("First Number:");
t1=new JTextField(20);
l2=new JLabel("Second Number:");
t2=new JTextField(20);
b=new JButton("Add");
l3=new JLabel("Result");
add(l1);
add(t1);
add(l2);
add(t2);
add(b);
add(l3);
b.addActionListener(this);
setVisible(true);
setSize(250,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
int num1=Integer.parseInt(t1.getText());
int num2=Integer.parseInt(t2.getText());
int value=num1+num2;
l3.setText(""+value);
}
}
No comments:
Post a Comment