Java Learning Phase II-encapsulation-classes and students

Source: Internet
Author: User

 Packagep05;ImportJava.util.Scanner; Public classProgram { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSchoolclass J128 =NewSchoolclass ("J128", "July 2016"); J128.addstudent (NewStudent (201601, "half-hidden",true, 22)); J128.addstudent (NewStudent (201602, "Hunting Empty",false, 20)); J128.addstudent (NewStudent (201603, "Amy",false, 20)); J128.addstudent (NewStudent (201604, "source",true, 24)); J128.addstudent (NewStudent (201605, "McRae",true, 25)); //User Input Study number find studentsScanner input =NewScanner (system.in); System.out.print ("Please enter a number to find the student:"); intStudentid=Input.nextint (); BooleanIsfind=false;  for(Student item:J128.getStudents ()) {if(studentid==Item.getstudentid ())                {Item.print ();                System.out.println (); Isfind=true;  Break; }        }        if(!isfind) {System.out.println ("There is no such student!" \ n "); }        //output the class number and all of its student informationJ128.print ();        System.out.println (); //Let the user enter an age to find and output all students older than that ageSystem.out.print ("Please enter an Age:"); intAge=Input.nextint (); BooleanIsfindage=false;  for(Student item:J128.getStudents ()) {if(Item.getage () >Age ) {System.out.println ("Student" +item.getname () + "age greater than" +Age ); Isfindage=true; }        }        if(!isfindage) {System.out.println ("No students older than" +age+ "are found.); }    }}
 Packagep05; Public classSchoolclass {PrivateString number; PrivateString Classtime; Privatestudent[] students;  Publicstudent[] Getstudents () {returnstudents; }     Public voidsetstudents (student[] students) { This. Students =students; }     PublicString GetNumber () {returnNumber ; }     Public voidSetnumber (String number) { This. Number =Number ; }     PublicString Getclasstime () {returnClasstime; }     Public voidsetclasstime (String classtime) { This. Classtime =Classtime; }         PublicSchoolclass (String number,string classtime) {setnumber (number);        Setclasstime (Classtime); Setstudents (NewStudent[0]); }         Public voidaddstudent (Student newstudent) {student[] newstudents=NewStudent[getstudents (). length+1];  for(inti = 0; I < getstudents (). length; i++) {Newstudents[i]=getstudents () [i]; } newstudents[getstudents (). length]=newstudent;    Setstudents (newstudents); }         Public voidprint () {System.out.println ("Class Number:" +GetNumber ());  for(Student item:students) {item.print (); }    }}
 Packagep05; Public classStudent {Private intStudentID; PrivateString name; Private BooleanIsmale; Private intAge ;  PublicString Getismale () {if(Ismale) {returnMale; }        Else {            returnFemale; }    }     Public voidSetmale (BooleanIsmale) {         This. Ismale =Ismale; }     Public intGetstudentid () {returnStudentID; }     Public voidSetstudentid (intStudentID) {         This. StudentID =StudentID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }         PublicStudent (intStudentid,string name,BooleanIsmale,intAge )        {Setmale (Ismale);        Setstudentid (StudentID);        SetName (name);    Setage (age); }         Public voidprint () {System.out.println ("Student Number:" +getstudentid () + ", Name:" +getname () + ", Gender:" +getismale () + ", Age:" +getage ()); }    }

Java Learning Phase II-encapsulation-classes and students

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.