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

ID: 3843761 • 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 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 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 CalculateTrip.java • Call your output file HW3output.txt

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

CalculateTrip.java

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class CalculateTrip {

   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.text.DecimalFormat;
import java.util.Scanner;

public class CalProfitOrLoss {

   public static void main(String[] args) {
       //Declaring constant
       final double TRANX_FEE = 25.00;
      
       //DecimalFormat class is used to format the output
               DecimalFormat df=new DecimalFormat("#.##");
              
       //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 :$" + df.format(amt_of_purchase) + " ");
           fw.write("Amount of sale :$" + df.format(amt_sale) + " ");

           fw.write("Transaction Fees paid :$" + TRANX_FEE + " ");
           net = amt_sale- (amt_of_purchase + TRANX_FEE);
           fw.write("Net Profit :$" + df.format(net) +" ");
           fw.close();

       } catch (Exception e) {
           System.out.println("Exception " + e);
       }

   }

}

_______________

Input#1

What's your name? Joseph
What stock are you purchasing? MSFT
How many shares bought? 250
Buy Price? 28.31
Sell Price? 30.79
Data Written to File

Input#2

What's your name? Johnson
What stock are you purchasing? RILINFR
How many shares bought? 340
Buy Price? 134.50
Sell Price? 150.67
Data Written to File

______________

Input#3:

What's your name? Bobby

What stock are you purchasing? SAIL

How many shares bought? 500

Buy Price? 60.71

Sell Price? 69.88

Data Written to File

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

Statement of RILINFR Transaction for Johnson

----------------------------------------------

Number of Shares purchased :340

Amount of purchase :$45730

Amount of sale :$51227.8

Transaction Fees paid :$25.0

Net Profit :$5472.8

Statement of SAIL Transaction for Bobby

----------------------------------------------

Number of Shares purchased :500

Amount of purchase :$30355

Amount of sale :$34940

Transaction Fees paid :$25.0

Net Profit :$4560

_____________ .Thank You

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