简体   繁体   中英

how we can call the object of second call in the first class in java

here i have a code which i am giving u please correct my code and run it when i enter the extend or make object of attendence class in the student class then attendence class is not worked

import java.util.Scanner;

public class Attendence {
    private int c;
    public Attendence(){

    }
    public void project(){

        System.out.println("Enter the total no of students of the class");
        Scanner input=new Scanner(System.in);
        int c=input.nextInt();
        String[][] array=new String[c][6];

        for(int i=0; i<c; i++){
            for(int j=0;j<6;j++){
                  array[i][j]=input.next();
            }}
          System.out.println("RollNO \t       Name \t       Class \t     Attendence Mark \tTeacher Name \tSubject Name");
            for(int k=0; k<c; k++){
                for(int l=0;l<6;l++){

                     System.out.print(array[k][l]+    "\t\t"     );

                     }
                 System.out.println("\n");

            }
            }}

here this is second class

import java.util.Scanner;
public class student extends Attendence {


    private int password;
    private String ID;
    public student(int passsword , String ID){
        super();
        this.password= password;
        this.ID=ID;


    }



    public int getPassword() {
        return password;
    }



    public void setPassword(int password) {
        this.password = password;
    }



    public String getID() {
        return ID;
    }



    public void setID(String iD) {
        ID = iD;
    }



    public void mainfunction(){
        Scanner input=new Scanner(System.in);
        System.out.println("enter the password");
        int password=input.nextInt();

        System.out.println("enter the ID");
        String ID=input.next();
        if(password==123 || ID=="abc"){

            System.out.println("u enter right password and ID so you can acess the Attendance sheet");

        }
        else
            System.out.println("u enter wrong password and ID so you can't acess the Attendance sheet");
    }





    @Override
    public String toString(){
        return this.getPassword()+ this.getID()+super.toString();

    }


    public static void main(String[] args) {
        Attendence z = new Attendence();
        student a=new student(123, "abc");
        //a.mainfunction();

        a.mainfunction();

        z.project();
    }
}

It is working as expected. I tried to execute your code got following output . check it this is what you expect. enter the password
123
enter the ID
abc
u enter right password and ID so you can acess the Attendance sheet
Enter the total no of students of the class
1

2
xyz
2
2
2
abc
RollNO Name Class Attendence Mark Teacher Name S ubject Name
2 xyz 2 2 abc

or else ask precisely what you exactly want.

Call the Attendance class in if loop(if password and ID is correct)..and close the input(Scanner) object..

Student.class

import java.util.Scanner;

public class Student extends Attendence {
    private int password;
    private String ID;

    public Student(int passsword, String ID) {
        super();
        this.ID = ID;
    }

    public static void main(String[] args) {
        Student a = new Student(123, "abc");
        a.mainfunction();
    }

    public void mainfunction() {
        Scanner input = new Scanner(System.in);
        System.out.println("enter the password");
        int password = input.nextInt();
        System.out.println("enter the ID");
        String ID = input.next();
        if (password == 123 || ID == "abc") {
            System.out.println("u enter right password and ID so you can acess the Attendance sheet");
            Attendence z = new Attendence();
            z.project();
        } else {
            System.out.println("u enter wrong password and ID so you can't acess the Attendance sheet");
        }
        input.close();
    }

    @Override
    public String toString() {
        return this.getPassword() + this.getID() + super.toString();

    }

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }
}

Attendance.class

import java.util.Scanner;

public class Attendence {

    public void project() {
        System.out.println("Enter the total no of students of the class");
        Scanner input = new Scanner(System.in);
        Integer c = input.nextInt();
        String[][] array = new String[c][6];

        for (int i = 0; i < c; i++) {
            System.out.println("Enter the Roll No,Name,Class,Attendance Mark,Teacher Name,Student Name for student "+(i+1));
            for (int j = 0; j < 6; j++) {
                array[i][j] = input.next();
            }
        }
        System.out.println("RollNO \t Name \t Class \t Attendence Mark \tTeacher Name \t Subject Name");
        for (int k = 0; k < c; k++) {
            for (int l = 0; l < 6; l++) {
                System.out.print(array[k][l] + "\t\t");
            }
            System.out.println("\n");
        }
        input.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM