0

I'm new to Java, and even newer to OOP. I am trying to complete this assignment for one of my classes.

B. Array of Objects Write a program that meets the following requirements:

Create a class for students. The class must contain each student’s name, (String), ID (int) and status (int.)

The status indicates the student’s class standing: 1 for freshman, 2 for sophomore, 3 for junior and 4 for senior.

Create 20 students whose names are Name1, Name2 and so on, to Name20, and who’s IDs and status are assigned randomly. Print out the array of student objects.

Find all juniors and print their names and IDs.

NOTE: Use the Math.random() method to create student ID and status.

I know this is relatively simple, but I'm just not quite getting it. Here is my code so far:

class ArryOfObjects{
    public static void main(String[] args){
        String stuName;
        int stuID, stuStatus;
        Student [] name = new Student[20];
        int i;
        while(i < name.length){
            name[i] = new Student(creatStuInfo()); //hopefully this loads objects into arrays
            i++;
        }
    }
}


class Student{
    String stuName;
    int stuID, stuStatus;
    Student(){
        stuName = this.stuName;
        stuID = this.stuID;
        stuStatus = this.stuStatus;
    }
    public void creatStuInfo(int i){
        int min = 1;
        int max = 4;
        String stuName;
        int stuID, stuStatus;
        stuID = Math.random();
        stuStatus = randInt();
        stuName = Name + i;
    }
    public int randInt(int min, int max){
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
}

The method randInt is my attempt to use math.random(), but I'm not sure how to get it to display between 1 and 4 like the requirements ask.

I might as well ask while I'm here, does anyone have any ideas on scanning through the array to find the juniors?

And how would you print out the objects in this particular case?

Also could you look through my code and simply point out my dumb mistakes?

Thank you so much for any help you could give!

3 Answers3

1

You've got a few things messed up:

Student(){
    stuName = this.stuName;
    stuID = this.stuID;
    stuStatus = this.stuStatus;
}

you probably meant to do:

Student(String stuName, int stuID, int stuStatus){
    this.stuName = stuName;
    this.stuID = stuID;
    this.stuStatus = stuStatus;
}
  • Second, all the methods that generate the students should either be static or part of main() (which is also static). These methods should have "a view" of all the instances of the class Student.

  • See how to use Math.random() here: Math.random() explained

These are the basic errors, and I didn't even check the logic of your methods. Start with that and add some log-prints (or run in "debug" mode) to see logical errors

Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

I'm wondering if your code compiles? There is a stuName = Name + i; for which I don't see Name declared anywhere. To print the juniors,

In pseudocode,

for (int i=0;i<20;i++){//since you declared the array as 20
   if (array[i].stuStatus==3){
      System.out.println("i'm a junior.");
   }
}

I wouldn't name my Student array as 'name' too. It's quite confusing.

phantomhive
  • 145
  • 1
  • 1
  • 11
0

I do not frequently do this, but you have shown sufficient effort in this case that I think it is warranted. Along with the points mentioned in alfasin's answer, here is a correct version with inline comments and simplifications.

Note that the public class is class Classroom, so this code needs to live in a file called Classroom.java unless you rename the class.

import java.util.*;

class Student{
    String name;
    int id;
    int status;

    // Tool for generating random numbers
    static Random random = new Random();

    // Constructor
    public Student(String name){
        this.name = name;
        status = random.nextInt() % 4 + 1; // Get a number from 0 to 3 using mod, and then add 1 to make it 1 to 4
        id = random.nextInt();
    }
}

public class Classroom{
    public static void main(String[] args){
        // A better name would be "students" but I kept your variable name.
        Student[] name = new Student[20];

        // The standard way to go through an array is a for loop, not a while loop.
        for (int i = 0; i < name.length; i++) {
            String currentName = "Name" + (i+i);
            name[i] = new Student(currentName); //hopefully this loads objects into arrays
        }

        // Find all the juniors. Note that this could have been done in the earlier loop.
        for (int i = 0; i < name.length; i++) {
            if (name[i].status == 3)
                System.out.println(name[i].name + " " + name[i].id);
        }
    }
}
merlin2011
  • 71,677
  • 44
  • 195
  • 329