简体   繁体   中英

Why my variables cannot receive a value (interface)

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.

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

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM