Results 1 to 19 of 19
  1. #1
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Getting objects in an array to count to 10

    Hi,

    I am trying to get student objects in an array to count to 10, I have created a getCounter() method which counts from 0 - 9, but I want each of the student objects to count from 1 - 10. So for example the outcome that I am expecting is:
    MathStudent[1] - John: Count:1
    MathStudent[2] - Marcus: Count: 1
    ...
    ComputerStudent[3] - Alberto: Count: 1
    ...
    Upto the point that all reach Count: 10
    Also I have created the array in a thread class so it should sleep for 1 second in between the counts.

    Below is the code I have in thread class:
    Java Code:
    public class StudentThread extends Thread {
        
        public void run(){
           Student s[] = new Student[10];
           
            
            s[0] = new MathStudent("Smith");
            s[1] = new MathStudent("Jack");
            s[2] = new MathStudent("Victor");
            s[3] = new MathStudent("Mike");
            s[4] = new ScienceStudent("Dave");
            s[5] = new ScienceStudent("Oscar");
            s[6] = new ScienceStudent("Peter");
            s[7] = new ComputerStudent("Philip");
            s[8] = new ComputerStudent("Shaun");
            s[9] = new ComputerStudent("Scott");
            
            for (int loop = 0; loop < 10; loop++) {
                System.out.print(loop);
                System.out.println(s[loop].getSubjects());
            }
            for (int loop = 0; loop < 10; loop++) {
                s[0].getCounter();
            }
        }
        
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Getting objects in an array to count to 10

    What is your code doing at the moment?
    What should it be doing?

    Not too sure what your problem is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Getting objects in an array to count to 10

    In spite of the example, it looks like the OP wants to count the number of instances of each type of student. But it
    will be hard to help without an example of one of the classes. And of course, we don't know what issue is at this point.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    NirelG is offline Member
    Join Date
    Mar 2017
    Posts
    10
    Rep Power
    0

    Default Re: Getting objects in an array to count to 10

    Hi,
    I hope I understand what your problem with your code is.

    Do you mean that in the following code:
    for (int loop = 0; loop < 10; loop++) {
    System.out.print(loop);
    System.out.println(s[loop].getSubjects());
    }

    you want instead of printing the "loop" from 0-9 to be printing 1-10?

    if so you can do it very simply by changing this for loop into:
    for (int loop = 0; loop < 10; loop++) {
    System.out.print(loop + 1);
    System.out.println(s[loop].getSubjects());
    }

    By doing so it will add 1 to what ever the loop number is, meaning on your first try it will print 1 (0+1) and then s[0].getSubjects().
    hope this helps.

  5. #5
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Re: Getting objects in an array to count to 10

    Quote Originally Posted by NirelG View Post
    Hi,
    I hope I understand what your problem with your code is.

    Do you mean that in the following code:
    for (int loop = 0; loop < 10; loop++) {
    System.out.print(loop);
    System.out.println(s[loop].getSubjects());
    }

    you want instead of printing the "loop" from 0-9 to be printing 1-10?

    if so you can do it very simply by changing this for loop into:
    for (int loop = 0; loop < 10; loop++) {
    System.out.print(loop + 1);
    System.out.println(s[loop].getSubjects());
    }

    By doing so it will add 1 to what ever the loop number is, meaning on your first try it will print 1 (0+1) and then s[0].getSubjects().
    hope this helps.
    Hi
    Thanks for the answer, thats not exactly what Im looking to do, what I am looking to do is use the getCounter() method in the Student class to make each student count. So technically the output that I require is:
    MathStudent[1] Jack Count: 1
    MathStudent[2] Smith Count: 1
    MathStudent[3] Victor Count: 1
    MathStudent[4] Mike Count: 1
    ScienceStudent[5] Dave Count: 1
    ScienceStudent[6] Oscar Count: 1
    ScienceStudent[7] Peter Count: 1
    ComputerStudent[8] Philip Count 1
    ComputerStudent[9] Shaun Count 1
    ComputerStudent[10] Scott Count 1
    MathStudent[1] Jack Count: 2
    MathStudent[2] Smith Count: 2
    MathStudent[3] Victor Count: 2
    MathStudent[4] Mike Count: 2
    ScienceStudent[5] Dave Count: 2
    ScienceStudent[6] Oscar Count: 2
    ScienceStudent[7] Peter Count: 2
    ComputerStudent[8] Philip Count 2
    ComputerStudent[9] Shaun Count 2
    ComputerStudent[10] Scott Count 2
    :
    :
    :
    MathStudent[1] Jack Count: 10
    MathStudent[2] Smith Count: 10
    MathStudent[3] Victor Count: 10
    MathStudent[4] Mike Count: 10
    ScienceStudent[5] Dave Count: 10
    ScienceStudent[6] Oscar Count: 10
    ScienceStudent[7] Peter Count: 10
    ComputerStudent[8] Philip Count 10
    ComputerStudent[9] Shaun Count 10
    ComputerStudent[10] Scott Count 10

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    use the getCounter() method in the Student class to make each student count.
    Can you explain what "make each student count" means?

    The posted output looks like the digit at the end of each line is incremented each time a repeated line is displayed:
    MathStudent[1] Jack Count: 1
    MathStudent[1] Jack Count: 2
    ...
    MathStudent[1] Jack Count: 10
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Re: Getting objects in an array to count to 10

    Quote Originally Posted by Norm View Post
    Can you explain what "make each student count" means?

    The posted output looks like the digit at the end of each line is incremented each time a repeated line is displayed:
    MathStudent[1] Jack Count: 1
    MathStudent[1] Jack Count: 2
    ...
    MathStudent[1] Jack Count: 10
    Yes thats what I want to do increment the digit at the end by 1.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    Ok, where is the count kept for each line that is shown?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Re: Getting objects in an array to count to 10

    Quote Originally Posted by Norm View Post
    Ok, where is the count kept for each line that is shown?
    Sorry don't really understand what you are asking?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    Does the Student class have a counter variable that can be used?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Re: Getting objects in an array to count to 10

    Quote Originally Posted by Norm View Post
    Does the Student class have a counter variable that can be used?
    There is a getCounter() method in the student class.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    Ok. What about a method to increment the count being held in the Student object so that the value printed on each line is incremented as desired?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Getting objects in an array to count to 10

    Based on the previous examples:

    Since you need to have a common variable shared between all instances of a given class, I recommend you read up on on static variables.
    Since you want to count based on some object type (String in this case) I recommend you read up on HashMaps.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    Not sure static is appropriate. The sample print out shows each Student has its own count.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Getting objects in an array to count to 10

    I was thinking each class has a HashMap which would store the count using the students name as a key. And that you didn't want to instantiate
    a new HashMap for each instantiation of the given class. But that is all based on my interpretation of the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    based on my interpretation of the problem.
    Yep, me too. We'll have to wait until the OP clarifies what the program is supposed to do.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Counter giving random numbers

    I am trying to implement a program which will get the students to count from 1 - 10.

    The following is the code I have:

    Main class:

    Java Code:
    public class JavaLab5 {
            public static final int DEBUG = 0;
            
            public static void multipleThreads(int number) {
                StudentThread []st = new StudentThread[number];
                for (int loop = 0; loop < number;) {
                    st[loop] = new StudentThread();
                    st[loop].start();
                }
            }
            /**
             * @param args the command line arguments
             */
            public static void main(String[] {
                multipleThreads(10);  
            } 
        }
    Java Code:
    Student class:
    
        public class Student {
            static int studentCounter = 1;
            String name;
            private static int count = 0;
            public static int instances = 0;
        
            // Getters
            /**
             *
             * @return
             */
            public  String getName() {
                return this.name;
            }
            
            // Setters
            /**
             *
             * @param name
             */
            public void setName(String name) {
                if (JavaLab5.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
                this.name = name;
            }
            
            /**
             * Default constructor. 
             * Populates name with defaults
             */
            public Student() {
                instances++;
                this.name = "Not Set";
            }
            
            /** 
             * Constructor with parameters 
             * @param name String with the name
            */
            public Student(String name) {
                this.name = name;
            }
            
            /**
             * Destructor
             * @throws Throwable 
             */
            protected void finalize() throws Throwable {
                //do finalization here
                instances--;
                super.finalize(); //not necessary if extending Object.
            } 
            
            /**
             * @return 
             */
            public void getCounter() {
                count++;
                //return count;
                System.out.println(count);
            }
            
            @Override
            public String toString () {
                return this.name; 
            }
            
            /**
             *
             * @return
             */
            public String getSubjects() {
                return null;  
            }
        }
    Computer student class:

    Java Code:
    public class ComputerStudent extends Student {
            int studCountObj;
            
            /**
             * Default constructor
             */
            public ComputerStudent() {
                super();
            }
            
            public ComputerStudent(String name) {
                super(name);
                studCountObj=studentCounter;
                studentCounter++;
            }
            
            /**
             * Display information about the subject
             * @return 
             */
            @Override
            public String getSubjects(){
                return(" Computer Student" + "[" + studCountObj + "] " + name + ": "); 
            }
        }
    Science Student:

    Java Code:
    public class ScienceStudent extends Student {
            int studCountObj;
            
            /**
             * Default constructor
             */
            public ScienceStudent() {
                super();
            }
            
            public ScienceStudent(String name) {
                super(name);
                studCountObj=studentCounter;
                studentCounter++;
            }
            
            /**
             * Display information about the subject
             * @return 
             */
            @Override
            public String getSubjects(){
                return(" Science Student" + "[" + studCountObj + "]" + "-" + name + ": ");
            } 
        }
    Math student:

    Java Code:
    public class MathStudent extends Student {
            int studCountObj;
            
            /**
             * Default constructor
             * @param name
             */
            public MathStudent(String name) {
                super(name);
                studCountObj=studentCounter;
                studentCounter++;
            }
            
            public MathStudent() {
                super();
            }
            
            /**
             * Display information about the subject
             * @return 
             */
            @Override
            public String getSubjects(){
                return(" MathStudent" + "[" + studCountObj + "]" + "-" + name + ": ");
            }
        }
    Student Thread:

    Java Code:
    public class StudentThread extends Thread {
            
            @Override
            public void run(){
                Student s[] = new Student[10];
               
                //Student Array containing different types of students
                s[0] = new MathStudent("Smith");
                s[1] = new MathStudent("Jack");
                s[2] = new MathStudent("Victor");
                s[3] = new MathStudent("Mike");
                s[4] = new ScienceStudent("Dave");
                s[5] = new ScienceStudent("Oscar");
                s[6] = new ScienceStudent("Peter");
                s[7] = new ComputerStudent("Philip");
                s[8] = new ComputerStudent("Shaun");
                s[9] = new ComputerStudent("Scott");
                
                //Checking and looping till the count reaches 10
                //for (int j = 0; j < 10; j++) { 
                    //Checking and looping till the end of the student array
                    for (int i = 0; i < s.length; i++) {
                        System.out.println();
                        System.out.print(s[i].getSubjects() + " - " + "Count:");
                        s[i].getCounter();
                }
                try {
                Thread.sleep(1000);
                } catch (InterruptedException e) {
                System.out.println("Exception at StudentThread.run: " + e);
                }
                }
            
        }
    The output of the current code gives:

    MathStudent[12]-Smith: - Count:
    MathStudent[28]-Smith: - Count:2




    MathStudent[1]-Smith: - Count:3




    MathStudent[20]-Smith: - Count:4

    MathStudent[21]-Jack: - Count:5

    MathStudent[16]-Smith: - Count:6

    MathStudent[2]-Jack: - Count:7

    MathStudent[36]-Smith: - Count:8

    MathStudent[24]-Smith: - Count:9

    MathStudent[25]-Jack: - Count:10

    MathStudent[26]-Victor: - Count:11

    MathStudent[27]-Mike: - Count:12

    Science Student[52]-Dave: - Count:13
    MathStudent[37]-Jack: - Count:14

    MathStudent[38]-Victor: - Count:15

    MathStudent[39]-Mike: - Count: MathStudent[29]-Jack: - Count:17

    1

    MathStudent[13]-Jack: - Count:18

    MathStudent[14]-Victor: - Count:19

    MathStudent[30]-Victor: - Count:20

    MathStudent[32]-Smith: - Count:21

    MathStudent[1]-Smith: - Count:22

    MathStudent[33]-Jack: - Count:23

    MathStudent[31]-Mike: - Count:24

    MathStudent[15]-Mike: - Count:25

    Science Student[64]-Dave: - Count:26

    Science Student[65]-Oscar: - Count:27

    Science Student[66]-Peter: - Count:28

    16
    Computer Student[67] Philip: - Count:29


    MathStudent[4]-Victor: - Count:30

    MathStudent[17]-Jack: - Count:31

    MathStudent[18]-Victor: - Count:32

    MathStudent[19]-Mike: - Count:33

    MathStudent[22]-Victor: - Count:34

    MathStudent[8]-Smith: - Count:35

    MathStudent[9]-Jack: - Count:36

    MathStudent[10]-Victor: - Count:37

    MathStudent[11]-Mike: - Count:38

    MathStudent[23]-Mike: - Count:39

    Science Student[61]-Dave: - Count:40

    MathStudent[6]-Mike: - Count:41

    Science Student[46]-Dave: - Count:42

    Science Student[53]-Oscar: - Count:43

    Computer Student[70] Shaun: - Count:44

    Computer Student[73] Scott: - Count:45

    Science Student[55]-Dave: - Count:46

    Science Student[56]-Oscar: - Count:47

    MathStudent[34]-Victor: - Count:48

    MathStudent[35]-Mike: - Count:49

    Science Student[41]-Dave: - Count:50

    MathStudent[3]-Jack: - Count:51

    Science Student[43]-Oscar: - Count:52

    Science Student[44]-Peter: - Count:53

    Science Student[57]-Peter: - Count:54

    Science Student[49]-Dave: - Count:55

    Science Student[50]-Oscar: - Count:56

    Science Student[51]-Peter: - Count:57

    Computer Student[97] Philip: - Count:58

    Computer Student[98] Shaun: - Count:59

    Computer Student[99] Scott: - Count:60
    Science Student[54]-Peter: - Count:61

    Computer Student[88] Philip: - Count:62

    Computer Student[89] Shaun: - Count:63

    Computer Student[90] Scott: - Count:64
    Science Student[47]-Oscar: - Count:65

    Science Student[48]-Peter: - Count:66

    Computer Student[76] Philip: - Count:67

    Computer Student[77] Shaun: - Count:68

    Computer Student[78] Scott: - Count:69
    Science Student[62]-Oscar: - Count:70

    Science Student[63]-Peter: - Count:71

    Computer Student[79] Philip: - Count:72

    Computer Student[80] Shaun: - Count:73

    Science Student[58]-Dave: - Count:74

    Science Student[59]-Oscar: - Count:75

    Science Student[60]-Peter: - Count:76

    Science Student[68]-Dave: - Count:77

    Computer Student[82] Philip: - Count:78

    Computer Student[81] Scott: - Count:79
    Computer Student[85] Philip: - Count:80

    Computer Student[86] Shaun: - Count:81

    Computer Student[94] Philip: - Count:82

    MathStudent[5]-Victor: - Count:83

    Computer Student[95] Shaun: - Count:84

    Computer Student[87] Scott: - Count:85
    Computer Student[83] Shaun: - Count:86

    Science Student[69]-Oscar: - Count:87

    Computer Student[84] Scott: - Count:88
    Computer Student[96] Scott: - Count:89
    MathStudent[7]-Mike: - Count:90

    Science Student[71]-Peter: - Count:91

    Science Student[40]-Dave: - Count:92

    Science Student[42]-Oscar: - Count:93

    Computer Student[72] Philip: - Count:94

    Science Student[45]-Peter: - Count:95

    Computer Student[91] Philip: - Count:96

    Computer Student[74] Shaun: - Count:97

    Computer Student[92] Shaun: - Count:98

    Computer Student[93] Scott: - Count:99
    Computer Student[75] Scott: - Count:100
    As you can see the current output gives a randon count values and random student values which I don't understand why.

    The output that I require is:

    MathStudent[1]-Smith: - Count:1
    MathStudent[2]-Jack: - Count:1
    MathStudent[3]-Victor: - Count:1
    MathStudent[4]-Mike: - Count:1
    Science Student[5]-Dave: - Count:1
    Science Student[6]-Oscar: - Count:1
    Science Student[7]-Peter: - Count:1
    Computer Student[8] Philip: - Count:1
    Computer Student[9] Shaun: - Count:1
    Computer Student[10] Scott: - Count:1
    MathStudent[1]-Smith: - Count:2
    MathStudent[2]-Jack: - Count:2
    MathStudent[3]-Victor: - Count:2
    MathStudent[4]-Mike: - Count:2
    Science Student[5]-Dave: - Count:2
    Science Student[6]-Oscar: - Count:2
    Science Student[7]-Peter: - Count:2
    Computer Student[8] Philip: - Count:2
    Computer Student[9] Shaun: - Count:2
    Computer Student[10] Scott: - Count:2
    MathStudent[1]-Smith: - Count:3
    MathStudent[2]-Jack: - Count:3
    MathStudent[3]-Victor: - Count:3
    MathStudent[4]-Mike: - Count:3
    Science Student[5]-Dave: - Count:3
    Science Student[6]-Oscar: - Count:3
    Science Student[7]-Peter: - Count:3
    Computer Student[8] Philip: - Count:3
    Computer Student[9] Shaun: - Count:3
    Computer Student[10] Scott: - Count:3
    MathStudent[1]-Smith: - Count:4
    MathStudent[2]-Jack: - Count:4
    MathStudent[3]-Victor: - Count:4
    MathStudent[4]-Mike: - Count:4
    Science Student[5]-Dave: - Count:4
    Science Student[6]-Oscar: - Count:4
    Science Student[7]-Peter: - Count:4
    Computer Student[8] Philip: - Count:4
    Computer Student[9] Shaun: - Count:4
    Computer Student[10] Scott: - Count:4
    MathStudent[1]-Smith: - Count:5
    MathStudent[2]-Jack: - Count:5
    MathStudent[3]-Victor: - Count:5
    MathStudent[4]-Mike: - Count:5
    Science Student[5]-Dave: - Count:5
    Science Student[6]-Oscar: - Count:5
    Science Student[7]-Peter: - Count:5
    Computer Student[8] Philip: - Count:5
    Computer Student[9] Shaun: - Count:5
    Computer Student[10] Scott: - Count:5
    MathStudent[1]-Smith: - Count:6
    MathStudent[2]-Jack: - Count:6
    MathStudent[3]-Victor: - Count:6
    MathStudent[4]-Mike: - Count:6
    Science Student[5]-Dave: - Count:6
    Science Student[6]-Oscar: - Count:6
    Science Student[7]-Peter: - Count:6
    Computer Student[8] Philip: - Count:6
    Computer Student[9] Shaun: - Count:6
    Computer Student[10] Scott: - Count:6
    MathStudent[1]-Smith: - Count:7
    MathStudent[2]-Jack: - Count:7
    MathStudent[3]-Victor: - Count:7
    MathStudent[4]-Mike: - Count:7
    Science Student[5]-Dave: - Count:7
    Science Student[6]-Oscar: - Count:7
    Science Student[7]-Peter: - Count:7
    Computer Student[8] Philip: - Count:7
    Computer Student[9] Shaun: - Count:7
    Computer Student[10] Scott: - Count:7
    MathStudent[1]-Smith: - Count:8
    MathStudent[2]-Jack: - Count:8
    MathStudent[3]-Victor: - Count:8
    MathStudent[4]-Mike: - Count:8
    Science Student[5]-Dave: - Count:8
    Science Student[6]-Oscar: - Count:8
    Science Student[7]-Peter: - Count:8
    Computer Student[8] Philip: - Count:8
    Computer Student[9] Shaun: - Count:8
    Computer Student[10] Scott: - Count:8
    MathStudent[1]-Smith: - Count:9
    MathStudent[2]-Jack: - Count:9
    MathStudent[3]-Victor: - Count:9
    MathStudent[4]-Mike: - Count:9
    Science Student[5]-Dave: - Count:9
    Science Student[6]-Oscar: - Count:9
    Science Student[7]-Peter: - Count:9
    Computer Student[8] Philip: - Count:9
    Computer Student[9] Shaun: - Count:9
    Computer Student[10] Scott: - Count:9
    MathStudent[1]-Smith: - Count:10
    MathStudent[2]-Jack: - Count:10
    MathStudent[3]-Victor: - Count:10
    MathStudent[4]-Mike: - Count:10
    Science Student[5]-Dave: - Count:10
    Science Student[6]-Oscar: - Count:10
    Science Student[7]-Peter: - Count:10
    Computer Student[8] Philip: - Count:10
    Computer Student[9] Shaun: - Count:10
    Computer Student[10] Scott: - Count:10
    Why is the output printing out the random count values and random student numbers and how I can I fix this?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting objects in an array to count to 10

    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Getting objects in an array to count to 10

    Quote Originally Posted by Norm View Post
    They came up with the same answer that you and Jim had last Friday: "what is the requirement?"

    @OP: the seemingly random number come up, because you create 10 threads that each create 10 instances.

    If I had to create the output mentioned at the end of #17, I would:

    - create a student class. The student class has the properties String name, String studentType, Integer count. Constructor with all properties. getter for count should be sufficient. count defaults to 0.
    - in the student class implement a private static List<Student> studentList = new ArrayList<Student>(); add all students into the ArrayList in the constructor
    - create the 10 Student instances
    - for the requirement create an outer loop from 0 to 9, create an inner loop that loops through studentList.
    - in the inner loop:
    +tick the count for each student up by one
    +print out: -studentType, index in studentList, name, student.getCount() //all text requirement for output ignored here.

Similar Threads

  1. Replies: 1
    Last Post: 08-08-2016, 10:55 AM
  2. Replies: 3
    Last Post: 07-31-2014, 09:47 AM
  3. trouble with name count and array name
    By OmegaZero69 in forum New To Java
    Replies: 2
    Last Post: 03-24-2013, 02:51 PM
  4. Example Count From Array Snippet
    By Vinx in forum Reviews / Advertising
    Replies: 1
    Last Post: 05-18-2012, 05:22 AM
  5. i can do count, but having problem with array
    By judy318 in forum New To Java
    Replies: 4
    Last Post: 10-29-2009, 09:23 PM

Tags for this Thread

Posting Permissions

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