Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I need help explaining the output.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help explaining the output.

    Hi! Well, I could really use some help with explaining this code output.

    Here's the code:

    class Uppgift2 {
    public static void main(String[]args) {
    Adda[]alla=new Adda[5];
    for (int x=0; x<alla.length; x++)
    if (x==1 || x==3)
    alla[x]=new Dokee();
    else alla[x]=new Bjubb();

    for (int x=0; x<alla.length; x++)
    alla[x].skriv();
    }
    }

    class Adda {
    private String ord="Lire";
    private int antal=2;

    public Adda(String ord, int antal) {
    this.ord=ord;
    this.antal=antal;
    }
    public Adda() {
    ord="Pesetas";
    }
    public void skriv() {
    for (int x=0; x<antal; x++)
    System.out.print(ord);
    System.out.println();
    }
    }
    class Bjubb extends Adda {
    private static String[] ord={"Kronor","Dollar","Sedlar","Pengar"};
    private static int pos=0;

    public Bjubb() {
    super(ord[pos++], pos);
    }
    }
    class Dokee extends Adda {
    private String sak="Euro";

    public void skriv(){
    super.skriv();
    System.out.println(sak);
    }
    }

    The output is supposed to look like this:

    Kronor
    PesetasPesetas
    Euro
    DollarDollar
    PesetasPesetas
    Euro
    SedlarSedlarSedlar

    Can anyone explain to me why the output is the way it is? I can figure out some parts, but I'm still a "newbie" when it comes to Java. Thankful for every bit of advice I can get.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help explaining the output.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    You forgot to post the program's current output.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  2. Help with explaining the code, pleaseeeeeee
    By katarina in forum Other Programming Languages
    Replies: 1
    Last Post: May 6th, 2012, 03:26 PM
  3. What would be the output of this?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2011, 03:51 PM
  4. Why is my output not right?
    By Bryan229 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2011, 07:17 AM
  5. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM