Saturday, October 13, 2007

Inheritance,Overloading,Overriding

This Arrow Plotting program is created for the request from fizkiz
Find fizkiz's request/specifications at:
https://www.blogger.com/comment.g?blogID=5133484865049964130&postID=7557509313208084077


/*
The size of the arrow is determined by two numbers,
one for the length of the "tail," which is 12 and for the width, which is 7.

The width of the base cannot be an even number, and your constructors
and mutator methods should check to make sure that it is alway odd.

Write a test program for each class that tests all the methods in the class.
You can assume that the base of the arrowhead is 3 or more.
Here is an example of the arrow to be drawn.
*
* *
* *
************ *
* *
* *
*/

/*Class Name 'Arrow' will fit better than this 'Figure' */

class Figure{
protected int ArrowLength=0,ArrowWidth=0;

public Figure(){
this.ArrowLength=12;
this.ArrowWidth=7;
}

public Figure(int ArrowLength,int ArrowWidth){
this.ArrowLength=ArrowLength;
this.ArrowWidth=checkArrowWidth(ArrowLength,ArrowWidth);
}

public void printArrowWidthConstraints(){
System.out.println("Arrow Width should not be an even value..\n It should be an Odd Value...\n Also Arrow Width should be lesser than Arrow Length..\n Minimum value of Arrow Width is 3..");
}

public int getNewArrowWidth(String s){
System.out.println(s);
try{
java.util.Scanner scan=new java.util.Scanner(System.in);
return Integer.parseInt(scan.nextLine());
}catch(Exception e){
System.out.println("InValid Input..Taking Default Value..7");
e.printStackTrace();
}
return 7;
}

public int checkArrowWidth(int ArrowLength,int ArrowWidth){
if(ArrowWidth<3){
printArrowWidthConstraints();
return checkArrowWidth(ArrowLength,getNewArrowWidth("Please Enter an a value Greater than 2...:"));
}
if(ArrowWidth%2==0){
printArrowWidthConstraints();
return checkArrowWidth(ArrowLength,getNewArrowWidth("Please Enter an Odd Value...:"));
}
if(ArrowWidth>=ArrowLength){
printArrowWidthConstraints();
return checkArrowWidth(ArrowLength,getNewArrowWidth("Please Enter a new valid Arrow Width...:"));
}
return ArrowWidth;
}

public void setValues(int ArrowLength,int ArrowWidth){
this.ArrowLength=ArrowLength;
this.ArrowWidth=checkArrowWidth(ArrowLength,ArrowWidth);
}

public int getArrowLength(){
return this.ArrowLength;
}

public int getArrowWidth(){
return this.ArrowWidth;
}

public void drawStars(int space,int noStars){
String s="";
if(space>0){
for(int i=0;i<space;i++)
s+=" ";
}
for(int i=0;i<noStars;i++)
s+="*";
System.out.println(s);
}

public void drawStars(int noStars){
String s="";
for(int i=0;i<noStars;i++)
s+="*";
System.out.println(s);
}
}

//-------------------End of Figure Class--------------------------

class RightArrow extends Figure{
RightArrow(){
super();
}
RightArrow(int ArrowLength,int ArrowWidth){
super(ArrowLength,ArrowWidth);
}
public void draw(int ArrowLength,int ArrowWidth){
setValues(ArrowLength,ArrowWidth);
draw();
}
public void draw(){
int middleLine=(getArrowWidth()/2)+1;
int space=getArrowLength()-(getArrowWidth()/2)-1,noStars=0;
for(int i=1;i<=getArrowWidth();i++){
if(i==middleLine)
drawStars(getArrowLength());
else{
if(i<middleLine)
drawStars(space,++noStars);
if(i>middleLine)
drawStars(space,noStars--);
}
}
}
}

//------------------End of RightArrow Class--------------------


class LeftArrow extends Figure{
LeftArrow(){
super();
}
LeftArrow(int ArrowLength,int ArrowWidth){
super(ArrowLength,ArrowWidth);
}
public void draw(int ArrowLength,int ArrowWidth){
setValues(ArrowLength,ArrowWidth);
draw();
}
public void draw(){
int middleLine=getArrowWidth()/2+1;
int space=getArrowWidth()/2+1,noStars=0;
for(int i=1;i<=getArrowWidth();i++){
if(i==middleLine)
drawStars(getArrowLength());
else{
if(i<middleLine)
drawStars(--space,++noStars);
if(i>middleLine)
drawStars(space++,noStars--);
}
}
}
}

//------------------End of LeftArrow Class--------------------

public class Demo{
public static void main(String args[]){
java.util.Scanner sc;
while(true){
RightArrow ra=null;
LeftArrow la=null;
System.out.println("Enter 1 to draw Right Arrow"); System.out.println("Enter 2 to draw Left Arrow"); System.out.println("Any other entry to Exit");
try{
sc=new java.util.Scanner(System.in);
int ArrowType=Integer.parseInt(sc.nextLine());
if(ArrowType<1 || ArrowType>2){
System.out.println("Terminating..");
System.exit(0);
}
System.out.println("Enter 3 to use Default Arrow Length and Default Arrow Width --default Arrow Length is 12, default Arrow Width is 7"); System.out.println("Enter 4 if you want to enter new Arrow Length and Arrow Width");
sc=new java.util.Scanner(System.in);
int paramType=Integer.parseInt(sc.nextLine());
if(paramType<3 || ArrowType>4){
System.out.println("InValid Input Terminating..");
System.exit(0);
}
if(paramType==3){
if(ArrowType==1){
ra=new RightArrow();
ra.draw();
}else if(ArrowType==2){
la=new LeftArrow();
la.draw();
}
}
if(paramType==4){
System.out.println("Enter Arrow Length..[should be greater than Arrow Width..]..:");
sc=new java.util.Scanner(System.in);
int ArrowLength=Integer.parseInt(sc.nextLine());
System.out.println("Enter Arrow Width...:");
sc=new java.util.Scanner(System.in);
int ArrowWidth=Integer.parseInt(sc.nextLine());
if(ArrowType==1){
ra=new RightArrow(ArrowLength,ArrowWidth);
ra.draw();
}else if(ArrowType==2){
la=new LeftArrow(ArrowLength,ArrowWidth);
la.draw();
}
}
System.out.println("Enter 5 if you want to Change Arrow Length and Arrow Width");
sc=new java.util.Scanner(System.in);
int changeType=Integer.parseInt(sc.nextLine());
if(changeType==5){
System.out.println("Enter new Arrow Length.:");
sc=new java.util.Scanner(System.in);
int ArrowLength=Integer.parseInt(sc.nextLine());
System.out.println("Enter new Arrow Width...:");
sc=new java.util.Scanner(System.in);
int ArrowWidth=Integer.parseInt(sc.nextLine());
if(ArrowType==1){
if(ra!=null)
ra.draw(ArrowLength,ArrowWidth);
}else if(ArrowType==2){
if(la!=null)
la.draw(ArrowLength,ArrowWidth);
}
}

}catch(Exception e){
System.out.println("InValid Entry Terminating..");
System.exit(0);
}
}
}
}

Leave your comments..
FizKiz i dinot check all the test cases [don't have enough time.. busy have many other works to do..] so if you find any error..plz report...

No comments: