Java compiler error - java

I am getting a Exception in Thread main
java.lang.NoSuchMethodException: com.laurens.Main.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)
Can someone explain where I am going wrong here?
Main
package com.laurens;
public class Main {
private player player;
public Main(com.laurens.player player) {
this.player = player;
}
public com.laurens.player getPlayer() {
return player;
}
public void setPlayer (int performance, String name) {
if (performance < 4) {
boolean injured = true;
}
}
#Override
public String toString() {
return "com.laurens.Main{" +
"player=" + player +
'}';
}
}
player
package com.laurens;
/**
* Created by laurensvanoorschot on 20-01-16.
*/
public class player {
private String name;
private int performance;
private boolean injured;
public player(int performance, boolean injured, String name) {
this.injured = injured;
this.name = name;
this.performance = performance;
}
public boolean isInjured() {
return injured;
}
public void setInjured(boolean injured) {
this.injured = injured;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPerformance() {
return performance;
}
public void setPerformance(int performance) {
this.performance = performance;
}
}

You don't have a method called main, which is what it is looking for to run your program. Notice that when you create a template application for a java console application in intelliJ it has a method:
public static void main(string[] args) {
}
That needs to be there for your program to run.

Related

Comparing States in Java

In my project, we have to create a system where we create an object Facility and we have to run inspections and maintenance on it to check if it's working. We created a Facility object and then we created a state machine that changes the state of the Facility to resting, working, maintenance, or working.
Here is the SuperFacility class:
import java.util.Map;
public interface SuperFacility {
public void setName(String name);
public void setAddress(String address);
public void setDescription(String description);
public void setRefNumber(int refNumber);
public void setCapacity(int capacity);
public void setCost(double cost);
public void setProblemRate(int problemRate);
public String getName();
public String getAddress();
public String getDescription();
public int getRefNumber();
public int getCapacity();
public double getCost();
public int getProblemRate();
public void oxygenator(boolean oxygenator);
public void nuclearReactor(boolean nuclearReactor);
public void innerAirlocks(boolean innerAirlocks);
public void externalAirlocks(boolean externalAirlocks);
public void comms(boolean comms);
public void waterMaking(boolean waterMaking);
public void startMachines();
public Map getMap();
public void getFacilityStatus();
public void getFacilityStatus(Map<String, Boolean> map);
}
Here is the Facility class:
import java.util.*;
public class Facility extends StateMachine implements SuperFacility {
public String name, address, description;
public int refNumber, capacity, problemRate;
private double cost;
private Map<String, Boolean> map = new HashMap<String, Boolean>();
private boolean[] machines = new boolean[6];
private boolean oxygenator, innerAirlocks, externalAirlocks,
comms, nuclearReactor, waterMaking;
private final int numberOfMachines = 6; // Number of Machines inside Facility
// Setters
public void setName(String name){
this.name = name;
}
public void setAddress(String address){
this.address = address;
}
public void setDescription(String description){
this.description = description;
}
public void setRefNumber(int refNumber){
this.refNumber = refNumber;
}
public void setCapacity(int capacity){
this.capacity = capacity;
}
public void setCost(double cost){
this.cost = cost;
}
public void setProblemRate(int problemRate){
this.problemRate = problemRate;
}
// Getters
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public String getDescription(){
return description;
}
public int getRefNumber(){
return refNumber;
}
public int getCapacity(){
return capacity;
}
public double getCost(){
return cost;
}
public int getProblemRate(){
return problemRate;
}
public void oxygenator(boolean oxygenator){
this.oxygenator = oxygenator;
}
public void nuclearReactor(boolean nuclearReactor){
this.nuclearReactor = nuclearReactor;
}
public void innerAirlocks(boolean innerAirlocks){
this.innerAirlocks = innerAirlocks;
}
public void externalAirlocks(boolean externalAirlocks){
this.externalAirlocks = externalAirlocks;
}
public void comms(boolean comms){
this.comms = comms;
}
public void waterMaking(boolean waterMaking){
this.waterMaking = waterMaking;
}
public boolean[] getMachines(){
machines[0] = oxygenator;
machines[1] = nuclearReactor;
machines[2] = innerAirlocks;
machines[3] = externalAirlocks;
machines[4] = comms;
machines[5] = waterMaking;
return machines;
}
// Set machines to false
public void breakMachines(){
oxygenator(false);
nuclearReactor(false);
innerAirlocks(false);
externalAirlocks(false);
comms(false);
waterMaking(false);
map.clear();
initializeMap(map);
}
public void startMachines(){
// Set all of the booleans from this Facility to true;
// This booleans are what we call "the machines from the Facility"
oxygenator(true);
nuclearReactor(true);
innerAirlocks(true);
externalAirlocks(true);
comms(true);
waterMaking(true);
map.clear();
initializeMap(map);
}
public void initializeMap(Map<String, Boolean> map){
this.map.put("Oxygenator", oxygenator);
this.map.put("Inner Airlocks", innerAirlocks);
this.map.put("External Airlocks", externalAirlocks);
this.map.put("Nuclear Reactor", nuclearReactor);
this.map.put("Comms", comms);
this.map.put("WaterMaking", waterMaking);
}
public Map<String, Boolean> getMap(){
return map;
}
public void getFacilityStatus(){ // The status of the map in this object
for (Map.Entry<String, Boolean> i: map.entrySet()){
System.out.println(i.getKey() + ": " + i.getValue());
}
}
public void getFacilityStatus(Map<String, Boolean> map){ // The status of any Facility map
for (Map.Entry<String, Boolean> i: map.entrySet()){
System.out.println(i.getKey() + ": " + i.getValue());
}
}
}
Here is the StateMachine class:
public class StateMachine {
public State state = State.RESTING;
enum State {
WORKING, RESTING, MAINTENANCE, BROKEN
}
public State getFacilityState(){
return state;
}
public void setStateWorking(Facility fac){
fac.state = State.WORKING;
}
public void setStateResting(Facility fac){
fac.state = State.RESTING;
}
public void setStateMaintenance(Facility fac){
fac.state = State.MAINTENANCE;
}
public void setStateBroken(Facility fac) { fac.state = State.BROKEN;}
public State getState(){
return state;
}
}
In my Inspection class, I have two methods that have to check the state of the Facility to see if it's working, but I am having trouble with my if statement:
import java.util.*;
public class Inspection {
private Facility fac;
public boolean isBroken(){
if (fac.state == State.BROKEN)
return true;
else
return false;
}
public void makeMaintenanceRequest(Control c){
if (fac.state == State.BROKEN){
c.scheduleMaintenance(fac);
}
}
I want the methods to be able to compare the current state of a Facility to the Broken state. How should I compare the states? I keep getting the "cannot find symbol" error for State.BROKEN
Although I cannot understand very well what you're trying to do, I can tell you the Inspection class might already work as you'd like.
I see you commented out the constructor, why? It was okay to inject a Facility instance inside Inspection. However, you should accept a StateMachine instead.
public class Inspection {
private final StateMachine stateMachine;
public Inspection(final StateMachine stateMachine) {
this.stateMachine = stateMachine;
}
...
}
Then, inside your Inspection#isBroken method
public boolean isBroken() {
return this.stateMachine.getFacilityState() == State.BROKEN; // "this" not necessary
}
As Facility extends StateMachine, it exposes a getFacilityState() method.
And because Facility extends StateMachine, Inspection is able to accept it.
final Facility facility = new Facility(...);
final Inspection inspection = new Inspection(facility);
final boolean isBroken = inspection.isBroken();
A simple solution would be to replace State.BROKEN with StateMachine.State.BROKEN
public boolean isBroken(){
if (fac.state == StateMachine.State.BROKEN)
return true;
else
return false;
}

Guice AssistedInject can not find matching constructor

I just started learning Guice, but I've already encountered a problem. I have an interface PlayerFactory with one implementation BlackjackPlayer
PlayerFactory.java
public interface PlayerFactory {
Player createPlayer(String name);
Player createPlayer(String name, boolean isDealer);
}
BlackjackPlayer.java
public class BlackjackPlayer implements PlayerFactory {
private PointsCalculator pointsCalculator;
public BlackjackPlayer(){
pointsCalculator = new BlackjackPointsCalculator();
}
#Override
public Player createPlayer(String name) {
return new Player(pointsCalculator, name);
}
#Override
public Player createPlayer(String name, boolean isDealer) {
return new Player(pointsCalculator, name, isDealer);
}
}
Player.class
public class Player{
private PointsCalculator pointsCalculator;
private List<Card> cardsInHand;
private Integer points;
private String name;
private boolean isDealer;
private boolean endedTurn;
#AssistedInject
public Player(PointsCalculator blackjackPointsCalculator, String name){
pointsCalculator = blackjackPointsCalculator;
cardsInHand = new ArrayList<>();
points = 0;
this.name = name;
isDealer = false;
endedTurn = false;
}
#AssistedInject
public Player(PointsCalculator blackjackPointsCalculator, String name, boolean isDealer){
pointsCalculator = blackjackPointsCalculator;
cardsInHand = new ArrayList<>();
points = 0;
this.name = name;
this.isDealer = isDealer;
endedTurn = false;
}
public void addCardToHand(Card card) {
cardsInHand.add(card);
updatePoints();
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Player)) return false;
Player player = (Player) o;
return getPoints() == player.getPoints() &&
isDealer() == player.isDealer() &&
isEndedTurn() == player.isEndedTurn() &&
Objects.equals(pointsCalculator, player.pointsCalculator) &&
Objects.equals(getCardsInHand(), player.getCardsInHand()) &&
Objects.equals(getName(), player.getName());
}
#Override
public int hashCode() {
return Objects.hash(pointsCalculator, getCardsInHand(), getPoints(), getName(), isDealer(), isEndedTurn());
}
public void updatePoints() {
points = pointsCalculator.calculatePoints(cardsInHand);
}
public List<Card> getCardsInHand(){
return cardsInHand;
}
public Integer getPoints(){
return points;
}
public String getName(){
return name;
}
public boolean isDealer() {
return isDealer;
}
public boolean isEndedTurn() {
return endedTurn;
}
public void setName(String name){
this.name = name;
}
public void setDealer(boolean isDealer){
this.isDealer = isDealer;
}
public void setEndedTurn(boolean endedTurn){
this.endedTurn = endedTurn;
}
}
I want to use Guice assisted inject to create Player. Previously I did it as follows:
install(new FactoryModuleBuilder().build(PlayerFactory.class));
which I know is wrong way, because I receive error message:
1) com.github.blackjack.model.Player has #AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer(). Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)
2) com.github.blackjack.model.Player has #AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer(). Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)
I tried to add constructors Player(String name), Player(String name, boolean isDealer) but it didn't help. Does someone know what should I do to fix the problem?
Thanks in advance!
You need to use the #Assisted annotation on the injectee parameters:
PlayerFactory.java
public interface PlayerFactory {
Player createPlayer(String name);
Player createPlayer(String name, boolean isDealer);
}
BlackjackPlayer.java (Change it from a factory to the actual player)
public class BlackjackPlayer implements Player {
private final PointCalculator pointsCalculator;
private final String name;
private final boolean isDealer;
#AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, #Assisted String name) {
this.pointsCalculator = pointsCalculator;
this.name = name;
this.isDealer = false;
}
#AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, #Assisted String name, #Assisted boolean isDealer) {
this.pointsCalculator = pointsCalculator;
this.name = name;
this.isDealer = isDealer;
}
}
And use the module as the following:
install(new FactoryModuleBuilder()
.implement(Player.class, BlackjackPlayer.class)
.build(PlayerFactory.class)
);

can i print a setter value (sysout) or do i have to use only getters in order to get an output ? (java)

I have just recently learned about setter , getters and this.(somthing).
I having quite a hard time undersatnding when to use getters and when to use setters .
Another thing , can i use setter method to print out ?
For Example :
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
newAge = workerAge;
}
public void setWorkerName(String newName) {
newName = workerName;
}
public int setIde(int ide) {
ide = workerIde;
return ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde());
}
}
the system out print shows an error and i didnt understand why , is it because only getters can be used in the sysout command ?
No offense intended, but your setters are all wrong. You should assign your properties to the values passed in the setter, not setting the value again. So your code should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
}
If you need getters, it should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
public int getIde() {
return workerIde;
}
}
Then you can print, e.g. System.out.println(worker1.getIde());
You should be using a getter method to get the values.
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName=newName;
}
public int getIde() {
return workerIde;
}
public void setIde(int ide) {
workerIde = ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.getIde());
}
}
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
this.workerAge = newAge;
}
public void setWorkerName(String newName) {
this.workerName = newName;
}
public int setIde(int ide) {
this.workerIde = ide;
return this.workerIde;
}
}
public class Car {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde(56));
}
}

Trying to print object through toString()

I am trying to print the individual objects in the players class(performance, injured and name) and on the main class I am trying to print the entire player object however when I try executing the toString(); method on both classes, I just receive player#2eb3998c or Main#37e6e526. Where am I going wrong?
Thanks for any help.
Player class:
package com.laurens;
/**
* Created by laurensvanoorschot on 20-01-16.
*/
public class player {
private String name;
private int performance;
private boolean injured;
public player(int performance, boolean injured, String name) {
this.injured = injured;
this.name = name;
this.performance = performance;
}
public boolean isInjured() {
return injured;
}
public void setInjured(boolean injured) {
this.injured = injured;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPerformance() {
return performance;
}
public void setPerformance(int performance) {
this.performance = performance;
}
#Override
public String toString() {
return "com.laurens.player{" +
"injured=" + injured +
", name='" + name + '\'' +
", performance=" + performance +
'}';
}
}
main Class:
package com.laurens;
public class Main {
private player player;
public static void main(String[] args) {
player player = new player (4, true, "laurens");
player.toString();
}
public com.laurens.player getPlayer() {
return player;
}
#Override
public String toString() {
return super.toString();
}
public void setPlayer (int performance, String name) {
if (performance < 4) {
boolean injured = true;
}
}
}
Most likely, you simply forgot to recompile your classes; your Player code is fine (and you have errors in your Main class that you aren't aware of, which suggests no recompile). That said, there's nothing in your posted code that actually prints anything. System.out.println (actually, any PrintWriter print methods) will automatically call toString() on an object, so there's no need to do that yourself, just
System.out.println(player);
Use
System.out.println(player.toString());
toString() returns a string/textual representation of the object. How to use the toString method in Java?
Explicitly calling print, displays your properties fine:
System.out.println(player.toString());
as well as:
System.out.println(player);

Java FX populate tableview with specific model

Hello I have got a question about TableView in JavaFX and populating the table with data from an object in the model via a getter method of this object, which is part of the model .
First of all, here is my model:
package model;
import java.util.List;
public class Carmodel {
private int carmodelID;
private Cartype cartype;
private Manufacturer manufacturer;
private DrivingLicense drivingLicense;
private String label;
private int seats;
private int kw;
private String fuelType;
private double priceDay;
private double priceKM;
private int axes;
private int loadVolume;
private int loadCapacity;
private List<Equipment> equipmentList;
public Carmodel() {
}
public int getCarmodelID() {
return carmodelID;
}
public void setCarmodelID(int carmodelID) {
this.carmodelID = carmodelID;
}
public Cartype getCartype() {
return cartype;
}
public void setCartype(Cartype cartype) {
this.cartype = cartype;
}
public Manufacturer getManufacturer() {
return manufacturer;
}
public void setManufacturer(Manufacturer manufacturer) {
this.manufacturer = manufacturer;
}
public DrivingLicense getDrivingLicense() {
return drivingLicense;
}
public void setDrivingLicense(DrivingLicense drivingLicense) {
this.drivingLicense = drivingLicense;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public int getKw() {
return kw;
}
public void setKw(int kw) {
this.kw = kw;
}
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
public double getPriceDay() {
return priceDay;
}
public void setPriceDay(double priceDay) {
this.priceDay = priceDay;
}
public double getPriceKM() {
return priceKM;
}
public void setPriceKM(double priceKM) {
this.priceKM = priceKM;
}
public int getAxes() {
return axes;
}
public void setAxes(int axes) {
this.axes = axes;
}
public int getLoadVolume() {
return loadVolume;
}
public void setLoadVolume(int loadVolume) {
this.loadVolume = loadVolume;
}
public int getLoadCapacity() {
return loadCapacity;
}
public void setLoadCapacity(int loadCapacity) {
this.loadCapacity = loadCapacity;
}
public List<Equipment> getEquipmentList() {
return equipmentList;
}
public void setEquipmentList(List<Equipment> equipmentList) {
this.equipmentList = equipmentList;
}
As you can see there is a specific member (private Manufacturer manufacturer) It is an object from the type "Manufacturer". And the Manufacturer class looks like this:
public class Manufacturer {
private int manufacturerID;
private String name;
public Manufacturer() {
}
public int getManufacturerID() {
return manufacturerID;
}
public void setManufacturerID(int manufacturerID) {
this.manufacturerID = manufacturerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
This is my controller for the JavaFX View:
public class CarmodelController implements Initializable {
CarmodelRepository carmodelRepository;
#FXML public TableView CarmodelTable;
#FXML public TableColumn<Carmodel,Integer> tableColumnID ;
#FXML public TableColumn<Carmodel,String> tableColumnLabel ;
#FXML public TableColumn<Carmodel, String> tableColumnManufacturer ;
#FXML public TableColumn<Carmodel,String> tableColumnCartype ;
public void initialize(URL location, ResourceBundle resources) {
carmodelRepository= new CarmodelRepository();
List<Carmodel> carmodelList= carmodelRepository.readAll();
ObservableList<Carmodel> carmodelObservableList = FXCollections.observableArrayList(carmodelList);
tableColumnID.setCellValueFactory(new PropertyValueFactory<Carmodel, Integer>("carmodelID"));
tableColumnLabel.setCellValueFactory(new PropertyValueFactory<Carmodel, String>("label"));
tableColumnManufacturer.setCellValueFactory(new PropertyValueFactory<Carmodel, String>("manufacturer")
And here is the problem:
Can I do here something like PropertyValueFactory("manufacturer.getName()"); This way it didn't work. It just populate the column of the table with memory adress
So my question is:
How can I get the name of the manufacturer, normally, in other code, you can do this by calling the method: "manufacturer.getName();" and it will give you the String with the name of the manufacturer, but how can I do this while I will populate the table with these specific carmodels?
And the end of the controller code ( filling the Table with values).
CarmodelTable.setItems(carmodelObservableList);
}
Thank you in advance!
You can do
tableColumnManufacturer.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue().getManufacturer().getName());
The setCellValueFactory method expects a Callback<CellDataFeatures<Carmodel, String>, ObservableValue<String>> object. Hence cellData in this code is a CellDataFeatures<Carmodel, String> object, and cellData.getValue() gives the CarModel object for the row. Then cellData.getValue().getManufacturer().getName() gives the value you want; you just have to wrap it in a ReadOnlyObservableWrapper to get an ObservableValue<String> containing that value.

Categories