Reputation: 25
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;
}
}
Upvotes: 0
Views: 151
Reputation: 4541
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.
Upvotes: 2