Search code examples
oopjava.util.scannerabstraction

How can I apply Scanner and multiple objects at the same time? - newbie


how can I satisfy all the conditions here? can't find any example online. I just don't know what code to use. I tried doing some coding but I know it was way off to what was actually ask to me. The problem was

"Write a java class called Student. The Student class will have four data members: name (astring), age (an int), semesterNo (an int), and GPA (a float). Write a java program to test your Student class. Your program should: Create three Student objects, enter their name, age, semesterNo (assuming a value from 1 to 8), and GPA(assuming value from 0.0 to 4.0) and display the data for each Student. Continuing the Student class, calculate and display the average GPA of the three Students.(user input required)"

and the code I come up was `

import java.util.Scanner;

class Student {

  public static void main(String[] args) {
    
    String name, name1, name2;
    int age1, age2, age3, semesterNo1, semesterNo2, semesterNo3;
    float GPA1, GPA2, GPA3, Average;
  
    
   Scanner in = new Scanner(System.in);        
    System.out.print("Name:");
    name = in.nextLine();
    System.out.print("Age:");
    age1 = in.nextInt();
    System.out.print("Semester:");
    semesterNo1 = in.nextInt(9);
    System.out.println("GPA:");
    GPA1 = in.nextFloat(); 
   
   Scanner input = new Scanner(System.in);
    System.out.print("Name:");
    name1 = input.nextLine();
    System.out.print("Age:");
    age2 = input.nextInt();
    System.out.print("Semester:");
    semesterNo2 = input.nextInt(9);
    System.out.println("GPA:");
    GPA2 = input.nextFloat(); 
    
    Scanner inputs = new Scanner(System.in);
    System.out.print("Name:");
    name2 = inputs.nextLine();
    System.out.print("Age:");
    age3 = inputs.nextInt();
    System.out.print("Semester:");
    semesterNo3 = inputs.nextInt(9);
    System.out.println("GPA:");
    GPA3 = inputs.nextFloat(); 
    
   Average = (GPA1 + GPA2 + GPA2) / 3;
   System.out.println("Average GPA:" + Average); 

  }

}

`


Solution

  • According to your question, you should create an object of the student class. So student class should contain

    class Student {
       // data members
        String name;
        int age, semester number;
        float GPA;
        
    }
    

    And from main method you should be creating object of student class like:

    Student std1 = new Student();
    Student std2 = new Student();
    Student std2 = new Student();
    

    Now you can access the data members of each object like:

    std1.name = "Some name"  // std1.name = in.nextLine();
    std1.age = // some age
    std1.GPA = // your GPA
    

    You can do the same for each object . Use appropriate way of taking input from the scanner object as you do and store it as shown above.

    Finally calculate GPA and display:)

    [Edited] Simple Example:

    // import Scanner class for console base input
    import java.util.Scanner;
    
    // your student class
    class Student {
        // data members
        String name;
        int age;
        float GPA;
        
    
        // display method 
        void display() {
            System.out.println("Your name: "+this.name);
            System.out.println("Your age: "+this.age);
            System.out.println("Your GPA: "+this.GPA+"\n");
        }
    }
    
    // class which contain main method (enty point)
    class StudentInfo {
        public static void main(String[] args) {
            // create object of scnner 
            Scanner in = new Scanner(System.in);
    
            // create three different object of student
            Student std1 = new Student();
            Student std2 = new Student();
            Student std3 = new Student();
    
            // new set data for student
            System.out.println("Enter name, age and GPA of student:");
    
            //for first student
            std1.name = in.next();
            std1.age = in.nextInt();
            std1.GPA = in.nextFloat();
    
            // for second student
            System.out.println("Enter name, age and GPA of student:");
            std2.name = in.next();
            std2.age = in.nextInt();
            std2.GPA = in.nextFloat();
    
            // for third student
            System.out.println("Enter name, age and GPA of student:");
            std3.name = in.next();
            std3.age = in.nextInt();
            std3.GPA = in.nextFloat();
    
            // display info of each student
            std1.display();
            std2.display();
            std3.display();
    
            // now calculate Average GPA and display
            float avg = (std1.GPA + std2.GPA + std3.GPA)/3;
            System.out.println("Average GPA: "+ avg);
    
    
        }
    }