Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please help me out with this excercise ? *Answer must be in Java pro

ID: 3863782 • Letter: C

Question

Can someone please help me out with this excercise ? *Answer must be in Java program format*

Set total to zero Set grade counter to one While grade counter is less than or equal to ten Prompt the user to enter the next grade Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Exercises Write the Java program corresponding to the above algorithm. Modify the program to read the data from a file called student.dat Extend the program to assign letter grades Extend the program to count how many students will get an A

Explanation / Answer

/* Program number 1*/

import java.util.Scanner;//IMPORTing for scanning input

class Grades{

public static void main(String []args){
Scanner sc=new Scanner(System.in);//scanner created to take input from stdin
int total = 0;//total sum
int counter = 1;//count of grades
int grade;//temprory variable

while(counter<=10)
{
System.out.println("Enter the next Grade");//asking input for grade in numerics
grade=sc.nextInt();//taking input
total+=grade;//adding to total
counter+=1;//raising the counter by 1
  
}
  
float class_Average = (float)total/10;//typecasting the average to float and dividing by 10 to take the average
System.out.println(class_Average+" ");//printing the average
}
}

/*When handling with a file*/

import java.util.Scanner;
import java.io.*;//taking the file

class Grades{

public static void main(String []args)throws FileNotFoundException{//new exception added as if file is not found the program might give FileNotFound exception
Scanner sc=new Scanner(new File("student.dat"));//making scanner object for taking input from the file
  
int total = 0;//total sum of grade
int counter = 1;//total count
int grade=0;//temporary grade
while(counter<=10)
{
//System.out.println("Enter the next Grade");
//grade=sc.nextInt();
  
if(sc.hasNextInt())//if file that is mentioned contains a next Integer
grade=sc.nextInt();//take that integer as input
  
total+=grade;//add grade to the total
counter+=1;//raise the counter
  
}
  
float class_Average = (float)total/10;
System.out.println(class_Average+" "); //same as part1
}
}

/*Part 3 : Assign letter grades*/


import java.util.Scanner;
import java.io.*;


class Grades{

public static void main(String []args){ // Grades are assigned as A or 10, B for 9, C for 8 and so on
Scanner sc=new Scanner(System.in);//taking input from the user

int total = 0;//total grades
int counter = 1;//counter
int grade=0;//temporary variable to take input
char grade_char;//temprory char input

while(counter<=10)
{
System.out.println("Enter the next Grade");
grade_char=sc.next().charAt(0);//to take the first char in the Line. In java while taking input as a char, you need to take it via sc.next(). This wil take a string. Now to take the first char, you use charAt(0)
grade=10 - (grade_char-'A'); //to take the value of grades, ascii value of A is reduced from the ascii value of grade . In this way we can have value 10 for A, 9 for B and so on
total+=grade; //Adding grades
counter+=1; //raising counter
  
}
  
float class_Average = (float)total/10; //same as previous program
int integralavg = (int)class_Average; // to convert the average of all the grades into an integer
int charGrade = 10-integralavg +'A'; // this function converts grades from numeric back to char form. This gives A for , B for 9 and so on. The results can be verified eaisly
  
System.out.println((char)charGrade); //printing the average grade in the form of a character
  
for(int i = 0 ; i < 10 ; i++)
System.out.println(map[i]);
}
}


/*Count number of A's*/
//this program is 99% similar to the previous program. The only difference is this program stores the value of grades in an array map. The same array is used to count the number of A's in the program


import java.util.Scanner;
import java.io.*;

class Grades{

public static void main(String []args){
Scanner sc=new Scanner(System.in);
int map[]=new int[10];//array declaration
  
int total = 0;
int counter = 1;
int grade=0;
char grade_char;

while(counter<=10)
{
System.out.println("Enter the next Grade");
grade_char=sc.next().charAt(0);
  
grade=10 - (grade_char-'A');
map[counter-1]=10 - (grade_char-'A');//it stores the numerical value of grades entered. As in 10 is stored in place of A . 9 for B and so on
  
total+=grade;
counter+=1;


  
}
  
float class_Average = (float)total/10;
int integralavg = (int)class_Average;
int charGrade = 10-integralavg +'A';
  
System.out.println((char)charGrade);//printing the average grade
int a_grades=0;
  
for(int i = 0 ; i < 10 ; i++)//loop counts the number of A's in the program in the variable a_grades
if(map[i]==10)
a_grades++;
System.out.println(a_grades);//printing number of A's
}
}