0

I am new in java and I trying to get information

for five students and save them into an array of classes. how can I do this?

I want to use class person for five students whit different informations

import java.io.IOException;
import java.util.*;

public class exam 
{
public static void main(String[] args)
   {
   Scanner s  = new Scanner(System.in);

// I want to get and save information in this array 
  person[] f  = new student[5];



   }    
}

class person defined for get name and family name.


import java.util.*;

 public abstract class person {
     Scanner scr  = new Scanner(System.in);

private String name , fname;

    public void SetName() {

        System.out.println("enter name and familyNAme :");
        name = scr.next();

    } 

    public String getname() {

        return name;
    }

    public void setfname () {
        System.out.println("enter familyname:");
        fname = scr.next();
    }
    public String getfname() {
        return fname;
    }
}

class student that inherits from the class person for get studentID and student Scores .

import java.util.*;

class student extends person {
float[] p = new float[5];
int id , sum  ;
float min;

public void inputs() {

    System.out.println("enter the id :");
    id = scr.nextInt();

}

public void sumation() {
    System.out.println("enter points of student:");
    sum= 0;
    for(int i = 0 ; i<5 ; i++){
        p[i]=scr.nextFloat();
        sum+=p[i];
    }
}

public void miangin() {

    min = (float)sum/4;
}

}

1
  • 1
    It's a bad idea to get the input inside the object you want to create from the input, better to separate the handling of input from your model classes (Person, Student) into a separate class and then creating Student objects using a constructor that takes all input as parameters. Commented Dec 27, 2019 at 14:55

3 Answers 3

1

So first things first, when creating Java objects, refrain from getting input inside the object so that if you decide to change the way you get input (e.g. transition from command line to GUI) you don't need to modify the Java object.

Second, getters and setters should only get or set. This would save some confusion when debugging since we don't have to check these methods/functions.

So here's the person object:

public abstract class Person {

    protected String name, fname;

    public Person (String name, String fname) {
        this.name = name;
        this.fname = fname;
    }

    public void setName (String name) {
        this.name = name;
    }

    public String getName () {
        return name;
    }

    public void setFname (String fname) {
        this.fname = fname;
    }

    public String getFname () {
        return fname;
    }

}

And here's the student object (tip: you can make as much constructors as you want to make object creation easier for you):

public class Student extends Person {

    private float[] p;
    private int id;

    public Student (String name, String fname) {
        this (name, fname, -1, null);
    }

    public Student (String name, String fname, int id, float[] p) {
        super (name, fname);
        this.id = id;
        this.p = p;
    } 

    public void setP (float[] p) {
        this.p = p;
    }

    public float[] getP () {
        return p;
    }

    public void setId (int id) {
        this.id = id;
    }

    public int getId () {
        return id;
    }

    public float summation () {
        float sum = 0;
        for (int i = 0; i < p.length; i++)
            sum += p[i];
        return sum;
    }

    public float miangin () {
        return summation () / 4.0f;
    }

    @Override
    public String toString () {
        return new StringBuilder ()
            .append ("Name: ").append (name)
            .append (" Family name: ").append (fname)
            .append (" Id: ").append (id)
            .append (" min: ").append (miangin ())
            .toString ();
    }

}

And lastly, wherever your main method is, that is where you should get input from. Take note that when you make an array, each index is initialized to null so you still need to instantiate each array index before using. I made a sample below but you can modify it depending on what you need.

import java.util.*;

public class Exam {

    Scanner sc;
    Person[] people;

    Exam () {
        sc = new Scanner (System.in);
        people = new Person[5];
    }

    public void getInput () {
        for (int i = 0; i < people.length; i++) {
            System.out.print ("Enter name: ");
            String name = sc.nextLine ();

            System.out.print ("Enter family name: ");
            String fname = sc.nextLine ();

            System.out.print ("Enter id: ");
            int id = sc.nextInt (); sc.nextLine ();

            System.out.println ("Enter points: ");
            float[] points = new float[5];
            for (int j = 0; j < points.length; j++) {
                System.out.printf ("[%d] ", j + 1);
                points[j] = sc.nextFloat (); sc.nextLine ();
            }

            people[i] = new Student (name, fname, id, points);
        }
    }

    public void printInput () {
        for (Person p: people)
            System.out.println (p);
    }

    public void run () {
        getInput ();

        printInput ();
    }

    public static void main (String[] args) {
        new Exam ().run ();
    }

}

Just one last tip, if you ever need dynamic arrays in Java, check out ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

0

You can add a class attribute, and then add class information for each student, or you can add a class class, define an array of students in the class class, and add an add student attribute, and you can add students to that class.

Comments

0

First of all, please write class names with capital letter (Student, Exam <...>).

Exam class:

import java.util.Scanner;

public class Exam {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Student[] students = new Student[]{
                new Student(),
                new Student(),
                new Student(),
                new Student(),
                new Student()
        };

        for (int i = 0; i < 5; i++) {
            students[i].setFirstName();
            students[i].setLastName();
            students[i].setId();
        }
    }
}

Person class:

import java.util.Scanner;

public class Person {
    String firstName, lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName() {
        System.out.println("Type firstName: ");
        this.firstName = new Scanner(System.in).next();
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName() {
        System.out.println("Type lastName: ");
        this.lastName = new Scanner(System.in).next();
    }
}

Student class: import java.util.Scanner;

public class Student extends Person{
    int id;

    public int getId() {
        return id;
    }

    public void setId() {
        //Converting String line into Integer by Integer.parseInt(String s)
        System.out.println("Type id: ");
        this.id = Integer.parseInt(new Scanner(System.in).next());
    }
}

1 Comment

This is not how you should use set... methods, they should take a parameter and set an instance variable from that parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.