1

Please help me to add array in Student class. i will input the values in main class which needs to be stored in student class and then can be displayed by calling method ShowData().

import java.util.Scanner;

public class Library {

public static void main(String[] args) {
    Student record = new Student();
    int roll;
    String name;
    int mob;
    int i;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter roll numer of student \n");
    roll = sc.nextInt();
    System.out.println("Enter name of the student \n");
    name = sc.next();
    System.out.println("Enter mobile number of the student \n");
    mob = sc.nextInt();

    record.getData(roll, name, mob);

record.showData();
}
}

This is a subclass Student

public class Student {

int roll;
String name;
int mob;
int i = 0;

void getData(int roll, String name, int mob) {

    this.roll = roll;
    this.name = name;
    this.mob = mob;

}

void showData() {

    //for(i=0; i < 3; i++)

    System.out.println("roll number " + roll + "\nname " +  name + "\nmobile number " + mob);
}
}

3 Answers3

0

You can go about this just like the other variables. Just add another parameter to the getData function and a instance variable to student (Don't forget to also set the value of the the variable in the getData function). Then you can just pass the array with all the other data to getData. So you can handle the array just like all the other variables (e.g roll, name).

Another way to go about this are getters and setters. Those are usually used to access instance variables of classes. Getters are just functions called getXyz() (or isXyz if Xyz is a boolean) which return the value of the variable and Setters are functions called setXyz() which take one parameter and set Xyz to the passed value. An example would be:

public int getRoll() {
    return roll;
}

public void setRoll(int roll) {
    this.roll = roll;
}

Also usually instance variables are declared private. To do that just add the private keyword to each instance variable.

private int roll;

To read more on getters and setters you can read this, this or this

Community
  • 1
  • 1
Leon
  • 2,926
  • 1
  • 25
  • 34
0

So... I opened eclipse and using your code got the functionality you were asking for with your own code. That is, the output I got when I used input: 4, this, 3, was:

roll number 4
name this
mobile number 3

However you are asking for an array in your Student class(I think?). The way you have it right now works well; however, if you want to use an array you probably want an array of type String: I used the following class of type Student to get the same results but I used a String array (Note: to make a consistent array of one type I used String.valueOf(int i)):

String[] info;
int i = 0; //whatever this is for

void getData(int roll, String name, int mob) {

    info = new String[3]; 
    info[0] = String.valueOf(roll);
    info[1] = name;
    info[2] = String.valueOf(mob);

}

void showData() {
    System.out.println("roll number " + info[0] + "\nname " +  info[1] + "\nmobile number " + info[2]);
}

Your wording was a little hard to understand, but I hope this helps solve the problem.

ezPaint
  • 152
  • 1
  • 8
0

You Have to Inherit the subclass to the main class.. Inheritance is the process that enables one class to acquire the properties(methods and variable) of another.. The class inheriting the properties of another is called Subclass (also called derived Class).where the Class whose properties are inherited is the Super class (Base Class or parent Class) Example:

class Ajay extends Jay{
//
}

Here

  • Ajay is Main class..

  • Jay is Sub class..

For your Code:

import java.util.Scanner;
public class Library extends Student{
public static void main(String[] args) {
    Student record = new Student();
    int roll;
    String name;
    int mob;
    int i;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter roll numer of student \n");
    roll = sc.nextInt();
    System.out.println("Enter name of the student \n");
    name = sc.next();
    System.out.println("Enter mobile number of the student \n");
    mob = sc.nextInt();

    record.getData(roll, name, mob);

record.showData();
}
}
public class Student
{
 int roll;
String name;
int mob;
int i = 0;

void getData(int roll, String name, int mob) {

    this.roll = roll;
    this.name = name;
    this.mob = mob;

}

void showData() {

    //for(i=0; i < 3; i++)
System.out.println("roll number " + roll + "\nname " +  name + "\nmobile number " + mob);
}
}

Please Check now..And Vote me back.!

user6427267
  • 76
  • 1
  • 9