In: Computer Science
import java.util.Scanner;
public class TestScore
{
public static void main(String[] args) {
String firstName;
String lastName;
int numTest;
int i;
int score;
double totalScore;
double avgScore;
String grade;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Name");
firstName = input.nextLine();
System.out.println("Enter Last Name");
lastName = input.nextLine();
System.out.println("How many test score do you want to enter: ");
numTest = input.nextInt();
while(!input.hasNextDouble())
{
numTest = input.nextInt();
System.out.println("Invalid entry, try again");
}
totalScore = 0;
for (i = 0; i <= numTest - 1; i++) {
score = input.nextInt();
totalScore = totalScore + score;
}
avgScore = totalScore / numTest;
if (avgScore >= 90) {
grade = "A";
} else {
if (avgScore >= 80) {
grade = "B";
} else {
if (avgScore >= 75) {
grade = "C";
} else {
if (avgScore >= 50) {
grade = "D";
} else {
grade = "F";
}
}
}
}
System.out.println(firstName + " " + lastName + " " + avgScore + " " + grade);
}
}
Updated code with OOP's classes is given below-
There are 2 classes- First is PersonalInfo and Second is Marks class. And then, we have a class Main, that has main method.
import java.util.Scanner;
class PersonalInfo{
String firstName;
String lastName;
public PersonalInfo(){
firstName="";
lastName="";
}
public void setFirstName(String fname){
firstName=fname;
}
public void setLastName(String lname){
lastName=lname;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
}
class Marks{
int numTest;
int i;
int score;
double totalScore=0;
double avgScore;
String grade;
public void setNumTest(int n){
numTest=n;
}
public void setScore(int s){
score=s;
totalScore=totalScore+s;
}
public int getNumTest(){
return numTest;
}
public int getScore(){
return score;
}
public void calResult(){
avgScore=totalScore/numTest;
if (avgScore >= 90) {
grade = "A";
} else {
if (avgScore >= 80) {
grade = "B";
} else {
if (avgScore >= 75) {
grade = "C";
} else {
if (avgScore >= 50) {
grade = "D";
} else {
grade = "F";
}
}
}
}
System.out.println("Your average is: "+avgScore);
System.out.println("Your grade is: "+grade);
}
}
public class Main
{
public static void main(String[] args) {
String firstName, lastName, grade;
int numTest, i, score;
double totalScore, avgScore;
PersonalInfo p=new PersonalInfo();
Marks m=new Marks();
Scanner input = new Scanner(System.in);
System.out.println("Enter First Name");
firstName = input.nextLine();
p.setFirstName(firstName);
System.out.println("Enter Last Name");
lastName = input.nextLine();
p.setLastName(lastName);
System.out.println("How many test score do you want to enter:
");
numTest = input.nextInt();
if(numTest<=0){
while(numTest<=0){
System.out.println("Enter correct number of tests!!!");
numTest = input.nextInt();
}
}
m.setNumTest(numTest);
for (i = 0; i <= numTest - 1; i++) {
System.out.println("Enter the marks for test "+(i+1)+":");
score = input.nextInt();
m.setScore(score);
}
m.calResult();
System.out.println("For Student with firstName:
"+p.getFirstName()+" and LastName: "+p.getLastName());
}
}
UML Class Diagram for each class is given below-
1) PersonalInfo class
2) Marks class
3) Main class
Do tell if there is any problem in answer!!! Will take care in future :)