Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I 20 Points Purpose: The purpose of this exercise is to become familiar wit

ID: 3843960 • Letter: P

Question

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 aroad 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 (users inputs are shown in bold Ralph Please enter your First Name: Please enter the MPG of your car: 20 Please enter the miles to be traveled: 160 What is the price per gallon of gas: 2.00 Hello, Ralph! Thank you for providing your information! Car MPG: 20 Miles to Drive: 160 Price per gallon of gas: $2.00 Your trip will cost: $16.00 Program File Suggestions: Call your application CalculateT rip.Java Call your output file HW3output.txt

Explanation / Answer

Part-1

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
*
* @author SuNiL
*/
public class CalculateTrip {
public static void main(String args[]) throws FileNotFoundException, IOException{
String name;
float mpg,miles,gasolineCost,tripCost;
Scanner input= new Scanner(System.in);
System.out.print("Enter your first name= ");
name=input.next();
System.out.print("Please enter MPG of your car= ");
mpg=input.nextFloat();
System.out.print("Please enter the miles to be travelled= ");
miles=input.nextFloat();
System.out.print("What is the price per gallon of gas= ");
gasolineCost=input.nextFloat();
tripCost=(miles/mpg)*gasolineCost;
System.out.println("Trip cost is= "+tripCost);
  
BufferedWriter bw=new BufferedWriter(new FileWriter("C:\Users\SuNiL\Desktop\HW3Output.txt"));
bw.write("Hello, "+name+"! Thank you for providing your information ");
bw.newLine();
bw.write("Car MPG: "+mpg);
bw.newLine();
bw.write("Miles to drive: "+miles);
bw.newLine();
bw.write("Price per gallon: $"+gasolineCost);
bw.newLine();
bw.write("Your trip will cost: $"+tripCost);
bw.flush();
bw.close();
}
}

Part-2

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
*
* @author SuNiL
*/
public class CalculateShare {
public static void main(String args[]) throws FileNotFoundException, IOException{
String yourName,stock;
float numberShare,buyPrice,sellPrice,total,buy,sale;
final float buyTransactionFees=15,sellTransactionFees=10;
Scanner input= new Scanner(System.in);
System.out.print("What is your name= ");
yourName=input.next();
System.out.print("What stock are you purchasing= ");
stock=input.next();
System.out.print("How many share bought= ");
numberShare=input.nextFloat();
System.out.print("Buy price= ");
buyPrice=input.nextFloat();
System.out.print("Sale price= ");
sellPrice=input.nextFloat();
buy=numberShare*buyPrice;
sale=numberShare*sellPrice;
total=sale-(buy+buyTransactionFees+sellTransactionFees);
System.out.println("Profit/loss is $= "+total);
  
BufferedWriter bw=new BufferedWriter(new FileWriter("C:\Users\SuNiL\Desktop\HW3Output.txt"));
bw.write(" Statement of "+stock+" transaction for "+yourName);
bw.newLine();
bw.newLine();
bw.write("Number of share purchased: "+numberShare);
bw.newLine();
bw.write("Amount of purchase: $"+buy);
bw.newLine();
bw.write("Amount of sale: $"+sale);
bw.newLine();
bw.write("Transaction fees paid: $25.0");
bw.newLine();
if(total>=0){
bw.write("Net profit: $"+total);
} else{
bw.write("Net loss: $"+total);
}
bw.flush();
bw.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote