Question

This is the Java Object-oriend project. Please show the detail code and comment for each class....

This is the Java Object-oriend project. Please show the detail code and comment for each class. Thank you


Create a class Door with the following UML specification:
+---------------------+
| Door |
+---------------------+
| - isOpen: boolean |
+---------------------+
| + Door() |
| + isOpen(): boolean |
| + open(): void |
| + close(): void |
| + testDoor(): void |
+---------------------+
where isOpen is an instance variable indicating whether the door is currently open or not. The default state of a door when it is created is to be closed.
Then create a class Car with the following UML specification:
+---------------------------------------+
| Car |
+---------------------------------------+
| - name: String |
| - doors: Door[] |
+---------------------------------------+
| + Car(String name, int numberOfDoors) |
| + listDoors(): void |
| + countOpenDoors(): int |
| + openOneDoor(int doorNumber): void |
| + testCar(): void |
+---------------------------------------+
Trying to create a car with less than one door must throw a BadCarException with the message "A car must have at least one door!". When a car is created, all its doors are closed.
The method listDoors prints one line of output for each door of the car. Each line starts with the name of the car followed by a message indicating whether the door is currently open or closed. For example, if a car has the name "Tiny" and has two doors, one which is currently open and one which is currently closed, then calling the listDoors method of the car object should produce the following output:
Tiny: door is open
Tiny: door is closed

The method countOpenDoors counts the number of doors of the car which are currently open.
The method openOneDoor opens a specific door, as indicated by the door's number. Doors are numbered starting
from 1 (not 0!) Trying to open a nonexistent door should throw a BadDoorException with the message "Door
XXX does not exist!
", where XXX is replaced with the number of the nonexistent door. Trying to open a door
which is already open does nothing.
Use enhanced for loops as much as possible instead of using normal for loops.

// This is the Start class contain the main method
public class Start {

   public static void main(String[] args) throws BadCarException, BadDoorException {
       // Testing each class' testclass
       Door.testDoor();
       System.out.println();
       Car.testCar();
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Door {
        private boolean isOpen;

        public Door() {
                this.isOpen = false;
        }

        public boolean isOpen() {
                return isOpen;
        }

        public void close() {
                this.isOpen = false;
        }
        public void open() {
                this.isOpen = true;
        }
        
        public void testDoor() {
                if(isOpen) {
                        System.out.println("door is open");
                } else {
                        System.out.println("door is closed");
                }
        }
}

class Car {
        private String name;
        private Door doors[];
        
        public Car(String name, int numberOfDoors) {
                if(numberOfDoors < 1) {
                        throw new BadCarException("A car must have at least one door!");
                }
                this.name = name;
                doors = new Door[numberOfDoors];
                for(int i=0; i<numberOfDoors; i++) {
                        doors[i] = new Door();
                }
        }
        
        public void listDoors() {
                for(Door d: doors) {
                        System.out.print(name + ": ");
                        d.testDoor();
                }
        }
        
        public int countOpenDoors() {
                int count = 0;
                for(Door d: doors) {
                        if(d.isOpen()) {
                                count++;
                        };
                }
                return count;
        }
        
        public void openOneDoor(int i) {
                i = i - 1;
                
                if(i < 0 || i >= doors.length) {
                        throw new BadDoorException("Door " + (i+1) + " does not exist!");
                }
                
                doors[i].open();
        }
        
        public void testCar() {
                listDoors();
        }
}



public class Start {

   public static void main(String[] args) throws BadCarException, BadDoorException {
       // Testing each class' testclass
       Door d = new Door();
           d.testDoor();
       System.out.println();
       Car c = new Car("Tiny", 3);
           c.testCar();
   }

}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
This is the Java Object-oriend project. Please show the detail code and comment for each class....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

  • Design a restaurant class to help manage multiple restaurants and their menus equiremen The class...

    program is C# Design a restaurant class to help manage multiple restaurants and their menus equiremen The class contains: 1) A static int variable named storeCounter that starts at 1 and increments for each new restaurant item that is created 2) A string array called Menu 3) An int variable called StoreID 4) A Boolean variable called isOpen 5) A no-argument constructor that performs the following a. Sets storelD to storeCounter, then increments storeCounter b. Sets isOpen to true c....

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • please write code in java, assignment is due soon Thanks. public interface Named The Named interface...

    please write code in java, assignment is due soon Thanks. public interface Named The Named interface is a simple interface which applies to anything which has a name. The only thing required is the following method: String name() which returns the name of the instance. public interface MessageSink, It includes: void deliver(Named sender, String message) delivers the specified message from the given sender. public interface Sharable, which is Named, It includes no method. public abstract class GeneralCapability It incudes: The...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void...

    Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void +setWeight(double):void +compareName(String):boolean +compareTo(Horse):int +toString():String Team -team:ArrayList<Horse> +Team():void +addHorse(String, int, double):void +addHorse(Horse):void +averageAge():int +averageWeight():int +getHorse(int):Horse +getSize():int +findHorse(String):Horse +largestHorse():Horse +oldestHorse():Horse +removeHorse(String):void +smallestHorse():Horse +toString():String +youngestHorse():Horse HorseTeamDriver +main(String[]):void Create a class (Horse from the corresponding UML diagram) that handles names of horse, the age of the horse, and the weight of the horse. In addition to the standard accessor and mutator methods from the instance fields, the class...

  • How can the java code be edited that when someone exits the lot, they receive an...

    How can the java code be edited that when someone exits the lot, they receive an exiting timestamp. And also i would need that exit timestamp to be subtracted from the timestamp the driver gets when entering the lot to find out the total amount of time they were in the parking lot. please explain. should only need a couple of lines added. //////////////////////////////ParkingCarInfo.java/////////////////////////////////////////// package test; import java.sql.Timestamp; //Class: ParkingCarInfo public class ParkingCarInfo { private String name; //name private Timestamp...

  • ******Java Programming Hi guys, I really need you help. I created a code for my java...

    ******Java Programming Hi guys, I really need you help. I created a code for my java course, but it keep giving me error messages. Majority of my code is fine but some keep display error on my console. I was hoping someone could pin points the problem. .There are three classes with the testCenter class being the main class. In the following is the assignment, and the bottom is my code. Please help! Assignment: Concepts: GUI User Design Graphics Deployment...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT