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

CPS 180 - Lab Assignment #2 Due: 05/24/17 Points: 40 Part I (20 Points): Purpose

ID: 3843473 • Letter: C

Question

CPS 180 - Lab Assignment #2

            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

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

Please enter your First Name:   ds
Please enter the MPG of your car:   20
Please enter the miles to be traveled:   24
What is the price per gallon of gas:   2
Hello ds! Thank you for providing your information!
Car MPG:   20
Miles to Drive:   24
Price per gallon of gas:   $2.0
Your trip will cost:   $2.4

Please enter your First Name:   Ralph
Please enter the MPG of your car:   30
Please enter the miles to be traveled:   60
What is the price per gallon of gas:   3.0

HelloRalph! Thank you for providing your information!
Car MPG:   30
Miles to Drive:   60
Price per gallon of gas:   $3.0
Your trip will cost:   $6.0


Please enter your First Name:   Darshan
Please enter the MPG of your car:   5
Please enter the miles to be traveled:   20
What is the price per gallon of gas:   20

Hello Darshan! Thank you for providing your information!
Car MPG:   5
Miles to Drive:   20
Price per gallon of gas:   $20.0
Your trip will cost:   $80.0

Output:

What’s your name?    darshan
What stock are you purchasing?    google
How many shares bought?    20
Buy price?    10
Sale price?    12
Number of shares purchased:   $20
Amount of purchase:   $200.0
Amount of sale:   $240.0
Transaction fees paid::   $25.0
Net profit:   $15.0

What’s your name?    dsd
What stock are you purchasing?    mst
How many shares bought?    100
Buy price?    10
Sale price?    15
Number of shares purchased:   $100
Amount of purchase:   $1000.0
Amount of sale:   $1500.0
Transaction fees paid::   $25.0
Net profit:   $475.0


What’s your name?    ama
What stock are you purchasing?    amazon
How many shares bought?    4
Buy price?    20
Sale price?    23
Number of shares purchased:   $4
Amount of purchase:   $80.0
Amount of sale:   $92.0
Transaction fees paid::   $25.0
Net profit:   $-13.0