2016-09-29 5 views
0

私は2つのJavaファイルXCompanyShortlist.javaとStudentDemo.javaを作成しました。 XCompanyShortlist.javaには、メインメソッドと、学生登録番号、名前、学期、GPA、CGPA、支店名、配置ステータス、およびインターンシップステータスなどのすべてのユーザー入力が含まれています。サブクラスデータメンバの値が表示されないのはなぜですか?

StudentDemo.javaには、SuperクラスStudentDemoがあり、Regを初期化します。 No.、Name、Semester、GPA、CGPAを含み、そこにすべての情報を表示するメソッドdisplay()も含まれています。

クラスBranchStudentは、StudentDemoクラスを拡張し、BranchNameという名前の追加のStringを含みます。このクラスには、スーパークラスのdisplay()メソッドを呼び出し、BranchNameも出力するdisplay()メソッドも含まれています。別のクラスのStudentPlacementには、InternshipStatus、PlacementStatus、および優先企業リストの配列の変数が含まれています。ここで

はStudentDemo.javaファイルのコードです:ここで

class StudentDemo { 
    long RegNo; 
    String fname; 
    short sem; 
    float gpa; 
    float cgpa; 

    StudentDemo() { 
     RegNo = 0; 
     fname = ""; 
     sem = 0; 
     gpa = (float) 0.0; 
     cgpa = (float)0.0; 
    } 
    StudentDemo(long RegNo, String fname, short sem, float gpa, float cgpa) { 
     this.RegNo = RegNo; 
     this.fname = fname; 
     this.sem = sem; 
     this.gpa = gpa; 
     this.cgpa = cgpa; 
    } 
    StudentDemo(StudentDemo obj) { 
     RegNo = obj.RegNo; 
     fname = obj.fname; 
     sem = obj.sem; 
     gpa = obj.gpa; 
     cgpa = obj.cgpa; 
    } 
    void display() { 
     System.out.println("------------------------------------------"); 
     System.out.println("Registration No. :"+RegNo); 
     System.out.println("Full Name: "+fname); 
     System.out.println("Semester: "+sem); 
     System.out.println("GPA: "+gpa); 
     System.out.println("CGPA: "+cgpa); 
     System.out.println("------------------------------------------"); 
    } 
} 
class BranchStudent extends StudentDemo { 
    public String BranchName; 
    BranchStudent(long RegNo,String fname,short sem,float gpa,float cgpa,String BranchName) { 
     super(RegNo,fname,sem,gpa,cgpa); 
     this.BranchName = BranchName; 
    } 
    BranchStudent() { 
     super(); 
     BranchName = "CSE"; 
    } 
    BranchStudent(BranchStudent obj) { 
     super(obj); 
     BranchName = obj.BranchName; 
    } 

    void display() { 
     super.display(); 
     System.out.println("Student Branch: "+BranchName); 
    } 
} 
class StudentPlacement extends BranchStudent { 
    String compList[]; 
    int StatusPlacement, StatusIntern; 

    StudentPlacement() { 
     super(); 
     StatusPlacement = 0; 
     StatusIntern = 0; 
     compList = new String[3]; 
    } 

    StudentPlacement(StudentPlacement obj) { 
     super(obj); 
     StatusPlacement = obj.StatusPlacement; 
     StatusIntern = obj.StatusIntern; 
     compList = obj.compList; 
    } 

    StudentPlacement(long RegNo, String fname, short sem, float gpa, float cgpa, String BranchName,String compList[], int StatusPlacement,int StatusIntern) { 
     super(RegNo, fname, sem, gpa, cgpa, BranchName); 
     this.compList = compList; 
     this.StatusPlacement = StatusPlacement; 
     this.StatusIntern = StatusIntern; 
    } 
} 

はXCompanyShortlist.javaファイルのコードです:

import java.util.Scanner; 
public class XCompanyShortlist { 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Please Enter The Number Of Students: "); 
     int n = sc.nextInt(); 

     StudentPlacement obj[] = new StudentPlacement[n]; 
     for(int i = 0; i < n; i++) { 
      obj[i] = new StudentPlacement(); 
     } 
     System.out.println("Please Enter The Student Details: "); 
     for(int i = 0; i < n; i++) { 
      System.out.print("Please Enter The Reg. No. :"); 
      long RegNo = sc.nextLong(); 
      sc.nextLine(); 
      System.out.print("Please Enter The Full Name: "); 
      String fname = sc.nextLine(); 
      System.out.print("Please Enter The Semester: "); 
      short sem = sc.nextShort(); 
      System.out.print("Please Enter The GPA: "); 
      float gpa = sc.nextFloat(); 
      System.out.print("Please Enter The CGPA: "); 
      float cgpa = sc.nextFloat(); 
      System.out.print("Please Enter Branch Name:"); 
      String branchName = sc.nextLine(); 
      sc.nextLine(); 
      System.out.println("Please Enter 3 Preferred Choice: "); 
      String compList[] = new String[3]; 
      for(int x = 0; x < 3; x++) { 
       compList[x] = sc.nextLine(); 
      } 
      System.out.print("Please Enter The Status Of Placement(0/1): "); 
      int statusPlacement = sc.nextInt(); 
      System.out.print("Please Enter Status Of Internship(0/1): "); 
      int statusIntern = sc.nextInt(); 
      obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern); 
      System.out.println(); 
     } 
     for(int i = 0; i < n; i++) { 
      obj[i].display(); 
     } 
     sc.close(); 
    } 

} 

私が直面しています問題はStudentDemoからのすべての学生の詳細ということですスーパークラスは表示されていますが、BranchStudentサブクラスはBranchNameを印刷していません。自分のコードで問題を見つけることができません。

OUTPUT:

Please Enter The Number Of Students: 
1 
Please Enter The Student Details: 
Please Enter The Reg. No. :159101046 
Please Enter The Full Name: Bitan Basak 
Please Enter The Semester: 3 
Please Enter The GPA: 8.86 
Please Enter The CGPA: 8.64 
Please Enter Branch Name:CSE 
Please Enter 3 Preferred Choice: 
HP 
Dell 
Microsoft 
Please Enter The Status Of Placement(0/1): 0 
Please Enter Status Of Internship(0/1): 0 

------------------------------------------ 
Registration No. :159101046 
Full Name: Bitan Basak 
Semester: 3 
GPA: 8.86 
CGPA: 8.64 
------------------------------------------ 
Student Branch: 

これは私のプログラムによって与えられた出力です。あなたが見ることができるように、学生ブランチは印刷されていません。なぜ私は理解できません。無効ディスプレイで

+0

コードをデビューしようとしましたか? – Spotted

+1

^彼は**デバッグ**を意味しました。 – progyammer

答えて

3

を追加しているので、あなたはスーパー表示()メソッドを呼び出しているメソッドは何もしています継承を行うのではなく、コンストラクタに空の行を渡すことです。

これは、Scanner.nextLine()メソッドの使用に何か問題があることを意味します。あなたのコードを次のように変更した場合:

public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Please Enter The Number Of Students: "); 
     int n = sc.nextInt(); 

     StudentPlacement obj[] = new StudentPlacement[n]; 
     for(int i = 0; i < n; i++) { 
      obj[i] = new StudentPlacement(); 
     } 
     System.out.println("Please Enter The Student Details: "); 
     for(int i = 0; i < n; i++) { 
      System.out.print("Please Enter The Reg. No. :"); 
      long RegNo = sc.nextLong(); 
      sc.nextLine(); 
      System.out.print("Please Enter The Full Name: "); 
      String fname = sc.nextLine(); 
      System.out.print("Please Enter The Semester: "); 
      short sem = sc.nextShort(); 
      System.out.print("Please Enter The GPA: "); 
      float gpa = sc.nextFloat(); 
      System.out.print("Please Enter The CGPA: "); 
      float cgpa = sc.nextFloat(); 
      sc.nextLine(); 
      System.out.print("Please Enter Branch Name:"); 
      String branchName = sc.nextLine(); 
      System.out.println("Please Enter 3 Preferred Choice: "); 
      String compList[] = new String[3]; 
      for(int x = 0; x < 3; x++) { 
       compList[x] = sc.nextLine(); 
      } 
      System.out.print("Please Enter The Status Of Placement(0/1): "); 
      int statusPlacement = sc.nextInt(); 
      System.out.print("Please Enter Status Of Internship(0/1): "); 
      int statusIntern = sc.nextInt(); 
      obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern); 
      System.out.println(); 
     } 
     for(int i = 0; i < n; i++) { 
      obj[i].display(); 
     } 
     sc.close(); 
    } 

Ie.スキャナがブランチ名入力の前にsc.nextLine()を移動すると、スキャナはコンソールから正しい値を取得します。

希望に役立ちます。

ご挨拶

+0

私は愚かな間違いをしました。とにかく助けてくれてありがとう! –

0

()スーパー表示()メソッドは、そのブランチの表示方法をいない呼び出して、私は問題を伝えることができるものからthis.branchname

+1

それは間違っています。 'super.display();'は単に親クラスの 'display();'バージョンを呼び出し、それ自身のバージョンを処理します。 – kevto

+0

@kevtoが正しく表示されているのは、 "Student Branch:' "という見出しが出力に表示されているということです(コロンが欠落しているはずの実際のブランチ名のみ)。 –

関連する問題