Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree3Likes

Thread: Class troubble

  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Smile 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.

    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();
        }
      }
    }
    and here is the error messages:

    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
    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.
    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

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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.

  3. #3
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default 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

    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;
    	}
    and also been trying to fix it to boolean A and boolean B with no luck there.
    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.

  4. #4
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default 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;
    	}

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Class troubble

    Java Code:
    public Person(String Navn, Blod navn = new Blod(A,B)) {
    Parameters go between the brackets ( ) not lines of code. Have you tried moving that inside the body of the constructor?

  6. #6
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: Class troubble

    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();
    	}
    gave me tons of error messages.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.
    Java Code:
    Foo f = new Foo("one", "two");
    Bar b = new Bar(f);
    //constructor
    public Bar (Foo f) {
        ....
    }
    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:
    Bar b = new Bar("one", "two");
    //constructor
    public Bar (String x, String y) {
        Foo f = new Foo(x, y);
    }
    What you have is just a mish-mash of code all over the place.
    Juukamen likes this.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.
    Juukamen likes this.

  9. #9
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: Class troubble

    Quote Originally Posted by Junky View Post
    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.
    Java Code:
    Foo f = new Foo("one", "two");
    Bar b = new Bar(f);
    //constructor
    public Bar (Foo f) {
        ....
    }
    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:
    Bar b = new Bar("one", "two");
    //constructor
    public Bar (String x, String y) {
        Foo f = new Foo(x, y);
    }
    What you have is just a mish-mash of code all over the place.
    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. #10
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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).

  11. #11
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default 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) { ... }
    DiamondSoul likes this.

  12. #12
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: Class troubble

    yeh, but how do that work on strings ?

  13. #13
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Class troubble

    It doesn't. Was thinking more of your getType() method.

  14. #14
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: Class troubble

    Quote Originally Posted by Junky View Post
    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.
    fixed.

    but i've found one interessting error that i cannot get to understand.

    Java Code:
    else if (blodet2.equals("O") && (blodet3.equals("O") || blodet3.equals("A") || blodet3.equals("B") || blodet3.equals("AB")))
    now, if blodet2 = O and blodet3 = O why do I get one false ?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Class troubble

    It works for me:

    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");
          }
       }
    Moral: show us compilable and runnable code that proves your assertion.

  16. #16
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: Class troubble

    *post deleted* need to have some better look on my code here =) brb

  17. #17
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default 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.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  19. #19
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsø
    Posts
    54
    Rep Power
    0

    Default Re: Class troubble

    ok, here is the complete code. and works as it should i hope =)

    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();
        }
      }
    }
    now i've not done the (A && !B) tho as suggested.
    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")
    "

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 06:49 AM
  2. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  3. Replies: 8
    Last Post: 04-14-2010, 06:49 AM
  4. Replies: 7
    Last Post: 05-06-2009, 01:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •