Introdução aos Objectos/Exercício 01: Gato simples: Difference between revisions

From Wiki**3

Root (talk | contribs)
No edit summary
Root (talk | contribs)
No edit summary
Line 14: Line 14:


== Solução ==
== Solução ==
=== Class "Cat" ===
<java5>
public class Cat {
/**
* The cat's name.
*/
private String _name;
/**
* The cat's age.
*/
private int _age;
/**
* The cat's weight.
*/
private double _weight;
/**
* Fully specify the cat's initial state.
*
* @param name
* @param age
* @param weight
*/
public Cat(String name, int age, double weight) {
_name = name;
_age = age;
_weight = weight;
}
/**
* @return the _name
*/
public String getName() {
return _name;
}
/**
* @param name the name to set
*/
public void set_name(String name) {
_name = name;
}
/**
* @return the age
*/
public int getAge() {
return _age;
}
/**
* @param age the age to set
*/
public void set_age(int age) {
_age = age;
}
/**
* @return the weight
*/
public double getWeight() {
return _weight;
}
/**
* @param weight the weight to set
*/
public void set_weight(double weight) {
_weight = weight;
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
if (o instanceof Cat) {
Cat cat = (Cat) o;
return _name.equals(cat.getName()) && _age == cat.getAge() && _weight == cat.getWeight();
}
return false;
}
/**
* @see java.lang.Object#toString()
*/
@Override
@SuppressWarnings("nls")
public String toString() {
return _name + " (cat) (" + _name + ":" + _age + ":" + _weight + ")";
}
}
</java5>
=== Test application (main) ===
<java5>
public class Application {
public static void main(String[] args) {
Cat cat = new Cat("Tareco", 12, 3.1415);
System.out.println(cat.equals(new Cat("Tareco", 12, 3.141)));  // prints "true"
System.out.println(cat.equals(new Cat("Pantufa", 1, 2.718)));  // prints "false"
System.out.println(cat);
}
}
</java5>


[[category:Ensino]]
[[category:Ensino]]
[[category:PO]]
[[category:PO]]

Revision as of 10:55, 3 October 2013

Problema

Modele e implemente uma classe que represente uma versão muito simples do conceito Gato.

Um Gato tem como características o nome, a idade e o peso.

Implemente o método de comparação (equals), por forma a considerar que dois gatos são iguais se as suas características forem iguais.

Implemente o método de apresentação (toString), por forma a produzir uma cadeia de caracteres onde seja apresentado o nome, a idade e o peso do gato.

Implemente métodos de acesso às variáveis internas do gato.

Implemente um programa ("main") que ilustre a utilização dos métodos anteriores.

Solução

Class "Cat"

<java5> public class Cat {

/** * The cat's name. */ private String _name;

/** * The cat's age. */ private int _age;

/** * The cat's weight. */ private double _weight;

/** * Fully specify the cat's initial state. * * @param name * @param age * @param weight */ public Cat(String name, int age, double weight) { _name = name; _age = age; _weight = weight; }

/** * @return the _name */ public String getName() { return _name; }

/** * @param name the name to set */ public void set_name(String name) { _name = name; }

/** * @return the age */ public int getAge() { return _age; }

/** * @param age the age to set */ public void set_age(int age) { _age = age; }

/** * @return the weight */ public double getWeight() { return _weight; }

/** * @param weight the weight to set */ public void set_weight(double weight) { _weight = weight; }

/** * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object o) { if (o instanceof Cat) { Cat cat = (Cat) o; return _name.equals(cat.getName()) && _age == cat.getAge() && _weight == cat.getWeight(); } return false; }

/** * @see java.lang.Object#toString() */ @Override @SuppressWarnings("nls") public String toString() { return _name + " (cat) (" + _name + ":" + _age + ":" + _weight + ")"; } } </java5>

Test application (main)

<java5> public class Application { public static void main(String[] args) { Cat cat = new Cat("Tareco", 12, 3.1415); System.out.println(cat.equals(new Cat("Tareco", 12, 3.141))); // prints "true" System.out.println(cat.equals(new Cat("Pantufa", 1, 2.718))); // prints "false" System.out.println(cat); } } </java5>