Results 1 to 1 of 1
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Data Structure (Name Surfer Assignment)

    Hi Folks,
    I am doing this NameSurfer assignment of Stanford CS106A.I think I am almost there but stuck in one part.

    How can I pass data from main program to another class?In this project main program gets user-input and it should be passed another class called myCanvas.java.I think i should create another class which get user-input from the main program and passes the user input to the myCanvas class, is that right?

    By the way this program takes names of people and shows their ranks in the particular year in a linear graph.

    If you look around the middle of the code of myCanvas.java and read my message in CAPITAL Letters(// THE MAIN QUESTION I AM STUCK AT ?
    ).You will understand my question.

    Thank you in advance.

    Here is the code of the main program called resized.java
    Java Code:
    //
    
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    import acm.program.*;
    import acm.util.ErrorException;
    import acm.graphics.*;
    
    
     public class resized extends Program{
    	 
    	 
    	 public void run(){
    		 
    		
    /*These are Jbuttons and nameFiled where user can enter names*/
    
    			nameField= new JTextField(10);
    			
    			
    			add(new JLabel(" Name "),SOUTH);
    			add(nameField,SOUTH);
    			nameField.setActionCommand("name");
    			nameField.addActionListener(this);
    			
    			Graph= new JButton("Graph");
    			add(Graph,SOUTH);		
    			Clear=new JButton("Clear");
    			add(Clear,SOUTH);
    			 canvas = new myCanvas();
    			 
    			 add(canvas);
    			
    			}
    			
    	 
    		 
    /*This incomplete at the moment */
    			public void actionPerformed(ActionEvent e){
    				
    				
    				if(e.getActionCommand().equals("name")){
    					
    				
    					
    					
    				
    					
    			  
    				}
    	
    		 
    			}
    	
    			
    			
    			private JTextField nameField;
    			private JButton Graph;
    			private JButton Clear;
    			private GLabel name;
    			private GLine line; 
    			private GOval dotted;
    			private myCanvas canvas;
    		
     }

    And here is the code of myCanvas.java which displays the name surfer graph:
    Java Code:
    import acm.program.*;
    import acm.graphics.*;
    
    import java.awt.event.*;
    
    import acm.program.*;
    import acm.util.*;
    
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    import acm.graphics.*;
    import java.awt.color.*;
    import java.awt.*;
    
    /*This class uses ComponentListener for resizing.Aim of the project is to 
    create a name surfer program which resizes itself when the applet size is reduced or enlarged*/
    
    public class myCanvas extends GCanvas implements ComponentListener{
    	
    	
    	
    	public myCanvas(){
    		
    	
    		
    		addComponentListener(this);
    	
    		displayGraph();
    		
    		
          
     		
     		}
    
    /*This medthod diplays the name surfer linear graph*/
    
    		public void displayGraph(){
    			
    			
    			
    			for(int i=1;i<11;i++){
    
    		         line1=new GLine(100,0,100,1200);
    				 m=i*10;
    				 
    							
    				
    			add(line1,(getWidth()-((getWidth()*m)/100)),(getHeight()-line1.getHeight())/2);
    			
    			}
    			
    //array containing y axis of the lines
    
    			int y[]=new int[21];
    	         
    	         for(int i=0;i<20;i++){
    	        	 
    	        	
    	        	 if(parseRank(i+1)==0){
    	        		 
    	        		 y[i]=parseRank(i+1)+1000;
    	        		 
    	        	 }else
    	        	 y[i]=parseRank(i+1);
    	        	 
    	        
    	        	 
    	         }
    		
    //array containing x axis of the lines
    
    			int x[]={1000,900,900,800,800,700,700,600,600,500,500,400,400,300,300,200,200,100,100,0};
    			
    			int counter=m;
    			
    			
    			
    			 for(int row=0;row<x.length-1;row++){
    				 for(int column=0;column<x.length-1;column++){
    					
    					 rank="  ";
    					 
    					 rank+=y[column];
    			 
    					 
    		
    		  //  GLabel na = new GLabel(name);
    		    GLabel ra = new GLabel(rank);
    		    
    		    
    		    
    		    add(ra,getWidth()-getWidth()*x[column]/1000+25,y[column]);
    		  //  add(na,getWidth()-getWidth()*x[column]/1000,y[column]);
    					 
    					 
    			 line = new GLine(getWidth()-(getWidth()*x[column])/1000,y[column],getWidth()-getWidth()*x[column+1]/1000,y[column+1]);
    			 
    			 if(counter==0){
    			line.setColor(Color.black);
    			 }else if(counter==1){
    				 
    				 line.setColor(Color.red);
    				 
    			 }else if(counter==2){
    				 
    				 line.setColor(Color.blue);
    			 }else if(counter==3){
    				 line.setColor(Color.MAGENTA);
    				 
    			 }
    			
    			 
    		
    			add(line,getWidth()-getWidth()*x[column]/1000,y[column]);
    			 
    				 }
    			 }
    			 
    			 
    			
    			 
    			}
    		
    					
    	
    	public void update(){
    		
    		removeAll();
    		displayGraph();
    		
    		
    		
    		}
    		
    		 
    		
    	
    	
    	public void componentResized(ComponentEvent e){update();}
    	public void componentHidden(ComponentEvent e){}
    	public void componentMoved(ComponentEvent e){}
    	public void componentShown(ComponentEvent e){}
    	
    	
    	public int parseRank(int rank){
    		
    		
    			
    		int result=0;
    		String data=loadData();
    		
    		int number1=0;
    		//int number2=0;
    				
    		int  endNum1=data.indexOf(" ");
    		
    		//int endFirstNum=data.indexOf(endNum,"");
    		
    		//println(endNum1);
    		
    		String first=data.substring(endNum1+1);
    	
    		
    		
    		int endNum2=first.indexOf(" ");
    		
    		//println(endNum2);
    		
    		
    	   String second=first.substring(endNum2+1);
    		
    	  // println(second);
    		
    	   int endNum3=second.indexOf(" ");
    		
    
    	   String third=second.substring(endNum3+1);
    	   
    	   int endNum4=third.indexOf(" ");
    
         String fourth=third.substring(endNum4+1);
    	   
    	   int endNum5=fourth.indexOf(" ");
    	   
         String fifth=fourth.substring(endNum5+1);
    	   
    	   int endNum6=fifth.indexOf(" ");
    	   
           String sixth=fifth.substring(endNum6+1);
    	   
    	   int endNum7=sixth.indexOf(" ");
    	   
     String seventh=sixth.substring(endNum7+1);
    	   
    	   int endNum8=seventh.indexOf(" ");
    	   
     String eight=seventh.substring(endNum8+1);
    	   
    	   int endNum9=eight.indexOf(" ");
    	   
     String ninth=eight.substring(endNum9+1);
    	   
    	   int endNum10=ninth.indexOf(" ");
    	   
     String tenth=ninth.substring(endNum10+1);
    	   
    	   int endNum11=tenth.indexOf(" ");
    	   
     String eleventh=tenth.substring(endNum11+1);
    	   
    	   int endNum12=eleventh.indexOf(" ");
    	   
    		
    	
    		if(rank==1){
    			
    			 number1=Integer.parseInt(first.substring(0,endNum2));
    			 
    			 result=number1;
    		}
    		
    		
    		
    		else if(rank==2){
    		
    		
    		
    			 number2=Integer.parseInt(second.substring(0,endNum3));
    			 
    		  result=number2;
    		  
    		  
    			
    		}else if (rank==3){
    			
    			
    			number3=Integer.parseInt(third.substring(0,endNum4));
    			  result=number2;
    			
    		}else if(rank==4){
    			
    			number4=Integer.parseInt(fourth.substring(0,endNum5));
    			  result=number3;
    			
    		}else if(rank==5){
    			
    			number5=Integer.parseInt(fifth.substring(0,endNum6));
    			  result=number3;
    			
    		}else if(rank==6){
    			
    			number6=Integer.parseInt(sixth.substring(0,endNum7));
    			  result=number4;
    			
    		}else if(rank==7){
    			
    			number7=Integer.parseInt(seventh.substring(0,endNum8));
    			  result=number4;
    			
    		}else if(rank==8){
    			
    			number8=Integer.parseInt(eight.substring(0,endNum9));
    			  result=number5;
    			
    		}else if(rank==9){
    			
    			number9=Integer.parseInt(ninth.substring(0,endNum10));
    			  result=number5;
    			
    		}else if(rank==10){
    			
    			number10=Integer.parseInt(tenth.substring(0,endNum11));
    			  result=number6;
    			
    		}else if(rank==11){
    			
    			number11=Integer.parseInt(eleventh.substring(0));
    			  result=number6;
    			
    		}else if(rank==12){
    			
    			number12=Integer.parseInt(eleventh.substring(0));
    			  result=number7;
    	
    		}else if(rank==13){
    			
    			number13=Integer.parseInt(eleventh.substring(0));
    			  result=number7;
    	
    		}else if(rank==14){
    			
    			number14=Integer.parseInt(eleventh.substring(0));
    			  result=number8;
    	
    		}else if(rank==15){
    			
    			number15=Integer.parseInt(eleventh.substring(0));
    			  result=number8;
    	
    		}else if(rank==16){
    			
    			number16=Integer.parseInt(eleventh.substring(0));
    			  result=number9;
    	
    		}else if(rank==17){
    			
    			number17=Integer.parseInt(eleventh.substring(0));
    			  result=number9;
    	
    		}else if(rank==18){
    			
    			number18=Integer.parseInt(eleventh.substring(0));
    			  result=number10;
    	
    		}else if(rank==19){
    			
    			number19=Integer.parseInt(eleventh.substring(0));
    			  result=number10;
    	
    		}else if(rank==20){
    			
    			number20=Integer.parseInt(eleventh.substring(0));
    			  result=number11;
    	
    		}else if(rank==21){
    			
    			number21=Integer.parseInt(eleventh.substring(0));
    			  result=number11;
    	
    		}
    		
    		
    		
    		
    	return result;
    		
    	}
    
    // THE MAIN QUESTION I AM STUCK AT ?
    //HOW CAN WE PASS THE NAME THE USER PUT IN THE MAIN PROGRAM TO //HERE AND PUT IT IN THE PLACE WHERE IT SAYS "Ethan".name is constant //just now with the value "Ethan" but IT SHOULD VARIABLE HENCE IT //CHANGES.
    //PLZ HELP ME WITH THIS.
    //
    
    	public String loadData(){
    		
    		  String data=" ";
    		
    		 name="Ethan";
    		 
    		 
    		
    			try {
    			BufferedReader rd = new BufferedReader(
    			new FileReader("names-data.txt"));
    			while (true) {
    			String line = rd.readLine();
    			if (line == null) break;
    			
    			int endName=line.indexOf(" ");
    			
    			if(line.substring(0,endName).equalsIgnoreCase(name)){
    				
    				 data=line.substring(0);
    				
    				
    				
    			}
    			
    			
    			}
    			rd.close();
    			} catch(IOException ex) {
    			throw new ErrorException(ex);
    			}
    			return data;
    			}
    	
    	
    	private int number2,number3,number4,number5,number6,number7,number8,number9,number10,number11,number12,
    	number13,number14,number15,number16,number17,number18,number19,number20,number21;
    	
    	private GRect rect;
    	
     
    	
         private GLine line,line1;
         private int[] y,x;
    	private int m;
    	
    	private String rank;
    	private String name;
    
    }
    Last edited by ccie007; 03-30-2011 at 05:39 PM.

Similar Threads

  1. Which data structure to choose ?
    By lumpy in forum New To Java
    Replies: 2
    Last Post: 02-20-2010, 05:43 AM
  2. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 06:48 AM
  3. data file structure
    By Nicholas Jordan in forum Advanced Java
    Replies: 2
    Last Post: 01-07-2009, 05:16 AM
  4. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:35 PM
  5. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •