Jump to content

can you solve this problem with java

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
kadriatil

kadriatil

    Newbie

  • Members
  • Pip
  • 2 posts
Write a short program in java (including pseudo-code) to iterate over a structure and print out the names of all users which have a score less than 70. At the end of the structure, also print out the average score of all users who scored 70 or higher. Assume a global function/procedure called print which prints all parameters passed to it and that the structure is called Structure.

Structure Prototype = [[Name1, Score1], [Name2, Score2], …]
Example Structure = [[‘Tim’, 98], [‘Tom’, 80], [‘Mike’, 50], [‘Jason’, 25]]
Example Output =
Mike
Jason
89

#2
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
while I(and hopefully no one else on the site) will not do your homework for you, we can help.

step 1: read about Iterator (Java 2 Platform SE 5.0) from that site

it'll be a very simple loop depending if the list "hasNext"

#3
kadriatil

kadriatil

    Newbie

  • Members
  • Pip
  • 2 posts
thanks ice pack.. it not a home work it just a question that my father asked me. I dont know java so i cant do it. He is teasing me all the time. lol

#4
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
well i wish my father did stuff like this!

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Ideal situation for an LRStruct :)

#6
Lawtonfogle

Lawtonfogle

    Newbie

  • Members
  • Pip
  • 8 posts
I read this problem so that an actual iterator isn't needed. A simple while loop can be used.

#7
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts

kadriatil said:

Write a short program in java (including pseudo-code) to iterate over a structure and print out the names of all users which have a score less than 70. At the end of the structure, also print out the average score of all users who scored 70 or higher. Assume a global function/procedure called print which prints all parameters passed to it and that the structure is called Structure.

Structure Prototype = [[Name1, Score1], [Name2, Score2], …]
Example Structure = [[‘Tim’, 98], [‘Tom’, 80], [‘Mike’, 50], [‘Jason’, 25]]
Example Output =
Mike
Jason
89
I guess you have experience with C/C++

The following might contain error but it must be easy to fix it:

class Pair{

  private String name;

  private int score;

  Pair(String name, int score){

    this.name=name;

    this.score=score;

  }

  public int getScore(){

    return score;

  }

  public String getName(){

    return name;

  }

}


class ....{

...

  public static void main(String args[]){

    ...

    Iterable<Pair> myStructure = .... // initialize your structure.

    ...

    long sum=0;

    int count=0;

    for( Pair p : myStructure ){

       if( p.getScore() < 70 ){

           System.out.println( p.getName() );

       }

       else{

           sum+=p.getScore();

           count++;

       }

    }

     System.out.println( (double)sum/(double)count );

  }

...

}

Hope this helps...