SOURCE CODE #6
//A SIMPLE JAVA PROGRAM THAT EXPLAINS HIERARCHIAL INHERITANCE
//A SIMPLE JAVA PROGRAM THAT EXPLAINS HIERARCHIAL INHERITANCE
/* HERE InheritanceOne is the base calss, Truck, Airplane &
Boat are all derived classes.
* constructor is used in
all the classes.
* The keyword
"extends" is used to show that Inheritance concept is used here.
* Every method prints a
simple line on the screen.
* The output shows the
flow of control throughtout the source code.
* I hope this explains
the concept of hierarchial inheritance very well.
* Author - Hemapriya
Gopal
*/
public class
InheritanceOne
{
float position;
float speed;
float fuellevel;
public
InheritanceOne()
{
System.out.println("Inside the constructor of
the base class");
}
public void move()
{
System.out.println("Inside the InheritanceOne
class move() method ");
}
public void
addPassenger()
{
System.out.println("Inside the InheritanceOne
class addPassenger() method ");
}
public void
displayPosition()
{
System.out.println("The position of the vehicle
is 45' NE ");
}
public void
displaySpeed()
{
System.out.println("The vehicle is moving at the
speed of 60km/hr ");
}
public void
displayFuellevel()
{
System.out.println("The fuel tank in the vehicle
is half-full");
}
public static void
main(String args[])
{
InheritanceOne io = new InheritanceOne();
io.move();
io.addPassenger();
Airplane a = new Airplane();
a.operateUnderCarriage();
a.displayFuellevel();
Truck t = new Truck();
t.getTyrePressure();
t.displaySpeed();
Boat b = new Boat();
b.displayPosition();
b.dock();
}
/* InheritanceOne()
{
System.out.println("Inside
the destructor of the base class");
}
*/
}
class
Airplane extends InheritanceOne
{
float heightfromGround;
public
Airplane()
{
//super();
System.out.println("Inside the constructor of the derived class Airplane");
}
public void
operateUnderCarriage()
{
System.out.println("Inside the derived class Airplane in the method
operateUndercarriage");
}
}
class Truck extends
InheritanceOne
{
public void
getTyrePressure()
{
System.out.println("Inside the method getTyrePressure() of the class
Truck");
}
public Truck()
{
System.out.println("Inside the constructor of the derived class Truck");
}
}
class Boat extends
InheritanceOne
{
public void dock()
{
System.out.println("Inside the method dock() of the derived class Boat");
}
public Boat()
{
System.out.println("Inside the constructor of the derived class Boat");
}
}
HIERARCHIAL
INHERITANCE
OUTPUT:
Inside the constructor of the base class
Inside the InheritanceOne class move() method
Inside the InheritanceOne class addPassenger() method
Inside the constructor of the base class
Inside the constructor of the derived class Airplane
Inside the derived class Airplane in the method
operateUndercarriage
The fuel tank in the vehicle is half-full
Inside the constructor of the base class
Inside the constructor of the derived class Truck
Inside the method getTyrePressure() of the class Truck
The vehicle is moving at the speed of 60km/hr
Inside the constructor of the base class
Inside the constructor of the derived class Boat
The position of the vehicle is 45' NE
Inside the method dock() of the derived class Boat
SOURCE CODE #7
/*A very
simple program implementing all the basic concepts of javax.swing
* Reference - 1.The
Complete Refernce 5th Edition --Herbert Schildt
* 2.NUS
- ISS S.E Notes for OOP
* 3.http://www.wattpad.com/6660884-java-swing-gui-development-chapter-two-your-first
* Author - Hemapriya Gopal
*/
import
java.awt.*;
import
javax.swing.*;
public
class SwingHelloWorldForPractice
{
public void FrameOne()
{
JFrame frame = new JFrame
("Hello World Application");
frame.setSize(270,70);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel
("Hello world");
JButton button = new JButton
("OK");
frame.add(label);
frame.add(button);
frame.setVisible(true);
}
public void OddOrEvenUsingGUI()
{
String num =
JOptionPane.showInputDialog (null,"Enter a number");
int num1=Integer.parseInt(num);
if(num1==0)
{
JOptionPane.showMessageDialog
(null, "The number entered is zero");
}
else if(num1 %2 ==0)
{
JOptionPane.showMessageDialog(null,
"The number entered is an even number");
}
else
{
JOptionPane.showMessageDialog(null,
"The number entered is a odd number");
}
}
public void HelloWorldGUI()
{
JOptionPane.showMessageDialog(null,"Hello
World!", "Hello",JOptionPane.PLAIN_MESSAGE);
}
public
static void main(String args[])
{
SwingHelloWorldForPractice shwfp = new
SwingHelloWorldForPractice();
shwfp.HelloWorldGUI();
shwfp.OddOrEvenUsingGUI();
shwfp.FrameOne();
}}
OUTPUT:
Really nice site, the contents are very much in details, which is very much helpful. Thanks for sharing .. If anyone wants to know a lot more about java than visit: More details
ReplyDeleteThank you Brave Little Coder
Delete