Programming Assignment Refactoring is the process of taking existing code and im
ID: 3722341 • Letter: P
Question
Programming Assignment
Refactoring is the process of taking existing code and improving its design without changing the functionality. In this programming assignment, you are going to take your code from Programming Assignment and refactor it so that it uses functions.
The program should still do the following:
Get the name of a student
Get the name of three assignments
Get the grade of three assignments as integers
Calculates the average of the three assignments as a double
Displays the name of the student with his/her average to one decimal place
Displays the names and grades of the assignments neatly as a table.
However, you will now need to create 5 functions. You should write a function for each of the following:
Get the name of the student
Get the name of the three assignments
Get the grade of each of the three assignments
Get the average of the three assignments
Display the grade information to the student including the student’s name, average, and list of grades
As you can see, you basically need a function for each task your program performs. You will have a total of 6 functions including main. The main function should do nothing but create needed variables to pass to the functions and then call the functions in the correct order to produce the desired output.
Make sure you use good programming style. Comment every variable, calculation, and function. Use self-documenting names for variables and functions. You should be able to reuse most of your code that you wrote for Programming Assignment. You should use arrays for the assignment names and the assignment grades. The code will need to be moved into the appropriate function. Some minor changes may need to be made to account for passing data into the function and returning a value from the function.
Notice that the possible output for a sample run will look like this
Enter student first and last name: Roy Biv
Enter the name of assignment 1: Homework 1
Enter the name of assignment 2: Programming Lab 1
Enter the name of assignment 3: Exam 1
Enter the grade for Homework 1: 95
Enter the grade for Programming Lab 1: 88
Enter the grade for Exam 1: 100
The average for Roy Biv is 94.3
Here are your grades:
Homework 1: 95
Programming Lab 1: 88
Exam 1: 100
Thank you for playing.
Explanation / Answer
Ans :)
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter Student First and Last Name :");
//Getting the student name
String studentName=sc.next();
//Assign the size of assignments
String assignment[]=new String[3];
for(int i=0;i<3;i++){
System.out.println("Enter the name of assignment "+(i+1)+" :");
assignment[i]=sc.next();
}
//Assign the size of grades
int grade[]=new int[assignment.length];
for(int i=0;i<assignment.length;i++){
System.out.println("Enter the grade for "+assignment[i]+" :");
grade[i]=sc.nextInt();
}
Test4 t=new Test4();
t.studentAssignments(assignment);
t.studentGrades(grade, assignment);
t.studentInfo(t.getStudentName(studentName), grade, assignment, t.getAvg(grade));
}
public String getStudentName(String studentName){
return studentName;
}
public void studentAssignments(String assignments[]){
for(int i=0;i<assignments.length;i++){
System.out.println("Assignment "+(i+1)+" : "+assignments[i]);
}
}
public void studentGrades(int grades[],String assignments[]){
for(int i=0;i<assignments.length;i++){
System.out.println("Grade For "+assignments[i]+" : "+grades[i]);
}
}
public double getAvg(int grades[]){
// for avg
double avg=0;
// temp for total
int temp=0;
for(int i=0;i<grades.length;i++){
temp=temp+grades[i];
}
//calculation
avg=(double)temp/grades.length;
return avg;
}
public void studentInfo(String studentName,int grades[],String assignments[],double avg){
System.out.println("The Final OutPut is: ");
System.out.println("The Student Name is : "+studentName);
System.out.print(String.format("|%-30s|", "S.No."));
System.out.print(String.format("|%-30s|", "Assignment"));
System.out.print(String.format("|%-30s|", "Grade"));
for(int i=0;i<assignments.length;i++){
System.out.println();
System.out.print(String.format("|%-30d|", (i+1)));
System.out.print(String.format("|%-30s|", assignments[i]));
System.out.print(String.format("|%-30d|", grades[i]));
}
System.out.println();
System.out.println("The Average for "+studentName+" is : "+String.format("%.1f", avg));
}
}
Explanation : Please check the above code , is in java, bcz you didnt mention which language.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.