Showing posts with label JAVA LAB. Show all posts

Wednesday, 4 January 2012

Vehicle Class Hierarchy in Java


Vehicle Class Hierarchy in Java

Question

Design a Vehicle class hierarchy in Java. Write a test program to demonstrate
polymorphism.

(Program written by Santhosh Kumar S)


Program

import java.util.*;
abstract class Vehicle
{
boolean engstatus=false;
abstract void start();
abstract void moveForward();
abstract void moveBackward();
abstract void applyBrake();
abstract void turnLeft();
abstract void turnRight();
abstract void stop();
}
class Porsche extends Vehicle
{
void truckManufacture()
{
System.out.println("Porsche World Wide Car manufactures ");
}

void gearSystem()
{
System.out.println("\n automatic gearing system.. So don't worry !!");
}
void start()
{
engstatus=true;
System.out.println("\n engine is ON ");
}
void moveForward()
{

if(engstatus==true)
System.out.println("\nCar is moving in forward direction .. Slowly gathering speed ");
else
System.out.println("\n Car is off...Start engine to move");
}
void moveBackward()
{ if(engstatus=true)
System.out.println("\n Car is moving in reverse direction ...");
else
System.out.println("\n Car is off...Start engine to move");
}
void applyBrake()
{
System.out.println("\n Speed is decreasing gradually");
}
void turnLeft()
{
System.out.println("\n Taking a left turn.. Look in the mirror and turn ");
}
void turnRight()
{
System.out.println("\n Taking a right turn.. Look in the mirror and turn ");
}
void stop()
{
engstatus=false;
System.out.println("\n Car is off ");
}
}
class Volvo extends Vehicle
{
void truckManufacture()
{
System.out.println("Volvo Truck Manufactures");
}
void gearSystem()
{
System.out.println("\nManual Gear system...ooops....take care while driving ") ;
}
void start()
{
engstatus=true;
System.out.println("\n engine is ON ");
}
void moveForward()
{

if(engstatus==true)
System.out.println("\n truck is moving in forward direction .. Slowly gathering speed ");
else
System.out.println("\n truck is off...Start engine to move");
}
void moveBackward()
{ if(engstatus=true)
System.out.println("\n truck is moving in reverse direction ...");
else
System.out.println("\n truck is off...Start engine to move");
}
void applyBrake()
{
System.out.println("\n Speed is decreasing gradually");
}
void turnLeft()
{
System.out.println("\n Taking a left turn.. Look in the mirror and turn ");
}
void turnRight()
{
System.out.println("\n Taking a right turn.. Look in the mirror and turn ");
}
void stop()
{
engstatus=false;
System.out.println("\n Truck is off ");
}
}

class Vehicle1
{
public static void main(String[] args)
{
Porsche v1=new Porsche();
v1.truckManufacture();
v1.start();
v1.gearSystem();
v1.moveForward();
v1.applyBrake();
v1.turnLeft();
v1.moveForward();
v1.applyBrake();
v1.turnRight();
v1.moveBackward();
v1.stop();
v1.moveForward();

Volvo v2= new Volvo();
v2.truckManufacture();
v2.start();
v2.gearSystem();
v2.moveForward();
v2.applyBrake();
v2.turnLeft();
v2.moveForward();
v2.applyBrake();
v2.turnRight();
v2.moveBackward();
v2.stop();
v2.moveForward();

}
}

Download the Source Code

Calendar Class in Java


Calendar Class in Java

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Used to provide an example that exercises most of the functionality of the
* java.util.Calendar Class
* @author Vivek Venkatesh G
* @version
*/
public class CalendarExample {

/**
* Calendar's getTime() method returns a Date Object
* This can then be passed to println() to print today's date (and time) in the traditional format
* @param No parameter for this function
* @return No return value for this function
*/
public static void doCalendarTimeExample()
{
System.out.println("CURRENT DATE/TIME\n\n");
Date now = Calendar.getInstance().getTime();
System.out.println("Calendar.getInstance().getTime():"+ now);
System.out.println();
}

/**
* Simple Date Format from java.text.package
* @param for this no parameter required
*/

public static void doSimpleDateFormat()
{
System.out.println("Simple Date Format \n\n");
// Get Today's Date
Calendar now = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println(" It is now :"+ formatter.format(now.getTime()));
System.out.println();
}

/**
* Simple Date difference from java.text.Package
*/
public static void doDataDifference()
{
System.out.println("DIFFERENCE BETWEEN TWO DATES\n\n");
Date startDate1 = new GregorianCalendar(1994,02,14,14,00).getTime();
Date endDate1 = new Date();
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println("Difference between " + endDate1);
System.out.println(" and "+ startDate1 + "is" + (diff/(1000L*60L*60L*24L)) + "days");
System.out.println();
}

public static void doGetMethods()
{
System.out.println("CALENDAR GET METHODS");
Calendar c=Calendar.getInstance();

System.out.println("YEAR : " + c.get(Calendar.YEAR));
System.out.println("MONTH : " + c.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH : " + c.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK : " + c.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_YEAR : " + c.get(Calendar.DAY_OF_YEAR));
System.out.println("WEEK_OF_YEAR : " + c.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH : " + c.get(Calendar.WEEK_OF_MONTH));
System.out.println("DAY_OF_WEEK_IN_MONTH : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("HOUR : " + c.get(Calendar.HOUR));
System.out.println("AM_PM : " + c.get(Calendar.AM_PM));
System.out.println("HOUR OF DAY(24-hour) : " + c.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE : " + c.get(Calendar.MINUTE));
System.out.println("SECOND : " + c.get(Calendar.SECOND));
System.out.println();

}

public static void main(String[] args)
{
System.out.println();
doCalendarTimeExample();
doSimpleDateFormat();
doDataDifference();
doGetMethods();
}
}


Download the Source Code

Rational Number Class in Java


Java Lab - Program 1

Difficulty Level - Very Easy

Rational Number Class in Java

Question:

Develop Rational number class in Java. Use JavaDoc comments for documentation.
Your implementation should use efficient representation for a rational number, i.e. (500 / 1000) should be represented as (½).

My Solution

(any further improvements welcomed).

Two classes "rational_main.java" and "rational.java" has been used to implement this.

rational_main.java

import java.util.Scanner;
class rational_main
{
/** Class that contains the main function
* @author Vivek Venkatesh (Your name)
*/

public static void main(String args[])
{
/** Create an instance of "rational" class
* to call the function in it.
*/

rational x = new rational();

//To get input from user

Scanner s = new Scanner(System.in);
int a,b;
System.out.println("\n Enter the Numerator and Denominator:");
a = s.nextInt();
b = s.nextInt();

x.efficient_rep(a,b);
}
}

rational.java

public class rational
{

/** Rational number class
* To give an efficient representation of user's input rational number
* For ex: 500/1000 to be displayed as 1/2
*/

public void efficient_rep(int a, int b)
{

int x=b;
if(a=2;i--)
{
if(a%i==0 && b%i==0)
{
a=a/i;
b=b/i;
}
}
System.out.println(a + "/" + b);
}
}

(This is only one of the solution, and there is a gr8 possibility of even more efficient solution. If u know pls post it.)


Download Source Code


Click the following links,

rational_main.java

rational.java