Problem:
Write a class Student having the following instance variables:
1. firstName (String), lastName (String), age (int), IDNumber (int), gender (int 0 for male and 1 for female), firstGrate (Double), secondGrade and thirdGrade.
Your class should include getter methods for all instance variables, setter methods for the grades and the constructor should allow the user to specify all values (except for the grades since all the other information should not be modified) when creating an instance of the class.
Next, you should write a calculateAverage method that calculates the average of the student and returns the value. Your toString method should return the following String:
[First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: ..., ... and ...].
In your tester method (StudentTest), you will create 3 different Student objects then calculate the average of each and the overall average.
In addition, you should compute the number of males and the number of females as well as the average age.
The output should be as follows:
First Student: [First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: and....] [Average: ...]
- Second Student: [First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: ..., ... and ...] [Average: ...]
- Third Student: [First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: ..., ... and ...] [Average: ...]
- Overall Average: ...
- Average age: ...
1. firstName (String), lastName (String), age (int), IDNumber (int), gender (int 0 for male and 1 for female), firstGrate (Double), secondGrade and thirdGrade.
Your class should include getter methods for all instance variables, setter methods for the grades and the constructor should allow the user to specify all values (except for the grades since all the other information should not be modified) when creating an instance of the class.
Next, you should write a calculateAverage method that calculates the average of the student and returns the value. Your toString method should return the following String:
[First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: ..., ... and ...].
In your tester method (StudentTest), you will create 3 different Student objects then calculate the average of each and the overall average.
In addition, you should compute the number of males and the number of females as well as the average age.
The output should be as follows:
First Student: [First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: and....] [Average: ...]
- Second Student: [First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: ..., ... and ...] [Average: ...]
- Third Student: [First Name: ...] [Last Name: ...] [Age: ...] [ID: ...] [Gender: ...] [Grades: ..., ... and ...] [Average: ...]
- Overall Average: ...
- Average age: ...
Output:
Not needed.
Solution:
public class Student { private String firstName; private String lastName; private int age; private long IDNumber; private int gender; private double firstGrade; private double secondGrade; private double thirdGrade; private double x; private double Average; public Student(String a, String b, int c, long d, int e, double f, double g, double h) { firstName = a; lastName = b; age = c; IDNumber = d; gender = e; firstGrade = f; secondGrade = g; thirdGrade = h; } public String getfirstName() { return firstName; } public String getlastName() { return lastName; } public int getage() { return age; } public long getIDNumber() { return IDNumber; } public int getgender() { return gender; } public double getfirstGrade() { return firstGrade; } public double getsecondGrade() { return secondGrade; } public double getthirdGrade() { return thirdGrade; } public void setfirstGrade(double x) { firstGrade = x; } public void setsecondGrade(double x) { secondGrade = x; } public void setthirdGrade(double x) { thirdGrade = x; } public double calculateAverage() { Average = firstGrade + secondGrade + thirdGrade; return Average; } public String toString() { return "First Student: [First Name: " + firstName + "] [Last Name: " + lastName + "] [Age: " + age + "] [ID: " + IDNumber + "] [Gender: " + gender + "] [Grades : " + firstGrade + ", " +secondGrade + " and " + thirdGrade + "] [Average: " + Average + "]"; } public static void main(String args[]) { Student first = new Student("Fouad", "Mouawad", 20, 200904935, 0, 89, 92, 92); System.out.println("\n" + first); Student second = new Student("Maha", "Diab", 10, 200904965, 1, 89, 92, 92); System.out.println("\n" + second); Student third = new Student("Amer", "Mouawad", 10, 200904975, 0, 89, 92, 92); System.out.println("\n" + third); double Overalaverage = ( first.getfirstGrade() + second.getfirstGrade() + third.getfirstGrade() + first.getsecondGrade() + second.getsecondGrade() + third.getsecondGrade() + first.getthirdGrade() + second.getthirdGrade() + third.getthirdGrade() ) / 9; System.out.println("\nOverall Average: " + Overalaverage); double AverageAge = ( first.getage() + second.getage() + third.getage() ) / 3; System.out.println("Average age: " + AverageAge); int Gender = first.getgender() + second.getgender() + third.getgender(); System.out.println("\nThere are " + Gender + " female(s) in the classe."); int male = 3-Gender; System.out.println("there are " + male + " male(s) in the classe."); } }
No comments :
Post a Comment