4a. For this exercise, you will use the Scanner Class and the Math Class. Write
ID: 3586496 • Letter: 4
Question
4a. For this exercise, you will use the Scanner Class and the Math Class. Write a program that uses a Scanner object to read in two different numbers. Determine which is the largest and smallest of the two numbers. Print the number that is the largest of the two. Then subtract the smallest number from the largest of the two numbers and print this result. Calculate and print the square this result. Calculate and print the square of the small number. Then print out the largest of the two numbers. See below for sample output.
Type the first number: 10
Type the second number: 60
The largest number is 60.0
60.0 minus 10.0 is 50.0
The square of 50.0 is 2500.0
The square of 10.0 is 100.0
2500.0 is larger than 100.0
Test the program with three sets of data and include the results in this report. Do not concern yourself with controlling the display of the decimal point.
Program:
Output (3 different outputs):
4b. Write a program that uses a Scanner object that asks for your name and your examen, assignment and lab marks. The examen counts for 40%, assignments 40% and labs 20%. The program computes your grade percentage. Test the program with three sets of data and include the results in this report. Do not concern yourself with controlling the display of the decimal point.
Sample output:
Please enter your name: ..
Please enter your examen mark: ..
Please enter your assignment mark: ..
Please enter your lab mark: ..
Final grade: ..%
Program:
Output (3 different outputs):
SOLUTIONS AND OUTPUTS NEED TO BE IN JAVA FORMAT. (PLEASE SHARE YOUR SCREEN SHOT AS WELL).
NOTE: 3 OUTPUTS FOR EVERY QUESTION (3 OUTPUTS FOR 4A AND 3 OUTPUTS FOR 4B)
Explanation / Answer
4a)
FindLargestAmongTwo.java
import java.util.Scanner;
public class FindLargestAmongTwo {
public static void main(String[] args) {
//Declaring variables
double num1, num2, largest, smallest, res;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the first input entered by the user
System.out.print("Type the first number: ");
num1 = sc.nextInt();
//Getting the second input entered by the user
System.out.print("Type the second number: ");
num2 = sc.nextInt();
//finding the largest number
if (num1 > num2) {
largest = num1;
smallest = num2;
} else {
largest = num2;
smallest = num1;
}
//Displaying the output
System.out.println("The largest number is " + largest);
//calculating the difference
res = (largest - smallest);
System.out.println(largest + " minus " + smallest + " is " + res);
//calculating the square of numbers
num1 = Math.pow(res, 2);
num2 = Math.pow(smallest, 2);
//Displaying the output
System.out.println("The square of " + res + " is " + num1);
System.out.println("The square of " + smallest + " is " + num2);
if (num1 > num2)
System.out.println(num1 + " is larger than " + num2);
else
System.out.println(num2 + " is larger than " + num1);
}
}
_________________
Output:
Type the first number: 10
Type the second number: 60
The largest number is 60.0
60.0 minus 10.0 is 50.0
The square of 50.0 is 2500.0
The square of 10.0 is 100.0
2500.0 is larger than 100.0
__________________
4)a)
CalculateGradePercent.java
import java.util.Scanner;
public class CalculateGradePercent {
public static void main(String[] args) {
//Declaring variables
String name;
int examen, assignment, lab;
double final_grade;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Please enter your name:");
name = sc.nextLine();
System.out.print("Please enter your examen mark:");
examen = sc.nextInt();
System.out.print("Please enter your assignment mark:");
assignment = sc.nextInt();
System.out.print("Please enter your lab mark:");
lab = sc.nextInt();
//Calculating the final grade
final_grade = (examen * 0.40) + (assignment * 0.40) + (lab * 0.20);
//Displaying the output
System.out.print("Final grade:" + final_grade + "%");
}
}
___________________
Output:
Output#1
Please enter your name:Kane Williams
Please enter your examen mark:93
Please enter your assignment mark:89
Please enter your lab mark:78
Final grade:88.4%
_________________
Output#2:
Please enter your name:Mitchel Johnson
Please enter your examen mark:67
Please enter your assignment mark:78
Please enter your lab mark:71
Final grade:72.2%
_________________
Output#3:
Please enter your name:Billy Jones
Please enter your examen mark:56
Please enter your assignment mark:54
Please enter your lab mark:43
Final grade:52.6%
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.