0

This is my code

class TestResepter {
    public static void main(String[] args) {
        Legemiddel legemiddel = new Legemiddel("Ibuton", 200, 30.5);
        Lege lege = new Lege("Petter");
        Militærresepter militærresepter = new Militærresepter();
        Presepter presepter = new Presepter();
        BlaaResepter blaaresepter = new BlaaResepter();
        Resept resept=new Resept(legemiddel, lege, 650, 21);
    }
}

Legemiddel, Lege, Militærresepter, Presepter, BlaaResepter and Resept are other classes. However, I get this error:

testresepter.java:3: error: constructor Legemiddel in class Legemiddel cannot be applied to given types;
    Legemiddel legemiddel=new Legemiddel("Ibuton", 200, 30.5);
                          ^
  required: no arguments
  found: String,int,double
  reason: actual and formal argument lists differ in length
testresepter.java:4: error: constructor Lege in class Lege cannot be applied to given types;
    Lege lege=new Lege("Petter");
              ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length
testresepter.java:8: error: constructor Resept in class Resept cannot be applied to given types;
    Resept resept=new Resept(legemiddel, lege, 650, 21);
                  ^
  required: no arguments
  found: Legemiddel,Lege,int,int
  reason: actual and formal argument lists differ in length
3 errors

Why is that? What do I have to do to make it work?

This is the class Legemiddel

class Legemiddel {
    static int Id=-1;
    static String navnet;
    static double prisen;
    static double virkestoffet;

    public static void main(String navn, double pris, double virkestoff) {
        Id++;
        navnet = navn;
        prisen = pris;
        virkestoffet=virkestoff;
    }
}
QBrute
  • 4,405
  • 6
  • 34
  • 40
pkrist
  • 25
  • 3
  • 6
    Your `Legemiddel` constructor (not posted) doesn't take any arguments. Call `new Legemiddel();` (no arguments). – Elliott Frisch Feb 26 '18 at 13:54
  • 1
    [The Java Tutorials - Providing Constructors for Your Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html) – OH GOD SPIDERS Feb 26 '18 at 13:55
  • If you look at the edited version, I have posted Legemiddel. So I think the question is how do I implement the params, if not as above? – pkrist Feb 26 '18 at 14:11

2 Answers2

2

As of now, your class Legemiddel doesn't have a constructor that takes 3 arguments. I think you're confusing how to create a constructor and how to write a main method.

You're calling new Legemiddel("Ibuton", 200, 30.5);, so your class should have a constructor that looks like this:

public Legemiddel(String navn, double pris, double virkestoff) {
    ...
}

Also, another potential source of error is, that you probably want to have instance variables instead of all static class variables. Otherwise, when you're creating multiple instances of Legemiddel, it would change all values of all previous created instances.

For example:

Legemiddel l1 = new Legemiddel("Test", 10, 20);
Legemiddel l2 = new Legemiddel("Oops", 0, 0);

System.out.println(l1.Id); // Would print 1 instead of 0
System.out.println(l1.navnet); // Would print Oops instead of Test
System.out.println(l1.prisen); // Would print 0.0 instead of 10.0
System.out.println(l1.virkestoffet); // Would print 0.0 instead of 20.0

So your class should instead look like

class Legemiddel {
    private static int ID_GEN = -1;

    private final int id;
    private final String navnet;
    private final double prisen;
    private final double virkestoffet;

    public Legemiddel(String navn, double pris, double virkestoff) {
        id = ++ID_GEN;
        navnet = navn;
        prisen = pris;
        virkestoffet = virkestoff;
    }

    public int getId() {
        return id;
    }
    public String getNavnet() {
        return navnet;
    }
    public double getPrisen() {
        return prisen;
    }
    public double getVirkestoffet() {
        return virkestoffet;
    }
}

ID_GEN is now a class variable, which is "shared" among all instances of Legemiddel. In this case it is desirable, because you want to have a common generator for each instance that retains its state between different instantiations. If you want each instance to have its own id though, you need a separate instance variable id, that is not shared, but unique to each instance.

All other fields should be instance variables (no static), i.e. they belong to one and only one instance. If they remain static you'll run into trouble when creating multiple instances of Legemiddel, because they would share the same values of navnet, prisen and virkestoffet across all instances like I've shown in the code before.

Now, with the changed class, you don't have this kind of behaviour anymore:

Legemiddel l1 = new Legemiddel("Test", 10, 20);
Legemiddel l2 = new Legemiddel("Oops", 0, 0);

System.out.println(l1.getId()); // Prints 0
System.out.println(l1.getNavnet()); // Prints "Test"
System.out.println(l1.getPrisen()); // Prints 10.0
System.out.println(l1.getVirkestoffet()); // Prints 20.0

Here you can read more about class members.

QBrute
  • 4,405
  • 6
  • 34
  • 40
1

Try to implement Legemiddel constructor with given 3 params :)

Chorizzo
  • 68
  • 1
  • 11
  • If you look at the edited version, I have posted Legemiddel. So I think the question is how do I implement the params, if not as above? – pkrist Feb 26 '18 at 14:10
  • Replace `static void main` by Legemidel, that is the constructor. And remove all `static` – Joop Eggen Feb 26 '18 at 14:12
  • I have tried that, but then the subclasses that extends Legemiddel doesnt work – pkrist Feb 26 '18 at 14:16
  • @pkrist instead of randomly trying stuff you should probably read up on how constructors work and understand that befor continuing. I provided you a link to the oracle tutorial but there are dozens of good tutorials all over the internet. – OH GOD SPIDERS Feb 26 '18 at 14:18
  • I have read it, and made it work. But it says nothing about subclasses, and if I use class Legemiddel{public Legemiddel{}} it works, bur the subclasses extending the class Legemiddel dont. Cant find anything on Oracle about subclasses – pkrist Feb 26 '18 at 14:23
  • @pkrist [Here](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html), [here](https://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html) and [here](https://docs.oracle.com/javase/tutorial/java/IandI/summaryinherit.html). I stopped because I found three. I could find more. But you should work on your searching. – Elliott Frisch Feb 26 '18 at 14:32
  • This might help: [Java error: Implicit super constructor is undefined for default constructor](https://stackoverflow.com/questions/1197634/java-error-implicit-super-constructor-is-undefined-for-default-constructor) – OH GOD SPIDERS Feb 26 '18 at 14:35