In java homework. Question 1: Write a Class- Dog Write a complete class that des
ID: 3667866 • Letter: I
Question
In java homework.
Question 1: Write a Class- Dog
Write a complete class that describes a Dog. A Dog is described by a name, age, and whether or not it has been vaccinated for rabies. (This class might be used in a system for managing a doggie day care or veterinary office.)
Write one or more constructors, getters and setters, and a toString method.
Write a driver program to create objects and invoke methods (to demonstrate that your class works).
Question 2: Write a Class- Computer
Write a complete class that represents a Computer. A Computer is described by the brand, the name, and the size of memory (e.g., HP All-in-One 1TB or Apple iMac 128GB). (This class might be used for inventory management.)
Write one or more constructors; getters and setters; and a toString method.
Write a driver program to create objects and invoke methods (to demonstrate that your class works).
Question 3: Describing Class Design
In your own words, answer two of the following:
What is the job of the constructor?
Why are instance data variables made private?
What is the purposes of getters and setters?
Why do we override the toString method?
Explanation / Answer
Dog.java
public class Dog {
private String name;
private int age;
private boolean isVaccinated;
public Dog()
{
}
public Dog(String dogName,int dogAge,boolean vaccinated)
{
name=dogName;
age=dogAge;
isVaccinated=vaccinated;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isVaccinated() {
return isVaccinated;
}
public void setVaccinated(boolean isVaccinated) {
this.isVaccinated = isVaccinated;
}
public String toString()
{
String s="Name of Dog: "+name+" Age: "+age+" Dog is Vaccinated for rabies :"+isVaccinated;
return s;
}
}
DogTest.java
public class DogTest {
public static void main(String[] args) {
Dog d=new Dog("Rocky",12,true);
System.out.println(d.toString());
Dog dog=new Dog();
dog.setName("murphy");
dog.setAge(8);
dog.setVaccinated(false);
System.out.println(dog.toString());
}
}
sample output:
Name of Dog: Rocky Age: 12 Dog is Vaccinated for rabies :true
Name of Dog: murphy Age: 8 Dog is Vaccinated for rabies :false
Computer.java
public class Computer {
private String brand;
private String name;
private String memorySize;
public Computer()
{
}
public Computer(String cBrand,String cName,String cMemorySize)
{
brand=cBrand;
name=cName;
memorySize=cMemorySize;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMemorySize() {
return memorySize;
}
public void setMemorySize(String memorySize) {
this.memorySize = memorySize;
}
public String toString()
{
String s=brand+" "+name+" "+memorySize;
return s;
}
}
ComputerTest.java
public class ComputerTest {
public static void main(String[] args) {
Computer c1=new Computer("HP","DV6","1TB");
System.out.println(c1.toString());
Computer c2=new Computer();
c2.setBrand("Apple");
c2.setName("Macbook pro");
c2.setMemorySize("256 GB");
System.out.println(c2.toString());
}
}
sample output
HP DV6 1TB
Apple Macbook pro 256 GB
3)
1.Job of constructors is to intialize the private data members of a class when a new instance of that class is created.
3.Setters and Getters are required for accessing and modifying private instance members of the class.
We make instance varibales private and have setters and getters instead to have control over varibales for example we can make variable read only by making setter as private.
other than that we can have some logic in setters which will prevent user from setting a invalid value in the variable
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.