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: 3843731 • 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 II (20 Points):      

Purpose: To become familiar with Java language basics (variable declaration, numeric expression, assignment statement, input using Scanner, and formatting output).

Problem Description: Write a program to accept inputs from a user including yourName, numberShares shares (only whole shares allowed) purchased of some stock (for example the Microsoft stock symbol is MSFT) at the price of buyPrice per share and paid the stockbroker $15 buyTransactionFee. Two weeks later, the person sold the numberShares shares at sellPrice per share and paid another $10 for the sellTransactionFee.

Create the necessary input, process and output variables, choosing appropriate data types, and write a Java program to accept input, calculate and display the following:

dollar amount paid for the shares

dollar amount of the shares sold

total transaction fee paid to the broker (including both buy and sell) – Use constants/final

amount of profit (or loss) made after selling the shares.

Input: yourName, numberShares, buyPrice, sellPrice

Sample Run (user’s inputs are shown in bold):

What’s your name? Joseph

What stock are you purchasing? MSFT

How many shares bought? 250

Buy price? 28.31

Sale price? 30.79

Using Java to get the Inputs above, Calculate and generate the following formatted Output:

                                    Statement of MSFT Transactions for Joseph

Number of shares purchased:     250

Amount of purchase:                                   $7077.50

Amount of sale:                                           $7697.50

Transaction fees paid:                               $25.00

Net profit:                                                      $595.00

Submit four files zipped up to Blackboard:

Your program’s source (Called CalculateTrip.java) on Blackboard. Do not submit the .class file or other project files. Programs must be submitted with a working java program (i.e. debugged without syntax errors, and compile-able).

Your source program that is a file with .java extension, e.g. StockTransaction.java.

The files containing the output produced by your programs. Provide at least 3 sample runs. Save the output to a Word document or text file.

e.g. Lab2Part1-Output.txt and Lab2Part1-Output.txt

Explanation / Answer

CalculateTrip

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:

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.70
Amount of sale :$30588.6
Transaction Fees paid :$25.0
Net Profit :$2072.90

__________Plz Could You rate me well.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