M1L2
MODULE 1 LESSON 2
THIS IS MY JAVA AND C PROGRAM
IM NOT SURE ABOUT THE C PROGRAM
M1L2 ACTIVITY
Chooses Question:
1.1.
(Gas Mileage). Drivers and operators
are concerned with the mileage of their vehicles get. A driver has kept track
of several tankful of fuel by recording the miles driven and gallons used for
each tankful. Develop an application that will input the miles driven and
gallons used for each tankful. The program should calculate and display the
miles per gallon obtained for each tankful and display the combined miles per
gallon obtained for all tankful up to this point. All averaging calculations
should produce a floating – point results.
You need to use a sentinel -controlled loop to obtain arbitrary number
of entries of data from the user.
THIS IS THE BLUE PRINT
|
Problem: Create a computer program that asks for two integer inputs. The program will determine the sum of the two values and display the result. |
||
|
Input Requirements: First Score: 10 SecondScore: 30 ThirdScore:60 FourthScore: 100 FifthScore:150 SixthScore:210 SeventhScore:280 EighthScore:360 NinthScore: 450 TenthScore: 550 |
Output Requirements/Specifications: First Score: 10/1 SecondScore: 20/2 ThirdScore:30/3 FourthScore:40/4 FifthScore: 50/5 SixthScore:60/6 SeventhScore:70/7 EighthScore:80/8 NinthScore:90/9 TenthScore:100/10 |
Constants and Literals, formulas: int Miles = 0; int Miles1 = 0; int Liter = 0; int Liter1 = 0; float driven = (float) 0.0; float totalofdrive = (float) 0.0; int average = 0; int average2 = 0; int totalaverage = 0; |
|
Processing Logic / Solution / Algorithm (Pseudocode or Flowchart): The program lets you add all the gallons used in every trip which it will add the old score to the new score 10+20= 30 + 30 = 60 + 40 = 100 +50 = 150 +60 =210 + 70 = 280 + 80 = 360 + 90 = 450 + 100 = total 550 BEGIN Step 1: Enter miles per gallon Step 3: the score is added Step 5: Display the total gallon used with the value of miles per gallon. END |
||
JAVA
/* Name of Program: Application2.java
Date of Submission: 01/07/2021
Programmer: JONATHAN JULES L. SERATO
Purpose: This Program aims to calculate the gallons used on a trip.
*/
import java.util.Scanner;
public class M1L2 {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int Miles = 0;
int Miles1 = 0;
int Liter1 = 0;
float totalofdrive = (float) 0.0;
int average = 0;
int average2 = 0;
int totalaverage = 0;
while (Miles != -1) {
System.out.print(" Enter Miles per Gallon or -1 to exit: ");
Miles = input.nextInt();
if (Miles == -1) break;
System.out.print(" Enter the Gallon used or -1 to exit: ");
int Liter = input.nextInt();
totalaverage++;
Miles1 = Miles1 + Miles;
Liter1 = Liter1 + Liter;
totalofdrive = (float) Miles1 / Liter1;
float driven = (float) Miles / Liter;
System.out.println(" The Miles per Gallon is " + driven);
System.out.println(" ");
if ( totalaverage > 1 ) {
System.out.println( " The Total Miles trip is " + Miles1);
System.out.println( " The Total Gallon used in the trip is " + Liter1);
}
} }
System.out.println(" ");
System.out.print("THANK YOU AND GODBLESS");
}
}
C PROGRAM
import java.util.Scanner;
public class M1L2 {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
float Miles = 0;
float Miles1 = 0;
float Liter1 = 0;
float totalofdrive = (float) 0.0;
float average = 0;
float average2 = 0;
float totalaverage = 0;
while (Miles != -1) {
System.out.print(" Enter Miles per Gallon or -1 to exit: ");
Miles = input.nextInt();
if (Miles == -1) break;
System.out.print(" Enter the Gallon used or -1 to exit: ");
float Liter = input.nextInt();
totalaverage++;
Miles1 = Miles1 + Miles;
Liter1 = Liter1 + Liter;
totalofdrive = (float) Miles1 ./ Liter1;
float driven = (float) Miles / Liter;
System.out.println(" The Miles per Gallon is " + driven);
System.out.println(" ");
if ( totalaverage > 1 ) {
System.out.println( " The Total Miles trip is " + Miles1);
System.out.println( " The Total Gallon used in the trip is " + Liter1);
}
} }
System.out.println(" ");
System.out.print("THANK YOU AND GODBLESS");
}
}
Comments
Post a Comment