1

How do I create a class that has different lengths of arguments?

public static void main(String[] args) {
    group g1 = new group("Redskins");
    group g2 = new group("Zack", "Mills", 21);
    group g3 = new group("John","Smith",20);
    group g4 = new group("Fred","Fonsi",44);
    group g5 = new group("Jeb","Bush",26);


    System.out.println(g1.getName());
}

}

I want to be able to display the team name (redskins) and then each member after that using one method.

I've tried using two methods and got that to work, but can't get one.

I was thinking about possibly using an array but not sure if that would work.

Thanks for any help.

I have three classes the main, student, and group. I need the group class to display the group name and then figure out how to display the students information underneath. The only thing, is that my assignment is vague about whether I can use two methods or one.

public class student {
    String firstName;
String lastName;
    int age; 
    student(String informedFirstName, String informedLastName, int informedAge){
        firstName = informedFirstName;
        lastName = informedLastName;
        age = informedAge;
    }

    String getName()
{
    return "Name = " + firstName + " " + lastName + ", " + "Age = " + age;


}   

}

user1566796
  • 35
  • 2
  • 9
  • Can you post the source code of the relevant classes and methods that you have implemented here? – Anderson Green Sep 15 '13 at 02:25
  • 2
    It's not clear what you're trying to do here; "Redskins" as a team and "Zack Mills" as a player are very different kinds of things, and they should be represented by different classes. (Also, capitalize your class names like `Group`; it's a universal Java convention and makes your code much easier to read.) – chrylis -cautiouslyoptimistic- Sep 15 '13 at 02:26
  • Would you be able to use one method if you use two classes? I guess the same method for both classes? Thanks – user1566796 Sep 15 '13 at 03:00

2 Answers2

2
public class Team{
   String name;
   Set<Player> players;

   public Team(String name){
      this.name = name;
   }

   public void addPlayer(Player p){
      players.add(p);
   }
}


public class Player{
   String name;
   etc
}

EDIT for revised question:

Ok, Im going to show a lot here. Heres what a proper Java versio of what you want for student.

public class Student {

    private String firstName;
    private String lastName;
    private int age;

    public Student(String firstName, String lastName, int age){
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }   

    /*
     * Use:
     * Student s = new Student(Bill, Nye, 57);
     * System.out.println(s.toString()); 
     */
    @Override
    public String toString() {      
        return "First Name: " + firstName + ", Last Name: " + lastName + ", Age: " + age;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

The Things to take away from this. 1) Capitalize the first letter of class names! (Student)

2) note the class variables are private (look here for a tutorial Java Class Accessibility) and have getters and setter to control access outside the class.

3) I dont say "getName()" and return name and age. This doesnt make sense. Instead i make it so when you go toString() it shows all the relevant information.

4) Java is an object oriented language which means the classes that model data are supposed (to some extent) model appropriately to the way they are used in real life. This makes it more intuitive to people reading your code.

5) if your Group class (note the capital!) needs to contain many Students use a LIST such as an ArrayList. Arrays would make no sense because you dont know how many Students are going to be in each Group. A SET like i used above is similar to a list but only allows ONE of each item. For simplicity use a list though

6) the THIS operator refers to class (object) variables. In the constructor this.firstName refers to the firstName within the Class (object...an instance of the class) whereas just firstName would refer to the variable in the contructor and not alter the class variable.

Community
  • 1
  • 1
Alex
  • 271
  • 3
  • 8
  • Is there a tutorial for the this. operator? I've been reading about in one of my text books but I need extra examples to get a better handle on it. Thanks. – user1566796 Sep 15 '13 at 02:58
  • `this` is just a reference to the current object, nothing out of the ordinary there. You can obviate it most of the time, except when a variable name on a certain scope is the same than an attribute name of the class of your object. – asermax Sep 15 '13 at 03:10
0

use the constructor for that

     class group {
    String fname,lname;

     group(String fname ){
    this.fname=fname;
    }
    group(String fname,String lname){
    this.fname=fname;
    this.lname=lname;

    }
    group(String fname,String lname,int age){
    this.fname=fname;
    this.lname=lname;
    this.age=age;
    }

  public  String getName(){

return fname+lname+age;    
}
    }
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • This technically gives the OP what he/she was asking for, but the code suggested by Alex is what should really be used. This does not accurately mirror what the program needs to model, which is the whole point of object-oriented programming. – musical_coder Sep 15 '13 at 02:28
  • within one constructor you cannot pass all the arguments ... generics only to differ the datatypes of the arguments.. – Nambi Sep 15 '13 at 03:45