Jak obliczyć średnią z ArrayList'y, która jest typu innej klasy??

0

Witam,
mam problem z policzeniem średniej wszystkich ocen z przedmiotu w ArrayList, która znajduje się w klasie Student. Oceny zawarte są w klasie Przedmiot. Da się tu jakoś użyć rzutowania? Wklejam kod i proszę o pomoc.

 
import java.util.ArrayList;


public class Przedmiot{
	 private String nazwa;
    private ArrayList<Integer> oceny;

    	
       public Przedmiot() {
		super();
		// TODO Auto-generated constructor stub
	}


		public Przedmiot(String nazwa, ArrayList<Integer> oceny) {
		super();
		this.nazwa = nazwa;
		this.oceny = oceny;
	}
       
       
       public String getNazwa(){
       	return nazwa;
       }
       
       
       public void setNazwa(String nazwa) {
			this.nazwa = nazwa;
		}


       public ArrayList<Integer> getOceny() {
           return oceny;
       }

       public void setOceny(ArrayList<Integer> oceny) {
           this.oceny = oceny;
       }

       public float psr_p(ArrayList<Integer> oceny){
    	   this.oceny = oceny;
   			float a = oceny.get(0);
   			float b = oceny.get(1);
   			float d = oceny.size();
   			float c = (a+b)/d;
   			return c;
   	}
   


}

package pl.edu.wat.wcy.jtp.lab1;
import java.util.ArrayList;


public class Student{
    private String m_imieStudenta;
    private String m_nazwiskoStudenta;
    private String m_PESELStudenta;
    ArrayList<Przedmiot> przedmiot;

    public Student(){}

	public Student(String m_imieStudenta, String m_nazwiskoStudenta,
			String m_PESELStudenta, ArrayList<Przedmiot> przedmiot) {
		super();
		this.m_imieStudenta = m_imieStudenta;
		this.m_nazwiskoStudenta = m_nazwiskoStudenta;
		this.m_PESELStudenta = m_PESELStudenta;
		this.przedmiot = przedmiot;
	}

	public String getM_imieStudenta() {
		return m_imieStudenta;
	}

	public void setM_imieStudenta(String m_imieStudenta) {
		this.m_imieStudenta = m_imieStudenta;
	}

	public String getM_nazwiskoStudenta() {
		return m_nazwiskoStudenta;
	}

	public void setM_nazwiskoStudenta(String m_nazwiskoStudenta) {
		this.m_nazwiskoStudenta = m_nazwiskoStudenta;
	}

	public String getM_PESELStudenta() {
		return m_PESELStudenta;
	}

	public void setM_PESELStudenta(String m_PESELStudenta) {
		this.m_PESELStudenta = m_PESELStudenta;
	}

	public ArrayList<Przedmiot> getPrzedmiot() {
		return przedmiot;
	}

	public void setPrzedmiot(ArrayList<Przedmiot> przedmiot) {
		this.przedmiot = przedmiot;
	}

    
	public float psr_s(ArrayList<Przedmiot> przedmiot){
		this.przedmiot=przedmiot;
		Przedmiot a = przedmiot.get(0);
		Przedmiot b = przedmiot.get(1);
		float d = przedmiot.size();
		float c = 				//???
		
		return c;
	}
    

}


package pl.edu.wat.wcy.jtp.lab1;
import java.util.ArrayList;


public class Main{
public static void main(String[] args) {
    	
    	ArrayList<Integer> lista = new ArrayList<Integer>();		//tworzona jest lista ocen z przedmiotu
        lista.add(3);		
        lista.add(4);
        Przedmiot p1 = new Przedmiot("analiza matematyczna", lista);
        
    	
        ArrayList<Integer> lista3 = new ArrayList<Integer>();
        lista3.add(2);
        lista3.add(4);
        Przedmiot p3 = new Przedmiot("analiza matematyczna", lista3);
        
        
        ArrayList<Integer> lista2 = new ArrayList<Integer>();
        lista2.add(4);
        lista2.add(2);
        Przedmiot p2 = new Przedmiot("matematyka dyskretna", lista2);
       
        ArrayList<Integer> lista4 = new ArrayList<Integer>();
        lista4.add(3);
        lista4.add(5);
        Przedmiot p4 = new Przedmiot("matematyka dyskretna", lista4);
        ArrayList<Przedmiot> list_przed = new ArrayList<Przedmiot>();
        list_przed.add(p1);
        list_przed.add(p2);
        
        Student s1 = new Student("Jan", "Kowalski", "92120287639", list_przed);
        
        ArrayList<Przedmiot> list_przed2 = new ArrayList<Przedmiot>();
        list_przed2.add(p3);
        list_przed2.add(p4);
        
        
        Student s2 = new Student("Karol", "Nowak", "91070820657", list_przed2);
        
        System.out.printf(" "+s1.getM_imieStudenta()+" " +s1.getM_nazwiskoStudenta()+" "+s1.getM_PESELStudenta());
        System.out.printf(" "+p1.getNazwa()+" "+p1.getOceny());
        System.out.println(" "+p2.getNazwa()+" "+p2.getOceny());
        System.out.println("Srednia z przedmiotu "+p1.getNazwa()+" wynosi: "+p1.psr_p(lista));
        System.out.println("Srednia z przedmiotu "+p2.getNazwa()+" wynosi: "+p2.psr_p(lista2));
        
        System.out.printf(" "+s2.getM_imieStudenta()+" "+s2.getM_nazwiskoStudenta()+" "+s2.getM_PESELStudenta());
        System.out.printf(" " +p3.getNazwa()+" "+p3.getOceny());
        System.out.println(" "+p4.getNazwa()+" "+p4.getOceny());
        System.out.println("Srednia z przedmiotu "+p1.getNazwa()+" wynosi: "+p1.psr_p(lista3));
        System.out.println("Srednia z przedmiotu "+p2.getNazwa()+" wynosi: "+p2.psr_p(lista4));
	     
    }
}


0

Dlaczego nie liczysz w pętli?

ArrayList<Double> oceny = new ArrayList<Double>();
Collections.addAll(oceny, 2.0, 4.0, 3.5, 5.0, 4.0);
		
double srednia = 0.0;

for (Double d : oceny)
	srednia += d; 
srednia /= oceny.size();
0

Imo, program jest lekko skopany. W klasie Przedmiot brak metody pozwalającej policzyć średnią ocen na podstawie istniejącego w klasie pola ArrayList<Integer> oceny. Oceny powinny być (jak zauważył już @Linxaddic) typu zmiennoprzecinkowego.

0

W dalszym ciągu nie rozumiem jak powinnienem utworzyć tą metodę w klasie Student. Nie chciało mi się już tych int'ów zamieniać na float'y.

0

Metoda w klasie Przedmiot:

public float srednia()
{
    float suma = 0.0;
    for (Integer d : oceny)
        suma += d; 
    return suma /oceny.size();    
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1