CPS 180 Lab Assignment #2 Assigned: 05/17/17 Due: 05/24/17 Points: 40 Part I (20
ID: 3843701 • Letter: C
Question
CPS 180 Lab Assignment #2 Assigned: 05/17/17 Due: 05/24/17 Points: 40 Part I (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: S2.00 Your trip will cost S16.00 Program File Suggestions: Call your application Calculate Trip,java Call your output file HW3outnut.txtExplanation / Answer
CalCostOFTrip.java
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class CalCostOFTrip {
public static void main(String[] args) {
//Declaring variables
String fname;
int mpg, milesPlanned;
double price;
//Opening the output file
File f = new File("D:\HW3output.txt");
FileWriter fw = null;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Getting the information entered by the user
System.out.print("Please Enter Your Firstname :");
fname = sc.next();
System.out.print("Please Enter MPG of your Car :");
mpg = sc.nextInt();
System.out.print("Please Enter the miles to be Travelled :");
milesPlanned = sc.nextInt();
System.out.print("What is the Price Per gallon of Gas :");
price = sc.nextDouble();
// creates the output file
try {
f.createNewFile();
// creates a FileWriter Object
fw = new FileWriter(f);
fw.write("Hello " + fname+ "! Thank you for providing your information! ");
fw.write("Car MPG :" + mpg+" ");
fw.write("Miles to Drive :" + milesPlanned+" ");
fw.write("Price Per gallon of gas :" + price+" ");
fw.write("Your trip will cost :$" + ((double) milesPlanned / mpg)* price);
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
_____________________
input:
Please Enter Your Firstname :Ralph
Please Enter MPG of your Car :20
Please Enter the miles to be Travelled :160
What is the Price Per gallon of Gas :2.00
Outputfile
HW3output.txt(We can See this file under D Drive.As we specified the path of the output file as D:\HW3output.txt)
_________________
Hello Ralph! Thank you for providing your information!
Car MPG :20
Miles to Drive :160
Price Per gallon of gas :2.0
Your trip will cost :$16.0
_____________
2)
CalProfitOrLoss.java
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class CalProfitOrLoss {
public static void main(String[] args) {
//Declaring constant
final double TRANX_FEE = 25.00;
//Declaring variables
String name, stockName;
int no_of_shares_pur;
double buy_price, sell_price;
double amt_of_purchase = 0, amt_sale = 0, net;
String filePath = "D://Lab2Part1-Output.txt";
// Creating the reference of File and FileWrite classes
File f;
FileWriter fw = null;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Getting the inputs entered by the user
System.out.print("What's your name? ");
name = sc.nextLine();
System.out.print("What stock are you purchasing? ");
stockName = sc.next();
System.out.print("How many shares bought? ");
no_of_shares_pur = sc.nextInt();
System.out.print("Buy Price? ");
buy_price = sc.nextDouble();
System.out.print("Sell Price? ");
sell_price = sc.nextDouble();
amt_of_purchase=no_of_shares_pur*buy_price;
amt_sale=no_of_shares_pur*sell_price;
// Writing the data to the file
try {
f = new File(filePath);
// If file already exists then write data to the existing file
if (f.exists()) {
fw = new FileWriter(f, true);
System.out.println("Data Written to File");
} else {
// If no file already exists.Create new File
f.createNewFile();
fw = new FileWriter(f);
System.out.println("Data Written to File");
}
fw.write("Statement of " + stockName + " Transaction for " + name
+ " ");
fw.write("---------------------------------------------- ");
fw.write(" ");
fw.write("Number of Shares purchased :" + no_of_shares_pur + " ");
fw.write("Amount of purchase :$" + amt_of_purchase + " ");
fw.write("Amount of sale :$" + amt_sale + " ");
fw.write("Transaction Fees paid :$" + TRANX_FEE + " ");
net = amt_sale- (amt_of_purchase + TRANX_FEE);
fw.write("Net Profit :$" + net+" ");
fw.close();
} catch (Exception e) {
System.out.println("Exception " + e);
}
}
}
______________________
Input:
What's your name? Bobby
What stock are you purchasing? RILINFR
How many shares bought? 210
Buy Price? 135.67
Sell Price? 145.66
_______________
Output:
Lab2Part1-Output.txt (We can See this file under D Drive.As we specified the path of the output file as D:\Lab2Part1-Output .txt)
_________________
Statement of MSFT Transaction for Joseph
----------------------------------------------
Number of Shares purchased :250
Amount of purchase :$7077.5
Amount of sale :$7697.5
Transaction Fees paid :$25.0
Net Profit :$595.0
Statement of SAIL Transaction for Johnson
----------------------------------------------
Number of Shares purchased :450
Amount of purchase :$20551.5
Amount of sale :$25551.0
Transaction Fees paid :$25.0
Net Profit :$4974.5
Statement of RILINFR Transaction for Bobby
----------------------------------------------
Number of Shares purchased :210
Amount of purchase :$28490.699999999997
Amount of sale :$30588.6
Transaction Fees paid :$25.0
Net Profit :$2072.9000000000015
__________Plz Could You rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.