STEP 1. We should have user to enter a value for score and gpa 1. Tell student t
ID: 3640291 • Letter: S
Question
STEP 1.We should have user to enter a value for score and gpa
1. Tell student to enter the score. Get it and parse it using Double.parseDouble( s ) method
2. Tell student to enter gpa. Get it and parse it using Double.parseDouble( s) method
STEP2.
The assignment asks you to write a method called ‘accepted’ that takes in two arguments, one for score and one for gpa
and returns voikd. We should have the following logic in the accepted method.
If(score >85 ) student is accepted, no matter what the gpa is
If( score >= 60.0 and gpa >=3.0), student is accepted
If(score <60 or gpa <3),student is rejected
If user enters a value >100 or < 0 for score, then send an error message
If use enters a value <0 or > 4.0 for gpa, then print a message indicating that it is invalid
HOW TO WRITE THE CODE?
There can be different implementations of this assignment. It is given to us in the rubric that the header of the accepted
method should be:
public void accepted( double score, double gpa)
The way I have solved is as:
Written two Java classes:
1. Admissions.java
2. AdmissionsTest.java
Admissions.java
In Admissions class, I have two private fields and one static field of the type boolean. I am setting this boolean variable
‘isAccepted’ to be true, if it meets the condition for admission. If it does not, I am setting it to false. Then in the
AdmissionsTest.java class I am checking the value of this boolean variable to determine whether the user is accepted or
rejected and displaying appropriate messages.
In the AdmissionTest.java class, I am asking user to enter the score and gpa. I convert them into double values and call
the ‘accepted’ function. I am also checking if the user is entering gpa and score with in the correct range. If not, I am
showing them the appropriate message and closing the application. The code I have written is on the following lines.
Explanation / Answer
public class Admissions {
private boolean accepted=false;
public Admissions() {
input(new java.util.Scanner(System.in));
}
void accepted(double score, double gpa) {
if (score > 85) {
accepted = true;
}
if (score >= 60.0 && gpa >= 3.0) {
accepted = true;
System.out.println("I am here");
}
if (score < 60 || gpa < 3) {
accepted = false;
}
System.out.println(accepted);
}
boolean isAccepted() {
return accepted;
}
void input(java.util.Scanner input) {
System.out.print("Enter Score : ");
double score = input.nextDouble();
if (score > 100 || score < 0) {
System.out.println("Invalid Score, shoud be in range [0-100]");
System.exit(0);
}
System.out.print("Enter GPA : ");
double gpa = input.nextDouble();
if (gpa < 0 || gpa > 4.0) {
System.out.println("Invalid GPA, shoud be in range [0-4.0]");
System.exit(0);
}
accepted(score, gpa);
}
}
public class AdmissionsTest {
public static void main(String[] args) {
Admissions admin = new Admissions();
if (admin.isAccepted()) {
System.out.println(" Student Accepted");
} else {
System.out.println(" Student Rejected");
}
}
}
public class Admissions {
private boolean accepted=false;
public Admissions() {
input(new java.util.Scanner(System.in));
}
void accepted(double score, double gpa) {
if (score > 85) {
accepted = true;
}
if (score >= 60.0 && gpa >= 3.0) {
accepted = true;
System.out.println("I am here");
}
if (score < 60 || gpa < 3) {
accepted = false;
}
System.out.println(accepted);
}
boolean isAccepted() {
return accepted;
}
void input(java.util.Scanner input) {
System.out.print("Enter Score : ");
double score = input.nextDouble();
if (score > 100 || score < 0) {
System.out.println("Invalid Score, shoud be in range [0-100]");
System.exit(0);
}
System.out.print("Enter GPA : ");
double gpa = input.nextDouble();
if (gpa < 0 || gpa > 4.0) {
System.out.println("Invalid GPA, shoud be in range [0-4.0]");
System.exit(0);
}
accepted(score, gpa);
}
}
public class AdmissionsTest {
public static void main(String[] args) {
Admissions admin = new Admissions();
if (admin.isAccepted()) {
System.out.println(" Student Accepted");
} else {
System.out.println(" Student Rejected");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.