Results 1 to 6 of 6
  1. #1
    i8java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default How to create an array that can be modified

    hello there all,

    I have being asked to create a simple array which contains two different variables that are able to be modified, but i do not have a clue how to modify the array.

    If anybody there has some sample projects with this info i will greatfully accept it and will be a real help. a

    any information you can also give me will be great, thanks again

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by i8java View Post
    hello there all,

    I have being asked to create a simple array which contains two different variables that are able to be modified, but i do not have a clue how to modify the array.

    If anybody there has some sample projects with this info i will greatfully accept it and will be a real help. a

    any information you can also give me will be great, thanks again
    Arrays don't contain variables, they contain values and every value in an array can be modified. Please explain what you really want. An example would be fine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by i8java View Post
    I have being asked to create a simple array which contains two different variables that are able to be modified,
    Maybe it's me, but this statement doesn't make much sense. Care to clarify this?

  4. #4
    i8java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default reply

    if for example a person is asked to enter their name and town, after this information has being entered i have to be able to modify either one of these. If for example they move town.

    Sorry about the clarity but i am lost and have being hitting walls constantly he last few days.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So you will likely need a class to hold your name and town String, and then have your array be an array of this type of class, correct?

    I'm still not sure of what you're needing though. Do you need to post the assignment itself?

  6. #6
    i8java is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default This is what i have done so far

    Java Code:
    package ie.ds.abs;
    
    import javax.swing.JOptionPane;
    
    public abstract class Player
    {
      
      protected String name;
      protected String town;
      protected String country;
      protected String club;
      protected String position;
      protected int caps;
     
    
    
      public abstract String getPackage();
    
      
      
      public String getName() 
      { return this.name; } 
      
      public void setName(String name) { this.name = name; }
    
      public String getPosition() {
    	  return this.position; } 
      
      public void setPosition(String position) 
      { this.position = position;
      }
      
      public String getTown() {
    	  return this.town; } 
      
      public void setTown(String town) { this.town = town; }
      
      public String getCountry() {
    	  return this.country; } 
      
      public void setCountry(String country) { this.country = country; }
      
      
      public String getClub() {
    	  return this.club; } 
      
      public void setClub(String club) { this.club = club; }
      
      public int getCaps() {
    	  return this.caps; } 
      
      public void setCaps(int caps) { this.caps = caps; }
      
    
      
      
      
    
      public void setPlayerInfo() {
      
    	  this.name = JOptionPane.showInputDialog("Enter the Players Name :"); 
          this.country = JOptionPane.showInputDialog("Enter the Players Country :");
    	  this.club = JOptionPane.showInputDialog("Please enter your Players Club :"); 
          this.position = JOptionPane.showInputDialog("Enter the Players Position :");    
          this.caps = Integer.parseInt(JOptionPane.showInputDialog("Enter the Players Value In Millions"));  
      }
      public String toString()
      {
    
    
    	   String g = "\n";
    	    g = g + "Players League : " + getPackage() + "   ";
    	    g = g + "Players Name : " + getName() + "   ";
    	    g = g + "Players Country  : " + getCountry() + "   ";
    	    g = g + "Position : " + getPosition() + "   ";
    	    g = g + "Players Club : " + getClub() + "   ";
    	    g = g + "Players Value : €" + getCaps() + "Million   ";
    	 
    	    return g;
      }
    }



    Java Code:
    package ie.ds.sportsapp;
    
    import ie.ds.abs.Player;
    import ie.ds.impl.Prem;
    import ie.ds.impl.Scot;
    
    
    import ie.ds.utils.Utilities;
    
    import javax.swing.JOptionPane;
    
    public class PlayerApp 
    {
    	static int numPlayers=0,maxPlayers=3;   
    ////////////////////////////////////////////////////////////////////////////////////////
    	public boolean isFull(Player[] client)
    	{
    		return (numPlayers == maxPlayers);
    	}
    ////////////////////////////////////////////////////////////////////////////////////////
    	public boolean isEmpty(Player[] client)
    	{
    		return (numPlayers == 0);
    	}
    ////////////////////////////////////////////////////////////////////////////////////////
    
    public String registerPlayer(Player[] client)
    
    {
    
    
    String num = "";
    String name = "";
    int type = 0;
    
    do{
    	String strType = JOptionPane.showInputDialog("Please enter 1 for Premiership or 2 for a Scottish League"); 
    	type= Integer.parseInt(strType);
    
    	if (type == 1)
    		{
    Prem bPackage = new Prem();
    bPackage.setPlayerInfo();
    
    
    		
            client[numPlayers] = bPackage;
            numPlayers += 1;
    		}
    	
    	
    	else
    		if (type == 2)
    		{
    			Scot dPackage = new Scot();
    			dPackage.setPlayerInfo();
    			 
    					
    			        client[numPlayers] = dPackage;
    			        numPlayers += 1;
    					}
    	
    }
    while(num != name);
    return name;	
    }
    	//////////////
    public void listPlayers(Player[] client)
    {
        String G = "";
        for (int i = 0; i < numPlayers; ++i)
        {
          G = G + client[i];
        }
        JOptionPane.showMessageDialog(null, G, "Player Info", 1);
      }
    ///////////////////////////////////////////////////////////////////////////////////////
    
    /[COLOR="red"]//////////////////////////////////////////////////////////////////////////////////
    
    I HAVE TO PUT THE MODIFY FUNCTION HERE
    
    
    ////////////////////////////////////////////////////////////////[/COLOR]////////////////////////
    
    public void findPlayers(Player[] client)
    {
    	Player a = client[0];
      for (int i = 0; i < numPlayers; ++i)
      {
        if (a.getCaps() < client[i].getCaps())
          a = client[i];
      }
      JOptionPane.showMessageDialog(null, a, "Most Expensive Player In Database.", 1);
    }
    ////////////////////////////////////////////////////////////////////////////////////////
    
    
    public static void main(String[] args) 
    	{
    	int option;   
    	PlayerApp myApp = new PlayerApp();
    	Player[] team = new Player[maxPlayers];	
    		   
    		   do 
    		   {
    		   option = new Utilities().showMenu();
    		   
    		   	switch(option) 
    		   		{
    		   		case 1:
    		   			
    					if(!myApp.isFull(team))
    		   					myApp.registerPlayer(team);
    		   			else
    		   				JOptionPane.showMessageDialog(null, "No More Players Can be Registered","Player App Data Error",JOptionPane.WARNING_MESSAGE);
    
    		   			break;
    		   		case 2:
    		   			if(!myApp.isEmpty(team))	
    		   					myApp.listPlayers(team);
    		   			break;
    		   		case 3:
    		   			if(!myApp.isEmpty(team))	
    		   				myApp.findPlayers(team);
    			   			break;
    		   			
    		   		case 4:
    			   			if(!myApp.isEmpty(team))	
    			   				myApp.removePlayers(team);
    			   			break;
    		   		case 5:
    		   			// Exiting System
    			   			break;
    		   		default: break;
    		   		}
    		   }
    		   while(option != 5);
    		} 					
    	}
    moderator edit: code tags added
    Last edited by Fubarable; 12-19-2010 at 07:34 PM. Reason: moderator edit: code tags added

Similar Threads

  1. Create a Get/set for an array
    By mechdesignron in forum New To Java
    Replies: 4
    Last Post: 05-04-2010, 09:39 PM
  2. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  3. Modified Pixels
    By monkey04 in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:15 AM
  4. JSP Last Modified Date
    By thagentleman in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-18-2008, 04:52 AM
  5. Checking of file was modified on the server
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:18 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
  •