Java Object help

Discussion in 'Mac Programming' started by bigMAC28, Apr 19, 2012.

  1. macrumors member

    Joined:
    Apr 16, 2012
    Location:
    Chicago
    #1
    I am creating this program for class and i need to change the values of the 2nd object(c2) and just that individual object. The values i want which field to hold are
    name = mittens
    isDeclawed = true
    species = sabertooth

    I don't know how to specifically change the c2 object. can someone point me in the right direction?

    Code:
     public class Cat
    {
    	private String name;
    	private boolean isDeclawed;
    	private String species;
    	
    	//DEFAULT CONSTRUCTOR
    	//SETS NAME TO ------, isDeclawed TO FALSE, SPECIES TO " ";
    	public Cat()
    	{	
    		name= "------";
    		isDeclawed= false;
    		species = "------";
    	
    	}//end Cat.
    	
    	public Cat(Cat c)
    	{
    		setName(c.getName());
    		setIsDeclawed(c.getIsDeclawed());
    		setSpecies(c.getSpecies());
    	}//end copy constructor
    
    	//MUTATOR METHODS
    	public void setName(String na)			{name = na;}//end setname
    	public void setIsDeclawed(boolean dc)	{isDeclawed=dc;}//end setIsDeclawed
    	public void setSpecies(String sp)		{species=sp;}//end setSpecies
    	
    	//ACCESSOR METHODS
    	public String getName()			{return name;}//end getName
    	public boolean getIsDeclawed()	{return isDeclawed;}//end getIsDeclawed
    	public String getSpecies()		{return species;}//end getSpecies
    	
    //Prints the values of the object c1.
    	public String toString()
    	{
    		String s ="Name:\t\t"+getName()+ "\n"+
    		"Declawed?:\t"+getIsDeclawed()+"\n"+
    		"Species:\t"+getSpecies();
    		
    	return 	s;
    	}
    }//end CAT 


    And where i instantiate (ignore the other classes for which they refer to a different problem)
    Code:
    public class assignment2Problem1Test
    {
    	public static void main(String[]args)
    	{
    		testProblemOne();	
    	}//end main
    	
    //creates an array of size 200
    public static void problemTwo()
    {
    	Cat catArray [] = new Cat[200];
    	int count = 0;
    	for(int i=0;i<200;i++)
    		{
    		catArray[i] =new Cat();
    		count++;
    		System.out.println(catArray[i]+"Number:\t"+count);
    		}//end FORLOOP
    		System.out.println(catArray[2]);
    }
    
    public static void testProblemOne()
    {	Cat c1 =new Cat();
    	Cat c2 =new Cat();
    	System.out.println(c2);
    }//end testProblemOne.
    }//end assignment2Problem1Test
     
  2. elppa, Apr 19, 2012
    Last edited: Apr 19, 2012

    macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #2
    c2 is an instance of cat.

    Just call methods on c2 after you have instantiated it.

    Code:
    Cat c2 =new Cat();
    c2.setName("mittens");
    c2.setIsDeclawed(true);
    c2.setSpecies("sabertooth");
    
     
  3. thread starter macrumors member

    Joined:
    Apr 16, 2012
    Location:
    Chicago
    #3
    Hah duh....:eek:
     
  4. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #4
    It's alright - there are no stupid questions.

    It is good to ask lots of questions when learning. The more you ask, the more you find out, the quicker your learn.
     
  5. thread starter macrumors member

    Joined:
    Apr 16, 2012
    Location:
    Chicago
    #5
    heres another question. I have to store the objects in an array that hold 200 objects. How can i modify the Cat[2] while its in the array?
     
  6. macrumors 6502

    Joined:
    Mar 8, 2004
    #6
    Arrays in Java (and the majority of languages) are zero indexed. That means that for a collection of 200 cats, the indexes are 0 - 199.

    Ergo to access the second Cat in catArray you type catArray[1]. To change the name of the second Cat you invoke setName on that cat. i.e.:

    Code:
    catArray[1].setName("FurBall");
    
     
  7. elppa, Apr 19, 2012
    Last edited: Apr 19, 2012

    macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #7
    Mac_Max is correct and I was going to ask for clarification.

    They are probably trying to test your understanding of base 0.
    cats[0] = 1st Cat
    cats[1] = 2nd Cat
    cats[2] = 3rd Cat
    etc.

    Have they asked for the second cat created, or the third?

    Code:
        public static void problemTwo() {
            Cat[] cats = new Cat[200];
            for (int i = 0; i < cats.length; i++) {
                cats[i] = new Cat();
                if(i == 2)
                {
                    // third cat created is a mountain lion
                    cats[i].setSpecies("Mountain Lion");
                }
                System.out.println(cats[i] + "\nNumber:\t" + (i + 1));
            }
            System.out.println(cats[2]);
        }
    
     
  8. thread starter macrumors member

    Joined:
    Apr 16, 2012
    Location:
    Chicago
    #8
    Double duh...thanks for all your help.
     

Share This Page