Question

In: Computer Science

Convert this code written in Python to Java: students = int(input("How many students are in your...

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)

Solutions

Expert Solution

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:


Related Solutions

Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):     count=2;     flag=0;     # Loop to check the divisors of a number.     while(count<num and flag==0):         if(num%count!=0):             # Put flag=0 if the number has no divisor.             flag=0         else:             # Put flag=1 if the number has divisor.             flag=1         # Increment the count.         count=count+1     # Return flag value.     return flag # Intialize list. list=[]...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
Answer in Python: show all code 3) Modify your code to take as input from the...
Answer in Python: show all code 3) Modify your code to take as input from the user the starting balance, the starting and ending interest rates, and the number of years the money is in the account. In addition, change your code (from problem 2) so that the program only prints out the balance at the end of the last year the money is in the account. (That is, don’t print out the balance during the intervening years.) Sample Interaction:...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT