Ramesh Padmanabhan
Ramesh Padmanabhan

Reputation: 31

No output in Java

I am learning Java, I am trying to create a class and instantiate it. But I am not getting any output when I execute the below code in Eclipse.

package day1;

class student {
    int mark1 = 0;
    int mark2 = 0;
    int mark3 = 0;
    int total = 0;

    void bio(int newmark) {
        mark1 = mark1 + newmark;

    }

    void chemistry(int newmark) {
        mark2 = mark2 + newmark;

    }

    void maths(int newmark) {
        mark3 = mark3 + newmark;

    }

    void printmarks() {
        System.out.println(mark1 + mark2 + mark3);

    }
}

public class May24 {

    public static void main(String[] args) {

        student student1 = new student();
        student student2 = new student();
        student1.bio(10);
        student1.chemistry(20);
        student1.maths(30);

        student2.bio(40);
        student2.chemistry(30);
        student2.maths(40);
    }

}

Can some one point out where am I wrong. Thanks.

Upvotes: 0

Views: 100

Answers (2)

Adam
Adam

Reputation: 1151

public static void main(String[] args) {

        student student1 = new student();
        student student2 = new student();
        student1.bio(10);
        student1.chemistry(20);
        student1.maths(30);
        student1.printmarks();

        student2.bio(40);
        student2.chemistry(30);
        student2.maths(40);
        student2.printmarks();

    }

Upvotes: 0

Namrata Choudhary
Namrata Choudhary

Reputation: 121

because whatever you did is, just assigning values to class data member, you haven't call printmarks() to print output. add following lines to your main()

student1.printmarks();
student2.printmarks();

Upvotes: 3

Related Questions