Introdução aos Objectos/Exercício 01: Gato simples: Difference between revisions
From Wiki**3
No edit summary |
|||
| Line 24: | Line 24: | ||
* The cat's name. | * The cat's name. | ||
*/ | */ | ||
private String | private String name; | ||
/** | /** | ||
* The cat's age. | * The cat's age. | ||
*/ | */ | ||
private int | private int age; | ||
/** | /** | ||
* The cat's weight. | * The cat's weight. | ||
*/ | */ | ||
private double | private double weight; | ||
/** | /** | ||
| Line 44: | Line 44: | ||
*/ | */ | ||
public Cat(String name, int age, double weight) { | public Cat(String name, int age, double weight) { | ||
this.name = name; | |||
this.age = age; | |||
this.weight = weight; | |||
} | } | ||
/** | /** | ||
* @return the | * @return the name | ||
*/ | */ | ||
public String getName() { | public String getName() { | ||
return | return this.name; | ||
} | } | ||
| Line 59: | Line 59: | ||
* @param name the name to set | * @param name the name to set | ||
*/ | */ | ||
public void | public void setName(String name) { | ||
this.name = name; | |||
} | } | ||
| Line 67: | Line 67: | ||
*/ | */ | ||
public int getAge() { | public int getAge() { | ||
return | return this.age; | ||
} | } | ||
| Line 73: | Line 73: | ||
* @param age the age to set | * @param age the age to set | ||
*/ | */ | ||
public void | public void setAge(int age) { | ||
this.age = age; | |||
} | } | ||
| Line 81: | Line 81: | ||
*/ | */ | ||
public double getWeight() { | public double getWeight() { | ||
return | return this.weight; | ||
} | } | ||
| Line 87: | Line 87: | ||
* @param weight the weight to set | * @param weight the weight to set | ||
*/ | */ | ||
public void | public void setWeight(double weight) { | ||
this.weight = weight; | |||
} | } | ||
| Line 98: | Line 98: | ||
if (o instanceof Cat) { | if (o instanceof Cat) { | ||
Cat cat = (Cat) o; | Cat cat = (Cat) o; | ||
return | return this.name.equals(cat.name) && this.age == cat.age && this.weight == cat.weight; | ||
} | } | ||
return false; | return false; | ||
| Line 109: | Line 109: | ||
@SuppressWarnings("nls") | @SuppressWarnings("nls") | ||
public String toString() { | public String toString() { | ||
return | return this.name + " (cat) (" + this.age + ":" + this.weight + ")"; | ||
} | } | ||
} | } | ||
Revision as of 16:38, 23 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) { this.name = name; this.age = age; this.weight = weight; }
/** * @return the name */ public String getName() { return this.name; }
/** * @param name the name to set */ public void setName(String name) { this.name = name; }
/** * @return the age */ public int getAge() { return this.age; }
/** * @param age the age to set */ public void setAge(int age) { this.age = age; }
/** * @return the weight */ public double getWeight() { return this.weight; }
/** * @param weight the weight to set */ public void setWeight(double weight) { this.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 this.name.equals(cat.name) && this.age == cat.age && this.weight == cat.weight; } return false; }
/** * @see java.lang.Object#toString() */ @Override @SuppressWarnings("nls") public String toString() { return this.name + " (cat) (" + this.age + ":" + this.weight + ")"; } } </java5>
Test application (main)
<java5> public class Application { public static void main(String[] args) { Cat cat = new Cat("Tareco", 12, 3.141); 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>