Design a Java class for Person with the following behaviours:
• talk, in which a person can talk a sentence, if s/he is at least two-year old.
• eat, in which a person can eat something if s/he is hungry. If the person eats something, then s/he becomes full (not hungry).
• needFood, in which the person’s status will become hungry if s/he is currently full.
• walk, in which a person will walk a specific distance. If a person walks more than four kilometers, then s/he becomes tired and is not able to walk anymore.
• sleep, in which a person will sleep if s/he is awake. If a person sleeps, then s/he is no tired anymore, her/him walking distance will reset, and s/he can walk again. Note that if a person is sleeping, s/he can’t walk.
• awake, in which the person is awaken if s/he is currently sleeping.
• grow, in which the person’s age will increase by one year. If a person reaches to 55, then his ability to walk will decrease by half every five years.
You need to indicate all the instance variables (properties) a give person should have to be able to handle the above list of behaviours, and store the current states of a given person. For instance, every person has name, age, current distance of walking, sleeping status, walking distance capability, etc. After designing the Person class, implement it as a Java class. Then write a tester Java program to test that class by creating some Person instance objects and do some actions for each of them. To make sure that your tester program tests all the functionalities of a given person, you have to call every method of the class at least once.
Bonus (3 points): Assume that every person can have one friend, which is another person. Therefore, two other behaviors for a person could be getFriend and changeFriend. Modify the Person class that you have designed to be able to handle these two behaviours as well. Note that you need to have another instance variable as well.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Person.java
public class Person {
// attributes
private String name;
private int age;
private boolean isHungry;
private boolean isTired;
private boolean isSleeping;
private double WALKING_CAPACITY = 4;
private double distanceWalked;
private Person friend;
// constructor to initialize a person with given name and age
public Person(String name, int age) {
this.name = name;
this.age = age;
// default values for all other fields
isHungry = true;// hungry
isTired = false; // not tired
isSleeping = false; // awake
distanceWalked = 0; // not walked any distance
friend = null; // no friends yet
}
// method to simulate talking
public void talk() {
if (age >= 2) {
// simulating talking
System.out.println(name + " is talking");
} else {
// could not talk
System.out.println(name + " has not learnt to talk yet");
}
}
// method to simulate eating
public void eat() {
if (isHungry) {
System.out.println(name + " is eating food.");
isHungry = false;
} else {
System.out.println(name + " is not hungry.");
}
}
// method to simulate need for eating food
public void needFood() {
if (!isHungry) {
System.out.println(name + " is now hungry");
isHungry = true;
} else {
System.out.println(name + " is already hungry");
}
}
// method to simulate walking
public void walk(double distance) {
if (isTired) {
// tired, could not walk
System.out.println(name
+ " is tired, need some sleep before walking");
} else if ((distance + distanceWalked) > WALKING_CAPACITY) {
// distance is greater than the capacity
System.out.println(name + " can't walk that far");
} else if (isSleeping) {
// no walk while sleep
System.out.println(name + " cannot walk while sleeping");
} else {
//simulating walk
System.out.println(name + " walked for " + distance + " km");
distanceWalked += distance;
System.out.println(name + " has now walked " + distanceWalked
+ " km without sleeping");
if (distanceWalked >= WALKING_CAPACITY) {
//is now tired
isTired = true;
}
}
}
// method to simulate sleeping
public void sleep() {
if (isSleeping) {
System.out.println(name + " is already sleeping");
} else {
isSleeping = true;
System.out.println(name + " is now sleeping");
isTired = false;
distanceWalked = 0;
}
}
// method to simulate waking up
public void awake() {
if (isSleeping) {
System.out.println(name + " is now awake");
isSleeping = false;
} else {
System.out.println(name + " is already awake");
}
}
// method to simulate growing old by 1
public void grow() {
age++;
System.out.println(name + " is now " + age + " years old");
if (age >= 55) {
if (age % 5 == 0) {
WALKING_CAPACITY = WALKING_CAPACITY / 2.0;
System.out.println(name + "'s walking capacity is now "
+ WALKING_CAPACITY + " kms");
}
}
}
// method to return the friend
public Person getFriend() {
return friend;
}
// method to set a new friend
public void changeFriend(Person newFriend) {
friend = newFriend;
if (friend != null)
System.out.println(name + " is now friends with " + newFriend.name);
}
// returns a String containing info about all attributes
@Override
public String toString() {
String data = "Name: " + name + ", Age: " + age + "\n";
data += "Hungry: " + isHungry + ", Tired: " + isTired + ", Sleeping: "
+ isSleeping + "\n";
data += "Walking capacity: " + WALKING_CAPACITY + ", Distance walked: "
+ distanceWalked + "\n";
data += "Friend: ";
if (friend == null) {
data += "None";
} else {
data += friend.name;
}
return data;
}
//getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean isHungry() {
return isHungry;
}
public boolean isTired() {
return isTired;
}
public boolean isSleeping() {
return isSleeping;
}
public double getWalkingCapacity() {
return WALKING_CAPACITY;
}
public double getDistanceWalked() {
return distanceWalked;
}
}
// Test.java
public class Test {
public static void main(String[] args) {
//creating some Person objects
Person p1 = new Person("Billy", 1);
Person p2=new Person("Oliver", 22);
Person p3=new Person("John", 54);
//testing all methods
p1.talk();
p2.talk();
p3.talk();
p1.eat();
p1.eat();
p1.needFood();
p3.walk(3);
p3.walk(1);
p3.walk(2);
p3.sleep();
p3.sleep();
p2.awake();
p2.sleep();
p2.walk(2);
p1.grow();
p2.grow();
p3.grow();
p1.changeFriend(p2);
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
}
}
/*OUTPUT*/
Billy has not learnt to talk yet
Oliver is talking
John is talking
Billy is eating food.
Billy is not hungry.
Billy is now hungry
John walked for 3.0 km
John has now walked 3.0 km without sleeping
John walked for 1.0 km
John has now walked 4.0 km without sleeping
John is tired, need some sleep before walking
John is now sleeping
John is already sleeping
Oliver is already awake
Oliver is now sleeping
Oliver cannot walk while sleeping
Billy is now 2 years old
Oliver is now 23 years old
John is now 55 years old
John's walking capacity is now 2.0 kms
Billy is now friends with Oliver
Name: Billy, Age: 2
Hungry: true, Tired: false, Sleeping: false
Walking capacity: 4.0, Distance walked: 0.0
Friend: Oliver
Name: Oliver, Age: 23
Hungry: true, Tired: false, Sleeping: true
Walking capacity: 4.0, Distance walked: 0.0
Friend: None
Name: John, Age: 55
Hungry: true, Tired: false, Sleeping: true
Walking capacity: 2.0, Distance walked: 0.0
Friend: None
Design a Java class for Person with the following behaviours: • talk, in which a person...
USING JAVA,
Class Design: Person The Person class is intended to be an abstract and simplified representation of a person. Each person will have an array of "friends" - which are other "Person" instances. Data to Store (2 Points) Name private instance variable with only private setter (2 Points) Friends private instance array with neither getter nor setter Actions Constructor o 1 Point) Take in as argument the name of the person (enforce invariants) o (1 Point) Initialize the array...
Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...
JAVA You are given a class Critter which represents an animal. Critter class will be the superclass of a set of subclasses classes. A Critter has a weight and a position on a number line. The constructor initializes the position to 0 and sets the weight from the parameter. Critter has an ArrayList of Strings to keep a log of its activities. It has other methods which you can view here You are to implement the following subclasses of Critter...
Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...
Question 1 1 pts Which of the following is not a valid class name in Java? O MyClass MyClass1 My_Class MyClass# Question 2 1 pts Which of the following statements is False about instance variables and methods? Instance variables are usually defined with private access modifier. Instance variables are defined inside instance methods. Instance methods are invoked on an object of the class that contains the methods. A class can have more than one instance variables and methods. Question 3...
Please use Java Question 3: Person and Customer classes: Design a class named Person with properties for holding a person's name, address, and telephone number. Next, design a class named Customer, which is derived from the Person class. The Customer class should have a property for a customer number and a Boolean property indicating whether wishes to be on a mailing list. Demonstrate an object of customer class in a simple GUI application. Write the code include appropriate input validation,...
About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...
this is java m. please use netbeans if you can.
7. Person and Customer Classes Design a class named Person with fields for holding a person's name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class's fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the cus- tomer wishes to...
IN JAVA: Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to...
IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...