In: Computer Science
Convert this code written in Python to Java:
students = int(input("How many students are in your class?" ))
while students<0:
print("Invalid # of students, try again.")
students = int(input("How many students are in your class?" ))
tests = int(input("How many tests in this class? "))
while tests<0:
print("Invalid # of tests, try again.")
tests = int(input("How many tests in this class? "))
print("Here we go!")
# Here we are creating a list called class_average to store average of all students.
class_average = []
for i in range(1, students+1):
# Here we are creating a list called student_marks to store the marks of the student.
student_marks = []
print("**** Student #",i, "****")
for j in range(1,tests+1):
marks = int(input("Enter score for test #{}: ".format(j)))
while marks<0:
print("Invalid score, try again")
marks = int(input("Enter score for test #{}: ".format(j)))
# If marks is valid then we are adding it to student_marks list.
student_marks.append(marks)
# Here we are finding average of student marks and storing in student_average variable.
student_average = sum(student_marks) / len(student_marks)
# Here we are rounding of average to get perfect decimal
rounded_average = round(student_average, 3)
print("Average score for student #", i, "is ", rounded_average)
# After finding student average, we are adding it to class_average list.
class_average.append(rounded_average)
# Here we are calculating whole class average and storing in variable whole_class_average
whole_class_average = sum(class_average) / len(class_average)
# Here we are rounding off result to get it prefect decimal
rounded_class_average = round(whole_class_average, 2)
print("Average score for all students is: ", rounded_class_average)
JAVA Program:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
int students,tests;
double marks;
Scanner sc = new
Scanner(System.in);
System.out.print("How many students
are in your class?");
students = sc.nextInt();
while(students<0){
System.out.println("Invalid # of
students, try again.");
System.out.print("How many students
are in your class?");
students = sc.nextInt();
}
System.out.print("How many tests in
this class? ");
tests = sc.nextInt();
while(tests<0){
System.out.println("Invalid # of
tests, try again.");
System.out.print("How many tests in
this class? ");
tests = sc.nextInt();
}
System.out.println("Here we
go!");
//Here we are creating a list
called class_average to store average of all students.
ArrayList <Double>
class_average = new ArrayList<Double>();
for(int
i=1;i<=students;i++){
//Here we are creating a list
called student_marks to store the marks of the student.
ArrayList <Double>
student_marks = new ArrayList<Double>();
System.out.println("**** Student
#"+i+"****");
for(int j=1;j<=tests;j++){
System.out.print("Enter score for
test #"+j+": ");
marks = sc.nextDouble();
while(marks<0){
System.out.println("Invalid score,
try again");
System.out.print("Enter score for
test #"+j+": ");
marks = sc.nextDouble();
}
student_marks.add(marks);
}
double student_average=0;
for(int j = 0; j<
student_marks.size(); j++)
student_average += student_marks.get(j);
student_average /= student_marks.size();
System.out.printf("Average score for student # %d is
%.3f\n",i,student_average);
class_average.add(student_average);
}
double whole_class_average=0;
for(int j = 0; j<
class_average.size(); j++)
whole_class_average += class_average.get(j);
whole_class_average /= class_average.size();
System.out.printf("Average score for all students is:
%.2f",whole_class_average);
}
}
Output: