PDA

View Full Version : Java Object help




bigMAC28
Apr 19, 2012, 03:27 PM
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?

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)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
Apr 19, 2012, 04:23 PM
c2 is an instance of cat.

Just call methods on c2 after you have instantiated it.


Cat c2 =new Cat();
c2.setName("mittens");
c2.setIsDeclawed(true);
c2.setSpecies("sabertooth");

bigMAC28
Apr 19, 2012, 04:26 PM
c2 is an instance of cat.

Just call methods on c2 after you have instantiated it.


Cat c2 =new Cat();
c2.setName("mittens');
c2.setIsDeclawed(true);
c2.setSpecies("sabertooth");


Hah duh....:o

elppa
Apr 19, 2012, 04:30 PM
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
Apr 19, 2012, 04:47 PM
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
Apr 19, 2012, 04:54 PM
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.:


catArray[1].setName("FurBall");

elppa
Apr 19, 2012, 04:55 PM
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?


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]);
}

bigMAC28
Apr 19, 2012, 05:01 PM
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.:


catArray[1].setName("FurBall");


Double duh...thanks for all your help.