Write the code to create a class that models a door object

Assignment Help JAVA Programming
Reference no: EM132075896

Answer the following question

1) Object-oriented languages like Java are designed to make it easy for programmers to implement software versions of real-world objects. In learning Java, an important skill to master is the ability to represent an object in code. Objects that we model are described using Java classes, so we have chosen to begin this lab by modeling a very simple, everyday object: a door.

Write the code to create a class that models a door object. Don't worry about the internal details just yet. Just give the class a name and an empty body for the moment. We will add more to the class shortly.

2) When modeling an object as a class, we also need to describe the properties it possesses. An everyday object that we wish to model always has one or more properties that describe it. For instance a door object might have a name like "Front" or "Side" to distinguish it from other doors.

Another property that could describe a door is its state: "open" or "closed". Properties of objects are described in code by using nouns like "state" or "name" to create instance variables that hold values.

Add instance variables to your Door class for the name of the door and its state. Experience has shown that we almost always want to limit the visibility of instance variables inside the same class, so make the access modifiers of state and name private. And because the state and name properties have values like "open" or "front", let the instance variables you create be of type String.

3) Objects also have operations which can be invoked on the object and which may change the object in some way. For instance, the operations for a door object could be "open" and "close". An operation on an object corresponds to a Java method and is described in code by using a verb like "open" or "close". Invoking a method may change the value of an instance variable. For example, invoking close() would change the value of the state variable from "open" to "closed".

Declare methods for open and close. Because we usually want to allow free access to the methods a class contains, make the access modifier for each method public.

4) Now that we have a Door class, we would like to be able to create some Door objects. Java constructors are components of a class whose purpose is to create objects of the given class and to initialize the object's instance variables.

Java provides a constructor with no arguments (the "default" constructor) for every class and the Door class is no exception. Unfortunately, the default constructor that Java provides initializes the state and name variables to null. This is unacceptable.

Add a constructor for the Door class that receives two arguments: the name of the door and its initial state. Because we want to use the constructor outside of the class, make the access modifier for the constructor public.

5) It is often convenient to have accessor methods that operate on a single instance variable. Here is an accessor method for the name variable:

public String getName() { return name; }

The word String in front of getName() indicates that the method returns a String when it is invoked. The body is simple and just returns the value of name.

Add this method to your class and write a similar accessor method for the instance variable state.

6) Many instance variables in a class will have corresponding mutator methods that allow you to change the value of the variable. Here is a mutator method for the name variable:

public void setName(String newName) { name = newName; }
The word void in front of setName() indicates that the method does not return a value when it is invoked. The body is simple and copies the value of the parameter variable newName to instance variable name.

Add this method to the class and write a similar mutator method for the instance variable state.

7) Unit test the code you have written until you are convinced it is correct. compile and run the code below:

/** A class to test the Door class. */ public class DoorTester { /** Tests the methods of the Door class @param args not used */

Reference no: EM132075896

Questions Cloud

What is the smallest positive number representable : What is the smallest positive number (not including the +0) and largest negative number representable in 32-bit IEEE 754 single precision floating point?
Observe who is involved in the core business process : Your first course of action is to visit the restaurant and observe who is involved in the core business process, as well as the distinct business activities tha
Methodologies for business settings and situations : Determine appropriate project selection methodologies for business settings and situations
What amount of the purchase price : Plowman Company purchased land and a building for $430,000. What amount of the purchase price should Plowman Company allocate to the land and the building
Write the code to create a class that models a door object : Describe a door is its state:"open" or "closed". Properties of objects are described in code by using nouns like "state" or "name" to create instance variables.
What is the price per share of equity under plan : Destin Corp. is comparing two different capital structures. Plan I would result in 10,000 shares of stock and $90,000 in debt.
What is the present value of a annuity payment : What's the present value of a $900 annuity payment over five years if interest rates are 8 percent? (Do not round intermediate calculations).
What are the tax consequences, : She is not currently able to make payments on the loan. What are the tax consequences, if any, to Andrea in this situation
How much can miley except to receive for sale of these bonds : The discount rate for such securities is 10%. How much can Miley except to receive for the sale of these bonds? This is from financial accounting

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd