0

I having a problem to send a value on the variables. Why my variable cannot receive the value when I input a value? I've no idea to do...

import java.io.*;
public class StudentApp {
public static void main (String [] args) throws IOException {

    student studentObject = new GradeResult();
    studentObject.getName();
    student studentObject2 = new GradeResult();
    studentObject2.getIdNum();
    student studentObject3 = new GradeResult();
    studentObject3.getMark();
    student studentObject4 = new GradeResult();
    studentObject4.setName();
    student studentObject5 = new GradeResult();
    studentObject5.setIdNum();
    student studentObject6 = new GradeResult();
    studentObject6.setMark();
    student studentObject7 = new GradeResult();
    studentObject7.showGrade();
    student studentObject8 = new GradeResult();
    studentObject8.showResult();
}

interface StudentInterface{
    void getName() throws IOException;
    void getIdNum()throws IOException;
    void setName();
    void setIdNum();
    void getMark()throws IOException;
    void setMark();
}

interface student extends StudentInterface{
    void showResult();
    void showGrade();
}

class GradeResult implements student {
    String name ,name2 ,name3;
    int a ,mark;

    public void getName () throws IOException {
        InputStreamReader inStream = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(inStream);
        System.out.print("Please Input Your Name :");
        name = stdin.readLine();

    }

    public void getIdNum()throws IOException {
        BufferedReader stdin=new BufferedReader (new InputStreamReader(System.in));
        System.out.print("Please Input Your ID Number :");
        name2 = stdin.readLine();
        a = Integer.parseInt(name2);
    }

    public void setName() {
        System.out.print("Your Name :"+name);
    }

    public void setIdNum() {    
        System.out.print("\nYour ID Number :"+name2);
    }

    public void getMark()throws IOException {
        BufferedReader stdin=new BufferedReader (new InputStreamReader(System.in));
        System.out.print("Please Input Your Mark :");
        name3 = stdin.readLine();
        mark = Integer.parseInt(name3);
    }

    public void setMark() {
        System.out.print("\nYour Mark :"+mark);
    }

    public void showResult() {
        if ((mark>=40)&&(mark<=100)) {
            System.out.print("\nYou Are PASS");
        } else {
            System.out.print("\nYou Are FAIL");
        }
    }

    public void showGrade() {
        if((mark>=80)&&(mark<=100)) {
            System.out.print("\nYour Grade Is A");
        } else if ((mark>=65)&&(mark<=80)) {
            System.out.print("\nYour Grade Is B");
        } else if ((mark>=50)&&(mark<=65)) {
            System.out.print("\nYour Grade Is C");
        } else if ((mark>=40)&&(mark<=50)) {
            System.out.print("\nYour Grade Is D");
        } else {
            System.out.print("\nYour Grade Is E");
        }
    }
}

I'm not sure which one is my mistake. Should I need to use object reference to receive the value or just call as usual on main method.

Aadi Droid
  • 1,689
  • 4
  • 22
  • 46
Edie
  • 65
  • 1
  • 2
  • 11

3 Answers3

2

Best working solution is to do the following in your GradeResult class

private static String name, name2, name3;
private static int a, mark;

This will work..!

The output:

Please Input Your Name :Mohammad
Please Input Your ID Number :10001
Please Input Your Mark :85
Your Name :Mohammad
Your ID Number :10001
Your Mark :85
Your Grade Is A
You Are PASS
Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31
  • Thank you sir. But could you explain for me which on is my mistake? – Edie Oct 08 '14 at 17:41
  • You are creating a new object every time. This will cause the class instance variables to be re-initialized to their original values (i.e. "" for `String` and `0` for `int`). Once you make them static, they will not be overridden by instantiating new class objects. – Mohammad Najar Oct 08 '14 at 17:46
  • @Ajk_P what exactly is a pretty bad practice? – Mohammad Najar Oct 08 '14 at 17:47
  • @Mohammad Thank you sir.. I appricated so much. Eid Mubarak. ^^ – Edie Oct 08 '14 at 17:49
  • @Mohammad Making instance variables static for no reason other than "making code work" (i.e. no design decisions involved) is pretty bad practice. You're putting a workaround to his problem, which is creating new objects for no reason. – Ajk_P Oct 08 '14 at 17:50
  • @Ajk_P If I were looking at your code, I would suggest a different solution (which might have involved a re-design suggestion). But for this simple program, you can make some variables static for a very good reason, which is to make the code work. – Mohammad Najar Oct 08 '14 at 17:55
1

You are creating new objects everytime, therefore your results get put in different objects.

Try this:

student studentObject = new GradeResult();
studentObject.getName();
studentObject.setName();

EDIT: In case you didn't know it, instance variables values are held within an object. So all your different objects (of the same class) will have different name, name2, name3, etc. The objects from which you call the System.out.println(...) methods have uninitialized instance variables (name, name2, name3) and hence will not print any values for those.

Ajk_P
  • 1,874
  • 18
  • 23
  • Works.. !!! thank you sir... I'm too rush until I didn't realised that you was change name of StudentObject. ^^ – Edie Oct 08 '14 at 17:50
0

create object set value for that object and access value from same object. In your case you are setting value to some object and accessing data from other object that is why you are getting null for some fields.

public static void main (String [] args) throws IOException{

    student studentObject = new GradeResult();
    studentObject.getName();
    studentObject.getIdNum();
    studentObject.getMark();
    studentObject.setName();
    studentObject.setIdNum();
    studentObject.showGrade();

    studentObject.showResult();
}
Rustam
  • 6,485
  • 1
  • 25
  • 25