update,
This commit is contained in:
24
it114105/itp3914/Lab10/Ex2/Ex2b.java
Normal file
24
it114105/itp3914/Lab10/Ex2/Ex2b.java
Normal file
@@ -0,0 +1,24 @@
|
||||
public class Ex2b {
|
||||
public static void main(String[] args) {
|
||||
Student stud1 = new Student();
|
||||
Student stud2 = new Student();
|
||||
Student stud3 = new Student();
|
||||
|
||||
stud1.setId(310567);
|
||||
stud1.setName("Cheung Siu Ming");
|
||||
stud1.setScore(87.1);
|
||||
|
||||
stud2.setId(451267);
|
||||
stud2.setName("Ng Wau Man");
|
||||
stud2.setScore(77.5);
|
||||
|
||||
stud3.setId(789014);
|
||||
stud3.setName("Wong Sui Kai");
|
||||
stud3.setScore(83.4);
|
||||
|
||||
System.out.printf("Student: name=%s id=%d score=%.1f\n", stud1.getName(), stud1.getId(), stud1.getScore());
|
||||
System.out.printf("Student: name=%s id=%d score=%.1f\n", stud2.getName(), stud2.getId(), stud2.getScore());
|
||||
System.out.printf("Student: name=%s id=%d score=%.1f\n", stud3.getName(), stud3.getId(), stud3.getScore());
|
||||
System.out.println("Average Score =" + (stud1.getScore()+stud2.getScore()+stud3.getScore())/ 3);
|
||||
}
|
||||
}
|
15
it114105/itp3914/Lab10/Ex2/Ex2c.java
Normal file
15
it114105/itp3914/Lab10/Ex2/Ex2c.java
Normal file
@@ -0,0 +1,15 @@
|
||||
public class Ex2c {
|
||||
public static void main(String[] args) {
|
||||
Student[] students = new Student[3];
|
||||
students[0] = new Student("Cheung Siu Ming", 310567, 87.1);
|
||||
students[1] = new Student("CNg Wai Man", 451267, 77.5);
|
||||
students[2] = new Student("Wong Sui Kai", 789014, 83.4);
|
||||
double totalScore = 0.0;
|
||||
for (Student student : students) {
|
||||
student.printDetails();
|
||||
totalScore += student.getScore();
|
||||
}
|
||||
System.out.println("Average Score = " + totalScore/students.length);
|
||||
|
||||
}
|
||||
}
|
41
it114105/itp3914/Lab10/Ex2/Student.java
Normal file
41
it114105/itp3914/Lab10/Ex2/Student.java
Normal file
@@ -0,0 +1,41 @@
|
||||
public class Student {
|
||||
String name;
|
||||
int id;
|
||||
double score;
|
||||
|
||||
Student(){}
|
||||
|
||||
Student(String name, int id, double score) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setScore(double score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void printDetails(){
|
||||
System.out.println("Student: name="+name+" id=" + id + " score="+score);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user