MainOfpr
MainOfpr

Reputation: 1

why java method return same result?

i have problem with my program << this program take information for 3 student name and id and marks in 5 course then return information with marks avrage my program is work good but it return same avrage for all student its same number please can u help me

this is the class

StudentsMarks.java
        package programmersx;
    import java.io.BufferedWriter;
    import java.io.*;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Arrays;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    public class StudentsMarks {

        private String studentName;
        private int studentId;
        private double marks[];
        public static int NoInstntitd;

        public StudentsMarks() {
            NoInstntitd += 1; 
        }


        double avgCalculator(double[] marks) { //avg method
            double sum =0;
            double avg=0;

            for (int i = 0; i < marks.length; i++) {
                sum = sum + marks[i];
                avg=sum/5;
            }
            return avg;

        }
        @Override
        public String toString() {
            return studentName +" " + studentId +" " + Arrays.toString(marks) +" " ; 


        }

        void toFile(String fileName) throws IOException {

            FileOutputStream file= new FileOutputStream(fileName);
            PrintWriter writer= new PrintWriter(file);
            writer.print("reem ");

        String   information = "student information :" + getStudentName() + "" + getStudentId() + "  average " + avgCalculator(marks);

         LocalDate localDate = LocalDate.now();
        BufferedWriter writer1 = new BufferedWriter(new FileWriter("average.txt"));
        BufferedWriter writer2 = new BufferedWriter(new FileWriter("localDate.txt"));

            try {
                  writer.write(information);
                  writer1.write("  average " + avgCalculator(marks));
                  writer2.write(DateTimeFormatter.ofPattern("yyy/MM/dd").format(localDate));
                  writer2.close();
            } catch (IOException ex) {
                Logger.getLogger(Programmersx.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println("student information :" + getStudentName() + "" + getStudentId() + "  average " + avgCalculator(marks));
            System.out.println(" student average. : " + avgCalculator(marks));
            System.out.println(DateTimeFormatter.ofPattern("yyy/MM/dd").format(localDate));

        }


        { 
            NoInstntitd += 1; 
        } 
        void display( ) {
            System.out.println("display the count of the instantiated objects from StudentMarks" + getNoInstntitd());
        }
     public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }

        public int getStudentId() {
            return studentId;
        }
        public void setStudentId(int studentId) {
            this.studentId = studentId;
        }

        public double[] getMarks() {
            return marks;
        }
        public void setMarks(double[] marks) {
            this.marks = marks;
        }

        public  int getNoInstntitd() {
            return NoInstntitd;
        }
        public  void setNoInstntitd(int NoInstntitd) {
            this.NoInstntitd =  this.NoInstntitd + 1;
        }

    }

this is main

Programmersx.java
package programmersx;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Programmersx {


    public static void main(String[] args) throws IOException {
        StudentsMarks myObject = new StudentsMarks();
        StudentsMarks myObject1 = new StudentsMarks();   
         StudentsMarks myObject2 = new StudentsMarks();
        System.out.print("Please enter your student Name : ");
        Scanner myObj = new Scanner(System.in);  // Create a Scanner object
        myObject.setStudentName(myObj.next()) ;
        System.out.print("Please enter your student Id : ");
        myObject.setStudentId(myObj.nextInt()) ;
        System.out.println("Please enter your  marks of 5 courses : ");
        double marks[] = new double [5];
        for (int i = 0; i < 5; i++) {
            marks[i]= myObj.nextInt();
        }
         myObject.setMarks(marks) ;

             System.out.print("Please enter your student Name : ");
        Scanner myObj1 = new Scanner(System.in);  // Create a Scanner object
        myObject1.setStudentName(myObj1.next()) ;
        System.out.print("Please enter your student Id : ");
        myObject1.setStudentId(myObj1.nextInt()) ;
        System.out.println("Please enter your  marks of 5 courses : ");
        double marks1[] = new double [5];
        for (int i = 0; i < 5; i++) {
            marks1[i]= myObj1.nextInt();
        }
         myObject1.setMarks(marks) ;
           myObject1.toString();
             System.out.print("Please enter your student Name : ");
        Scanner myObj2 = new Scanner(System.in);  // Create a Scanner object
        myObject2.setStudentName(myObj2.next()) ;
        System.out.print("Please enter your student Id : ");
        myObject2.setStudentId(myObj2.nextInt()) ;
        System.out.println("Please enter your  marks of 5 courses : ");
        double marks2[] = new double [5];
        for (int i = 0; i < 5; i++) {
            marks2[i]= myObj2.nextInt();
        }
         myObject2.setMarks(marks) ;
          myObject2.toString();
        myObject.toString();
        myObject.toFile("Reem.txt");
                myObject1.toFile("MAria.txt");
                 myObject2.toFile("Abrar.txt");


        System.out.println("display the count of the instantiated objects from StudentMarks: " + StudentsMarks.NoInstntitd );


    }

}

the output is

run:

Please enter your student Name : jack
Please enter your student Id : 1123
Please enter your  marks of 5 courses : 
6
4
5
7
6
Please enter your student Name : mick
Please enter your student Id : 87534
Please enter your  marks of 5 courses : 
6
4
3
22
5
Please enter your student Name : meno
Please enter your student Id : 43433
Please enter your  marks of 5 courses : 
6
55
33
22
7
student information :jack1123  average 5.6
 student average. : 5.6
2019/12/06
student information :mick87534  average 5.6
 student average. : 5.6
2019/12/06
student information :meno43433  average 5.6
 student average. : 5.6
2019/12/06
display the count of the instantiated objects from StudentMarks: 6

Upvotes: 0

Views: 82

Answers (2)

scott661
scott661

Reputation: 21

All student are setted the same marks

myObject.setMarks(marks)
myObject1.setMarks(marks)
myObject2.setMarks(marks)

You should change your code like this

myObject.setMarks(marks)
myObject1.setMarks(marks1)
myObject2.setMarks(marks2)

Upvotes: 1

user10762593
user10762593

Reputation:

All of the StudentMarks objects have their marks set by a similar call:

 myObject2.setMarks(marks)

They all use the same marks array, so they all have the same marks.

Even if you change the content of the marks array between

 myObject.setMarks(marks)

and

 myObject1.setMarks(marks)

there's still only one marks array, shared by all StudentMarks objects: the StudentMarks setter does not copy the array.

Upvotes: 2

Related Questions