This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you!
This question contain 7 parts(questions)
Question 1
Create a class a class Cat with the following UML
diagram:
(the "-" means private , "+" means public)
+-----------------------------------+
| Cat |
+-----------------------------------+
| - name: String |
| - weight: double |
+-----------------------------------+
| + Cat(String name, double weight) |
| + getName(): String |
| + getWeight(): double |
| + feed(): void |
| + testCat(): void |(this method is the
static)
+-----------------------------------+
Feeding a cat adds 1.0 to its weight.
The testCat method is static and is used for
testing the Cat class. Here is the code for this
testCat method:
public static void testCat() {
Cat c = new Cat("Meow", 2.0);
System.out.println(c.getName() == "Meow");
System.out.println(c.getWeight() == 2.0);
c.feed();
// The name is still the same but the weight increased by
1.0:
System.out.println(c.getName() == "Meow");
System.out.println(c.getWeight() == 3.0);
}
And here is the Start class to test the
Cat class:
public class Start {
public static void main(String[] args) {
Cat.testCat();
}
}
Question 2
Add to your program a class Dog with the following
UML diagram:
+-----------------------------------+
| Dog |
+-----------------------------------+
| - name: String |
| - weight: double |
+-----------------------------------+
| + Dog(String name, double weight) |
| + getName(): String |
| + getWeight(): double |
| + feed(): void |
| + testDog(): void |(this method is also the
static)
+-----------------------------------+
Feeding a dog adds 2.0 to its weight.
The testDog method is static and is used for
testing the Dog class. Here is the code for this
testDog method:
public static void testDog() {
Dog d = new Dog("Woof", 2.0);
System.out.println(d.getName() == "Woof");
System.out.println(d.getWeight() == 2.0);
d.feed();
// The name is still the same but the weight increased by
2.0:
System.out.println(d.getName() == "Woof");
System.out.println(d.getWeight() == 4.0);
}
Do not forget to modify the main method of the
Start class to test the new Dog
class too!
Question 3
Add a Student class so that a student has a cat as
a pet:
+---------------------------------+
| Student |
+---------------------------------+
| - name: String |
| - pet: Cat |
+---------------------------------+
| + Student(String name, Cat pet) |
| + getName(): String |
| + getPet(): Cat |
| + testStudent(): void |
+---------------------------------+
Do not forget to write the testStudent method of
the Student class to test the
getName and getPet methods,
and to modify the main method of the
Start class to test the new
Student class too!
Question 4
If you look at the code of the Cat and
Dog classes above, you will see that they are
almost the same. The only
differences are that:
the names of the classes are different;
the names of the constructors are different (since the names of
the classes are different);
the feed methods add a different amount of
weight;
the testCat and testDog methods
are different (since the names of the classes are different).
Everything else (the name and
weight instance variables, the
getName and getWeight methods) is
the same. This
means that there is a lot of code duplication between the two
classes Cat and Dog. Therefore it
is a good idea to create
a new class Animal that will contain only one copy
of that code, and have the Cat and
Dog classes then inherit the
code from the Animal class.
Add a class Animal to the program above, with has
the following UML diagram:
+--------------------------------------+
| Animal |
+--------------------------------------+
| - name: String |
| - weight: double |
+--------------------------------------+
| + Animal(String name, double weight) |
| + getName(): String |
| + getWeight(): double |
| + setWeight(double weight): void |
| + testAnimal(): void |(this method is also
static)
+--------------------------------------+
The Dog and Cat classes should then be derived classes from the
Animal base class (in other words, the
Dog and Cat
classes should inherit from the Animal
class).
Which instance variables, constructors, and methods from the
Dog and Cat classes can be moved
to the Animal class?
Which instance variables, constructors, and methods of the
Dog and Cat classes must stay in
these classes? How
should they be modified?
After adding the Animal class, modify the
Student class so that the student has an animal as
a pet, not a cat.
Do not forget to:
change the main method of the
Start class to run the unit tests of the new
Animal class;
add new tests to the Student class to test that
you can now use an Animal object as the pet of a
student;
Now that the Student class uses an animal as pet,
can you still use a cat object as the pet of a student? Why or
why
not?
Can you now use a dog object as the pet of a student? Why or why
not?
Question 5
Add a Bird class to your program. A bird is an animal, therefore
your Bird class must be a class derived from
the
Animal class. The UML diagram of the Bird class is
as follows:
+-----------------------------------------------------+
| Bird |
+-----------------------------------------------------+
| - altitude: double |
+-----------------------------------------------------+
| + Bird(String name, double weight, double altitude) |
| + getAltitude(): double |
| + testBird(): void |(this method is
static)
+-----------------------------------------------------+
The altitude instance variable represents the
altitude at which the bird is flying. Cats and dogs do not fly so
they do
not have an altitude.
The constructor for the Bird class takes three
arguments: the name of the bird, the weight of the bird, and the
altitude
at which the bird is flying. The altitude argument
of the constructor is stored into the altitude instance variable
of
the Bird class. Where are the
name and weight of the bird
stored? How?
Do not forget to:
change the main method of the
Start class to run the unit tests of the new Bird
class;
add new tests to the Student class to test that
you can now use a Bird object as the pet of a
student;
Suppose a student has a bird as a pet. Can the student get the
altitude of his pet?
Question 6
Add a Chicken class to your program. A chicken is a bird, therefore
your Chicken class must be a class derived
from
the Bird class. The UML diagram of the
Chicken class is as follows:
+-------------------------+
| Chicken |
+-------------------------+
+-------------------------+
| + Chicken(String name) |
| + testChicken(): void |(this method is
static)
+-------------------------+
A chicken always has a weight of 5.0 and an altitude of 0.0
(chickens spend all their time on the ground).
Do not forget to:
change the main method of the
Start class to run the unit tests of the new
Chicken class;
add new tests to the Student class to test that
you can now use a Chicken object as the pet of a
student;
Question 7
Both the Student class and the
Animal class have a name instance
variable and a getName method, which leads
to
code duplication between these two classes. To solve this problem,
add a new LivingThing class to your program,
which becomes the superclass for the Student and
Animal classes, and which contains only one copy
of the code for
the name instance variable and the
getName method. The LivingThing
class has the following UML diagram:
+----------------------------+
| LivingThing |
+----------------------------+
| - name: String |
+----------------------------+
| + LivingThing(String name) |
| + getName(): String |
| + testLivingThing(): void |(this method is
static)
+----------------------------+
Change the Student class and
Animal class so that both classes are now derived
from the LivingThing class.
Then remove the name instance variables and the
getName methods from the Animal
and Student classes. The
other classes do not change.
Check that all your tests still work.
Do not forget to change the main method of the
Start class to run the unit tests of the new
LivingThing class.
LivingThing is now the top-most class. It has two
derived classes: Animal and
Student. Animal has three
derived classes: Cat, Dog, and
Bird. Bird has one derived class:
Chicken. Student,
Cat, Dog, and
Chicken do
not have derived classes.
//Question 1
public class Cat { private String name; private double weight; public Cat(String name, double weight) { this.name = name; this.weight = weight; } //getters public String getName() { return name; } public double getWeight() { return weight; } public void feed() { //Feeding a cat adds 1.0 to its weight. weight+=1.0; } public static void testCat() { Cat c = new Cat("Meow", 2.0); System.out.println(c.getName() == "Meow"); System.out.println(c.getWeight() == 2.0); c.feed(); // The name is still the same but the weight increased by 1.0: System.out.println(c.getName() == "Meow"); System.out.println(c.getWeight() == 3.0); } }
//=================================================
public class Start { public static void main(String[] args) { Cat.testCat(); } }
//========== Output==================
//Question 2
public class Dog { private String name; private double weight; public Dog(String name, double weight) { this.name = name; this.weight = weight; } //getter public String getName() { return name; } public double getWeight() { return weight; } public void feed() { //Feeding a dog adds 2.0 to its weight. weight+=2.0; } public static void testDog() { Dog d = new Dog("Woof", 2.0); System.out.println(d.getName() == "Woof"); System.out.println(d.getWeight() == 2.0); d.feed(); // The name is still the same but the weight increased by 2.0: System.out.println(d.getName() == "Woof"); System.out.println(d.getWeight() == 4.0); } }
//===========================================
public class Start { public static void main(String[] args) { Dog.testDog(); } }
//=====================================
//output
//Question 3
public class Student { private String name; private Cat pet; public Student(String name, Cat pet) { this.name = name; this.pet = pet; } public String getName() { return name; } public Cat getPet() { return pet; } public static void testStudent() { Student student = new Student("John",new Cat("Meow",2.0)); System.out.println(student.getName() == "John"); System.out.println(student.getPet().getWeight() == 2.0); student.getPet().feed(); // The name is still the same but the weight increased by 1.0: System.out.println(student.getPet().getName() == "Meow"); System.out.println(student.getPet().getWeight() == 3.0); } }
//===============================================================
public class Start { public static void main(String[] args) { Student.testStudent(); } }
//Output
//Question4
public class Animal { protected String name; protected double weight; public Animal(String name, double weight) { this.name = name; this.weight = weight; } public String getName() { return name; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public static void testAnimal() { } }
//==============================
public class Dog extends Animal{ public Dog(String name, double weight) { super(name,weight); } public void feed() { //Feeding a dog adds 2.0 to its weight. weight+=2.0; } public static void testAnimal() { Dog d = new Dog("Woof", 2.0); System.out.println(d.getName() == "Woof"); System.out.println(d.getWeight() == 2.0); d.feed(); // The name is still the same but the weight increased by 2.0: System.out.println(d.getName() == "Woof"); System.out.println(d.getWeight() == 4.0); } }
//=====================================
public class Cat extends Animal{ public Cat(String name, double weight) { super(name,weight); } public void feed() { //Feeding a cat adds 1.0 to its weight. weight+=1.0; } public static void testAnimal() { Cat c = new Cat("Meow", 2.0); System.out.println(c.getName() == "Meow"); System.out.println(c.getWeight() == 2.0); c.feed(); // The name is still the same but the weight increased by 1.0: System.out.println(c.getName() == "Meow"); System.out.println(c.getWeight() == 3.0); } }
//=======================================
public class Student { private String name; private Animal pet; public Student(String name, Animal pet) { this.name = name; this.pet = pet; } public String getName() { return name; } public Animal getPet() { return pet; } public static void testStudent() { Student student = new Student("John",new Cat("Meow",2.0)); System.out.println(student.getName() == "John"); System.out.println(student.getPet().getWeight() == 2.0); ((Cat)student.getPet()).feed(); // The name is still the same but the weight increased by 1.0: System.out.println(student.getPet().getName() == "Meow"); System.out.println(student.getPet().getWeight() == 3.0); } }
//======================================================
Now that the Student class uses an animal as
pet, can you still use a cat object as the pet of a student? Why or
why
not?
Ans:--> Yes we can use Cat as Pet, since it subclass of
Animal
Can you now use a dog object as the pet of a student? Why or why
not?
Ans:--> Yes, you can use also dog as a pet animal, since it is also a subclass of Animal.
//=======================================================
//Question 5
public class Bird extends Animal { private double altitude; public Bird(String name, double weight, double altitude) { super(name, weight); this.altitude = altitude; } public double getAltitude() { return altitude; } public static void testBird() { Bird bird = new Bird("Pigeon",2.0,10); System.out.println(bird.getWeight()==2.0); System.out.println(bird.getAltitude()==10.0); System.out.println(bird.getName()=="Pigeon"); } }
//====================================
public class Start { public static void main(String[] args) { Bird.testBird(); } }
//Output
//modification in Student class
public class Student { private String name; private Animal pet; public Student(String name, Animal pet) { this.name = name; this.pet = pet; } public String getName() { return name; } public Animal getPet() { return pet; } public static void testStudent() { Student student = new Student("John",new Bird("Pigeon",2.0,10.0)); System.out.println(student.getName() == "John"); System.out.println(student.getPet().getWeight() == 2.0); System.out.println(((Bird)student.getPet()).getAltitude()==10.0); // The name is still the same but the weight increased by 1.0: System.out.println(student.getPet().getName() == "Pigeon"); } }
//======================================================
//Question 6
public class Chicken extends Bird{ public Chicken(String name) { super(name, 5, 0); } public static void testChicken() { Chicken chicken = new Chicken("Roaster"); System.out.println(chicken.getAltitude()==0); System.out.println(chicken.getName()=="Roaster"); System.out.println(chicken.getWeight()==5.0); } }
//==========================================
public class Start { public static void main(String[] args) { Chicken.testChicken(); } }
//========================================
//Question 7
public class LivingThing { protected String name; public LivingThing(String name) { this.name = name; } public String getName() { return name; } public static void testLivingThing() { LivingThing livingThing = new LivingThing("living"); System.out.println(livingThing.getName()=="living"); } }
//================================================
public class Animal extends LivingThing{ protected double weight; public Animal(String name, double weight) { super(name); this.weight = weight; } public String getName() { return name; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public static void testAnimal() { } }
//=========================================
public class Student extends LivingThing{ private Animal pet; public Student(String name, Animal pet) { super(name); this.pet = pet; } public String getName() { return name; } public Animal getPet() { return pet; } public static void testStudent() { Student student = new Student("John",new Bird("Pigeon",2.0,10.0)); System.out.println(student.getName() == "John"); System.out.println(student.getPet().getWeight() == 2.0); System.out.println(((Bird)student.getPet()).getAltitude()==10.0); // The name is still the same but the weight increased by 1.0: System.out.println(student.getPet().getName() == "Pigeon"); } }
//If you need any help regarding this solution ......... please leave a comment....... thanks
This is the question about object-oriend programming(java) please show the detail comment and prefect code of...
PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1. What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** * Program: PRG/421 Week 1 Analyze Assignment * Purpose: Analyze the coding for...
This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...
What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...
Need help with this Java question in 1 hour please JAVA Using the files from the in-class assignment add two additional animals of your choice. For each animal add two additional methods appropriate to your particular animal. As the majority of the code for this program exists you will not need an algorithm. Use the 3 java classes below. Thank you //Animal.java public class Animal { public Animal() { System.out.println("A new animal has been created!"); } ...
LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information: Name: Dobby Age: 2 Pet Information: Name: Kreacher Age: 3 Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Pet myPet = new Pet(); Dog myDog = new Dog(); String petName, dogName, dogBreed; int petAge, dogAge; petName = scnr.nextLine(); petAge = scnr.nextInt(); scnr.nextLine();...
help with java OOP, here is the started code: package P2; public class Farm { private double availableFood; private Animal[] animals; public Farm() { setAvailableFood(1000); animals = new Animal[4]; animals[0] = new Chicken(); animals[1] = new Cow(); animals[2] = new Llama(); animals[3] = new Llama(); } public void makeNoise(){ // all animals make their sound (Moo, Cluck, etc) for(Animal...
Java Create four classes: 1. An Animal class that acts as a superclass for Dog and Bird 2. A Bird class that is a descendant of Animal 3. A Dog class that is a descendant of Animal 4. A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: · one instance variable, a private String variable named name · a single constructor that takes one argument, a String, used to set...
In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...
1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...