Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bigMAC28

macrumors member
Original poster
Apr 16, 2012
91
0
Chicago
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
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
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");
 
Last edited:

elppa

macrumors 68040
Nov 26, 2003
3,233
151
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.
 

bigMAC28

macrumors member
Original poster
Apr 16, 2012
91
0
Chicago
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.

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?
 

Mac_Max

macrumors 6502
Mar 8, 2004
404
1
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?

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");
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
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]);
    }
 
Last edited:

bigMAC28

macrumors member
Original poster
Apr 16, 2012
91
0
Chicago
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");

Double duh...thanks for all your help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.