views:

157

answers:

4
package javaapplication1;


public class Main
  {

       public static void main(String[] args)
        {
        Student stu = new Student();
        System.out.println (stu.getStudentid() +":" + stu.getStudentname()+":"+stu.getStudentgrade());
//1:Bill:A
        Student stu2 = new Student ();
        System.out.println (stu2.getStudentid() +":" + stu2.getStudentname()+":"+stu2.getStudentgrade());

//2:Mary:B
        Student stu3 = new Student ();
        System.out.println (stu3.getStudentid() +":" + stu3.getStudentname()+":"+stu3.getStudentgrade());
//3:Jame:C
        Student stu4 = new Student ();
        System.out.println (stu4.getStudentid() +":" + stu4.getStudentname()+":"+stu4.getStudentgrade());
//4:Helen:D

        }
  }

        class Student
        {
            int Studentid;
            String Studentname;
            String Studentgrade;

            Student()
            {
                Studentid = 1;
                Studentname = "Bill";
                Studentgrade = "A";
            }


  int getStudentid()
  {
   return Studentid;
  }

  String getStudentname()
  {
    return Studentname;
  }
 String getStudentgrade()
  {
    return Studentgrade;
  }

}

Problem 1: Design and implement a Student class which has - Data members: studendID, studentName, and studentGrade - Constructor: Student (int id, String name) which will set studentID to the argument ‘id’ and studentName to the argument ‘name’ (studentID = id; studentName = name) - Method members: setGrade( ), getGrade ( ), getName( ), and getID( )

Problem 2: Implement a Test class which has a ‘main’ function. The ‘main’ function will create 4 student objects from the Student class above with ids and names: 1, Bill; 2, Mary; 3, Jame; 4, Helen and set their grades to A, B, C, D. The main ( ) function will output: ID:Name:Grade 1:Bill:A 2:Mary:B 3:Jame:C 4:Helen:D This is an example that he gave us.

class Cis36L0413
{
  public static void main( String[] args )
  {
    NotSimple nsObj1 = new NotSimple();

    System.out.println( "nsObj1.data is\t" + nsObj1.getData() );
    System.out.println( "nsObj1.str is \t" + nsObj1.getStr() );

    NotSimple nsObj2 = new NotSimple( 50,
                         "Another immutable string!" );
    System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
    System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );

    nsObj2 = nsObj1;

    System.out.println( "nsObj2.data is\t" + nsObj2.getData() );
    System.out.println( "nsObj2.str is \t" + nsObj2.getStr() );

    return;
  }
}
class NotSimple
{
  NotSimple()
  {
    data = 5;
    str = new String( "Initialized!" );
  }

  NotSimple( int i, String str1 )
  {
    data = 5;
    str = str1;
  }

  void setData( int i )
  {
    data = i;

    return;
  }

  int getData()
  {
   return data;
  }

  void setStr( String str1)
  {
    str = str1;

    return;
  }

  String getStr()
  {
    return str;
  }

  private int data;
  private String str;
}

1) How do I change the data so I can print out 2:Mary:B on the second line? and 3:Jame:C on the third and 4:Helen:D on the fourth? So far I am printing out 4lines of 1:Bill:A. 2) If I am doing this wrong, can you please give me some examples?

thank you in advance

Hi, I am just following up on this. This is the correct coding that he wanted.

package test;

public class Test {

    public static void main(String[] args) {
    Student stu1 = new Student(1,"Bill");
    stu1.setGrade ("A");
    System.out.println(stu1.getId()+":"+stu1.getName()+":"+ stu1.getGrade());
    Student stu2 = new Student (2,"Mary");
    stu2.setGrade ("B");
    System.out.println(stu2.getId()+":"+stu2.getName()+":"+ stu2.getGrade());
    Student stu3 = new Student (3,"Jame");
    stu3.setGrade ("C");
    System.out.println(stu3.getId()+":"+stu3.getName()+":"+ stu3.getGrade());
    Student stu4 = new Student (4,"Helen");
    stu4.setGrade ("D");
    System.out.println(stu4.getId()+":"+stu4.getName()+":"+ stu4.getGrade());

     }
}
    class Student{
    int studentId;
    String studentName;
    String studentGrade;

    Student(int id, String name){
        studentId = id; 
        studentName = name;     
    }
    public String getName(){
        return studentName;
    }
    public int getId(){
        return studentId;
    }
    void setGrade(String i)
  {
    studentGrade =i;
    return;
        }
    public String getGrade(){
    return studentGrade;
    }  
}
A: 

You need to create a Student constructor which takes three parameters: the name, the grade, and the ID, like this:

Student(int id, String name, String grade)
{
    Studentid = id;
    Studentname = name;
    Studentgrade = grade;
}

Alternately (I'm not sure if this is beyond the scope of what you're learned so far or not) you can have the ID number be automatically generated by using a static counter, and instead of having a 3-argument constructor, only write a constructor that takes two arguments. Just in case you haven't gotten that far, or aren't supposed to do that, I'll leave that code up to you.

Matt Ball
A: 

You don't have Student (int id, String name) constructor and setter methods in your student class yet. Try to add them, have your main method initialize different student instances and go from there.

logoin
+1  A: 

You are misunderstanding the instructions: You want a Student class and a Test class which implements the method of main.

public class Test {
}

not:

public class Main {
}

Also, you need a Student constructor as such:

Student(int id, String name, String grade)
{
    Studentid = id;
    Studentname = name;
    Studentgrade = grade;
}
thyrgle
+4  A: 

Okay, I'll bite. Here are a couple of thoughts:

  1. Your Student class is utterly wrong. There's no way to create a Student other than the default, and the defaults make no sense.
  2. Why prepend each variable name in a Student class with "student"? Id, name, and grade will do.

More like this:

package student;

/**
 * Student
 * User: Michael
 * Date: Oct 8, 2010
 * Time: 9:08:17 PM
 */
public class Student
{
    private int id;
    private String name;
    private String grade;

    public Student(int id, String name)
    {
        this(id, name, "");
    }

    public Student(int id, String name, String grade)
    {
        this.id = id;
        this.name = name;
        this.grade = grade;
    }

    public int getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public String getGrade()
    {
        return grade;
    }

    public void setGrade(String grade)
    {
        this.grade = grade;
    }

    public String toString()
    {
        return "Student{" +
               "id='" + id + '\'' +
               ", name='" + name + '\'' +
               ", grade='" + grade + '\'' +
               '}';
    }
}

And here's the test class:

package student;

/**
 * StudentTest
 * User: Michael
 * Date: Oct 8, 2010
 * Time: 9:09:29 PM
 */
public class StudentTest
{
    public static void main(String[] args)
    {
        List<Student> students = new ArrayList<Student>()
        {{
            add(new Student(1, "Bill", "A"));
            add(new Student(2, "Mary", "B"));
            add(new Student(3, "Jane", "C"));
            add(new Student(4, "Helen", "D"));
        }};

        for (Student s : students)
        {
            System.out.println(s);
        }
    }

}
duffymo
@duffymo Thank you, but one quick question do i have to use an array?
CuriousStudent
I did not learn override yet, is there another way of doing it without that coding?
CuriousStudent
Took care of both. Better?
duffymo
@duffymo ThankS! if you have time can you look at the example that the teacher gave us up top. Is it possible to do it that way?
CuriousStudent
It's possible, but unlikely. I think I've already done enough. You carry the ball from here, my friend. You need to be a bit more "curious" and try it yourself.
duffymo
@duffymo thanks i'll try
CuriousStudent
just dont know why he told us to look at that example to do the assignment
CuriousStudent
Really? No idea? Would you be willing to tell us where you're going to school? What major? How far along? Is this your first programming language? Ever programmed before? If yes, what language?
duffymo
@duffymo I'm taking lower division classes at my local community college(oakland,ca). My major is computer science. I'm trying to transfer into one of the universities in california. This is my first programming language. My instructor is horrible. No one in the class knows how to do any of the homework. He doesn't teach it to us. He assigns it and we have to learn it ourselves one way or another. Then he tells us how to do the homework AFTER we learn how to do it ourselves? Most teachers teach you how to do it and then assign homework...not the other way around. sigh..
CuriousStudent
This is why I am posting questions every week :(
CuriousStudent
Okay, that's fine. The sad truth is that professors don't tell you how to do your homework. They present concepts, you practice with homework, they assess how well the material diffused to you, then tell you how you might do it. You can learn before the assignment/exam or afterwards. Your grades will be better if you learn before, but the point is to learn.
duffymo
One important lesson in computer science: Decomposition. Solve big problems by breaking them into smaller, more manageable pieces. Also: Don't Repeat Yourself. If you find yourself copying and pasting code, turn it into an object or method and simply call it when needed. You'll go a long way if you're ruthless about applying these two rules of thumb.
duffymo
If my answer helped, accept it. A high rate of acceptance will make people more willing to help you; a low acceptance rate will turn them off. That's how this site works.
duffymo
@duffymo 100% accept rate :)
CuriousStudent
@duffymo do i really need both of these? public Student(int id, String name) { this(id, name, ""); } public Student(int id, String name, String grade) { this.id = id; this.name = name; this.grade = grade; }They look really similar
CuriousStudent
can you think of a reason why you might find it useful? I did this for a reason.
duffymo