Design a class named Person with fields for holding a persons name, phone number
ID: 3558375 • Letter: D
Question
Design a class named Person with fields for holding a persons name, phone number and email address. Write one or more constructors and the appropriate mutator and accessor methods for the class fields. Next, design a class for a hotel system named Guest which extends the Person class. The Guest class should have fields for number of guests, number of nights and nightly rate. Write one or more constructors and the appropriate mutator and accessor methods for the class fields. Demonstrate an object of the Guest class in a simple program that accepts data for the six fields, instantiates the object, and displays the objects field values on the screen.
Explanation / Answer
// save Person.java
public class Person {
// the Bicycle class has three fields
public String name;
public String phoneNumber;
public String email;
// the Bicycle class has one constructor
public Person(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
// the Bicycle class has four methods
public void setName(String name) {
this.name = name;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getName() {
return name;
}
}
//Save Guest.java
public class Guest extends Person
{
// the Bicycle class has three fields
public int noGuests;
public int noNights;
public float nightlyRate;
// the Bicycle class has one constructor
public Guest(String name, String phoneNumber, String email, int noGuests, int noNights, float nightlyRate) {
super(name,phoneNumber,email);
this.noGuests = noGuests;
this.noNights = noNights;
this.nightlyRate = nightlyRate;
}
// the Bicycle class has four methods
public void setNoGuests(int noGuests) {
this.noGuests = noGuests;
}
public void setNoNights(int noNights) {
this.noNights = noNights;
}
public void setNightlyRate(float nightlyRate) {
this.nightlyRate = nightlyRate;
}
public int getNoGuests() {
return noGuests;
}
public int getNoNights() {
return noNights;
}
public float getNightlyRate() {
return nightlyRate;
}
}
//Save guestDemo.java
public class GuestDemo
{
public static void main(String args[])
{
Guest g=new Guest("abc","38493494","abc@xx.com",4,2,200.0);
System.out.println("guest name:"+g.getName());
System.out.println("guest phone number:"+g.getPhoneNumber());
System.out.println("guest email:"+g.getEmail());
System.out.println("Number of nights:"+g.getNoGuests());
System.out.println("Number of persons:"+g.getNoNights());
System.out.println("Night rate:"+g.getNightlyRate());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.