简体   繁体   中英

How to work with array,a value to hold an array of elements

   public class Student implements Comparable<Student> {
        String fName;
        String sName;
        String subject[];
        int subjectValuation;
         public Student(String fName, String sName, String[] subject, int subjectValuation) {
            this.fName = fName;
            this.sName = sName;
            this.subject = subject;
            this.subjectValuation = subjectValuation;
        }

        public String[] getSubject() {
            return subject;
        }

        public String getfName(){
            return fName;

        }
        public String getsName(){
            return sName;
        }

        public void setSubject(String[] subject) {
            this.subject = subject;
        }

        public int getSubjectValuation(){
            return subjectValuation;
        }
        public void setSubjectValuation(int subjectValuation){
            this.subjectValuation=subjectValuation;
        }
        public String toString(){
            return " fname "+getfName()+ " sname " +getsName()+ " subjects "+subject+ " valuation "+subjectValuation;
        }
        public boolean equals(Object o){
            if(o!=null && o instanceof Student){
                Student s=(Student) o;
                if(this.getfName().equalsIgnoreCase(s.fName) && this.getsName().equalsIgnoreCase(s.sName))
                    return true;
                else
                    return false;
            }
            return false;
        }
        public int compareTo(Student o){
            return this.subjectValuation-o.subjectValuation;
        }


        }






              import java.util.ArrayList;
              import java.util.Collections;
              import java.util.Iterator;

               public class Catalogue {
              private ArrayList<Student> s=new ArrayList<>();
              //performing 1.
            void add(String firstName,String secondName){
                s.add(new Student(firstName,secondName,[0],0));
            }
              //performing 2
            void add2(String firstname,String secondName,String subject[],int valuation){
                Student accSearch=search(firstname,secondName);
                if(accSearch!=null) {
                    accSearch.setSubject(subject);
                    accSearch.setSubjectValuation(valuation);
                }
                else{
                    System.out.println("couldn't find it");
                }

            }
                 //performing 3
            void delete(String firstName,String secondName){
                Student n=new Student(firstName,secondName,[0],0);
                    while(s.contains(n)) {
                        s.remove(n);
                    }

                }
                   //searching my Student
            public Student search(String firstName,String secondName){
                for(Student m:s){
                    if(m.getfName().equalsIgnoreCase(firstName) && m.getsName().equalsIgnoreCase(secondName))
                        return m;
                }
                return null;
            }
                public void listInformation(String firstName,String secondName){
                  for(Student m:s){
                      if(m.getfName().equalsIgnoreCase(firstName) && 
              m.getsName().equalsIgnoreCase(secondName))
                        System.out.println(m);

                }
            }

            void printall(){
                for(Student m:s){
                    System.out.println(m);
                }
            }
                  //performing 4.
            void listValuation(String firstName,String secondName) {
                Student searchingFor = search(firstName, secondName);
                Iterator<Student> i = s.iterator();
                while (i.hasNext()) {
                    Student s = i.next();
                    if (s.getfName().equalsIgnoreCase(firstName) && s.getsName().equalsIgnoreCase(secondName))
                        System.out.println(s.getSubjectValuation());
                }
             ``}
                  //the average not sure how am i going to do it
             void average(String firstName,String secondName){
                Student s=search(firstName,secondName);


            }
             void sort(){
                Collections.sort(s);



             }
           }
public static void main(String[] args) {
        Catalogue c=new Catalogue();
        c.add("a","b");
        c.add("b","c");
        c.add("d","f");
        c.add("a","b");
       // c.listInformation("a","b");
        c.delete("a","b");
        System.out.println("+");
        c.printall();
        c.add2("d","f","math",9);
        c.printall();
        c.add2("f","a","asdsdfd",1);
        System.out.println("++");
        c.printall();
        System.out.println("++++");
       c.listValuation("d","f");
   }
}

The above class was my Catalogue class.

I have to perform these tasks:

  1. Add a student;
  2. Add for a student subjects and notes;
  3. Delete a student;
  4. Search a student after name and print every grade for every subject;
  5. search a student after name and calculate the average of subject;

I don't know how to assign an array of subjects and grades for student for example I have a student named Jon Jon he can have multiple subjects like Math1,Math2,Math3.... and grades like Math1 grade is 1 Mah2 grade is 2 .....

The 5. is not implemented but I am more interested in how to implement the array of subjects and notes

If I understood your question right you're asking how to combine subjects and grades and put these into an array. One way you could solve this is implementing a seperate class Subject which also got grades as a attribute .

public class Subject {
    private String subject;
    private float grade;

    //getter and setter methods
}

You can use this class in your class Student :

public class Student implements Comparable<Student> {
        String fName;
        String sName;
        Subject subject[]; //array which can contain multiple subjects. each 
                           //entry got two attributes: the subject itself as String; the grade

        //your code
}

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