0% found this document useful (0 votes)
64 views8 pages

Programming+Projects+2.11 2.13

The document contains three Java applications: one calculates miles per gallon (MPG) and kilometers per liter (KPL) based on gas used and odometer readings, another computes the total value of coins in a jar, and the last generates a random phone number following specific formatting rules. Each application includes user input prompts and outputs the results in a formatted manner. The code is structured within a package named 'SummerProject' and includes necessary imports for functionality.

Uploaded by

aqua7879635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views8 pages

Programming+Projects+2.11 2.13

The document contains three Java applications: one calculates miles per gallon (MPG) and kilometers per liter (KPL) based on gas used and odometer readings, another computes the total value of coins in a jar, and the last generates a random phone number following specific formatting rules. Each application includes user input prompts and outputs the results in a formatted manner. The code is structured within a package named 'SummerProject' and includes necessary imports for functionality.

Uploaded by

aqua7879635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2.

11 = Write an application that computes the number of miles per gallon


(MPG) of gas for a trip. Accept as input a floating point number

that represents the total amount of gas used. Also accept two inte-
gers representing the odometer readings at the start and end of the

trip. Compute the number of kilometers per liter if you prefer.

Package SummerProject;
import [Link];

public class FuelEfficiencyCalc{

public static void main(String[] args) {


//Yashank Rathi 9/3/24
//Calculate miles per gallon (MPG) and kilometers per liter (KPL) for a trip.

//declare variables
double gasUsed;
int startOdometer, endOdometer;
double milesDriven, mpg;
double kilometersDriven, kpl;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Hi! Please enter the total amount of gas used during your trip.");
[Link]("Enter the amount of gas used (in gallons or liters): ");
gasUsed = [Link]();
[Link]("Now, please enter the odometer readings.");
[Link]("Enter the starting odometer reading: ");
startOdometer = [Link]();
[Link]("Enter the ending odometer reading: ");
endOdometer = [Link]();

// calculate and print answer


milesDriven = endOdometer - startOdometer;
if (milesDriven <= 0 || gasUsed <= 0) {
[Link]("Invalid input! Odometer readings or gas used must be
positive values.");
} else {
mpg = milesDriven / gasUsed;
[Link]("The fuel efficiency of your trip is: %.2f miles per gallon
(MPG).%n", mpg);
kilometersDriven = milesDriven * 1.60934;
kpl = kilometersDriven / (gasUsed * 3.78541);
[Link]("The fuel efficiency of your trip is: %.2f kilometers per liter
(KPL).%n", kpl);
}

[Link]();
}
}
2.12 = Write an application that determines the value of the coins in a jar
and prints the total in dollars and cents. Read integer values that
represent the number of quarters, dimes, nickels, and pennies. Use a
currency formatter to print the output.

Package SummerProject;

import [Link];

import [Link];

public class CoinVaueCalc {

public static void main(String[] args) {


//Yashank Rathi 9/3/24
//Converting coins to US currency
//declare variables
int quarters, dimes, nickels, pennies;
double totalValue;
Scanner kbd = new Scanner([Link]);

//ask user
[Link]("Enter the number of quarters:");
quarters = [Link]();
[Link]("Enter the number of dimes:");
dimes = [Link]();
[Link]("Enter the number of nickels:");
nickels = [Link]();
[Link]("Enter the number of pennies:");
pennies = [Link]();

//calculate and print answer


totalValue = quarters * 0.25 + dimes * 0.10 + nickels * 0.05 + pennies * 0.01;
NumberFormat currencyFormatter = [Link]();
[Link]("The total value of the coins is: " +
[Link](totalValue));

[Link]();
}
}
2.13 = Write an application that creates and prints a random phone number
of the form XXX-XXX-XXXX. Include the dashes in the output. Do not

let the first three digits contain an 8 or 9 (but don’t be more restric-
tive than that), and make sure that the second set of three digits is

not greater than 742. Hint: Think through the easiest way to con-
struct the phone number. Each digit does not have to be determined

separately.

Package SummerProject;
import [Link];

public class PhoneNumber {

public static void main(String[] args) {


//Yashank Rathi 9/3/24
//Computer generates a random phone number

//declare variables
Random rand = new Random();
int firstPart = [Link](700) + 100;
int secondPart = [Link](743);
int thirdPart = [Link](9000) + 1000;

//print answer
[Link]("Random Phone Number: %03d-%03d-%04d%n", firstPart,
secondPart, thirdPart);
}
}

You might also like