Part (20 Points): Purpose: The purpose of this exercise is to become familiar wi
ID: 3844550 • Letter: P
Question
Part (20 Points): Purpose: The purpose of this exercise is to become familiar with Java basic syntax, program structure, and problem solving. In addition, you should learn how to submit a program using the file submit capability of Blackboard Exercise: Write a Java program that calculates the cost of gas for a road trip. Your program will need to collect the following pieces of information from the user First name Miles per gallon (mpg) of the car Number of miles planned to drive Current price per gallon of gas After collecting the user's information and the performing the calculations your program should display the results in a format similar to: Sample Run (user's inputs are shown in bold): Please enter your First Name: Ralph Please enter the MPG of your car: 20 160 Please enter the miles to be traveled: What is the price per gallon of gas: 2.00 Hello, Ralph Thank you for providing your information! Car MPG: 20 160 Miles to Drive: Price per gallon of gas: $2.00 $16.00 Your trip will cost: Program File Suggestions: Call your application Calculate Trip java Call your output file HW3output.txtExplanation / Answer
Part I:
import java.util.*;
import java.lang.*;
import java.io.*;
class CalculateCost
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scan = new Scanner(System.in);
String firstName;
int milesPerGallon,noOfMiles;
double cost,currentPrice;
System.out.println("Please enter Your First Name");
firstName=scan.nextLine();
System.out.println("Please enter MPG of a car");
milesPerGallon=scan.nextInt();
System.out.println("Please miles to be Travelled");
noOfMiles=scan.nextInt();
System.out.println("What is the price for gallon of gas");
currentPrice=scan.nextInt();
cost=(noOfMiles*currentPrice)/milesPerGallon;
System.out.println("Hello,"+firstName+"! Thank you for providing information!");
System.out.println("Car MPG: "+milesPerGallon);
System.out.println("Miles To Drive: "+noOfMiles);
System.out.println("Price For Gallon Of Gas: "+"$"+currentPrice);
System.out.println("Your trip will cost: "+"$"+cost);
}
}
Input:
Please enter Your First Name
2
Part II:
import java.util.*;
import java.lang.*;
import java.io.*;
class Profit
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scan = new Scanner(System.in);
String name,stock;
int noOfShares;
double buyPrice,salePrice,profit;
System.out.println("What Is Your Name");
name=scan.nextLine();
System.out.println("What Stock are you purchasing");
stock=scan.nextLine();
System.out.println("How Many Shares Bought");
noOfShares=scan.nextInt();
System.out.println("Buy Price");
buyPrice=scan.nextDouble();
System.out.println("Sale Price");
salePrice=scan.nextDouble();
profit=((noOfShares*salePrice)-(noOfShares*buyPrice))-25;
System.out.println("Statement of MSFT transactions for "+name);
System.out.println("No of Shares Purchased: "+noOfShares);
System.out.println("Amount Of Purchase: "+"$"+(noOfShares*buyPrice));
System.out.println("Amount Of Sale: "+"$"+(noOfShares*salePrice));
System.out.println("Transaction Fee Paid: "+"$"+25);
System.out.println("Net Profit: "+"$"+profit);
}
}
Input:
What Is Your Name
30.79
Output: