【发布时间】:2015-12-04 04:04:48
【问题描述】:
public class Exams {
private int score1 = 0;
private int score2 = 0;
private int score3 = 0;
public void setScore1(int sc){
score1 = sc;
}
public void setScore2(int sc){
score2 = sc;
}
public void setScore3(int sc){
score3 = sc;
}
public int getScore1(){
return score1;
}
public int getScore2(){
return score2;
}
public int getScore3(){
return score3;
}
public String toString(){
return String.format("%-10s %-10s %4.2f\n", score1, score2, score3);
}
}
public class Student {
private String fName;
private String lName;
private Exams scores;
Student(String fn, String ln) {
fName = fn;
lName = ln;
scores = new scores();
}
public void setScore1(int sc) {
scores.setScore1(sc);
}
public void setScore2(int sc) {
scores.setScore2(sc);
}
public void setScore3(int sc) {
scores.setScore3(sc);
}
public String toSring(){
return String.format("%-10s %-10s %4.2f\n", fName, lName, scores);
}
public double getAverage(){
}
public int compareTo(Student s){
String name1 = lName + " " + fName;
String name2 = s.lName + " " + s.fName;
return ((lName + " " + fName).compareTo(s.lName + " " + s.fName));
}
}
public class ClassRoll {
private ArrayList<Student> students = new ArrayList<Student>();
private String title;
private String filename = "data.txt";
ClassRoll(String f) {
Scanner kb = new Scanner(System.in);
String inpFileName = kb.next();
File inpFile = new File(inpFileName);
Student s = new Student(fName, lName);
}
void Remove() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
students.remove(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void Display() {
}
void Add() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
System.out.println("What is the Student's second score?");
int score2 = kb.nextInt();
System.out.println("What is the Student's third score?");
int score3 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
System.out.println("Student already in class");
} else {
students.add(s);
}
}
}
void changeScore1() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore1(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void changeScore2() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore2(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
void changeScore3() {
Scanner kb = new Scanner(System.in);
System.out.println("What is the Student's first name?");
String fName = kb.next();
System.out.println("What is the Student's last name?");
String lName = kb.next();
System.out.println("What is the Student's first score?");
int score1 = kb.nextInt();
Student s = new Student(fName, lName);
for (int i = 0; i < students.size(); i++) {
if (s.compareTo(students.get(i)) == 0) {
s.setScore3(i);
} else {
System.out.println("Error: Student is not in Class");
}
}
}
public void sortAverage() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = i + 1; j < students.size(); j++) {
Student s1 = (Student) students.get(i);
Student s2 = (Student) students.get(j);
if (s1.getAverage() < s2.getAverage()) {
students.set(i, s2);
students.set(j, s1);
}
}
}
}
public void sortNames() {
for (int i = 0; i < students.size() - 1; i++) {
for (int j = i + 1; j < students.size(); j++) {
Student s1 = (Student) students.get(i);
Student s2 = (Student) students.get(j);
if (s1.compareTo(s2) > 0) {
students.set(i, s2);
students.set(j, s1);
}
}
}
}
public void save(){
}
}
我有一个包含几个不同课程的程序。在声明私有变量之后的 classroll 类中,我必须创建一个 classroll 构造函数“ClassRoll(String f)”,假设为.....
从输入文件 f 中读取班级卷数据,为每个学生创建学生对象并将它们添加到学生的 ArrayList 中。输入文件的第一行包含课程名称。每个学生的数据显示在一个单独的行中,由名字、姓氏、score1、score 2 和 score3 组成,至少用一个空格分隔。
我尽我最大的努力开始它,但我很困惑,不知道正确的制作方法。有人可以帮忙吗
谢谢
【问题讨论】:
-
向我们展示您的努力程度。让汗水滴落!展示你的尝试(即使它不起作用)。
-
嘿,如果你的学生设置分数方法使用考试中的设置分数方法来实际更改分数会发生什么?深思熟虑。可能与您的实际问题无关。
-
@JaskaranbirSingh ' 扫描仪 kb = new Scanner(System.in);字符串 inpFileName = kb.next();文件 inpFile = new File(inpFileName);学生 s = 新学生;'
-
@JaskaranbirSingh 在被卡住之前我做了我能想到的最好的事情
-
你写了
sc = score1;——当然应该是score1 = sc;; score2 和 score3 相同。
标签: java class methods constructor