Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #2155

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Develop #2155

Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/Bulldozer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

public class Bulldozer extends Machine {

public Bulldozer(String machine) {
super(machine);
}

@Override
public void doWork() {
System.out.println("Bulldozer" + " " + machine + " " + "is moving earth.");
}

@Override
public void stopWork() {
System.out.println("Bulldozer" + " " + machine + " " + "has finished moving earth.");
}

@Override
public String toString() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'machineType' field is not defined in this class. If 'machineType' is a field in the parent class, it should be accessed using 'super.machineType'. Or better yet, since this class already represents a specific machine type (Bulldozer), 'machineType' is not needed in the toString() method. You can return "Bulldozer" instead.

return "Bulldozer";
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Excavator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Excavator extends Machine {

public Excavator(String machine) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All constructors & toString methods should be removed.

super(machine);
}
MrGizmen marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void doWork() {
System.out.println("Excavator" + " " + machine + " " + "started its work.");
}

@Override
public void stopWork() {
System.out.println("Excavator" + " " + machine + " " + "stopped working.");

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read previous comments about these methods.


@Override
public String toString() {
return "Excavator";
}
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/Machine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax;

public abstract class Machine {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how this class should look like:

public abstract class Machine {
    public abstract void doWork();

    public abstract void stopWork();
}

protected String machine;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field 'machine' is somewhat ambiguous. Consider renaming it to something more descriptive that reflects its purpose in the context of a 'Machine', such as 'machineType' or 'machineName'.


public Machine(String machine) {
this.machine = machine;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor parameter 'machine' should be renamed for clarity. If this is intended to represent the type or name of the machine, consider renaming the parameter to 'machineType' or 'machineName' to match the suggested field name change.


public abstract void doWork();

public abstract void stopWork();
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/MainApp.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package core.basesyntax;

import java.util.Arrays;

public class MainApp {
public static void main(String[] args) {

Machine truck = new Truck("Mikki");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructors for Truck, Bulldozer, and Excavator classes are not defined in the task. If these classes have constructors that take parameters, mention it in the task definition. If not, use default constructors instead.

Machine bulldozer = new Bulldozer("Eduardo");
Machine excavator = new Excavator("Maks");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Mateusz mentioned in the previous comment, you should remove all constructors from Machine child classes. If Java class doesn't have any explicitly defined constructor it will use the default one, so in this case you will be able to create Truck/Bulldozer/Excavator instances like this: Machine truck = new Truck(); Notice that in this case you don't have to provide "name" of the machine, because you are able to use default constructor, i.e. new Truck(); That's why you should remove "name" fields in all child classes and delete a constructors you created.


Machine[] machines = {truck, bulldozer, excavator};
printTotal(machines);
}

private static void printTotal(Machine[] machines) {
for (Machine machine : machines) {
machine.doWork();
machine.stopWork();
}
System.out.println(Arrays.toString(machines));
MrGizmen marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The System.out.println(Arrays.toString(machines)); does not provide an informative message about the machines. It's better to have messages that indicate what type of machine is working. Consider overriding the toString method in each Machine subclass to provide more informative output.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is unnecessary. The printTotal method is supposed to invoke doWork() and stopWork() on each machine, there is no need to print an array of Machines.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, should only output this?

Truck Mikki started its work.
Truck Mikki stopped working.
Bulldozer Eduardo started its work.
Bulldozer Eduardo stopped working.
Excavator Maks started its work.
Excavator Maks stopped working.
image

}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Truck extends Machine {

public Truck(String machine) {
super(machine);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove constructor and "name" field to be able to create instances without providing a name


@Override
public void doWork() {
System.out.println("Truck" + " " + machine + " " + "started its work.");
}

@Override
public void stopWork() {
System.out.println("Truck" + " " + machine + " " + "stopped working. ");

}

@Override
public String toString() {
return "Truck";
}
}
Loading