Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 2.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

Lab 2.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. Starting with a new NetBeans Java application (project), add a class that models a door object (see the demo on How to define a Java class in NetBeans). Dont worry about the internal details just yet. Just give the class a name (Door) and an empty body for the moment. We will add more to the class shortly. Make sure your new class is in the same package as the main class.

Lab 2.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 orSide to distinguish it from other doors. Another property that could describe a door is its state: open or closed (or, open ornot-open). Properties of objects are described in code by using nouns like state or name to create instance variables that hold values. Add instance (member) variables to your Door class for the name of the door and its state. Note that we declare state as aboolean since it's either open or closed (not open):

public class Door { String name; boolean state; // true for open, false for closed } // end class Door

Lab 2.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 true (open) to false(closed). Declare member methods for open and close:

public class Door { String name; boolean state; // true for open, false for closed public void open() { state = true; } // end open method public void close() { state = false; } } // end class Door

Lab 2.4

Switch back to the main (application) class and add code to the main method that does the following:

Declare/create an array doors of type Door with size (length) 3.

Set the first element of the doors array to an open Side door.

Set the second element of the doors array to a closed Front door.

Set the third element of the doors array to a closed Back door.

Display the doors and their states. Maybe it would be a good idea to add an output method to the door class? Consider: public void output() { System.out.print(name); System.out.print(": "); if (state) { System.out.println("open"); } else { System.out.println("closed"); } }

Using a loop, switch the states of the doors in the doors array; i.e., if a door is open, then close it, and vice-versa.

Re-display the doors and their states.

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

Handbook Of Database Security Applications And Trends

Authors: Michael Gertz, Sushil Jajodia

1st Edition

1441943056, 978-1441943057

More Books

Students also viewed these Databases questions

Question

7. Discuss the implications of a skill-based pay plan for training.

Answered: 1 week ago

Question

4. Make recommendations on steps to take to melt the glass ceiling.

Answered: 1 week ago