Results 1 to 2 of 2

Thread: UML to Java question

  1. #1
    Join Date
    Apr 2010
    Posts
    1

    UML to Java question

    I have a UML that I have to convert to Java code. The only thing I cannot figure out is how to call method op2() in class B from classes C and D. If I create an object of class B, then the whole program goes into an infinite loop. And I cannot even make the method op2() a class method. Can anyone tell me where I need to correct the code?



    Code:
    public class B extends A {
      public static void main(String args[]) {
        new B();
      }
      private C c;
      private D d;
      public B() {
        System.out.println("B()");
        c = new C(this);     // 1:
        d = new D(this);     // 2:
        c.op3(d);            // 3:
        d.op4(c);            // 4:
      }
      public void op2() {
        System.out.println("B.op2()");
        op1();               // 3.1.1.1:  and  4.1.1.1:
      }
    
    }
    
    class A{
        public void op1() {
    	System.out.println("A.op1()");
    	}
    }
    
    
    class C {
    	private B b;
    	private D d;
    	public C(B b) {
    	System.out.println("C()");
    	}
    
    	public void op3(D d) {
    	System.out.println("C.op3()");
            d.op5();
    	}
    
    	public void op5() {
            System.out.println("C.op5()");
    	b.op2(); //erroneous part
    	}
    }
    
    class D {
    	private B b;
    	private C c;
    	public D(B b) {
    	System.out.println("D()");
    	}
    
    	public void op4(C c) {
    	System.out.println("D.op4()");
            c.op5();
    	}
    
    	public void op5() {
    	System.out.println("D.op5()");
            b.op2();  //erroneous part
            }
    }

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: UML to Java question

    Hello,
    What exactly are you trying to do in your classes. The op2() method is in class B. So, if you need to use this method in class C and class D then you will need to extend class C and class D with class B. I think this is the solution for it. Also, if you need some other way round then you can also you class B as an interface so that it can be called by class C and class D at the same time, that is multiple inheritance.

Similar Threads

  1. Basic Java question
    By colby02184 in forum Software Development
    Replies: 2
    Last Post: 11-11-2011, 05:08 PM
  2. Question to Hibernate in Java Development
    By Taarank in forum Software Development
    Replies: 5
    Last Post: 29-07-2010, 01:51 AM
  3. Java - question on enum
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 02-03-2010, 10:50 AM
  4. Java math question
    By Daren in forum Software Development
    Replies: 2
    Last Post: 02-09-2009, 06:33 PM
  5. Frequently asked Core Java Question for Inteview
    By Eaton in forum Education Career and Job Discussions
    Replies: 2
    Last Post: 05-11-2008, 01:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,763,358.44538 seconds with 16 queries