TextGraph


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: TextGraph

Threaded View

  1. #1
    Join Date
    May 2005
    Posts
    115

    TextGraph

    I had my Java term paper yesterday, it was terrible. After the exam, i've attempted the question again but i still can't get the correct output. So i would appreciate if you guys can help me out here...

    The question:

    Create a class, TextGraph, which will draw a diagonal graph using text graphics. Here is an example of what the output of this program would look like if we told it the size was 80x24:
    Code:
                                                                                                       000
                                                                                                0000
                                                                                           000
                                                                                     000
                                                                              0000
                                                                         000
                                                                  0000
                                                              000
                                                       0000
                                                 0000
                                           0000
                                     0000
    The formula of the graph is, x(height/width)=y

    Since the origin starts at the bottom left hand corner, at that position, the first four "0"s are calculated by subsituting x=0,1,2 or 3 into the equation which will give the value y=0 when you convert it to integer

    The last three "0"s (upper right hand) is computed based on the value of x=77,78 or 79. All produce y=23

    This is the tester class
    Code:
    class TextGraphTester{
    	public static void main(String[] args){
    		int width=80, height=24;
    
    		if(args.length>=1)width=Integer.parseInt(args[0]);
    		if(args.length>=2)height=Integer.parseInt(args[1]);
    
    		TextGraph g=new TextGraph(width, height);
    		System.out.print(g);
    
    		TextGraph h=new TextGraph(width, height);
    		System.out.print("Equals or not? " +h.equals(g));
    		h=new TextGraph(width, height*2);
    		System.out.println("Again? " +g.equals(h));
    	}
    }
    End of question...

    The following is my version of the code. The methods i used in my code are compulsary with the question.

    Code:
    public class TextGraph{
    	private char[][] screen;
    	private char zero;	//Automatically initialized as zero
    	private int scrW;
    	private int scrH;
    	private int x;		//for width
    	private int y;		//for height
    
    	public TextGraph(int width, int height){
    		scrW=width;
    		scrH=height;
    		screen= new char[scrW][scrH];	//Array is an object. Has to create with 'new'
    		setPixel(x,y,zero);
    	}
    
    	public TextGraph(){
    		scrW=80;
    		scrH=40;
    		screen= new char[scrW][scrH];	//Empty constructor initializing to default values
    	}
    	//-------------------------------------------------------------------------------------------
    	public int getWidth(){
    		return scrW;
    	}
    
    	public int getHeight(){
    		return scrH;
    	}
    
    	public boolean setPixel(int x,int y, char c){
    		if((x>scrW)&&(y>scrH)&&(c!='0'))
    			return false;
    		else
    			screen[x][y]=c;
    
    		return false;
    	}
    
    	public char getPixel(int x,int y){
    		return screen[x][y];
    	}
    
    	public void calculate(){
    		for(int i=0;i<scrW;i++){
    			this.y=i*(scrH/scrW);	//i=x;
    			setPixel(i,y,zero);
    		}
    	}
    
    	StringBuffer sb=new StringBuffer();      //Provided by sjalle, many thx!!
    	public String toString(){
    		sb.setLength(0);
    		for (int i=0; i<screen.length; i++) {
    			for (int j=0; j<screen[i].length; j++) {
    				sb.append(screen[i][j]);
    			}
    		}
    		return sb.toString();
    	}
    
    	public boolean equals(Object o){
    		if(o==null)
    			return false;
    		else if(o.getClass()!=this.getClass())
    			return false;
    		else{
    			TextGraph other=(TextGraph) o;
    			if((other.getWidth()==this.getWidth())&&(other.getHeight()==this.getHeight()))
    				return true;
    		}
    		return false;
    
    
    	}
    }
    I know this is quite a lengthy thread. For guys out there willing to help, many thanks in advance!!
    Last edited by Ant_Magma; 07-21-2005 at 09:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center