Insects 10 marks Create two classes Hosquito and Tack Each o
Solution
Though there is no discreption regarding Disease and person class, I have Just created them as markers to compile the code succesfully without errors.
Insect.java: as given
package lab;
// Insect class as given
public abstract class Insect {
public final String name;
protected int health;
protected Disease disease;
public Insect(String name)
{
this.name=name;
}
public abstract void bite(Person p);
}
Mosquito.java:
package lab;
// The mosquito class extends insect
public class Mosquito extends Insect {
// constructor with a name
public Mosquito(String name) {
// constructing the parent class
super(name);
// setting health to 100 as given
health=100;
// setting disease to null
// indicate no disease at beginning
disease=null;
// TODO Auto-generated constructor stub
}
// Constructor with health name disease as given
public Mosquito(String name,int health,Disease disease)
{
// setting values and calling parent class constructor
super(name);
super.health=health;
super.disease=disease;
}
// Overriding the bite method from the Insect class
public void bite(Person p)
{
// if mosquito health is less than 5o and has disease
if(super.health>50 && super.disease!=null)
{
// checking person has disease or not
if(p.disease==null)
{
// injecting disease to the person
p.disease=super.disease;
// reducing health ny 1
p.health--;
health--;
}
else // else just reducing the health
{
p.health--;
health--;
}
}
// else >=50 checking disease presents
else if(super.disease!=null)
{
// injecting disease and reducing health
p.disease=disease;
p.health--;
health--;
}
// else just reducing the health
else
{
p.health--;
health--;
}
}
}
Tick.java:
package lab;
// Tick class extending the Insect class
public class Tick extends Insect {
// Constructor setting the name
public Tick(String name)
{
// Parent class constructor
// setting default values
super(name);
disease=null;
health=100;
}
// Constructor with the name health and disease
public Tick(String name,int health,Disease disease)
{
// setting values
super(name);
super.health=health;
super.disease=disease;
}
// Overriding the bite method From the parent class
public void bite(Person p)
{
// If the disease is not null
if(super.disease!=null)
{
// Injecting the person that disease
p.disease=disease;
// reducing the health
p.health=p.health-5;
health--;;
}
}
}
Disease.java
package lab;
// The class has taken just to make sure there are no compilation errors
// while writing the program
public class Disease {
}
Person.java
package lab;
//The class has taken just to make sure there are no compilation errors
//while writing the program
public class Person {
int health;
Disease disease;
}


