Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic polymorphism

create shapes in applet,applet program to create shapes.

Program:
package date;
public class day
{
int day,month,year,y,m,tot_day=0;
int day_mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
String week[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
public void days(int a,int b,int c)
{
valid(a,b,c);
day=a;
month=b;year=c;
for(y=0;y
if(y%4==0)
tot_day+=366;
else
tot_day+=365;
if(month>2 && year%4==0)
day_mon[1]=29;
for(m=0;m<(month-1);m++)
tot_day+=day_mon[m];
tot_day+=day;
System.out.println(day+"-"+month+"-"+year+"  this is :"+"="+week[(tot_day+4)%7]);
}
public void valid(int dd,int mm,int yy)
{
if(yy<1)
{
System.out.println("invalid....");
System.exit(0);
}
switch(mm)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(dd>31 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
break;
case 4:
case 6:
case 9:
case 11:
if(dd>30 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
break;
case 2:
if(yy%4!=0 && dd>=29 && dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
else if(yy%4==0 || dd>28 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}break;
default:
System.out.println("invalid......");
System.exit(0);
}
}
}

Main program:

import date.*;
import java.io.*;
class chkd
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
day d=new day();
System.out.println("enter the date:(dd mm yyyy):");
int dd=Integer.parseInt(in.readLine());
int mm=Integer.parseInt(in.readLine());
int yy=Integer.parseInt(in.readLine());
d.days(dd,mm,yy);
}
}

Output:


C:\j2sdk1.4.0\bin>javac chkd.java
Note: chkd.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

C:\j2sdk1.4.0\bin>java chkd
enter the date:(dd mm yyyy):
30
04
1989
30-4-1989  this is :=sunday

C:\j2sdk1.4.0\bin>java chkd
enter the date:(dd mm yyyy):
30
02
2000
invalid......

3 comments:

Manikandan M said...

The coding does not match with the title....

Manikandan M said...

abstract class Shape
{
double dim1;
double dim2;
double PI=3.14;
Shape(double a, double b)
{
dim1 = a;
dim2 = b;
}
abstract double area();
}
class Rectangle extends Shape
{
Rectangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Shape
{
Triangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class Circle extends Shape
{
Circle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Circle.");
return PI * dim1 * dim1;
}
}
class Ellipse extends Shape
{
Ellipse(double a, double b)
{
super(a, b);
}
double area() {
System.out.println("Inside Area for Ellipse.");
return PI * dim1 * dim2;
}
}
class Square extends Shape
{
Square(double a, double b)
{
super(a, b);
}
double area() {
System.out.println("Inside Area for Square.");
return dim1 * dim1;
}
}
class AbstractAreas {
public static void main(String args[]) {
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Circle c=new Circle(5,5);
Ellipse e=new Ellipse(7,7);
Square s=new Square(6,6);
Shape figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = c;
System.out.println("Area is " + figref.area());
figref = e;
System.out.println("Area is " + figref.area());
figref = s;
System.out.println("Area is " + figref.area());
}
}

Retrospect said...

Thanks to u and mani as well, for all the help. I also found a link with free GATE test online, might be useful: http://thegateacademy.com/gate-free-test/