2

Okay, before anything, I would like to mention that this is for school so please DO NOT write any code that fixes my code as that will not teach me anything. Instead what I am looking for is references, explanations and proper terminologies if I use incorrect terminology.

So I am having a few issues here. Here is what I need to do,

*Include the following methods in the Student class: a. an accessor (i.e., getter) for each instance variable from part B1 b. a mutator (i.e., setter) for each instance variable from part B1

Note: All access and change to the instance variables of the Student class should be through accessor and mutator methods.

c. constructor using all of the input parameters d. print() to print specific student data (e.g., student ID, first name, last name) using accessors (i.e., getters)

Create a student Roster class with the following methods that contain all ArrayList method calls: a. public static void remove(String studentID) that removes students from the roster by student ID

Note: If the student ID doesn’t exist, the method should print an error message indicating that it is not found.

b. public static void print_all() that prints a complete tab-separated list of student data using accessor methods

Note: Tabs can be formatted as such: 1 [tab] First Name: John [tab] Last Name: Smith [tab] Age: 20 [tab] Grades: {88, 79, 59}. The print_all() method should loop through all the students in the student array list and call the print() method for each student.

c. public static void print_average_grade(String studentID) that correctly prints a student’s average grade by student ID d. public static void print_invalid_emails() that verifies student e-mail addresses and displays all invalid e-mail addresses to the use*

That above is where I am having trouble, The arraylist, the constructor and calling the Student class in the Roster class.

here is my code.

Student Class

 /**
  * Write a description of class Student here.
  * 
  * @author (Richard Talcik) 
  * @version (v1 3/5/17)
  */
 public class Student {
  // initialize instance variables
  private String csFirstName;  //I am using cs infront of the name because this is a class variable and a string
  private String csLastName;
  private String csEmail;
  private int ciAge;
  private int ciStudentID;
  private int[] ciaGrades;   //cia for class, integer, array;

  public Student(String sFirstName, String sLastName, String sEmail, int iAge, int iStudentID, String[] grades) {

    //doing the consturctor
    this.setFirstName(sFirstName);
    this.setLastName(sLastName); 
    this.setEmail(sEmail); 
    this.setAge(iAge);
    this.setStudentID(iStudentID);
    this.setGrades(grades);


} 
  /**he methods to get and then followed by set
 */
public String getFirstName()
{
    // put your code here
    return csFirstName; //returning the value of the first name when called.

}
 public String getLastName()
{
    // put your code here
    return csLastName;

}
 public String getEmail()
{
    // put your code here
    return csEmail;

}
 public int getStudentID()
{
    // put your code here
    return ciStudentID;

}
 public int getAge()
{
    // put your code here
    return ciAge;

}
 public int[] getGrades()
{
    // put your code here
    return ciaGrades;

}

// calling the sets from here on out
 public void setFirstName(String newFirstName)
{
    // put your code here
    csFirstName = newFirstName;
    //setting it

}
public void setLastName(String newLastName)
{
    // put your code here
    csLastName = newLastName;
    //setting it

}
public void setEmail(String newEmail)
{
    // put your code here
    csEmail = newEmail;
    //setting it

}
public void setAge(int newAge)
{
    // put your code here
    ciAge = newAge;
    //setting it

}
public void setStudentID(int newStudentID)
{
    // put your code here
    ciStudentID = newStudentID;
    //setting it

}
public void setGrades(int[] newGrades)
{
    // put your code here
    ciaGrades = newGrades;
    //setting it

 }
}

This roster class is asking me to add arguments when I call Student stu = new Student(); But I don't understand what arguments to add into there? I also dont really understand how I am going to incorporate my arraylist to add my methods in there from the student class? (sorry if i used wrong terminology, please correct me if needed)

Roster Class

import java.util.ArrayList;

 /**
  * Write a description of class Roster here.
  * 
  * @author (Richard Talcik) 
  * @version (v1)
  */
 public class Roster {




// instance variables - replace the example below with your own


/**
 * Constructor for objects of class Roster
 */
 public static void main(String args[]) {
    Student stu = new Student("John", "Smith", "John1989@gmail.com", 20, 1, Grades[1]);
    ArrayList<Student> myRoster = new ArrayList<Student>();
    myRoster.add(new Student("Suzan", "Erickson","Erickson_1990@gmailcom", 19, 2, [91,72,85]));

 }
 public static void remove(String studentID)
{
    // put your code here

}

public static void print_all()
{
    //do something
}
public static void print_average_grade(String studentID)
{
    //do something
}

public static void print_invalid_emails()
{
    //do something
}


}

please any tutorials that will help explain this will be well worth it.

Also, I work third shift and my school is fully online. My mentor isn't or tutor isn't available during the hours that I am awake and emails are not really best method of communication as it takes days for a response.

baron dune
  • 387
  • 1
  • 5
  • 23
  • `Student stu = new Student();` => You need to provide parameter data according to your CTOR `public Student(String sFirstName, String sLastName, String sEmail, int iAge, int iStudentID, int[] iaGrades)`. You could hard code them for demonstration purposes or read them from a file or ask them to be entered ... but for starters I'd hard code them (make some up). – Fildor Mar 14 '17 at 11:55
  • `ArrayList` you need an `ArrayList` and add some of those. Then you can iterate it. Or `get` an item of it (which then is a Student on which you can call its methods). – Fildor Mar 14 '17 at 11:57
  • BTW: (A) Prefixing with "cs" and "ci" is known as [Hungarian Notation](https://en.m.wikipedia.org/wiki/Hungarian_notation). No need for this with modern tools and languages as today‘s [IDE](https://en.m.wikipedia.org/wiki/Integrated_development_environment)s, interpreters, compilers, & debuggers comprehend types and scopes quite well and are able to communicate types and scopes to you clearly. Leave such drudgery to machines. `csEmail` -> `email` (B) These are *instance* (object) vars, not [class vars](https://en.m.wikipedia.org/wiki/Class_variable) (not object-oriented) as you state. – Basil Bourque Mar 14 '17 at 12:08
  • @basil, My father who is a very old C developer and doesn't understand java therefore he can't help me that much, told me to write with cs and ci. So i can understand if it is outdated. Also, thank you for all the responses – baron dune Mar 14 '17 at 12:11
  • @Fildor So for the Student stu = new Student(); => You need to provide parameter data according to your CTOR public Student(String sFirstName, String sLastName, String sEmail, int iAge, int iStudentID, int[] iaGrades) i can create arguments for it. but I dont understand what arguments to put, because i try creating a new String inside the parameters saying Student stu = new Student(String sFN, String sLN.......... int[] iaG) it doesn't like it – baron dune Mar 14 '17 at 12:16
  • 2
    The key words there are "very old". Hungarian Notation went out with [bell-bottom pants](https://en.wikipedia.org/wiki/Bell-bottoms#1960s_and_1970s) and the [Princess phone](https://en.m.wikipedia.org/wiki/Princess_telephone), aimed at solving problems we no longer have. – Basil Bourque Mar 14 '17 at 12:18
  • You need concrete values like so: `Student stu = new Student("Mary","Rotten", "mary@rotten.com", 23, ...)` – Fildor Mar 14 '17 at 12:21
  • okay, so I think i'm starting to understand. I create the concrete values, then I can add them to my array list and print them all in my printall method – baron dune Mar 14 '17 at 12:29
  • wait, I have multiple students. I wouldn't be able to hard code these. I would have to leave it open as a variable? No? – baron dune Mar 14 '17 at 12:44
  • 1
    `myRoster.Add( new Student("A","Student" ...)); myRoster.Add( new Student("B", "Student" ... ));` – Fildor Mar 14 '17 at 14:57

2 Answers2

0

You have written Student class with a constructor that takes parameters. So, in Roaster class you have to call matching constructor of Student class. My guess is that you did not quite understand my answer. I would suggest look for a java tutorial on oracle site and even better if you can get a copy of this book called The java programming language

  • So i've read the section on Inheritance and instanation in my book but I still just don't understand it. This is probably because I don't understand how to call the constructor to the other class. – baron dune Mar 14 '17 at 12:17
0

Firstly for a beginner, not bad :) However when using the ArrayList object type in java, the type of the list must be of the object you whish to put inside the ArrayList, i.e. if you want to keep a list of students, then you have to write it as

AssrayList<Student> = new ArrayList<Student>();

you can also specify the size of the list as a param, but remember, ArraList grows dynamically as things are added and removed from the list.

As far as you Constructor goes, you have to add the variables you assign values to on the inside as the params of the constructor. also the question states that you have to access all class objects with the use of their accessor and mutator methods, having that the current way you have your constructor at the moment is incorrect as you are directly assigning values to the class onjects of Student, you can change that to be similar to the following:

constructor (paramType param) {
  mutatorMethod(param)
}

so your constructor should be in the lines of

Student(String name, String surname) {
  setName(name)
  setSurname(surname)
}

Hope this helps :)

diegeelvis_SA
  • 453
  • 3
  • 8
  • Thanks for the reply, so I thought that if I used the return variables from the accessors and getters that I would be able to relate them in the constructor? and thus have it do what I want? You're telling me that this is incorrect? – baron dune Mar 14 '17 at 12:25
  • no, not at all incorrect as far as java goes, but it is incorrect as far as the question goes. snippet from the question "Note: All access and change to the instance variables of the Student class should be through accessor and mutator methods" – diegeelvis_SA Mar 14 '17 at 12:29
  • Okay, I guess that would make sense on how the names would change. – baron dune Mar 14 '17 at 12:32
  • //doing the consturctor this.setFirstName(sFirstName); this.setLastName(sLastName); this.setEmail(sEmail); this.setAge(iAge); this.setStudentID(iStudentID); this.setGrades(iaGrades); – baron dune Mar 14 '17 at 12:37
  • indent it by 4 spaces, with a new line between the code and normal text, if it does not work then you cant "code" in comments :) – diegeelvis_SA Mar 14 '17 at 12:57