practical1
practical1
Interface is a collection of abstract data members such as methods. All the members
of an interface are implicitly public and abstract.
In this example, the Printable interface has only one method, and its implementation
is provided in the A6 class.
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6. {
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
Output= Hello
Extends and Implements Together
Following example shows that a class can extend another class while implementing
interfaces.
//Interface one
Interface studentdata {
interface studentmethod {
void display(); }
class test {
sub1=a;
sub2=b; }
void printmarks(); {
} }
Void print()
display();
printmarks();
Class demointerface
s.getmarks(80,75);
s.print();
Age= 18
Marks of Subject1= 80
Marks of Subject2= 75
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void show();
6. }
7. class A7 implements Printable,Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. A7 obj = new A7();
13. obj.print();
14. obj.show();
15. }
16. }
Output= Hello
Welcome
Marker Interface
An interface that does not contain methods, fields, and constants is known
as marker interface.
1. import java.util.Scanner;
2. public class Product implements Cloneable
3. {
4. int pid;
5. String pname;
6. double pcost;
7. //Product class constructor
8. public Product (int pid, String pname, double pcost)
9. {
10. this.pid = pid;
11. this.pname = pname;
12. this.pcost = pcost;
13. }
14. //method that prints the detail on the console
15. public void showDetail()
16. {
17. System.out.println("Product ID: "+pid);
18. System.out.println("Product Name: "+pname);
19. System.out.println("Product Cost: "+pcost);
20. }
21. public static void main (String args[]) throws CloneNotSupportedException
22. {
23. //reading values of the product from the user
24. Scanner sc = new Scanner(System.in);
25. System.out.print("Enter product ID: ");
26. int pid = sc.nextInt();
27. System.out.print("Enter product name: ");
28. String pname = sc.next();
29. System.out.print("Enter product Cost: ");
30. double pcost = sc.nextDouble();
31. System.out.println("-------Product Detail--------");
32. Product p1 = new Product(pid, pname, pcost);
33. //cloning the object of the Product class using the clone() method
34. Product p2 = (Product) p1.clone();
35. //invoking the method to print detail
36. p2.showDetail();
37. }
38. }
Output: Enter product ID: 139872
Enter product name: Printer
Enter product Cost: 3459.67
-------Product Detail-------
Product ID: 139872
Product Name: Printer
Product Cost: 3459.67
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).
The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse
methods and fields of the parent class. Moreover, you can add new methods and
fields in your current class also.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality of the
terminology of Java, a class which is inherited is called a parent or superclass, and the
new class is called child or subclass.
Java Inheritance Example
As displayed in the above figure, Programmer is the subclass and Employee is the
superclass. The relationship between the two classes is Programmer IS-A Employee.
It means that Programmer is a type of Employee.
1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7. Programmer p=new Programmer();
8. System.out.println("Programmer salary is:"+p.salary);
9. System.out.println("Bonus of Programmer is:"+p.bonus);
10. }
11. }
Output=
Programmer salary is:40000.0
Bonus of programmer is:10000
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel
and hierarchical.
When one class inherits multiple classes, it is known as multiple inheritance. For
Example:
Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the example
given below, Dog class inherits the Animal class, so there is the single inheritance.
File: TestInheritance.java
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }}
Output:
barking...
eating...
File: TestInheritance2.java
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16. }}
Output:
weeping...
barking...
eating...
File: TestInheritance3.java
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){System.out.println("meowing...");}
9. }
10. class TestInheritance3{
11. public static void main(String args[]){
12. Cat c=new Cat();
13. c.meow();
14. c.eat();
15. //c.bark();//C.T.Error
16. }}
Output:
meowing...
What is a Linker?
A linker is a program in a system, also known as a link editor and binder, which
combines object modules into a single object file. Generally, it is a program that
performs the process of linking; it takes one or multiple object files, which are
generated by compiler. And, then combines these files into an executable files.
Modules are called for the different pieces of code, which are written in
programming languages. Linking is a process that helps to gather and maintain a
different piece of code into an executable file or single file. With the help of a linker,
a specific module is also linked into the system library.
Linker is a computer program that links and merges various object files together in
order to make an executable file. All these files might have been compiled by
separate assemblers. The major task of a linker is to search and locate referenced
module/routines in a program and to determine the memory location where these
codes will be loaded, making the program instruction to have absolute references.
Loader
Loader is a part of operating system and is responsible for loading executable files
into memory and execute them. It calculates the size of a program (instructions and
data) and creates memory space for it. It initializes various registers to initiate
execution.
Introduction to Compiler
o A compiler is a translator that converts the high-level language into the machine
language.
o High-level language is written by a developer and machine language can be
understood by the processor.
o Compiler is used to show errors to the programmer.
o The main purpose of compiler is to change the code written in one language without
changing the meaning of the program.
o When you execute a program which is written in HLL programming language then it
executes into two parts.
o In the first part, the source program compiled and translated into the object program
(low level language).
o In the second part, object program translated into the target program through the
assembler.
Phases of a Compiler:
There are two major phases of compilation, which in turn have many parts.
Each of them takes input from the output of the previous level and works in a
coordinated way.
Analysis Phase: An intermediate representation is created from the given
source code :
1. Lexical Analyzer
2. Syntax Analyzer
3. Semantic Analyzer
4. Intermediate Code Generator
Lexical analyzer divides the program into “tokens”, the Syntax analyzer
recognizes “sentences” in the program using the syntax of the language and
the Semantic analyzer checks the static semantics of each construct.
Intermediate Code Generator generates “abstract” code.
Synthesis Phase: Equivalent target program is created from the
intermediate representation. It has two parts :
1. Code Optimizer
2. Code Generator
Interpreter
An interpreter is also a software program that translates a source code into a
machine language. However, an interpreter converts high-level programming
language into machine language line-by-line while interpreting and running the
program.
Text Editor
Editors or text editors are software programs that enable the user to create
and edit text files. In the field of programming, the term editor usually refers
to source code editors that include many special features for writing and
editing code. Notepad, Wordpad are some of the common editors used on
Windows OS and vi, emacs, Jed, pico are the editors on UNIX OS. Features
normally associated with text editors are — moving the cursor, deleting,
replacing, pasting, finding, finding and replacing, saving etc.
Types of Editors
There are generally five types of editors as described below:
1. Line editor: In this, you can only edit one line at a time or an integral
number of lines. You cannot have a free-flowing sequence of characters.
It will take care of only one line.
Ex : Teleprinter, edlin, teco
Compiler Scheme
Compile and go loader:
In this scheme, the architecture of memory is like, an assembler present in memory
and it will always be there when we have a compile-and-go loading scheme. In
another part of memory, there is an assembled machine instruction which means the
assembled source program. Assembled machine instruction is placed directly into
their assigned memory location.
Working:
In this scheme, the source code goes into the translator line by line, and then that
single line of code loads into memory. In another language, chunks of source code go
into execution. Line-by-line code goes to the translator so there is no proper object
code. Because of that, if the user runs the same source program, every line of code
will again be translated by a translator. So here re-translation happens.
Compile and go loader scheme
The source program goes through the translator (compiler/assembler) and it consumes
one part of the memory ad the second part of the memory is consumed by the
assembler. The source program does not need that assembler but it is still there so this
is a waste of memory.
Advantages:
Disadvantages:
There is no use of the assembler but it is still there so a wastage of memory takes
place.
When source code runs multiple times the translation is also done every time. so
re-translation is happening.
Difficult to produce an orderly modular program
Difficult to handle multiple segments like if the source program is in a different
language. eg. one subroutine is assembly language & another subroutine is
FORTRAN.
Advantages:
1. The program need not be retranslated each time while running it. Thus us because
initially when source program gets executed and object program gets generated. If a
program is not modified, then the loader can make use of this object program to
convert it to executable form.
2. There is no wastage of memory because the assembler is not placed in the memory
instead of it, loader occupies some portion of the memory. And the size of the loader
is smaller than assembler so more memory is available to the user.
3. It is possible to write source program with multiple programs and multiple languages
because the source program is first converted to an object program always and
loader accepts these object modules to convert it to an executable format.
3. Presentation Graphics:
For the preparation of reports or summarising the financial, statistical,
mathematical, scientific, economic data for research reports,
managerial reports, moreover creation of bar graphs, pie charts, time
chart, can be done using the tools present in computer graphics.
4. Entertainment:
Computer graphics finds a major part of its utility in the movie industry
and game industry. Used for creating motion pictures , music video,
television shows, cartoon animation films. In the game industry where
focus and interactivity are the key players, computer graphics helps in
providing such features in the efficient way.
Advantages:
1. A CRT has the electron beam directed only to the parts of the screen where an image
is to be drawn.
2. Produce smooth line drawings.
3. High Resolution
Disadvantages:
1. Random-Scan monitors cannot display realistic shades scenes.
Raster Scan Display:
A Raster Scan Display is based on intensity control of pixels in the form of a
rectangular box called Raster on the screen. Information of on and off pixels is stored
in refresh buffer or Frame buffer. Televisions in our house are based on Raster Scan
Method. The raster scan system can store information of each pixel position, so it is
suitable for realistic display of objects. Raster Scan provides a refresh rate of 60 to 80
frames per second.
Advantages:
1. Realistic image
2. Million Different colors to be generated
3. Shadow Scenes are possible.
Disadvantages:
1. Low Resolution
2. Expensive
5. Refresh rate depends or resolution 5. Refresh rate does not depend on the picture.
7. Beam Penetration technology come under it. 7. Shadow mark technology came under this.
Input Devices
The Input Devices are the hardware that is used to transfer transfers input to the
computer. The data can be in the form of text, graphics, sound, and text. Output
device display data from the memory of the computer. Output can be text, numeric
data, line, polygon, and other objects.
Keyboard:
The most commonly used input device is a keyboard. The data is entered by pressing
the set of keys. All keys are labeled. A keyboard with 101 keys is called a QWERTY
keyboard.
Mouse:
A Mouse is a pointing device and used to position the pointer on the screen. It is a
small palm size box. There are two or three depression switches on the top. The
movement of the mouse along the x-axis helps in the horizontal movement of the
cursor and the movement along the y-axis helps in the vertical movement of the
cursor on the screen. The mouse cannot be used to enter text. Therefore, they are
used conjunction with a keyboard.
It is most widely used with those computer machines that can interact with the
user.
For Example: Smartphones, Tablet, Etc.
Light Pen: It is a tool that is light sensitive. It is used to draw pictures and
graphics on the computer screen. It is also used to select the objects.
The pictures made by the light pen can be stored in the computer and can be
improved as needed.
Advantages:
Disadvantages:
1. Cathode-Ray Tube(CRT)
2. Color CRT Monitor
3. Liquid crystal display(LCD)
4. Light Emitting Diode(LED)
5. Direct View Storage Tubes(DVST)
6. Plasma Display
7. 3D Display
1. Cathode-ray Tube (CRT): Here, CRT stands for Cathode ray tube. It is
a technology which is used in traditional computer monitor and
television.
Cathode ray tube is a particular type of vacuum tube that displays images
when an electron beam collides on the radiant surface.
Component of CRT
A powerful electron beam penetrates the CRT, it passes through the red layer
and excites the green layer within.
A beam with slow electrons excites only the red layer.
A beam with the medium speed of electrons, a mixture of red and green light
is emitted to display two more colors- orange and yellow.
Advantages:
1. Better Resolution
2. Half cost
3. Inexpensive
Disadvantages:
3. Liquid crystal display (LCD): The LCD depends upon the light modulating
properties of liquid crystals.
LCD is used in watches and portable computers. LCD requires an AC power
supply instead of DC, so it is difficult to use it in circuits.
It generally works on flat panel display technology. LCD consumes less power
than LED. The LCD screen uses the liquid crystal to turn pixels on or off.
Liquid Crystals are a mixture of solid and liquid. When the current flows inside
it, its position changes into the desired color.
For Example: TFT (Thin Film Transistor)
Advantages:
Disadvantages:
4. Light Emitting Diode (LED): LED is a device which emits when current
passes through it. It is a semiconductor device.
The size of the LED is small, so we can easily make any display unit by
arranging a large number of LEDs.
LED consumes more power compared to LCD. LED is used on TV,
smartphones, motor vehicles, traffic light, etc.
LEDs are powerful in structure, so they are capable of withstanding
mechanical pressure. LED also works at high temperatures.
Advantages:
Disadvantages:
Disadvantages:
6. Plasma Display: It is a type of flat panel display which uses tiny plasma
cells. It is also known as the Gas-Discharge display.
Components of Plasma display
1. Anode: It is used to deliver a positive voltage. It also has the line wires.
2. Cathode: It is used to provide negative voltage to gas cells. It also has
fine wires.
3. Gas Plates: These plates work as capacitors. When we pass the
voltage, the cell lights regularly.
4. Fluorescent cells: It contains small pockets of gas liquids when the
voltage is passed to this neon gas. It emits light.