Results 1 to 20 of 22
Thread: Class troubble
- 10-25-2011, 05:24 PM #1
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Class troubble
this is one school exercice to make one program to find out the blood types and whom can donate blood to whom.
Since the class is in norwegian so the variables etc is in norwegian, my deepest apologies for that.
here is the code i've gotten so far.
and here is the error messages:Java Code:class Blod { private final boolean A; private final boolean B; public Blod(boolean A, boolean B) { this.A = A; this.B = B; } public String getType() { String blodType = null; if( A == true) { blodType = "A"; } else if( B == true) { blodType = "B"; } else if ( A == false && B == false ) { blodType = "O"; } else if (A == true && B == true) { blodType = "AB"; } return blodType; } } class Person { private final String Navn; private Blod blodet; public Person(String Navn, Blod blodet) { this.Navn = Navn; this.blodet = blodet; } public String getNavn() { return Navn; } public String getType() { String blodet1 = blodet.getType(); return blodet1; } public boolean kanGiBlodTil(Person annen) { String blodet2 = getType(); String blodet3 = annen.getType(); boolean giblod; if (blodet2.equals("A") && (blodet3.equals("A") || blodet3.equals("AB"))) { giblod = true; } else if (blodet2.equals("B") && (blodet3.equals("B") || blodet3.equals("AB"))) { giblod = true; } else if (blodet2.equals("O")) { giblod = true; } else { giblod = false; } return giblod; } } class Blodgivingstest { public static void main(String[] args) { Person[] personene = new Person[4]; // Lager tabell med plass til fire Person-objekter personene[0] = new Person("Mann 1", false, false); personene[1] = new Person("Mann 2", true, false); personene[2] = new Person("Mann 3", false, true); personene[3] = new Person("Mann 4", true, true); for (int i = 0; i < personene.length; i++) { System.out.println(personene[i].getNavnet() + " har blodtype " + personene[i].finnType()); } System.out.println(); for (int i = 0; i < personene.length; i++) { for (int j = 0; j < personene.length; j++) { if (personene[i].kanGiBlodTil(personene[j])) { System.out.println(personene[i].getNavnet() + " kan gi blod til " + personene[j].getNavnet()); } else { System.out.println(personene[i].getNavnet() + " kan IKKE gi blod til " + personene[j].getNavnet()); } } System.out.println(); } } }
not i've sendt some message to teacher and i got two options, 1 to change the "Blodgivingstest" and two to fix the "Person" constructor.Java Code:G:\Coding\Skole\Øving 8\blod.java:75: error: constructor Person in class Person cannot be applied to given types; personene[0] = new Person("Mann 1", false, false); ^ required: String,Blod found: String,boolean,boolean reason: actual and formal argument lists differ in length G:\Coding\Skole\Øving 8\blod.java:76: error: constructor Person in class Person cannot be applied to given types; personene[1] = new Person("Mann 2", true, false); ^ required: String,Blod found: String,boolean,boolean reason: actual and formal argument lists differ in length G:\Coding\Skole\Øving 8\blod.java:77: error: constructor Person in class Person cannot be applied to given types; personene[2] = new Person("Mann 3", false, true); ^ required: String,Blod found: String,boolean,boolean reason: actual and formal argument lists differ in length G:\Coding\Skole\Øving 8\blod.java:78: error: constructor Person in class Person cannot be applied to given types; personene[3] = new Person("Mann 4", true, true); ^ required: String,Blod found: String,boolean,boolean reason: actual and formal argument lists differ in length G:\Coding\Skole\Øving 8\blod.java:81: error: cannot find symbol System.out.println(personene[i].getNavnet() ^ symbol: method getNavnet() location: class Person G:\Coding\Skole\Øving 8\blod.java:82: error: cannot find symbol + " har blodtype " + personene[i].finnType()); ^ symbol: method finnType() location: class Person G:\Coding\Skole\Øving 8\blod.java:90: error: cannot find symbol System.out.println(personene[i].getNavnet() ^ symbol: method getNavnet() location: class Person G:\Coding\Skole\Øving 8\blod.java:91: error: cannot find symbol + " kan gi blod til " + personene[j].getNavnet()); ^ symbol: method getNavnet() location: class Person G:\Coding\Skole\Øving 8\blod.java:93: error: cannot find symbol System.out.println(personene[i].getNavnet() ^ symbol: method getNavnet() location: class Person G:\Coding\Skole\Øving 8\blod.java:94: error: cannot find symbol + " kan IKKE gi blod til " + personene[j].getNavnet()); ^ symbol: method getNavnet() location: class Person 10 errors Tool completed with exit code 1
changing the client would be boring, and i'm wondering how the Person constrictor could be changed to take two boolean preferences. ?
Again, apologies the language and for the "simple" problem...
.Tor
- 10-25-2011, 07:14 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Class troubble
The Person constructor could indeed be changed to take two boolean preferences (as well as a String). Right now, the constructor for Person takes a String and a Blod, and the constructor for Blod takes two booleans. Using these two facts, it shouldn't be too hard to figure out how to make the constructor for Person take a String and two booleans. As a hint, remember that you can call one class's constructor within a different class's constructor.
- 10-25-2011, 11:15 PM #3
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
i've been looking around and in my book, aparently NOT covering that and pissing me off.
and i've been testing out, but to call from the Blod class that got two boolean variables. from the constructor of Person is annoying.
I've been thinking like
and also been trying to fix it to boolean A and boolean B with no luck there.Java Code:class Person { private final String Navn; private Blod blodet; public Person(String Navn, Blod A, Blod B) { this.Navn = Navn; this.blodet = blodet; }
as i see the Blod class also needs one name or will there be one deafult name in there ?
had a few looks into Trail: Learning the Java Language (The Java™ Tutorials) also but didn't get any wiser there. but downloaded it all for later use when i'm offline.
the last desperate try have been ""Java Code:public Person(String Navn, Blod navn = new Blod(A,B))
quite annoying.
- 10-25-2011, 11:19 PM #4
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
Been looking around and also in my book but no luck on calling the other class Blod constructor from the Person class constructor. this is what i've managed to come up with so far in my wicked mind.
Java Code:class Person { private final String Navn; private Blod blodet; public Person(String Navn, Blod navn = new Blod(A,B)) { this.Navn = Navn; this.blodet = blodet; }
- 10-25-2011, 11:28 PM #5
Re: Class troubble
Parameters go between the brackets ( ) not lines of code. Have you tried moving that inside the body of the constructor?Java Code:public Person(String Navn, Blod navn = new Blod(A,B)) {
- 10-26-2011, 12:13 AM #6
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
gave me tons of error messages.Java Code:class Person { private final String Navn; private Blod blodet; public Person(String Navn,Blod Navn(boolean A, boolean B)) { this.Navn = Navn; this.blodet = Blod Navn = new Blod(); }
- 10-26-2011, 12:21 AM #7
Re: Class troubble
You are just guessing. Have you ever seen example code that looks like what you have?
The first thing you need to get straight in your mind is should a Blod object be passed to the Person constructor or should the data needed to create a Blod object be passed?
If you want to pass a Blod object then it need to be create elsewhere and passed as a parameter.
If you want the data for an object to be passed then you need to pass that data as parameters and then inside the constructor create the object.Java Code:Foo f = new Foo("one", "two"); Bar b = new Bar(f); //constructor public Bar (Foo f) { .... }
What you have is just a mish-mash of code all over the place.Java Code:Bar b = new Bar("one", "two"); //constructor public Bar (String x, String y) { Foo f = new Foo(x, y); }
- 10-26-2011, 04:46 AM #8
Re: Class troubble
I just took a look at your code in the getType method and realised it is impossible for someone to be AB. Check your logic very carefully and see if you can see why.
- 10-26-2011, 06:11 PM #9
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
I have not seen example code of those things mate. Not in my book that I got nor anywhere else and I did google a little around but had no idea on what to search for and that was not helping my case either.
I did PM my teacher also and I got things a little sorted out now. Here is what I've done so far. I've not gone over my logical settings yet, will do those next.
I hope you didn't rip off all of your hairs on this thread and i would like to thank for pointing out mistakes and not just completing it as it should be.
https://pastebin.com/uYPkqy4B
.Juuka
- 10-26-2011, 10:57 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Class troubble
You now have the constructor thing right, at least. Does the program now work the way you want it to? (I'm guessing not, since, as Junky pointed out, your code will never reach the condition to check if someone is AB).
- 10-26-2011, 11:09 PM #11
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Class troubble
Minor thing which you might like to note: a boolean variable by itself is sufficient to control a conditional statement.
Java Code:boolean a = true, b = false; //the following two statements are equivalent if (a == true && b == false) { ... } if (a && !b) { ... }
- 10-26-2011, 11:22 PM #12
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
yeh, but how do that work on strings ?
- 10-26-2011, 11:25 PM #13
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Class troubble
It doesn't. Was thinking more of your getType() method.
- 10-27-2011, 05:50 PM #14
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
fixed.
.gif)
but i've found one interessting error that i cannot get to understand.
now, if blodet2 = O and blodet3 = O why do I get one false ?Java Code:else if (blodet2.equals("O") && (blodet3.equals("O") || blodet3.equals("A") || blodet3.equals("B") || blodet3.equals("AB")))
-
Re: Class troubble
It works for me:
Moral: show us compilable and runnable code that proves your assertion.Java Code:public static void main(String[] args) { String blodet2 = "O"; String blodet3 = "O"; if (blodet2.equals("O") && (blodet3.equals("O") || blodet3.equals("A") || blodet3.equals("B") || blodet3.equals("AB"))) { System.out.println("this is true"); } else { System.out.println("this is false"); } }
- 10-27-2011, 06:20 PM #16
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
*post deleted* need to have some better look on my code here =) brb
- 10-27-2011, 06:24 PM #17
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Class troubble
Look at your set of if statements in the kanGiBlodTil() method. You have one to check if the first person is type A, one to check if they're B, on to chick if they're O, and an else which returns false. You have none to check if they're AB.
-
Re: Class troubble
Mann 4 has "AB" blood type not "O", and your logic fails for if blodet2 is "AB"
And please do not delete your posts. This is not fair to us and to others.
- 10-27-2011, 06:28 PM #19
Member
- Join Date
- Oct 2011
- Location
- Tromsø
- Posts
- 41
- Rep Power
- 0
Re: Class troubble
ok, here is the complete code. and works as it should i hope =)
now i've not done the (A && !B) tho as suggested.Java Code:class Blod { private final boolean A; private final boolean B; public Blod(boolean A, boolean B) { this.A = A; this.B = B; } public String getType() { String blodType = null; if( A == true && B == false) { blodType = "A"; } else if( B == true && A == false) { blodType = "B"; } else if ( A == false && B == false ) { blodType = "O"; } else if (A == true && B == true) { blodType = "AB"; } return blodType; } } class Person { private final String Navn; private Blod blodet; public Person(String Navn, Blod blodet) { this.Navn = Navn; this.blodet = blodet; } public String getNavnet() { return Navn; } public String finnType() { String blodet1 = blodet.getType(); return blodet1; } public boolean kanGiBlodTil(Person annen) { String blodet2 = finnType(); String blodet3 = annen.finnType(); boolean giblod; if (blodet2.equals("A") && (blodet3.equals("A") || blodet3.equals("AB"))) { giblod = true; } else if (blodet2.equals("B") && (blodet3.equals("B") || blodet3.equals("AB"))) { giblod = true; } else if (blodet2.equals("O") && (blodet3.equals("O") || blodet3.equals("A") || blodet3.equals("B") || blodet3.equals("AB"))) { giblod = true; } else if (blodet2.equals("AB") && blodet3.equals("AB")) { giblod = true; } else { giblod = false; } return giblod; } } class Blodgivingstest { public static void main(String[] args) { Person[] personene = new Person[4]; // Lager tabell med plass til fire Person-objekter personene[0] = new Person("Mann 1", new Blod(false, false)); // O personene[1] = new Person("Mann 2", new Blod(true, false)); // A personene[2] = new Person("Mann 3", new Blod(false, true)); // B personene[3] = new Person("Mann 4", new Blod(true, true)); // AB for (int i = 0; i < personene.length; i++) { System.out.println(personene[i].getNavnet() + " har blodtype " + personene[i].finnType()); } System.out.println(); for (int i = 0; i < personene.length; i++) { for (int j = 0; j < personene.length; j++) { if (personene[i].kanGiBlodTil(personene[j])) { System.out.println(personene[i].getNavnet() + " kan gi blod til " + personene[j].getNavnet()); } else { System.out.println(personene[i].getNavnet() + " kan IKKE gi blod til " + personene[j].getNavnet()); } } System.out.println(); } } }
But i'm wondering about the string comparion. is there a way to have something like ""Java Code:blod2.equals(""A" || "B") && blod3.equals("O", || "A")
-
Re: Class troubble
Again, please undelete your deleted post above. This thread is not just for you but for all who search this information in the future, and our replies don't make sense without this post.
Similar Threads
-
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks