I have a litle problem
please help me whit:
I want to write a methode whit one paramter.
public int studentInformation(int hour){//How long time(hour) he was at school.
- - - - - - --- - - - - -
- - - - - - - - - - - - --
return ;

}
Now I want that the same information(above method) uses for every student.
How can I call it for every student.
How do I write code?
// first I want call it for student1
// then for student2, student3.........
and how do I print it out.

Dani AI

Generated

A clear way to handle this is to model each student as an object with instance methods to record and return hours, then keep those objects in a collection and iterate over them to print. This follows the direction from (create a student class and store objects) and the reminder from to instantiate objects — but adds concrete method naming and a printing pattern that works for any number of students.

Example (Student class plus a simple main that stores and prints students):

class Student {
    private String name;
    private int hours;

    public Student(String name) {
        this.name = name;
    }

    public void recordHours(int h) {
        this.hours = h;
    }

    public int getHours() {
        return hours;
    }

    @Override
    public String toString() {
        return name + ": " + hours + " hours";
    }
}
import java.util.ArrayList;
import java.util.List;

public class School {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice"));
        students.add(new Student("Bob"));

        students.get(0).recordHours(6); // student1
        students.get(1).recordHours(7); // student2

        for (Student s : students) {
            System.out.println(s);
        }
    }
}

Notes and common pitfalls: prefer an instance method (setter) that is void to store hours and a getter that returns the stored int when a value needs to be read; override toString() so System.out.println(s) prints useful info; use ArrayList<Student> for a variable number of students (arrays work too if the count is fixed); avoid making the hours method static — each student needs its own state.

I have a litle problem
please help me whit:
I want to write a methode whit one paramter.
public int studentInformation(int hour){//How long time(hour) he was at school.
- - - - - - --- - - - - -
- - - - - - - - - - - - --
return ;

}
Now I want that the same information(above method) uses for every student.
How can I call it for every student.
How do I write code?
// first I want call it for student1
// then for student2, student3.........
and how do I print it out.

If you need to store information on students (which all follows the same format) and then print it out again you may be better off creating an array.

If you want a number of objects (i.e. each a different student, each holding variables) you would have to creat a student class where the student class would have a method called studentInformation. You would then have a method called printStudentInformation in the same class to print out the information held by this student. In the main, you would create a student and give that student variables. You will be able to create a number of students.

I have a program which i think the architechter is similar, which involves a car class holding information on cars and a method within lets you change or print the information held on each car.

If you want to mail me for any help on this my e-mail is

Dan :)

according to me for this u need to create object for student i.e. s and then access the objects by calling them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.