Black Horse Programmer-io Flow synthesis exercise

Source: Internet
Author: User

There are 5 students with 3 courses, input the above data from the keyboard (including the student's name and the results of the three classes), the input format is zhangsan,30,40,50,
The total score is calculated and the student's information and calculated scores are stored in the disk file A.txt in the order of high to low.
Analysis:
1. Describe student objects
2. Define a tool class that can manipulate student objects
Ideas:
1. Enter a row of data by getting the keyboard, and the data in that row is taken out and encapsulated into a student object.
2. Because students have a lot of objects, so long need to store, use to the collection.
And because the student's total score is to be sorted, so use TreeSet.
3. Write the information in the collection to a file.

 Public classStudent Implements comparable<student>{PrivateString name;Private intMa,cn,en;Private intSum Public Student(String name,intMaintAnnintEN) {super (); This. name = name; This. Ma = ma; This. CN = CN; This. en = en;    sum = Ma + cn + en; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getma() {returnMa } Public void Setma(intMA) { This. Ma = ma; } Public int GETCN() {returnAny } Public void SETCN(intCN) { This. CN = CN; } Public int Geten() {returnEn } Public void Seten(intEN) { This. en = en; } Public int Getsum() {returnSum } Public void Setsum(intSUM) { This. sum = sum; } @Override Public int CompareTo(Student s) {intnum =NewInteger ( This. Sum). CompareTo (NewInteger (s.sum));if(num==0)return  This. Name.compareto (S.name);returnNum } @Override Public int hashcode() {returnName.hashcode () +sum* to; } @Override PublicBooleanequals(Object obj) {if(! (obj instanceof Student))Throw NewClassCastException ("Type Mismatch"); Student s = (Student) obj;return  This. Name.equals (S.name) && This. sum==s.sum; } @Override PublicStringtoString() {return "Student [name="+ name +", ma="+ Ma +", cn="+ CN +", en="+ en +"]"; }} Public classStudentinfotool { Public StaticSet<student>getstudents() throws ioexception{returnGetstudents (NULL); } Public StaticSet<student>getstudents(comparator<student> cmp) throws ioexception{BufferedReader Bufr =NewBufferedReader (NewInputStreamReader (System.inch)); String line =NULL; Set<student> Stus =NULL;if(cmp==NULL) Stus =NewTreeset<student> ();ElseStus =NewTreeset<student> (CMP); while((Line=bufr.readline ())! =NULL){if("Over". Equals (line)) Break; String[] Info = line.split (","); Student stu =NewStudent (info[0],integer.parseint (info[1]), Integer.parseint (info[2]), Integer.parseint (info[3]));        Stus.add (Stu); } bufr.close ();returnStus; } Public Static void Write2file(Set<student> stus) throws ioexception{bufferedwriter BUFW =NewBufferedWriter (NewFileWriter ("E:\\a.txt")); for(Student s:stus) {Bufw.write (s.tostring () +"\ T"); Bufw.write (S.getsum () +"");            Bufw.newline ();        Bufw.flush ();    } bufw.close (); }} Public classstudentinfotest { Public Static void Main(string[] args) throws ioexception{comparator<student> cmp = Collections.reverseorder ();        Set<student> stus = studentinfotool.getstudents (CMP);    Studentinfotool.write2file (Stus); }}

Black Horse Programmer-io Flow synthesis exercise

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.