import javax.swing.JOptionPane;

public class StudentReport 
{
    String name;            //name of the student
        String schoolName;      //name of the school
        String schoolGrade;     //grade in school
        int numOfGrades;        //number of grades or number of classes
        double grades;          //grades received
        double average;         //average of the grades
        double total=0;         //initialize total
        String trash;           //to convert string to double
    public static void main(String[]args)
    {

        public getInformation()
        {
        //ask user for Name, grade, school
        name=JOptionPane.showInputDialog("Name:");            
        schoolGrade=JOptionPane.showInputDialog("Grade:");    
        schoolName=JOptionPane.showInputDialog("School:");    


        //ask user to enter number of grades
        trash=JOptionPane.showInputDialog("Number of grades:");             
        numOfGrades=Integer.parseInt(trash);    
        }

        public getAvg()
       {  
        //get the grades added together in order to calculate the average
        for (int i = 1; i <=numOfGrades; i++) 
                {

            trash=JOptionPane.showInputDialog("Test " + i + " : " );
            grades      =Integer.parseInt(trash);

            //calculate total
                total+=grades;  

            //calculate average
             average=total/numOfGrades;
                }
       }



        //display window showing information
        JOptionPane.showMessageDialog(null, "School name: " + schoolName + "\n"         
                        + "Student: "+ name + " \n "                                    
                                                + "School grade: " + schoolGrade + "\n"                         
                        + "Average: " + average + ".");                                 




    }
    }
}

How can make this class to send information to another class
Like I am writing a class that calculate gpa and needs the average of this class and the information
of the user.
I don't know what to add in order to convert the methods and be able to call them from another class
I am new in java.

How can make this class to send information to another class
Like I am writing a class that calculate gpa and needs the average of this class and the information
of the user.

If you want a separate class to have access to the information in the class StudentReport, have the separate class (i.e. CalculateReport) extend the StudentReport class by using the keyword extends. Depending on the accessibility (variables and methods with private and protected access modifiers) of StudentReport's members (variables and methods), the class will inherit/have access to them. You would write it as the following:

class CalculateReport extends StudentReport{}

GPA is calculated by dividing the total amount of grade points earned by the total amount of credit hours attempted. Let's say in the StudentReport class you have three variables defined as double - totGradePts ,totCredHrs, and studentGPA. In the CalculateReport class, you can create a static method, so you don't have to create an instance of CalculateReport to call methods -

// inside CalculateReport class 
    public static double calcGPA(double gradePts, double credHrs)
    {
      // code to calculate a student's GPA
    }     

that calculates GPA by taking in both totGradePts and totCredHours as parameters.

Inside StudentReport class, all you have to do is call that static method and return the result to studentGPA which can be displayed the same way as you did with the other information. For example:

// inside StudentReport class
    studentGPA = CalculateReport.calcGPA(totGradePts, totCredHrs);    

Hope that helps.

Edited 2 Years Ago by blackwell87

Gerock: first learn the basics of Java, then try to work with classes.

the code you posted will never compile.

public static void main(String[]args)
    {
        public getInformation()
        {

(and so on)

you are creating your methods within the main method. that can't compile, and it shows you don't yet understand how to work with methods.

move those two methods outisde of the main method, and create an instance of your class to call those methods through.

also, provide return types for getInformation() and getAvg()

they are not constructors, yet you write them as such.

for instance: your getInformation method is to 'set the value' of numOfGrades, which you do at the end of that (erroneous) method.

which means, you don't need to return anything, so you can set the returntype to void

public void getInformation(){
// rest of your code for that method
}

now, to call that method:

public static void main(String[] args){
  StudentReport sR = new StudentReport(); 
  // you are trying to call instance methods and you haven't declared
  // a constructor yourself, so call the default one
  sR.getInformation();
}

but since you don't actually 'get' anything, you may want to rename that method.

This article has been dead for over six months. Start a new discussion instead.