Lab 5-Methods 1. Please copy the following code into your IDE and ensure it comp
ID: 3755758 • Letter: L
Question
Lab 5-Methods 1. Please copy the following code into your IDE and ensure it compiles: public class Lab5 public static void main(Stringl] args) Scanner scan- new Scanner(System.in); public static double getSum(double numl, double num2)t return numl + num2; 2. In your main method, prompt the user for two numbers of type double, and print their sum using the getSum) method included above. Make sure to notify the user that what you are printing is the sum of the two numbers. (You can print the result of the sum in main method). 3. Create another method in the Lab5 class named getAverage0 that, similar to getSum0, accepts two double variables but returns their average (instead of sum) also as a double. Test this method by printing to the user the average of the two numbers they inputted.(You can print the result of the average in main method too) In your main method, prompt the user for a student's name, and two scores (a midterml and midterm2.) Save the name in a String variable and the two scores as doubles. Create a new method with the information you just acquired (you can name it totalGradel(name, getAverage))) This means you will send 2 arguments. You can assume the totalGradel of a student is the average of the midterml and the midterm2. Print the information the user entered through the new method created (Hint: public static void totalGrade10). 4.Explanation / Answer
Here is the code in Java
.............................................................................................................................................................
import java.util.Scanner;
public class Lab5 {
public static void main (String[] args) {
double firstNum, secondNum;
Scanner scan = new Scanner(System.in);
//Input numbers to find their sum
System.out.println("Enter first double number: ");
firstNum= Double.parseDouble(scan.nextLine());
System.out.println("Enter second double number: ");
secondNum= Double.parseDouble(scan.nextLine());
System.out.println("Sum of the numbers is : "+getSum(firstNum,secondNum));
// Input numbers to find the average
System.out.println("Enter first double number: ");
firstNum= Double.parseDouble(scan.nextLine());
System.out.println("Enter second double number: ");
secondNum= Double.parseDouble(scan.nextLine());
System.out.println("Average of the two numbers is : "+getAverage(firstNum, secondNum));
// Input name and scores
System.out.println("Enter students name: ");
String studentName = scan.nextLine();
System.out.println("Enter first double number: ");
double firstScore= Double.parseDouble(scan.nextLine());
System.out.println("Enter second double number: ");
double secondScore= Double.parseDouble(scan.nextLine());
// Prints name and average score
totalGrade1(studentName,getAverage(firstScore,secondScore));
totalGrade2(studentName,firstScore,secondScore);
}
/*
* Adds to numbers and return their sum
*/
public static double getSum(double num1, double num2) {
return num1+num2;
}
/*
* Returns the average of two numbers
*/
public static double getAverage(double num1, double num2) {
return (num1+num2)/2;
}
/*
* Prints student name and average score
*/
public static void totalGrade1(String name, double average) {
System.out.println("Student's name is : "+name+" and his average score is : "+average);
}
/*
* Prints student name and average score
*/
public static void totalGrade2(String name, double firstScore, double secondScore) {
System.out.println("Student's name is : "+name+" and his average score is : "+getAverage(firstScore,secondScore));
}
}
Here is a sample output
.....................................................................................................................................
Enter first double number:
5.9
Enter second double number:
4.1
Sum of the numbers is : 10.0
Enter first double number:
8.222
Enter second double number:
11.778
Average of the two numbers is : 10.0
Enter students name:
John Keeper
Enter first double number:
50
Enter second double number:
100
Student's name is : John Keeper and his average score is : 75.0
Student's name is : John Keeper and his average score is : 75.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.