Ich bin derzeit in der Udacity Android Basics Nanodegree-Programm eingeschrieben. Eine Aufgabe besteht nun darin, eine ReportCard-Java-Klasse zu erstellen, die es einer Schule ermöglicht, die Noten eines Schülers für ein bestimmtes Jahr zu speichern. Während ich mein Projekt einreichte und es erfolgreich bestanden wurde, erhielt ich den Vorschlag, eine ArrayList zu verwenden, anstatt die Fächer, für die Noten vergeben werden konnten, hart zu codieren. Während ich diesen Vorschlag umsetze, habe ich extrem zu kämpfen. Ich habe StackOverflow schon stundenlang durchsucht und noch keine Problemlösung gefunden.So erstellen Sie eine ArrayList und initialisieren neue Byte-Variablen
So, hier ist mein Code:
package com.example.android.reportcardclass;
/**
* Report Card class
* This class prepares a student report card.
*/
public class ReportCard {
/**
* Class Attributes
*/
private String mStudentName;
private int mYear;
private byte mGerman;
private byte mEnglish;
private byte mMathematics;
private byte mGeography;
private byte mSocialSciencesAndEconomics;
private byte mPhysics;
private byte mReligion;
private byte mPhysEd;
private byte mMusic;
/**
* Default Constructor for ReportCard class
*
* @param studentName Name of the student
* @param year Year in which the student is in
* @param german Mark in the subject German
* @param english Mark in the subject English
* @param mathematics Mark in the subject Mathematic
* @param geography Mark in the subject Geography
* @param socialSciencesAndEconomics Mark in the subject Social Sciences and Economics
* @param physics Mark in the subject Physcics
* @param religion Mark in the subject Religion
* @param physEd Mark in the subject Physical Education
* @param music Mark in the subject Music
*/
public ReportCard(String studentName, int year,
byte german, byte english, byte mathematics,
byte geography, byte socialSciencesAndEconomics,
byte physics, byte religion, byte physEd,
byte music) {
mGerman = german;
mEnglish = english;
mMathematics = mathematics;
mGeography = geography;
mSocialSciencesAndEconomics = socialSciencesAndEconomics;
mPhysics = physics;
mReligion = religion;
mPhysEd = physEd;
mMusic = music;
}
/**
* This method is to get Student Name
*
* @return Student Name
*/
public String getStudentName() {
return mStudentName;
}
/**
* This method is to set Student Name
*
* @param studentName Name of the student
*/
public void setStudentName(String studentName) {
mStudentName = studentName;
}
/**
* This method is to get the year in which the student is in
*
* @return Year in which the student is in
*/
public int getYear() {
return mYear;
}
/**
* This method is to set the year in which the student is in
*
* @param year Year in which the student is in
*/
public void setYear(int year) {
mYear = year;
}
/**
* These are the setter and getter methods for the marks of the student in the respective subject
*/
public byte getGerman() {
return mGerman;
}
public void setGerman(byte german) {
mGerman = german;
}
public byte getEnglish() {
return mEnglish;
}
public void setEnglish(byte english) {
mEnglish = english;
}
public byte getMathematics() {
return mMathematics;
}
public void setMathematics(byte mathematics) {
mMathematics = mathematics;
}
public byte getGeography() {
return mGeography;
}
public void setGeography(byte geography) {
mGeography = geography;
}
public byte getSocialSciencesAndEconomics() {
return mSocialSciencesAndEconomics;
}
public void setSocialSciencesAndEconomics(byte socialSciencesAndEconomics) {
mSocialSciencesAndEconomics = socialSciencesAndEconomics;
}
public byte getPhysics() {
return mPhysics;
}
public void setPhysics(byte physics) {
mPhysics = physics;
}
public byte getReligion() {
return mReligion;
}
public void setReligion(byte religion) {
mReligion = religion;
}
public byte getPhysEd() {
return mPhysEd;
}
public void setPhysEd(byte physEd) {
mPhysEd = physEd;
}
public byte getMusic() {
return mMusic;
}
public void setMusic(byte music) {
mMusic = music;
}
@Override
public String toString() {
return "Student Name: " + getStudentName() + "\nYear: " + getYear() + "German: "
+ getGerman() + "\nEnglish: " + getEnglish() + "\nMathematics: " +
getMathematics() + "\nGeography: " + getGeography() +
"\nSocialSciencesAndEconomics: " + getSocialSciencesAndEconomics() +
"\nPhysics: " + getPhysics() + "\nReligion: " + getReligion() +
"\nPhysical Education: " + getPhysEd() + "\nMusic: " + getMusic();
}
}
Was ich will jetzt tun, ist es, die Klassenattribute am Anfang initialisiert zu speichern (mGerman, mEnglish, etc.) in einem Arraylist. Wie mache ich das?! Ich kämpfe extrem, da ich noch keine Werte setzen will, sondern einfach die Byte-Werte initialisiere (Grading-System in Deutschland geht von 0 bis 15 Punkte).
Bitte helfen Sie mir, vielen Dank im Voraus!
sollte wohl auf die Verwendung von HashMap als Schlüssel achten (Deutsch, Englisch ... als String oder Enum oder Objekt) und als Wert die Marke. Sie benötigen nur wenige Methoden, um Werte festzulegen. Damit können Sie sogar denken, dass ein Schüler manchmal nicht jeden Kurs belegt. Und wenn Sie das Objekt als Schlüssel verwenden, können Sie verfolgen, welcher Schüler dem Kurs folgt. – Timo
Verwenden Sie numerische Wrapper-Klassen anstelle des primitiven Typs. ('Byte' in Ihrem Fall) –