Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am wondering how to write a setter for this project. I know the usual way but wondering if there is something different I need

I am wondering how to write a setter for this project. I know the usual way but wondering if there is something different I need to do for an array.

Create a new project in BlueJ. Create two new classes in the project, with the following specifications:

Class name: Guest

Fields:

  • name (String)
  • roomNumber (int)
  • checkOutDate (Date) Tip: Date is a class in java.util, so you will need to import java.util.Date.

Constructor: Write a default constructor (no parameters) that initializes the fields to the following default values:

  • name: "" (empty string)
  • roomNumber: 100
  • checkOutDate: new Date() Tip: new Date() creates a date object with the current date

Methods: Write getters and setters for all fields. The getters and setters should be named according to standard Java conventions. Add validation to the setRoomNumber method so that the room number is only updated if the given room number is valid. This hotel has two floors with rooms numbered 100 - 140 and 200 - 240.

Class name: Hotel

Fields:

  • name (String)
  • guests (ArrayList)

Constructor: takes one parameter, the hotel name, and initializes the name field; creates an empty list to initialize guests.

Methods: Write getters and setters for all fields. Name the getters and setters according to standard Java conventions.

This is my code for both:

Guest

import java.util.ArrayList; import java.util.Date; /** * Class Guest with a name, roomNumber, checkOutDate * */

class Guest { private String name; private int roomNumber; private Date checkOutDate; /** * Constructor that takes no parameters */ public Guest() { setName(""); setRoomNumber(100); setCheckOutDate(new Date()); }

/** * Write getters for the following: name, roomNumber, and checkOutDate */ public String getName() { return name; }

public int getRoomNumber() { return roomNumber; }

public Date getCheckOutDate() { return checkOutDate; }

/** * Write setters for the following: name, roomNumber, and checkOutDate */ public void setName(String name) { this.name = name; }

/** * roomNumber is only updated if the room number is valid */ public void setRoomNumber(int roomNumber) { if ((roomNumber >= 100 && roomNumber <= 140) || (roomNumber >= 200 && roomNumber <= 240)) this.roomNumber = roomNumber; }

public void setCheckOutDate(Date checkOutDate) { this.checkOutDate = checkOutDate; } }

Hotel

import java.util.ArrayList; /** * Class Hotel with a guest name * */

public class Hotel { private String name; private ArrayListguests;

public Hotel(String name) { this.name = name; guests = new ArrayList(); }

/** * Write getter for name */ public String getName() { return name; }

/** * Create an array list that returns list of guests. */ public ArrayList getGuests() { return guests; }

/** * Write setters for name and guests */ public void setName(String name) { this.name = name; }

public void setGuests(ArrayList guests) { this.guests = guests; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

Knowledge of digital marketing and social media

Answered: 1 week ago

Question

=+ Have they changed the way employers view IP?

Answered: 1 week ago